aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--app.cpp151
-rw-r--r--trygvisio_soil_moisture.ino3
3 files changed, 152 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 39aa27c..286c386 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ BOARD_TAG = leonardo
MONITOR_BAUDRATE = 115200
MONITOR_PORT ?= /dev/ttyACM0
-ARDUINO_LIBS = BLE SPI
+ARDUINO_LIBS = BLE SPI EEPROM
ARDMK_DIR ?= /usr/share/arduino
-include $(ARDMK_DIR)/Arduino.mk
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 <Arduino.h>
+#include <EEPROM.h>
#include <HardwareSerial.h>
// 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);
}
}
diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino
index a8a7d1f..388dd8a 100644
--- a/trygvisio_soil_moisture.ino
+++ b/trygvisio_soil_moisture.ino
@@ -1,3 +1,4 @@
+#include <EEPROM.h>
#include <SPI.h>
#include <lib_aci.h>
#include <aci_setup.h>
@@ -6,7 +7,7 @@
#include "app.h"
static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT;
-PROGMEM static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] = SETUP_MESSAGES_CONTENT;
+static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] PROGMEM = SETUP_MESSAGES_CONTENT;
static struct aci_state_t aci_state;
static hal_aci_evt_t aci_data;