changeset 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 2db4bb90cfca
children b4440f9f6d47
files tempctrl.c
diffstat 1 files changed, 60 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/tempctrl.c	Mon Apr 01 20:51:11 2013 +1030
+++ b/tempctrl.c	Mon Apr 01 21:12:57 2013 +1030
@@ -27,6 +27,7 @@
  */
 
 #include <assert.h>
+#include <ctype.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -66,17 +67,19 @@
     char	mode;
     
     /* GPIO port the heater & cooler are on */
-    GPIO_TypeDef *coolport;
+    char	coolport;
     uint16_t	coolpin;
+    uint8_t	coolinv;
     
-    GPIO_TypeDef *heatport;
+    char 	heatport;
     uint16_t	heatpin;
+    uint8_t	heatinv;
 
     /* Check/stale times */
     int16_t	check_interval;
     int16_t	stale_factor;
 
-    uint8_t	pad[3]; /* Pad to multiple of 4 bytes */
+    uint8_t	pad[1]; /* Pad to multiple of 4 bytes */
 } settings_t;
 
 /* Current settings in RAM */
@@ -96,10 +99,12 @@
     .minheatontime = 60,
     .minheatofftime = 60,
     .mode = TC_MODE_AUTO,
-    .coolport = GPIOE,
-    .coolpin = GPIO_Pin_4,
-    .heatport = GPIOE,
-    .heatpin = GPIO_Pin_5,
+    .coolport = 'E',
+    .coolpin = 4,
+    .coolinv = 1,
+    .heatport = 'E',
+    .heatpin = 5,
+    .heatinv = 1,
     .check_interval = 10,
     .stale_factor = 3,
 };
@@ -113,6 +118,7 @@
 static void		setstate(char state);
 static const char *	state2long(char s);
 static void		printtemp(const char *name, int tmp, const char *trailer);
+static GPIO_TypeDef	*char2port(char port);
 
 /* 
  * tempctrl_init
@@ -121,6 +127,7 @@
 void
 tempctrl_init(void) {
     tempctrl_load_or_init_settings();
+    setstate('i');
 }
 
 /* 
@@ -326,15 +333,17 @@
 /* Set the relays to match the desired state */
 static void
 setstate(char state) {
+    uint8_t cool, heat;
+    
     switch (state) {
 	case 'c':
-	    GPIO_ResetBits(settings.heatport, settings.heatpin);
-	    GPIO_SetBits(settings.coolport, settings.coolpin);
+	    cool = 1;
+	    heat = 0;
 	    break;
 
 	case 'h':
-	    GPIO_ResetBits(settings.coolport, settings.coolpin);
-	    GPIO_SetBits(settings.heatport, settings.heatpin);
+	    cool = 0;
+	    heat = 1;
 	    break;
 
 	default:
@@ -342,10 +351,20 @@
 	    /* fallthrough */
 
 	case 'i':
-	    GPIO_ResetBits(settings.coolport, settings.coolpin);
-	    GPIO_ResetBits(settings.heatport, settings.heatpin);
+	    cool = 0;
+	    heat = 0;
 	    break;
     }
+    
+    if (cool ^ settings.coolinv)
+	GPIO_SetBits(char2port(settings.coolport), 1 << settings.coolpin);
+    else
+	GPIO_ResetBits(char2port(settings.coolport), 1 << settings.coolpin);
+
+    if (heat ^ settings.heatinv)
+	GPIO_SetBits(char2port(settings.heatport), 1 << settings.heatpin);
+    else
+	GPIO_ResetBits(char2port(settings.heatport), 1 << settings.heatpin);
 }
 
 /* Handle user command
@@ -411,7 +430,8 @@
 	       "Mode - %c, Target - %d, Hystersis - %d\r\n"
 	       "Min heat overshoot - %d, Min cool overshoot - %d\r\n"
 	       "Min cool on time - %d, Min cool off time - %d\r\n"
-	       "Min heat on time - %d, Min heat off time - %d\r\n",
+	       "Min heat on time - %d, Min heat off time - %d\r\n"
+	       "Cool: Port %c Pin %d Inv %d, Heat: Port %c Pin %d Inv %d\r\n",
 	       settings.fermenter_ROM[0], settings.fermenter_ROM[1], settings.fermenter_ROM[2], settings.fermenter_ROM[3], 	
 	       settings.fermenter_ROM[4], settings.fermenter_ROM[5], settings.fermenter_ROM[6], settings.fermenter_ROM[7], 	
 	       settings.fridge_ROM[0], settings.fridge_ROM[1], settings.fridge_ROM[2], settings.fridge_ROM[3], 
@@ -421,7 +441,9 @@
 	       settings.mode, settings.target_temp, settings.hysteresis,
 	       settings.minheatovershoot, settings.mincoolovershoot,
 	       settings.mincoolontime, settings.minheatontime,
-	       settings.minheatontime, settings.minheatofftime
+	       settings.minheatontime, settings.minheatofftime,
+	       settings.coolport, settings.coolpin, settings.coolinv,
+	       settings.heatport, settings.heatpin, settings.heatinv
 	    );
 	return;
     }
@@ -526,3 +548,26 @@
 	    break;
     }
 }
+
+/* Convert a port name into a number */
+static GPIO_TypeDef *
+char2port(char port) {
+    char p;
+    
+    p = toupper(port);
+    assert(p >= 'A' && p <= 'E');
+    
+    switch (p) {
+	case 'A':
+	    return GPIOA;
+	case 'B':
+	    return GPIOB;
+	case 'C':
+	    return GPIOC;
+	case 'D':
+	    return GPIOD;
+	case 'E':
+	    return GPIOE;
+    }
+}
+