comparison tempctrl.c @ 42:56b32b0c9a40

Store port & pin as char & number and map. Allow them to be inverted for active low setups.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 01 Apr 2013 21:12:57 +1030
parents d3b7d4964807
children b4440f9f6d47
comparison
equal deleted inserted replaced
41:2db4bb90cfca 42:56b32b0c9a40
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28 28
29 #include <assert.h> 29 #include <assert.h>
30 #include <ctype.h>
30 #include <stdio.h> 31 #include <stdio.h>
31 #include <stdint.h> 32 #include <stdint.h>
32 #include <stdlib.h> 33 #include <stdlib.h>
33 #include <string.h> 34 #include <string.h>
34 #include <sys/time.h> 35 #include <sys/time.h>
64 #define TC_MODE_IDLE 'i' /* Force idle */ 65 #define TC_MODE_IDLE 'i' /* Force idle */
65 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */ 66 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */
66 char mode; 67 char mode;
67 68
68 /* GPIO port the heater & cooler are on */ 69 /* GPIO port the heater & cooler are on */
69 GPIO_TypeDef *coolport; 70 char coolport;
70 uint16_t coolpin; 71 uint16_t coolpin;
71 72 uint8_t coolinv;
72 GPIO_TypeDef *heatport; 73
74 char heatport;
73 uint16_t heatpin; 75 uint16_t heatpin;
76 uint8_t heatinv;
74 77
75 /* Check/stale times */ 78 /* Check/stale times */
76 int16_t check_interval; 79 int16_t check_interval;
77 int16_t stale_factor; 80 int16_t stale_factor;
78 81
79 uint8_t pad[3]; /* Pad to multiple of 4 bytes */ 82 uint8_t pad[1]; /* Pad to multiple of 4 bytes */
80 } settings_t; 83 } settings_t;
81 84
82 /* Current settings in RAM */ 85 /* Current settings in RAM */
83 static settings_t settings; 86 static settings_t settings;
84 87
94 .mincoolontime = 300, 97 .mincoolontime = 300,
95 .mincoolofftime = 600, 98 .mincoolofftime = 600,
96 .minheatontime = 60, 99 .minheatontime = 60,
97 .minheatofftime = 60, 100 .minheatofftime = 60,
98 .mode = TC_MODE_AUTO, 101 .mode = TC_MODE_AUTO,
99 .coolport = GPIOE, 102 .coolport = 'E',
100 .coolpin = GPIO_Pin_4, 103 .coolpin = 4,
101 .heatport = GPIOE, 104 .coolinv = 1,
102 .heatpin = GPIO_Pin_5, 105 .heatport = 'E',
106 .heatpin = 5,
107 .heatinv = 1,
103 .check_interval = 10, 108 .check_interval = 10,
104 .stale_factor = 3, 109 .stale_factor = 3,
105 }; 110 };
106 111
107 /* Local variable declarations */ 112 /* Local variable declarations */
111 static void tempctrl_default_settings(void); 116 static void tempctrl_default_settings(void);
112 static void tempctrl_write_settings(void); 117 static void tempctrl_write_settings(void);
113 static void setstate(char state); 118 static void setstate(char state);
114 static const char * state2long(char s); 119 static const char * state2long(char s);
115 static void printtemp(const char *name, int tmp, const char *trailer); 120 static void printtemp(const char *name, int tmp, const char *trailer);
121 static GPIO_TypeDef *char2port(char port);
116 122
117 /* 123 /*
118 * tempctrl_init 124 * tempctrl_init
119 * 125 *
120 */ 126 */
121 void 127 void
122 tempctrl_init(void) { 128 tempctrl_init(void) {
123 tempctrl_load_or_init_settings(); 129 tempctrl_load_or_init_settings();
130 setstate('i');
124 } 131 }
125 132
126 /* 133 /*
127 * tempctrl_update 134 * tempctrl_update
128 * 135 *
324 } 331 }
325 332
326 /* Set the relays to match the desired state */ 333 /* Set the relays to match the desired state */
327 static void 334 static void
328 setstate(char state) { 335 setstate(char state) {
336 uint8_t cool, heat;
337
329 switch (state) { 338 switch (state) {
330 case 'c': 339 case 'c':
331 GPIO_ResetBits(settings.heatport, settings.heatpin); 340 cool = 1;
332 GPIO_SetBits(settings.coolport, settings.coolpin); 341 heat = 0;
333 break; 342 break;
334 343
335 case 'h': 344 case 'h':
336 GPIO_ResetBits(settings.coolport, settings.coolpin); 345 cool = 0;
337 GPIO_SetBits(settings.heatport, settings.heatpin); 346 heat = 1;
338 break; 347 break;
339 348
340 default: 349 default:
341 printf("Unknown state %c, setting idle\r\n", state); 350 printf("Unknown state %c, setting idle\r\n", state);
342 /* fallthrough */ 351 /* fallthrough */
343 352
344 case 'i': 353 case 'i':
345 GPIO_ResetBits(settings.coolport, settings.coolpin); 354 cool = 0;
346 GPIO_ResetBits(settings.heatport, settings.heatpin); 355 heat = 0;
347 break; 356 break;
348 } 357 }
358
359 if (cool ^ settings.coolinv)
360 GPIO_SetBits(char2port(settings.coolport), 1 << settings.coolpin);
361 else
362 GPIO_ResetBits(char2port(settings.coolport), 1 << settings.coolpin);
363
364 if (heat ^ settings.heatinv)
365 GPIO_SetBits(char2port(settings.heatport), 1 << settings.heatpin);
366 else
367 GPIO_ResetBits(char2port(settings.heatport), 1 << settings.heatpin);
349 } 368 }
350 369
351 /* Handle user command 370 /* Handle user command
352 * 371 *
353 */ 372 */
409 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" 428 "Fridge ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
410 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n" 429 "Ambient ROM ID %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n"
411 "Mode - %c, Target - %d, Hystersis - %d\r\n" 430 "Mode - %c, Target - %d, Hystersis - %d\r\n"
412 "Min heat overshoot - %d, Min cool overshoot - %d\r\n" 431 "Min heat overshoot - %d, Min cool overshoot - %d\r\n"
413 "Min cool on time - %d, Min cool off time - %d\r\n" 432 "Min cool on time - %d, Min cool off time - %d\r\n"
414 "Min heat on time - %d, Min heat off time - %d\r\n", 433 "Min heat on time - %d, Min heat off time - %d\r\n"
434 "Cool: Port %c Pin %d Inv %d, Heat: Port %c Pin %d Inv %d\r\n",
415 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], 435 settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3],
416 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], 436 settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7],
417 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], 437 settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3],
418 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7], 438 settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7],
419 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3], 439 settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3],
420 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7], 440 settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7],
421 settings.mode, settings.target_temp, settings.hysteresis, 441 settings.mode, settings.target_temp, settings.hysteresis,
422 settings.minheatovershoot, settings.mincoolovershoot, 442 settings.minheatovershoot, settings.mincoolovershoot,
423 settings.mincoolontime, settings.minheatontime, 443 settings.mincoolontime, settings.minheatontime,
424 settings.minheatontime, settings.minheatofftime 444 settings.minheatontime, settings.minheatofftime,
445 settings.coolport, settings.coolpin, settings.coolinv,
446 settings.heatport, settings.heatpin, settings.heatinv
425 ); 447 );
426 return; 448 return;
427 } 449 }
428 if (!strcasecmp(argv[0], "mode")) { 450 if (!strcasecmp(argv[0], "mode")) {
429 if (argc < 2) { 451 if (argc < 2) {
524 default: 546 default:
525 return "unknown"; 547 return "unknown";
526 break; 548 break;
527 } 549 }
528 } 550 }
551
552 /* Convert a port name into a number */
553 static GPIO_TypeDef *
554 char2port(char port) {
555 char p;
556
557 p = toupper(port);
558 assert(p >= 'A' && p <= 'E');
559
560 switch (p) {
561 case 'A':
562 return GPIOA;
563 case 'B':
564 return GPIOB;
565 case 'C':
566 return GPIOC;
567 case 'D':
568 return GPIOD;
569 case 'E':
570 return GPIOE;
571 }
572 }
573