From 3f99167f0c986b2cad9558ab28ec0563ad737eab Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 21 Jun 2015 14:31:06 +0200 Subject: o Getting the loading/saving of settings to work. o Doing sm_setup() once only, doesn't depend on the RF parts. --- app.cpp | 69 ++++++++++++++++++----------------------------------------------- 1 file changed, 19 insertions(+), 50 deletions(-) (limited to 'app.cpp') diff --git a/app.cpp b/app.cpp index 2d84700..5e45c6e 100644 --- a/app.cpp +++ b/app.cpp @@ -52,17 +52,10 @@ static boolean connected; static boolean blink; static unsigned long last_blink; -#if PERSISTENT_CONFIGURATION_SUPPORT == 1 -static boolean setup_settings(); -static boolean load_settings(); -static void store_settings(); -static void set_default_settings(); -#endif +static void setup_settings(); void sm_setup() { -#if PERSISTENT_CONFIGURATION_SUPPORT == 1 setup_settings(); -#endif for (int i = 0; i < SENSOR_COUNT; i++) { struct sm_sensor s = sensors[i]; @@ -73,6 +66,8 @@ void sm_setup() { } pinMode(LED_PIN, OUTPUT); + + Serial.println("sm_setup: done"); } void sm_on_connect() { @@ -93,27 +88,25 @@ void sm_on_disconnect() { } #if PERSISTENT_CONFIGURATION_SUPPORT == 1 -static int store_string(int index, uint8_t len, char* chars) { +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; + return 1 + len; } -static int load_string(int index, uint8_t max_length, uint8_t& length, char* chars) { - uint8_t stored_len = EEPROM.read(index++); +static int load_string(int index, const uint8_t max_length, uint8_t& length, char *chars) { + length = EEPROM.read(index++); - uint8_t len = min(stored_len, max_length); - for (int i = 0; i < stored_len; i++) { + uint8_t len = min(length, max_length); + for (int i = 0; i < len; i++) { chars[i] = EEPROM.read(index++); } - length = len; - - return stored_len; + return 1 + len; } static void set_default_settings() { @@ -124,8 +117,8 @@ static void set_default_settings() { for (int i = 0; i < SENSOR_COUNT; i++) { sensors[i].warning_value = 700; - int idx = 8; - strcpy(sensors[i].name, "123456 #"); + int idx = 9; + strcpy(sensors[i].name, "Sensor #"); sensors[i].name[idx++] = '0' + i; sensors[i].name_length = idx; } @@ -148,7 +141,6 @@ static boolean load_settings() { 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); } @@ -171,41 +163,16 @@ static void store_settings() { } static void setup_settings() { - /* - 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"); + Serial.println("Setting default settings"); set_default_settings(); - Serial.println("saving 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); @@ -219,9 +186,11 @@ static void setup_settings() { Serial.print("<-"); Serial.println(); } - /**/ - Serial.println("done"); + + Serial.println("setup_settings: done"); } +#else +static void setup_settings() {} #endif // PERSISTENT_CONFIGURATION_SUPPORT void sm_loop() { -- cgit v1.2.3