changeset 66:0916bfe3f418

Rename settings to tc_settings as it's global.
author Daniel O'Connor <darius@dons.net.au>
date Sun, 14 Apr 2013 14:28:14 +0930
parents 662e7f01c991
children dcac5f08f87a
files tempctrl.c
diffstat 1 files changed, 65 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/tempctrl.c	Sun Apr 14 14:26:46 2013 +0930
+++ b/tempctrl.c	Sun Apr 14 14:28:14 2013 +0930
@@ -41,6 +41,8 @@
 #include "flash.h"
 #include "tempctrl.h"
 
+#define TEMPCTRL_FLASH_ADDRESS 	0	/* First sector */
+
 /* Holds all the settings needed */
 typedef struct {
     uint8_t	fermenter_ROM[8];
@@ -83,13 +85,13 @@
     char	logfilefmt[64];
 
     uint8_t	pad[1]; /* Pad to multiple of 4 bytes */
-} settings_t;
+} tc_settings_t;
 
 /* Current settings in RAM */
-static settings_t	settings;
+static tc_settings_t	tc_settings;
 
 /* Defaults that are shoved into SPI flash if it isn't inited */
-const settings_t	default_settings = {
+const tc_settings_t	default_tc_settings = {
     .fermenter_ROM = { 0x10, 0x4c, 0x7d, 0x53, 0x01, 0x08, 0x00, 0xff },
     .fridge_ROM =  { 0x10, 0x6d, 0x40, 0x53, 0x01, 0x08, 0x00, 0x16 },
     .ambient_ROM = { 0x10, 0x76, 0x05, 0x53, 0x01, 0x08, 0x00, 0x8e },
@@ -179,10 +181,10 @@
     if (t < checktime)
 	return;
     
-    checktime = t + settings.check_interval;
+    checktime = t + tc_settings.check_interval;
 
     /* Don't do any logging, just force idle and leave */
-    if (settings.mode == TC_MODE_NOTHING) {
+    if (tc_settings.mode == TC_MODE_NOTHING) {
 	nextstate = 'i';
 	goto skip;
     }
@@ -190,9 +192,9 @@
     /* Update our temperatures
      * Can take a while (800ms each!)
      */
-    tempt = OWGetTemp(settings.fermenter_ROM);
-    fridge_temp = OWGetTemp(settings.fridge_ROM);
-    ambient_temp = OWGetTemp(settings.ambient_ROM);
+    tempt = OWGetTemp(tc_settings.fermenter_ROM);
+    fridge_temp = OWGetTemp(tc_settings.fridge_ROM);
+    ambient_temp = OWGetTemp(tc_settings.ambient_ROM);
     
     /* We only care about this one, only update the value we decide on
      * only if it is valid
@@ -203,38 +205,38 @@
     }
     
     /* Check for stale data */
-    if (lastdata + (settings.check_interval * settings.stale_factor) < t)
+    if (lastdata + (tc_settings.check_interval * tc_settings.stale_factor) < t)
 	stale = 1;
 
     /* Default to remaining as we are */
     nextstate = '-';
 
     /* Temperature diff, -ve => too cold, +ve => too warm */
-    diff = fermenter_temp - settings.target_temp;
+    diff = fermenter_temp - tc_settings.target_temp;
 
     switch (currstate) {
 	case 'i':
 	    /* If we're idle then only heat or cool if the temperate difference is out of the
 	     * hysteresis band and the heater/cooler have been off long enough
 	     */
-	    if (abs(diff) > settings.hysteresis) {
-		if (diff < 0 && settings.minheatofftime + lastheatoff < t)
+	    if (abs(diff) > tc_settings.hysteresis) {
+		if (diff < 0 && tc_settings.minheatofftime + lastheatoff < t)
 		    nextstate = 'h';
-		else if (diff > 0 && settings.mincoolofftime + lastcooloff < t)
+		else if (diff > 0 && tc_settings.mincoolofftime + lastcooloff < t)
 		    nextstate = 'c';
 	    }
 	    break;
 	    
 	case 'c':
 	    /* Work out if we should go idle (based on min on time & overshoot) */
-	    if (diff + settings.mincoolovershoot < 0 &&
-		settings.mincoolontime + lastcoolon < t)
+	    if (diff + tc_settings.mincoolovershoot < 0 &&
+		tc_settings.mincoolontime + lastcoolon < t)
 		nextstate = 'i';
 	    break;
 
 	case 'h':
-	    if (diff - settings.minheatovershoot > 0 &&
-		settings.minheatontime + lastheaton < t)
+	    if (diff - tc_settings.minheatovershoot > 0 &&
+		tc_settings.minheatontime + lastheaton < t)
 		nextstate = 'i';
 	    break;
 
@@ -249,16 +251,16 @@
 	nextstate = 'i';
     
     /* Handle state forcing */
-    if (settings.mode != TC_MODE_AUTO)
+    if (tc_settings.mode != TC_MODE_AUTO)
 	forced = 1;
     else
 	forced = 0;
     
-    if (settings.mode == TC_MODE_IDLE)
+    if (tc_settings.mode == TC_MODE_IDLE)
 	nextstate = 'i';
-    else if (settings.mode == TC_MODE_HEAT)
+    else if (tc_settings.mode == TC_MODE_HEAT)
 	nextstate = 'h';
-    else if (settings.mode == TC_MODE_COOL)
+    else if (tc_settings.mode == TC_MODE_COOL)
 	nextstate = 'c';
 
     /* Keep track of when we last turned things on or off */
@@ -287,11 +289,11 @@
 	currstate = nextstate;
 
     
-    if (settings.mode != TC_MODE_NOTHING) {
+    if (tc_settings.mode != TC_MODE_NOTHING) {
 	localtime_r(&t, &tm);
 	p = linebuf;
 	p += strftime(p, sizeof(linebuf) - 1, "%Y/%m/%d %H:%M:%S: ", &tm);
-	p += fmttemp(p, "Tr", settings.target_temp, ", ");
+	p += fmttemp(p, "Tr", tc_settings.target_temp, ", ");
 	p += fmttemp(p, "Fm", tempt, ", ");
 	p += fmttemp(p, "Fr", fridge_temp, ", ");
 	p += fmttemp(p, "Am", ambient_temp, ", ");
@@ -299,8 +301,8 @@
 		forced ? "F" : "", 
 		stale ? "S" : "");
 	fputs(linebuf, stdout);
-	if (settings.logfilefmt[0] != '\0') {
-	    strftime(fbuf, sizeof(fbuf) - 1, settings.logfilefmt, &tm);
+	if (tc_settings.logfilefmt[0] != '\0') {
+	    strftime(fbuf, sizeof(fbuf) - 1, tc_settings.logfilefmt, &tm);
 	    
 	    if ((fserr = f_open(&f, fbuf, FA_WRITE | FA_OPEN_ALWAYS)) != FR_OK) {
 		printf("Failed to open file: %d\n", fserr);
@@ -337,8 +339,7 @@
  */
 static void
 tempctrl_load_or_init_settings(void) {
-    /* XXX: todo */
-    if (!flashreadblock(0, sizeof(settings), &settings)) {
+    if (!flashreadblock(TEMPCTRL_FLASH_ADDRESS, sizeof(tc_settings), &tc_settings)) {
 	fputs("CRC fails, loading defaults\n", stdout);
 	tempctrl_default_settings();
 	tempctrl_write_settings();
@@ -348,13 +349,13 @@
 /* Load in the defaults from flash */
 static void
 tempctrl_default_settings(void) {
-    memcpy(&settings, &default_settings, sizeof(settings_t));
+    memcpy(&tc_settings, &default_tc_settings, sizeof(tc_settings_t));
 }
 
 /* Write the current settings out to SPI flash */
 static void
 tempctrl_write_settings(void) {
-    flashwriteblock(0, sizeof(settings), &settings);
+    flashwriteblock(TEMPCTRL_FLASH_ADDRESS, sizeof(tc_settings), &tc_settings);
 }
 
 /* Set the relays to match the desired state */
@@ -383,15 +384,15 @@
 	    break;
     }
     
-    if (cool ^ settings.coolinv)
-	GPIO_SetBits(char2port(settings.coolport), 1 << settings.coolpin);
+    if (cool ^ tc_settings.coolinv)
+	GPIO_SetBits(char2port(tc_settings.coolport), 1 << tc_settings.coolpin);
     else
-	GPIO_ResetBits(char2port(settings.coolport), 1 << settings.coolpin);
+	GPIO_ResetBits(char2port(tc_settings.coolport), 1 << tc_settings.coolpin);
 
-    if (heat ^ settings.heatinv)
-	GPIO_SetBits(char2port(settings.heatport), 1 << settings.heatpin);
+    if (heat ^ tc_settings.heatinv)
+	GPIO_SetBits(char2port(tc_settings.heatport), 1 << tc_settings.heatpin);
     else
-	GPIO_ResetBits(char2port(settings.heatport), 1 << settings.heatpin);
+	GPIO_ResetBits(char2port(tc_settings.heatport), 1 << tc_settings.heatpin);
 }
 
 /* Handle user command
@@ -460,19 +461,19 @@
 	       "Min heat on time - %d, Min heat off time - %d\n"
 	       "Cool: Port %c Pin %d Inv %d, Heat: Port %c Pin %d Inv %d\n"
 	       "Log format: %s\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], 
-	       settings.fridge_ROM[4], settings.fridge_ROM[5], settings.fridge_ROM[6], settings.fridge_ROM[7], 
-	       settings.ambient_ROM[0], settings.ambient_ROM[1], settings.ambient_ROM[2], settings.ambient_ROM[3], 
-	       settings.ambient_ROM[4], settings.ambient_ROM[5], settings.ambient_ROM[6], settings.ambient_ROM[7], 
-	       settings.mode, settings.target_temp, settings.hysteresis,
-	       settings.minheatovershoot, settings.mincoolovershoot,
-	       settings.mincoolontime, settings.minheatontime,
-	       settings.minheatontime, settings.minheatofftime,
-	       settings.coolport, settings.coolpin, settings.coolinv,
-	       settings.heatport, settings.heatpin, settings.heatinv,
-	       settings.logfilefmt[0] == '\0' ? "none" : settings.logfilefmt
+	       tc_settings.fermenter_ROM[0], tc_settings.fermenter_ROM[1], tc_settings.fermenter_ROM[2], tc_settings.fermenter_ROM[3], 	
+	       tc_settings.fermenter_ROM[4], tc_settings.fermenter_ROM[5], tc_settings.fermenter_ROM[6], tc_settings.fermenter_ROM[7], 	
+	       tc_settings.fridge_ROM[0], tc_settings.fridge_ROM[1], tc_settings.fridge_ROM[2], tc_settings.fridge_ROM[3], 
+	       tc_settings.fridge_ROM[4], tc_settings.fridge_ROM[5], tc_settings.fridge_ROM[6], tc_settings.fridge_ROM[7], 
+	       tc_settings.ambient_ROM[0], tc_settings.ambient_ROM[1], tc_settings.ambient_ROM[2], tc_settings.ambient_ROM[3], 
+	       tc_settings.ambient_ROM[4], tc_settings.ambient_ROM[5], tc_settings.ambient_ROM[6], tc_settings.ambient_ROM[7], 
+	       tc_settings.mode, tc_settings.target_temp, tc_settings.hysteresis,
+	       tc_settings.minheatovershoot, tc_settings.mincoolovershoot,
+	       tc_settings.mincoolontime, tc_settings.minheatontime,
+	       tc_settings.minheatontime, tc_settings.minheatofftime,
+	       tc_settings.coolport, tc_settings.coolpin, tc_settings.coolinv,
+	       tc_settings.heatport, tc_settings.heatpin, tc_settings.heatinv,
+	       tc_settings.logfilefmt[0] == '\0' ? "none" : tc_settings.logfilefmt
 	    );
 	return;
     }
@@ -488,7 +489,7 @@
 	    case TC_MODE_COOL:
 	    case TC_MODE_IDLE:
 	    case TC_MODE_NOTHING:
-		settings.mode = argv[1][0];
+		tc_settings.mode = argv[1][0];
 		break;
 		    
 	    default:
@@ -511,22 +512,22 @@
 	    printf("Unable to parse ROM ID\n");
 	} else {
 	    if (!strcasecmp(argv[0], "ferm"))
-		memcpy(&settings.fermenter_ROM, ROM, sizeof(ROM));
+		memcpy(&tc_settings.fermenter_ROM, ROM, sizeof(ROM));
 	    if (!strcasecmp(argv[0], "frg"))
-		memcpy(&settings.fridge_ROM, ROM, sizeof(ROM));
+		memcpy(&tc_settings.fridge_ROM, ROM, sizeof(ROM));
 	    if (!strcasecmp(argv[0], "amb"))
-		memcpy(&settings.ambient_ROM, ROM, sizeof(ROM));
+		memcpy(&tc_settings.ambient_ROM, ROM, sizeof(ROM));
 	}
 	return;
     }
     if (!strcasecmp(argv[0], "log")) {
 	if (argc == 1)
-	    bzero(settings.logfilefmt, sizeof(settings.logfilefmt));
+	    bzero(tc_settings.logfilefmt, sizeof(tc_settings.logfilefmt));
 	else {
-	    if (strlen(argv[1]) > sizeof(settings.logfilefmt) - 1)
-		printf("New path too log (%d > %d)\n", strlen(argv[1]), sizeof(settings.logfilefmt));
+	    if (strlen(argv[1]) > sizeof(tc_settings.logfilefmt) - 1)
+		printf("New path too log (%d > %d)\n", strlen(argv[1]), sizeof(tc_settings.logfilefmt));
 	    else
-		strcpy(settings.logfilefmt, argv[1]);
+		strcpy(tc_settings.logfilefmt, argv[1]);
 	}
 	return;
     }
@@ -544,21 +545,21 @@
     }
 
     if (!strcasecmp(argv[1], "targ")) {
-	settings.target_temp = data;
+	tc_settings.target_temp = data;
     } else if (!strcasecmp(argv[1], "hys")) {
-	settings.hysteresis = data;
+	tc_settings.hysteresis = data;
     } else if (!strcasecmp(argv[1], "mhov")) {
-	settings.minheatovershoot = data;
+	tc_settings.minheatovershoot = data;
     } else if (!strcasecmp(argv[1], "mcov")) {
-	settings.mincoolovershoot = data;
+	tc_settings.mincoolovershoot = data;
     } else if (!strcasecmp(argv[1], "mcon")) {
-	settings.mincoolontime = data;
+	tc_settings.mincoolontime = data;
     } else if (!strcasecmp(argv[1], "mcoff")) {
-	settings.mincoolofftime = data;
+	tc_settings.mincoolofftime = data;
     } else if (!strcasecmp(argv[1], "mhon")) {
-	settings.minheatontime = data;
+	tc_settings.minheatontime = data;
     } else if (!strcasecmp(argv[1], "mhoff")) {
-	settings.minheatofftime = data;
+	tc_settings.minheatofftime = data;
     } else {
 	printf("Unknown setting\n");
     }