diff options
-rw-r--r-- | app.cpp | 190 |
1 files changed, 96 insertions, 94 deletions
@@ -87,100 +87,6 @@ void sm_on_disconnect() { digitalWrite(LED_PIN, LOW); } -#if PERSISTENT_CONFIGURATION_SUPPORT == 1 -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 1 + len; -} - -static int load_string(int index, const uint8_t max_length, uint8_t& length, char *chars) { - length = EEPROM.read(index++); - - uint8_t len = min(length, max_length); - for (int i = 0; i < len; i++) { - chars[i] = EEPROM.read(index++); - } - - return 1 + len; -} - -/** - * 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; - 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"); -} - -static void setup_settings() { - if (!load_settings()) { - Serial.println("Could not load settings, storing defaults"); - store_settings(); - } else { - Serial.println("Settings loaded"); - } - - for (int i = 0; i < SENSOR_COUNT; i++) { - Serial.print("Sensor #"); - Serial.print(i, DEC); - - 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.print(": update_interval="); - Serial.print(s.update_interval, DEC); - Serial.print(", warning_value="); - Serial.print(s.warning_value, DEC); - - Serial.println(); - } - - Serial.println("setup_settings: done"); -} -#else -static void setup_settings() {} -#endif // PERSISTENT_CONFIGURATION_SUPPORT - void sm_loop() { static unsigned long last = 0, now; @@ -450,5 +356,101 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { notify_soil_moisture(res, body_len); } + +#if PERSISTENT_CONFIGURATION_SUPPORT == 1 +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 1 + len; +} + +static int load_string(int index, const uint8_t max_length, uint8_t& length, char *chars) { + length = EEPROM.read(index++); + + uint8_t len = min(length, max_length); + for (int i = 0; i < len; i++) { + chars[i] = EEPROM.read(index++); + } + + return 1 + len; +} + +/** + * 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; + 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"); +} + + +static void setup_settings() { + if (!load_settings()) { + Serial.println("Could not load settings, storing defaults"); + store_settings(); + } else { + Serial.println("Settings loaded"); + } + + for (int i = 0; i < SENSOR_COUNT; i++) { + Serial.print("Sensor #"); + Serial.print(i, DEC); + + 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.print(": update_interval="); + Serial.print(s.update_interval, DEC); + Serial.print(", warning_value="); + Serial.print(s.warning_value, DEC); + + Serial.println(); + } + + Serial.println("setup_settings: done"); +} +#else +static void setup_settings() {} +#endif // PERSISTENT_CONFIGURATION_SUPPORT + // vim: set ft=arduino: |