annotate tempctrl.c @ 61:9c5db7fee912

Make 'td log' work without printing an error.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 08 Apr 2013 23:11:55 +0930
parents bbdf5642a7b7
children 0916bfe3f418
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 /*
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 * Temperature control logic, copied from AVR version
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 * Copyright (c) 2012
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 * Redistribution and use in source and binary forms, with or without
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 * modification, are permitted provided that the following conditions
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 * are met:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 * 1. Redistributions of source code must retain the above copyright
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer.
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 * SUCH DAMAGE.
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 #include <assert.h>
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
30 #include <ctype.h>
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 #include <stdio.h>
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 #include <stdint.h>
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 #include <stdlib.h>
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 #include <string.h>
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 #include <sys/time.h>
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 #include <time.h>
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 #include "stm32f10x.h"
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 #include "1wire.h"
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
40 #include "ff.h"
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 #include "flash.h"
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 #include "tempctrl.h"
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 /* Holds all the settings needed */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 typedef struct {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 uint8_t fermenter_ROM[8];
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 uint8_t fridge_ROM[8];
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 uint8_t ambient_ROM[8];
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 int16_t target_temp;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 uint16_t hysteresis;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 /* How much to under/overshoot on heating/cooling */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 int16_t minheatovershoot;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 int16_t mincoolovershoot;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 /* Minimum time the cooler can be on/off */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 int16_t mincoolontime;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 int16_t mincoolofftime;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 /* Minimum time the heater can be on/off */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 int16_t minheatontime;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 int16_t minheatofftime;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 #define TC_MODE_AUTO 'a' /* Automatic control */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 #define TC_MODE_HEAT 'h' /* Force heating */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 #define TC_MODE_COOL 'c' /* Force cooling */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 #define TC_MODE_IDLE 'i' /* Force idle */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 char mode;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 /* GPIO port the heater & cooler are on */
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
71 char coolport;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 uint16_t coolpin;
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
73 uint8_t coolinv;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
75 char heatport;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 uint16_t heatpin;
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
77 uint8_t heatinv;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 /* Check/stale times */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 int16_t check_interval;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 int16_t stale_factor;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
83 char logfilefmt[64];
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
84
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
85 uint8_t pad[1]; /* Pad to multiple of 4 bytes */
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 } settings_t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 /* Current settings in RAM */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 static settings_t settings;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 /* Defaults that are shoved into SPI flash if it isn't inited */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 const settings_t default_settings = {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 .fermenter_ROM = { 0x10, 0x4c, 0x7d, 0x53, 0x01, 0x08, 0x00, 0xff },
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 .fridge_ROM = { 0x10, 0x6d, 0x40, 0x53, 0x01, 0x08, 0x00, 0x16 },
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 .ambient_ROM = { 0x10, 0x76, 0x05, 0x53, 0x01, 0x08, 0x00, 0x8e },
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 .target_temp = 1000,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 .hysteresis = 100,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 .minheatovershoot = 50,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 .mincoolovershoot = -50,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 .mincoolontime = 300,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 .mincoolofftime = 600,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 .minheatontime = 60,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 .minheatofftime = 60,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 .mode = TC_MODE_AUTO,
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
105 .coolport = 'E',
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
106 .coolpin = 4,
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
107 .coolinv = 1,
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
108 .heatport = 'E',
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
109 .heatpin = 5,
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
110 .heatinv = 1,
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 .check_interval = 10,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 .stale_factor = 3,
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
113 .logfilefmt = "/%Y%M%D.log"
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 };
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 /* Local variable declarations */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 /* Local function prototypes */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 static void tempctrl_load_or_init_settings(void);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 static void tempctrl_default_settings(void);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 static void tempctrl_write_settings(void);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 static void setstate(char state);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 static const char * state2long(char s);
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
124 static int fmttemp(char *buf, const char *name, int tmp, const char *trailer);
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
125 static GPIO_TypeDef *char2port(char port);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 /*
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128 * tempctrl_init
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
131 void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
132 tempctrl_init(void) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
133 tempctrl_load_or_init_settings();
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
134 setstate('i');
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 /*
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138 * tempctrl_update
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140 * Should be called in a normal context, could run things that take a long time.
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 * (ie 1wire bus stuff)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
143 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 tempctrl_update(void) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 /* State variables */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 static int32_t checktime = 0; // Time of next check
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 static int32_t lastdata = INT32_MIN; // Last time we got data
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 static int16_t fermenter_temp = 0; // Fermenter temperature
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 static int16_t fridge_temp = 0; // Fridge temperature
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152 static int16_t ambient_temp = 0; // Ambient temperature
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
153 static int32_t lastheaton = INT32_MIN; // Last time the heater was on
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 static int32_t lastheatoff = INT32_MIN;// Last time the heater was off
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
155 static int32_t lastcoolon = INT32_MIN; // Last time the cooler was on
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 static int32_t lastcooloff = INT32_MIN;// Last time the cooler was off
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 static char currstate = 'i'; // Current state
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 /* We init to times to INT32_MIN so that things function properly when
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159 * now < settings.minheat/cool/on/offtime */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
161 /* Temporary variables */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 int32_t tempt;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 int16_t diff;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 char nextstate;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165 int forced;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 int stale;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
167 time_t t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
168 struct tm tm;
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
169 char linebuf[90], *p;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
170 FRESULT fserr;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
171 FIL f;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
172 char fbuf[20];
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 t = time(NULL);
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
175 stale = 0;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
176 tempt = 0;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 /* Time to check temperatures? */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 if (t < checktime)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 checktime = t + settings.check_interval;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 /* Don't do any logging, just force idle and leave */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 if (settings.mode == TC_MODE_NOTHING) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 nextstate = 'i';
43
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
187 goto skip;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 /* Update our temperatures
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 * Can take a while (800ms each!)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 tempt = OWGetTemp(settings.fermenter_ROM);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 fridge_temp = OWGetTemp(settings.fridge_ROM);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 ambient_temp = OWGetTemp(settings.ambient_ROM);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 /* We only care about this one, only update the value we decide on
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 * only if it is valid
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
199 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 if (tempt > OW_TEMP_BADVAL) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 fermenter_temp = tempt;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 lastdata = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
204
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
205 /* Check for stale data */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
206 if (lastdata + (settings.check_interval * settings.stale_factor) < t)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
207 stale = 1;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
208
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
209 /* Default to remaining as we are */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
210 nextstate = '-';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
211
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
212 /* Temperature diff, -ve => too cold, +ve => too warm */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
213 diff = fermenter_temp - settings.target_temp;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
214
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
215 switch (currstate) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
216 case 'i':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 /* If we're idle then only heat or cool if the temperate difference is out of the
43
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
218 * hysteresis band and the heater/cooler have been off long enough
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
219 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
220 if (abs(diff) > settings.hysteresis) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
221 if (diff < 0 && settings.minheatofftime + lastheatoff < t)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
222 nextstate = 'h';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
223 else if (diff > 0 && settings.mincoolofftime + lastcooloff < t)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
224 nextstate = 'c';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
225 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
226 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 case 'c':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 /* Work out if we should go idle (based on min on time & overshoot) */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230 if (diff + settings.mincoolovershoot < 0 &&
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 settings.mincoolontime + lastcoolon < t)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232 nextstate = 'i';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
235 case 'h':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 if (diff - settings.minheatovershoot > 0 &&
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 settings.minheatontime + lastheaton < t)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 nextstate = 'i';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241 default:
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
242 printf("\nUnknown state %c, going to idle\n", currstate);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 nextstate = 'i';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
245 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
246
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
247 /* Override if we have stale data */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
248 if (stale)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
249 nextstate = 'i';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
250
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
251 /* Handle state forcing */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
252 if (settings.mode != TC_MODE_AUTO)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
253 forced = 1;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 else
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 forced = 0;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257 if (settings.mode == TC_MODE_IDLE)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 nextstate = 'i';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259 else if (settings.mode == TC_MODE_HEAT)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 nextstate = 'h';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 else if (settings.mode == TC_MODE_COOL)
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
262 nextstate = 'c';
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263
43
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
264 /* Keep track of when we last turned things on or off */
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
265 skip:
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266 switch (nextstate) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 case 'c':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268 if (currstate == 'h')
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 lastheatoff = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 lastcoolon = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 case 'h':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 if (currstate == 'c')
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 lastcooloff = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276 lastheaton = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
279 default:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 if (currstate == 'c')
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 lastcooloff = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 if (currstate == 'h')
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283 lastheatoff = t;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
284 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
285
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
286 if (nextstate != '-')
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
287 currstate = nextstate;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
288
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
289
43
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
290 if (settings.mode != TC_MODE_NOTHING) {
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
291 localtime_r(&t, &tm);
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
292 p = linebuf;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
293 p += strftime(p, sizeof(linebuf) - 1, "%Y/%m/%d %H:%M:%S: ", &tm);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
294 p += fmttemp(p, "Tr", settings.target_temp, ", ");
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
295 p += fmttemp(p, "Fm", tempt, ", ");
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
296 p += fmttemp(p, "Fr", fridge_temp, ", ");
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
297 p += fmttemp(p, "Am", ambient_temp, ", ");
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
298 sprintf(p, "St: %s, Fl: %s%s\n", state2long(currstate),
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
299 forced ? "F" : "",
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
300 stale ? "S" : "");
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
301 fputs(linebuf, stdout);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
302 if (settings.logfilefmt[0] != '\0') {
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
303 strftime(fbuf, sizeof(fbuf) - 1, settings.logfilefmt, &tm);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
304
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
305 if ((fserr = f_open(&f, fbuf, FA_WRITE | FA_OPEN_ALWAYS)) != FR_OK) {
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
306 printf("Failed to open file: %d\n", fserr);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
307 goto openerr;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
308 }
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
309
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
310 if ((fserr = f_lseek(&f, f_size(&f))) != FR_OK) {
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
311 printf("Failed to seek to end of file: %d\n", fserr);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
312 goto openerr;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
313 }
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
314
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
315 f_puts(linebuf, &f);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
316 f_close(&f);
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
317 }
43
b4440f9f6d47 Fix 'none' mode.
Daniel O'Connor <darius@dons.net.au>
parents: 42
diff changeset
318 }
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
319
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
320 openerr:
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
321 setstate(currstate);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
322 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
323
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
324 /*
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
325 * Format a temperature (or short error code) with specified trailer
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
326 */
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
327 static int
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
328 fmttemp(char *buf, const char *name, int tmp, const char *trailer) {
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
329 if (tmp > OW_TEMP_BADVAL)
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
330 return sprintf(buf, "%s: %d.%02d%s", name, GETWHOLE(tmp), GETFRAC(tmp), trailer);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
331 else
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
332 return sprintf(buf, "%s: %s%s", name, OWTempStatusStr(tmp, 1), trailer);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
333 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
334
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
335 /* Read the settings from SPI flash
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
336 * If the CRC fails then reload from onboard flash
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
337 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
338 static void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
339 tempctrl_load_or_init_settings(void) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
340 /* XXX: todo */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
341 if (!flashreadblock(0, sizeof(settings), &settings)) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
342 fputs("CRC fails, loading defaults\n", stdout);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
343 tempctrl_default_settings();
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
344 tempctrl_write_settings();
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
345 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
346 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
347
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
348 /* Load in the defaults from flash */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
349 static void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
350 tempctrl_default_settings(void) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
351 memcpy(&settings, &default_settings, sizeof(settings_t));
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
352 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
353
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
354 /* Write the current settings out to SPI flash */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
355 static void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
356 tempctrl_write_settings(void) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
357 flashwriteblock(0, sizeof(settings), &settings);
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
358 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
359
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
360 /* Set the relays to match the desired state */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
361 static void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
362 setstate(char state) {
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
363 uint8_t cool, heat;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
364
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
365 switch (state) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
366 case 'c':
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
367 cool = 1;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
368 heat = 0;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
369 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
370
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
371 case 'h':
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
372 cool = 0;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
373 heat = 1;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
374 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
375
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
376 default:
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
377 printf("Unknown state %c, setting idle\n", state);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
378 /* fallthrough */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
379
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
380 case 'i':
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
381 cool = 0;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
382 heat = 0;
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
383 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
384 }
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
385
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
386 if (cool ^ settings.coolinv)
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
387 GPIO_SetBits(char2port(settings.coolport), 1 << settings.coolpin);
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
388 else
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
389 GPIO_ResetBits(char2port(settings.coolport), 1 << settings.coolpin);
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
390
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
391 if (heat ^ settings.heatinv)
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
392 GPIO_SetBits(char2port(settings.heatport), 1 << settings.heatpin);
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
393 else
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
394 GPIO_ResetBits(char2port(settings.heatport), 1 << settings.heatpin);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
395 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
396
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
397 /* Handle user command
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
398 *
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
399 */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
400 void
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
401 tempctrl_cmd(int argc, char **argv) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
402 int16_t data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
403 uint8_t ROM[8];
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
404
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
405 if (argc < 1) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
406 printf("Unable to parse tc subcommand\n");
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
407 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
408 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
409
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
410 if (!strcasecmp(argv[0], "help")) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
411 printf("tc help This help\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
412 "tc save Save settings to EEPROM\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
413 "tc load Load or default settings from EEPROM\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
414 "tc dflt Load defaults from flash\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
415 "tc list List current settings\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
416 "tc mode [achin] Change control mode, must be one of\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
417 " a Auto\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
418 " c Always cool\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
419 " h Always heat\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
420 " i Always idle\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
421 " n Like idle but don't log anything\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
422 "tc X Y Set X to Y where X is one of\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
423 " targ Target temperature\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
424 " hys Hysteresis range\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
425 " mhov Minimum heat overshoot\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
426 " mcov Minimum cool overshoot\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
427 " mcon Minimum cool on time\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
428 " mcoff Minimum cool off time\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
429 " mhin Minimum heat on time\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
430 " mhoff Minimum heat off time\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
431 "tc A B Set temperature sensor ID\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
432 " Where A is ferm, frg or amb\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
433 " and B is of the form xx:xx:xx:xx:xx:xx:xx:xx\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
434 "\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
435 " Times are in seconds\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
436 " Temperatures are in hundredths of degrees Celcius\n"
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
437 );
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
438 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
439 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
440
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
441 if (!strcasecmp(argv[0], "save")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
442 tempctrl_write_settings();
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
443 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
444 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
445 if (!strcasecmp(argv[0], "load")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
446 tempctrl_load_or_init_settings();
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
447 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
448 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
449 if (!strcasecmp(argv[0], "dflt")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
450 tempctrl_default_settings();
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
451 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
452 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
453 if (!strcasecmp(argv[0], "list")) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
454 printf("Fermenter ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
455 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
456 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
457 "Mode - %c, Target - %d, Hystersis - %d\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
458 "Min heat overshoot - %d, Min cool overshoot - %d\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
459 "Min cool on time - %d, Min cool off time - %d\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
460 "Min heat on time - %d, Min heat off time - %d\n"
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
461 "Cool: Port %c Pin %d Inv %d, Heat: Port %c Pin %d Inv %d\n"
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
462 "Log format: %s\n",
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
463 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
464 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
465 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
466 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
467 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
468 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
469 settings.mode, settings.target_temp, settings.hysteresis,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
470 settings.minheatovershoot, settings.mincoolovershoot,
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
471 settings.mincoolontime, settings.minheatontime,
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
472 settings.minheatontime, settings.minheatofftime,
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
473 settings.coolport, settings.coolpin, settings.coolinv,
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
474 settings.heatport, settings.heatpin, settings.heatinv,
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
475 settings.logfilefmt[0] == '\0' ? "none" : settings.logfilefmt
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
476 );
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
477 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
478 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
479 if (!strcasecmp(argv[0], "mode")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
480 if (argc < 2) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
481 fputs("Incorrect number of arguments\n", stdout);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
482 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
483 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
484
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
485 switch (argv[1][0]) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
486 case TC_MODE_AUTO:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
487 case TC_MODE_HEAT:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
488 case TC_MODE_COOL:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
489 case TC_MODE_IDLE:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
490 case TC_MODE_NOTHING:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
491 settings.mode = argv[1][0];
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
492 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
493
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
494 default:
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
495 printf("Unknown mode character '%c'\n", argv[1][0]);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
496 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
497 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
498 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
499 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
500 if (!strcasecmp(argv[0], "ferm") ||
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
501 !strcasecmp(argv[0], "frg") ||
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
502 !strcasecmp(argv[0], "amb")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
503 if (argc < 2) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
504 fputs("Incorrect number of arguments\n", stdout);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
505 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
506 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
507
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
508 if (sscanf(argv[1], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
509 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
510 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
511 printf("Unable to parse ROM ID\n");
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
512 } else {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
513 if (!strcasecmp(argv[0], "ferm"))
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
514 memcpy(&settings.fermenter_ROM, ROM, sizeof(ROM));
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
515 if (!strcasecmp(argv[0], "frg"))
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
516 memcpy(&settings.fridge_ROM, ROM, sizeof(ROM));
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
517 if (!strcasecmp(argv[0], "amb"))
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
518 memcpy(&settings.ambient_ROM, ROM, sizeof(ROM));
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
519 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
520 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
521 }
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
522 if (!strcasecmp(argv[0], "log")) {
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
523 if (argc == 1)
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
524 bzero(settings.logfilefmt, sizeof(settings.logfilefmt));
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
525 else {
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
526 if (strlen(argv[1]) > sizeof(settings.logfilefmt) - 1)
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
527 printf("New path too log (%d > %d)\n", strlen(argv[1]), sizeof(settings.logfilefmt));
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
528 else
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
529 strcpy(settings.logfilefmt, argv[1]);
61
9c5db7fee912 Make 'td log' work without printing an error.
Daniel O'Connor <darius@dons.net.au>
parents: 60
diff changeset
530 }
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
531 return;
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
532 }
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
533
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
534 /* Handle setting the multitude of variables
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
535 * It's last to simplify things */
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
536 if (argc < 3) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
537 fputs("Incorrect number of arguments for variable/value\n", stdout);
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
538 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
539 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
540
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
541 if (sscanf(argv[2], "%hd", &data) != 1) {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
542 printf("Unable to parse value for variable\n");
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
543 return;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
544 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
545
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
546 if (!strcasecmp(argv[1], "targ")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
547 settings.target_temp = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
548 } else if (!strcasecmp(argv[1], "hys")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
549 settings.hysteresis = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
550 } else if (!strcasecmp(argv[1], "mhov")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
551 settings.minheatovershoot = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
552 } else if (!strcasecmp(argv[1], "mcov")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
553 settings.mincoolovershoot = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
554 } else if (!strcasecmp(argv[1], "mcon")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
555 settings.mincoolontime = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
556 } else if (!strcasecmp(argv[1], "mcoff")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
557 settings.mincoolofftime = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
558 } else if (!strcasecmp(argv[1], "mhon")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
559 settings.minheatontime = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
560 } else if (!strcasecmp(argv[1], "mhoff")) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
561 settings.minheatofftime = data;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
562 } else {
60
bbdf5642a7b7 Convert \r\n -> \n now _write() DTRT.
Daniel O'Connor <darius@dons.net.au>
parents: 57
diff changeset
563 printf("Unknown setting\n");
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
564 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
565 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
566
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
567 static const char*
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
568 state2long(char s) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
569 switch (s) {
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
570 case 'i':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
571 return "idle";
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
572 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
573
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
574 case 'c':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
575 return "cool";
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
576 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
577
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
578 case 'h':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
579 return "heat";
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
580 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
581
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
582 case '-':
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
583 return "-";
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
584 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
585
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
586 default:
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
587 return "unknown";
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
588 break;
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
589 }
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
590 }
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
591
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
592 /* Convert a port name into a number */
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
593 static GPIO_TypeDef *
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
594 char2port(char port) {
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
595 char p;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
596
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
597 p = toupper(port);
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
598 assert(p >= 'A' && p <= 'E');
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
599
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
600 switch (p) {
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
601 case 'A':
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
602 return GPIOA;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
603 case 'B':
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
604 return GPIOB;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
605 case 'C':
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
606 return GPIOC;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
607 case 'D':
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
608 return GPIOD;
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
609 case 'E':
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
610 return GPIOE;
57
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
611 default:
adc9b1555f9d Add the ability to log temperatures to SD card.
Daniel O'Connor <darius@dons.net.au>
parents: 43
diff changeset
612 assert(1 == 0); /* Silence GCC warning */
42
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
613 }
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
614 }
56b32b0c9a40 Store port & pin as char & number and map.
Daniel O'Connor <darius@dons.net.au>
parents: 33
diff changeset
615