From 08c4dca9cb42d419c471d8117848515e8100d614 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 26 Feb 2015 20:46:13 +0100 Subject: o Support for prototype-b board. --- app.cpp | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 149 insertions(+), 2 deletions(-) (limited to 'app.cpp') diff --git a/app.cpp b/app.cpp index 65b4b73..05feaaa 100644 --- a/app.cpp +++ b/app.cpp @@ -1,15 +1,17 @@ #include "app.h" #include +#include #include // http://bleaklow.com/2010/09/05/progmem_and_gcc_bug_34734.html #undef PROGMEM #define PROGMEM __attribute__((section(".progmem.data"))) -#define SENSOR_COUNT 2 #define LED_PIN 13 +#define EEPROM_MAGIC 0x5a + // See http://redbearlab.com/blendmicro/ for pins. struct sm_sensor { uint8_t a_pin; @@ -20,10 +22,22 @@ struct sm_sensor { uint16_t update_interval; uint8_t name_length; char name[SENSOR_NAME_LEN]; + +#define PROTOTYPE_VERSION 2 +#define SENSOR_COUNT 4 + } sensors[SENSOR_COUNT] = { // A Pin, D1 Pin, D2 Pin, Value, Warning Value, Update Interval, Length of Name, Name + +#if PROTOTYPE_VERSION == 1 { 8, 3, 5, 0, 10, 0, 9, "Sensor #1"}, { 10, 11, 12, 0, 10, 0, 9, "Sensor #2"}, +#elif PROTOTYPE_VERSION == 2 + { A0, 1, 2, 0, 10, 0, 3, "wat"}, + { A1, 3, 5, 0, 10, 0, 4, "woot"}, + { A2, 8, 10, 0, 10, 0, 3, "foo"}, + { A3, 11, 12, 0, 10, 0, 3, "bar"}, +#endif }; static unsigned long next_update[SENSOR_COUNT]; @@ -34,7 +48,63 @@ static boolean connected; static boolean blink; static unsigned long last_blink; +static boolean load_settings(); +static void store_settings(); +static void set_default_settings(); + void sm_setup() { + /* + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 16; j++) { + uint8_t value = EEPROM.read(i * 16 + j); + Serial.print(value, HEX); + Serial.print(" "); + Serial.print((char)value); + Serial.print(" "); + } + Serial.println(); + } + + for (int i = 0; i < SENSOR_COUNT; i++) { + struct sm_sensor s = sensors[i]; + + Serial.print("name len="); + Serial.print(s.name_length, DEC); + Serial.print(": ->"); + Serial.write((const uint8_t*) s.name, s.name_length); + Serial.print("<-"); + Serial.println(); + } +*/ +// set_default_settings(); + /* + if (!load_settings()) { + Serial.println("setting default settings"); + set_default_settings(); + + Serial.println("saving settings"); + store_settings(); + } else { + Serial.println("Settings loaded"); + } + /**/ +/**/ + for (int i = 0; i < SENSOR_COUNT; i++) { + Serial.print("XX: i="); + Serial.print(i, DEC); + + struct sm_sensor& s = sensors[i]; + + Serial.print(", name len="); + Serial.print(s.name_length, DEC); + Serial.print(": ->"); + Serial.write(s.name, s.name_length); + Serial.print("<-"); + Serial.println(); + } + /**/ + Serial.println("done!!"); + for (int i = 0; i < SENSOR_COUNT; i++) { struct sm_sensor s = sensors[i]; @@ -63,6 +133,83 @@ void sm_on_disconnect() { digitalWrite(LED_PIN, LOW); } +static int store_string(int index, uint8_t len, char* chars) { + EEPROM.write(index++, len); + + for (int i = 0; i < len; i++) { + EEPROM.write(index++, chars[i]); + } + + return index + 1 + len; +} + +static int load_string(int index, uint8_t max_length, uint8_t& length, char* chars) { + uint8_t stored_len = EEPROM.read(index++); + + uint8_t len = min(stored_len, max_length); + for (int i = 0; i < stored_len; i++) { + chars[i] = EEPROM.read(index++); + } + + length = len; + + return stored_len; +} + +static void set_default_settings() { +#if SENSOR_COUNT > 10 +#error SENSOR_COUNT cannot be bigger than 10 +#endif + + for (int i = 0; i < SENSOR_COUNT; i++) { + sensors[i].warning_value = 700; + + int idx = 8; + strcpy(sensors[i].name, "123456 #"); + sensors[i].name[idx++] = '0' + i; + sensors[i].name_length = idx; + } +} + +/** + * Returns true if the settings was successfully read. + */ +static boolean load_settings() { + int index = 0; + uint8_t magic = EEPROM.read(index++); + + // Check to see if the EEPROM contains a magic byte indicating if we have written anything before. + if (magic != EEPROM_MAGIC) { + return false; + } + + for (int i = 0; i < SENSOR_COUNT; i++) { + struct sm_sensor s = sensors[i]; + + s.warning_value = EEPROM.read(index++); + s.warning_value += EEPROM.read(index++) << 8; + s.name_length = EEPROM.read(index++); + index += load_string(index, SENSOR_NAME_LEN, s.name_length, s.name); + } + + return true; +} + +static void store_settings() { + int index = 0; + EEPROM.write(index++, EEPROM_MAGIC); + + for (int i = 0; i < SENSOR_COUNT; i++) { + struct sm_sensor s = sensors[i]; + + EEPROM.write(index++, s.warning_value); + EEPROM.write(index++, s.warning_value >> 8); + index += store_string(index, s.name_length, s.name); + } + + Serial.println("Settings saved"); +} + void sm_loop() { static unsigned long last = 0, now; @@ -74,7 +221,7 @@ void sm_loop() { last_blink = now; if (connected) { blink = !blink; - digitalWrite(LED_PIN, _blink ? HIGH : LOW); + digitalWrite(LED_PIN, blink ? HIGH : LOW); } } -- cgit v1.2.3