Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Restructured control blocks to use only EEPROM

  • Loading branch information...
commit 275c1b04de1f41eb5fd042c52beacc1d6cf41c41 1 parent fed922f
@johnmckerrell authored
View
97 YAHMS_Config.cpp
@@ -49,8 +49,6 @@ FLASH_STRING(OUTPUT_PINS_ARE,"Output pins are: ");
FLASH_STRING(CONTROL_BLOCKS,"Control Blocks:\n");
#endif
-ControlBlock *firstControlBlock = NULL, *lastControlBlock = NULL;
-
boolean configPresent = false;
unsigned long int configTime = 0;
@@ -62,31 +60,6 @@ char xbeeRX = -1, xbeeTX = -1;
byte settings[NUM_SETTINGS];
-void LogControlBlock(ControlBlock *currentControlBlock) {
- #ifdef LOGGING
- Serial.print(currentControlBlock->minute,DEC);
- BLANK.print(Serial);
- Serial.print(currentControlBlock->hour,DEC);
- BLANK.print(Serial);
- Serial.print(currentControlBlock->day,DEC);
- BLANK.print(Serial);
- Serial.print(currentControlBlock->month,DEC);
- BLANK.print(Serial);
- Serial.print(currentControlBlock->weekday,DEC);
- BLANK.print(Serial);
- Serial.print(currentControlBlock->len,DEC);
- BLANK.print(Serial);
- Serial.print(currentControlBlock->pin,DEC);
- BLANK.print(Serial);
- if (currentControlBlock->state) {
- PROG_ON.print(Serial);
- } else {
- PROG_OFF.print(Serial);
- }
- Serial.println();
- #endif
-}
-
void CheckAndUpdateConfig() {
/*
Do we have any config, or is the config too old?
@@ -268,20 +241,10 @@ void CheckAndUpdateConfig() {
CONFIG_SIZE_IS.print(Serial);
Serial.println(configSize);
#endif
- ControlBlock *currentControlBlock = NULL;
- while (firstControlBlock) {
- currentControlBlock = firstControlBlock;
- firstControlBlock = firstControlBlock->next;
- free(currentControlBlock);
- }
- lastControlBlock = NULL;
// Now parse the EEPROM contents line by line
int i = 0;
boolean xbeeChanged = false;
- currentControlBlock = NULL;
- int controlBlockValues[8];
- boolean startedControlBlock = false;
int intVal = -1;
int intIndex = 0;
byte c = '\0';
@@ -291,11 +254,6 @@ void CheckAndUpdateConfig() {
if (i < 13) {
// Ignore it
} else if (isdigit(c)) {
- if (lineMode == 'C' && !startedControlBlock) {
- memset(controlBlockValues,-1,sizeof(controlBlockValues));
- startedControlBlock = true;
- //Serial.println("Initialising controlBlockValues");
- }
if (intVal == -1)
intVal = 0;
intVal = (10*intVal) + (c - '0');
@@ -306,9 +264,7 @@ void CheckAndUpdateConfig() {
intVal = -1;
intIndex = 0;
} else if (c == '*' && lineMode == 'C') {
- if (intIndex < sizeof(controlBlockValues)) {
- controlBlockValues[intIndex] = -1;
- }
+ // Not important right now, control block specific
intVal = -1;
++intIndex;
} else if (intVal != -1) {
@@ -347,15 +303,7 @@ void CheckAndUpdateConfig() {
}
break;
case 'C':
- if (intIndex < sizeof(controlBlockValues)) {
- /*
- Serial.print("Storing controlBlockValues[");
- Serial.print(intIndex);
- Serial.print("]=");
- Serial.println(intVal);
- */
- controlBlockValues[intIndex] = intVal;
- }
+ // Ignore control blocks for now
break;
}
intVal = -1;
@@ -384,41 +332,10 @@ void CheckAndUpdateConfig() {
}
break;
case 'C':
- // Check that the length of the time block is greater than zero
- // and that the pin that's being turned on is zero or more
- if (controlBlockValues[5] > 0 && controlBlockValues[6] >= 0) {
- currentControlBlock = (ControlBlock*)malloc(sizeof(ControlBlock));
- // Copy the integer values into a ControlBlock
- currentControlBlock->minute = controlBlockValues[0];
- currentControlBlock->hour = controlBlockValues[1];
- currentControlBlock->day = controlBlockValues[2];
- currentControlBlock->month = controlBlockValues[3];
- currentControlBlock->weekday = controlBlockValues[4];
- currentControlBlock->len = controlBlockValues[5];
- currentControlBlock->pin = controlBlockValues[6];
- currentControlBlock->state = controlBlockValues[7];
- if (lastControlBlock) {
- lastControlBlock->next = currentControlBlock;
- }
- currentControlBlock->next = NULL;
- lastControlBlock = currentControlBlock;
- if (!firstControlBlock) {
- firstControlBlock = currentControlBlock;
- }
- } else {
- /*
- Serial.println("Not storing controlBlockValues");
- Serial.print("5=");
- Serial.println(controlBlockValues[5]);
- Serial.print("6=");
- Serial.println(controlBlockValues[6]);
- */
- //Serial.println(controlBlockValues);
- }
+ // Ignore control blocks for now
break;
}
lineMode = '\0';
- startedControlBlock = false;
}
++i;
}
@@ -483,14 +400,6 @@ void CheckAndUpdateConfig() {
Serial.println();
#endif
- currentControlBlock = firstControlBlock;
- #ifdef LOGGING
- CONTROL_BLOCKS.print(Serial);
- while (currentControlBlock) {
- LogControlBlock(currentControlBlock);
- currentControlBlock = currentControlBlock->next;
- }
- #endif
}
}
View
23 YAHMS_Config.h
@@ -47,27 +47,6 @@ extern XBee xbee;
extern SoftwareSerial *xbeeSerial;
extern char outputPins[];
extern byte settings[];
+extern boolean configPresent;
-#ifndef YAHMS_CONFIG_H
-#define YAHMS_CONFIG_H
-
-struct ControlBlock {
- char minute;
- char hour;
- char day;
- char month;
- char weekday;
- int len;
- char pin;
- boolean state;
- struct ControlBlock *next;
-};
-
-typedef struct ControlBlock ControlBlock;
-
-#endif
-
-extern ControlBlock *firstControlBlock;
-
-void LogControlBlock(ControlBlock *currentControlBlock);
void CheckAndUpdateConfig();
View
206 YAHMS_Controller.cpp
@@ -13,8 +13,26 @@ FLASH_STRING(PROG_HIGH,"HIGH");
FLASH_STRING(PROG_LOW,"LOW");
#endif
+#define CB_MINUTE 0
+#define CB_HOUR 1
+#define CB_DAY 2
+#define CB_MONTH 3
+#define CB_WEEKDAY 4
+#define CB_LEN 5
+#define CB_PIN 6
+#define CB_STATE 7
+
+
int currentPinState[NUM_OUTPUT_PINS];
+uint8_t * heapptr, * stackptr;
+void check_mem() {
+ stackptr = (uint8_t *)malloc(4); // use stackptr temporarily
+ heapptr = stackptr; // save value of heap pointer
+ free(stackptr); // free up the memory again (sets stackptr to 0)
+ stackptr = (uint8_t *)(SP); // save value of stack pointer
+}
+
void SetupController() {
for (int i = 0; i < NUM_OUTPUT_PINS; ++i) {
currentPinState[i] = -1;
@@ -22,75 +40,155 @@ void SetupController() {
}
void CheckAndUpdateState() {
+ if (!configPresent)
+ return;
+
unsigned long int t = now();
+ Serial.flush();
+ check_mem();
+ Serial.print("heapptr=");
+ Serial.println((int)heapptr,DEC);
+ Serial.print("stackptr=");
+ Serial.println((int)stackptr,DEC);
+Serial.flush();
// Add timezone/DST
if (settings[TIME_OFFSET_MINS_SETTING] > 0) {
t += settings[TIME_OFFSET_MINS_SETTING]*60;
}
int minsSinceMidnight = hour(t)*60+minute(t);
- ControlBlock *currentControlBlock = NULL;
for (int i = 0; i < NUM_OUTPUT_PINS; ++i) {
//Serial.print("i=");
//Serial.println(i);
if (outputPins[i] == -1)
continue;
- int pinState = -1;
- for (currentControlBlock = firstControlBlock;currentControlBlock;currentControlBlock = currentControlBlock->next) {
- if (currentControlBlock->pin != outputPins[i])
- continue;
-
- if (currentControlBlock->day != -1 && currentControlBlock->day != day(t))
- continue;
- if (currentControlBlock->month != -1 && currentControlBlock->month != month(t))
- continue;
-
- if (currentControlBlock->weekday != -1) {
- if (currentControlBlock->weekday == 8 && (weekday(t) == 1 || weekday(t) == 7) ) {
- // dow is valid - weekend
- } else if (currentControlBlock->weekday == 9 && weekday(t) >= 2 && weekday(t) <= 6) {
- // dow is valid - weekday
- } else if (currentControlBlock->weekday != weekday(t)) {
- // dow not valid, skip this one
- continue;
+ int pinState = -1;
+
+ unsigned long int configSize= 0;
+ int confi = 0;
+ for (confi = 9; confi < 13; ++confi) {
+ configSize = (256 * configSize) + EEPROM.read(confi);
+ }
+
+ int controlBlockValues[8];
+ int intVal = -1;
+ int intIndex = 0;
+ byte c = '\0';
+ char lineMode = '\0';
+ while (confi < configSize+13) {
+ c = EEPROM.read(confi);
+ if (confi < 13) {
+ // Ignore it
+ } else if (isdigit(c)) {
+ if (intVal == -1)
+ intVal = 0;
+ intVal = (10*intVal) + (c - '0');
+ } else if (lineMode == '\0') {
+ //Serial.print("lineMode=");
+ lineMode = c;
+ //Serial.println(lineMode);
+ intVal = -1;
+ intIndex = 0;
+ if (lineMode == 'C') {
+ memset(controlBlockValues,-1,sizeof(controlBlockValues));
}
- }
- if (currentControlBlock->hour != -1) {
- if (currentControlBlock->minute != -1) {
- int mins = (currentControlBlock->hour*60)+currentControlBlock->minute;
- if (mins <= minsSinceMidnight && (mins+currentControlBlock->len) > minsSinceMidnight) {
- pinState = currentControlBlock->state ? 1 : 0;
- #ifdef LOGGING
- ACTIVE_CONTROL_BLOCK.print(Serial);
- LogControlBlock(currentControlBlock);
- #endif
+ } else if (c == '*' && lineMode == 'C') {
+ if (intIndex < sizeof(controlBlockValues)) {
+ controlBlockValues[intIndex] = -1;
}
- } else if (currentControlBlock->hour == hour(t)) {
- pinState = currentControlBlock->state ? 1 : 0;
- #ifdef LOGGING
- ACTIVE_CONTROL_BLOCK.print(Serial);
- LogControlBlock(currentControlBlock);
- #endif
- }
- } else if (currentControlBlock->minute != -1) {
- if (currentControlBlock->minute <= minute(t) && (currentControlBlock->minute + currentControlBlock->len) > minute(t)) {
- pinState = currentControlBlock->state ? 1 : 0;
- #ifdef LOGGING
- ACTIVE_CONTROL_BLOCK.print(Serial);
- LogControlBlock(currentControlBlock);
- #endif
+ intVal = -1;
+ ++intIndex;
+ } else if (intVal != -1) {
+ switch(lineMode) {
+ case 'C':
+ if (intIndex < sizeof(controlBlockValues)) {
+ controlBlockValues[intIndex] = intVal;
+ }
+ break;
}
- } else {
- //pinState = 0;
- //Serial.println("Turned off by control block:");
- //LogControlBlock(currentControlBlock);
+ intVal = -1;
+ ++intIndex;
}
- if (pinState == 0) {
- // Any "off" blocks override others
- //Serial.println("Turned off by control block:");
- break;
+ if (c == '\n' || c == '\r' ) {
+ switch(lineMode) {
+ case 'C':
+ // Check that the length of the time block is greater than zero
+ // and that the pin that's being turned on is zero or more
+ for (int f = 0, fl = sizeof(controlBlockValues); f < fl; ++f) { Serial.print(controlBlockValues[f]); Serial.print(":"); }
+ Serial.println();
+
+ if (controlBlockValues[CB_LEN] > 0 && controlBlockValues[CB_PIN] >= 0) {
+
+
+ if (controlBlockValues[CB_PIN] != outputPins[i])
+ continue;
+
+ if (controlBlockValues[CB_DAY] != -1 && controlBlockValues[CB_DAY] != day(t))
+ continue;
+
+ if (controlBlockValues[CB_MONTH] != -1 && controlBlockValues[CB_MONTH] != month(t))
+ continue;
+
+ if (controlBlockValues[CB_WEEKDAY] != -1) {
+ if (controlBlockValues[CB_WEEKDAY] == 8 && (weekday(t) == 1 || weekday(t) == 7) ) {
+ // dow is valid - weekend
+ } else if (controlBlockValues[CB_WEEKDAY] == 9 && weekday(t) >= 2 && weekday(t) <= 6) {
+ // dow is valid - weekday
+ } else if (controlBlockValues[CB_WEEKDAY] != weekday(t)) {
+ // dow not valid, skip this one
+ continue;
+ }
+ }
+ if (controlBlockValues[CB_HOUR] != -1) {
+ if (controlBlockValues[CB_MINUTE] != -1) {
+ int mins = (controlBlockValues[CB_HOUR]*60)+controlBlockValues[CB_MINUTE];
+ if (mins <= minsSinceMidnight && (mins+controlBlockValues[CB_LEN]) > minsSinceMidnight) {
+ pinState = controlBlockValues[CB_STATE] ? 1 : 0;
+ #ifdef LOGGING
+ ACTIVE_CONTROL_BLOCK.print(Serial);
+ for (int f = 0, fl = sizeof(controlBlockValues); f < fl; ++f) { Serial.print(controlBlockValues[f]); Serial.print(":"); }
+ Serial.println();
+ #endif
+ }
+ } else if (controlBlockValues[CB_HOUR] == hour(t)) {
+ pinState = controlBlockValues[CB_STATE] ? 1 : 0;
+ #ifdef LOGGING
+ ACTIVE_CONTROL_BLOCK.print(Serial);
+ for (int f = 0, fl = sizeof(controlBlockValues); f < fl; ++f) { Serial.print(controlBlockValues[f]); Serial.print(":"); }
+ Serial.println();
+ #endif
+ }
+ } else if (controlBlockValues[CB_MINUTE] != -1) {
+ if (controlBlockValues[CB_MINUTE] <= minute(t) && (controlBlockValues[CB_MINUTE] + controlBlockValues[CB_LEN]) > minute(t)) {
+ pinState = controlBlockValues[CB_STATE] ? 1 : 0;
+ #ifdef LOGGING
+ ACTIVE_CONTROL_BLOCK.print(Serial);
+ for (int f = 0, fl = sizeof(controlBlockValues); f < fl; ++f) { Serial.print(controlBlockValues[f]); Serial.print(":"); }
+ Serial.println();
+ #endif
+ }
+ } else {
+ //pinState = 0;
+ //Serial.println("Turned off by control block:");
+ //LogControlBlock(currentControlBlock);
+ }
+ if (pinState == 0) {
+ // Any "off" blocks override others
+ //Serial.println("Turned off by control block:");
+ break;
+ }
+
+ }
+ break;
+ }
+ lineMode = '\0';
+ if (pinState == 0) {
+ break;
+ }
}
+ ++confi;
}
+
if (pinState == -1)
pinState = 0;
if (pinState != currentPinState[i]) {
@@ -111,4 +209,10 @@ void CheckAndUpdateState() {
currentPinState[i] = pinState;
}
}
+ check_mem();
+ Serial.print("heapptr=");
+ Serial.println((int)heapptr,DEC);
+ Serial.print("stackptr=");
+ Serial.println((int)stackptr,DEC);
+Serial.flush();
}
View
2  YAHMS_Defines.h
@@ -1,5 +1,5 @@
#define YAHMS_API_VERSION 1
-#define USERAGENT "YAHMS/0.1"
+#define USERAGENT "YAHMS/0.2"
#define YAHMS_SERVER "yahms.net"
#define RESPONSETIMEOUT 30
#define LOGGING
Please sign in to comment.
Something went wrong with that request. Please try again.