aboutsummaryrefslogtreecommitdiff
path: root/app.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'app.cpp')
-rw-r--r--app.cpp92
1 files changed, 42 insertions, 50 deletions
diff --git a/app.cpp b/app.cpp
index cd38d66..81a9622 100644
--- a/app.cpp
+++ b/app.cpp
@@ -1,8 +1,6 @@
#include "app.h"
-#include <Arduino.h>
#include <EEPROM.h>
-#include <HardwareSerial.h>
#if SM_BOARD_VERSION == 1
#define SENSOR_COUNT 2
@@ -18,7 +16,7 @@
#define LED_PIN 13
-#define EEPROM_MAGIC 0x5a
+#define EEPROM_MAGIC 0xa5
// See http://redbearlab.com/blendmicro/ for pins.
struct sm_sensor {
@@ -35,7 +33,7 @@ struct sm_sensor {
#if SM_BOARD_VERSION == 1
{ 8, 3, 5, 0, 200, 3000, 9, "Sensor #1"},
- { 10, 11, 12, 0, 200, 3000, 9, "Sensor #2"},
+ { 10, 11, 12, 0, 200, 5000, 9, "Sensor #2"},
#elif SM_BOARD_VERSION == 2
{ A0, 1, 2, 0, 200, 3000, 3, "wat"},
{ A1, 3, 5, 0, 200, 3000, 4, "woot"},
@@ -48,8 +46,8 @@ static unsigned long next_update[SENSOR_COUNT];
// How often the connected light should toggle, in ms
#define BLINK_WAIT 500
-static boolean connected;
-static boolean blink;
+static bool connected;
+static bool blink;
static unsigned long last_blink;
static void setup_settings();
@@ -71,9 +69,11 @@ void sm_setup() {
}
void sm_on_connect() {
+ unsigned long now = millis();
+
// Disable all updates until enabled.
for (int i = 0; i < SENSOR_COUNT; i++) {
- next_update[i] = 0;
+ next_update[i] = now + sensors[i].update_interval;
}
connected = true;
@@ -88,13 +88,11 @@ void sm_on_disconnect() {
}
void sm_loop() {
- static unsigned long last = 0, now;
-
- static uint8_t x = 10;
+ unsigned long now = millis();
- now = millis();
+ // TODO: make the blink configurable. It's nice for debugging, bad for power usage when continuously connected
- if (now - last_blink > BLINK_WAIT) {
+ if (now - last_blink >= BLINK_WAIT) {
last_blink = now;
if (connected) {
blink = !blink;
@@ -102,48 +100,41 @@ void sm_loop() {
}
}
- if (now - last > 3000) {
- last = now;
- for (int i = 0; i < SENSOR_COUNT; i++) {
- struct sm_sensor& s = sensors[i];
-
- digitalWrite(s.d1_pin, HIGH);
- digitalWrite(s.d2_pin, LOW);
+ // TODO: there is no reason to read the values unless someone are connected *and* have asked for notifications.
- s.value = analogRead(s.a_pin);
+ int count = 0;
+ for (uint8_t i = 0; i < SENSOR_COUNT; i++) {
+ struct sm_sensor &s = sensors[i];
- if (i > 0) {
- Serial.print(", ");
- }
- Serial.print("#");
- Serial.print(i, DEC);
- Serial.print(" = ");
- Serial.print(s.value, DEC);
+ if (now < next_update[i]) {
+ continue;
}
- Serial.println();
- }
+ next_update[i] += s.update_interval;
- // TODO: implement proper notification
- if (false && now - last > 3000) {
- last = now;
+ digitalWrite(s.d1_pin, HIGH);
+ digitalWrite(s.d2_pin, LOW);
- struct sm_res res;
- res.code = SM_CMD_GET_VALUE;
- res.get_value.value = x;
+ s.value = (uint16_t) analogRead(s.a_pin);
- x++;
- if(x == 0) {
- x = 10;
+ if (count++ > 0) {
+ Serial.print(", ");
}
+ Serial.print("#");
+ Serial.print(i, DEC);
+ Serial.print(" = ");
+ Serial.print(s.value, DEC);
-// if(x % 2 == 0) {
-// notify_soil_moisture(res);
-// } else {
-// notify_battery_level(x);
-// }
+ struct sm_res res;
+ res.code = SM_CMD_GET_VALUE;
+ res.get_value.sensor = i;
+ res.get_value.value = s.value;
- notify_soil_moisture(res, sizeof(sm_get_value_res));
+ notify_soil_moisture(res, sizeof(sm_get_value_res));
+ }
+
+ if (count > 0) {
+ Serial.println();
}
}
@@ -220,7 +211,9 @@ void write_res(struct sm_res const& res) {
case SM_CMD_GET_VALUE:
Serial.print("SM_CMD_GET_VALUE");
- Serial.print(": value=");
+ Serial.print(": sensor=");
+ Serial.print(res.get_value.sensor, DEC);
+ Serial.print(", value=");
Serial.print(res.get_value.value, DEC);
break;
@@ -254,7 +247,7 @@ void write_res(struct sm_res const& res) {
}
Serial.println();
}
-#endif
+#endif // SM_DEBUG
void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) {
struct sm_req *req = (struct sm_req *) data;
@@ -356,13 +349,12 @@ 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]);
+ EEPROM.write(index++, (uint8_t) chars[i]);
}
return 1 + len;
@@ -373,7 +365,7 @@ static int load_string(int index, const uint8_t max_length, uint8_t& length, cha
uint8_t len = min(length, max_length);
for (int i = 0; i < len; i++) {
- chars[i] = EEPROM.read(index++);
+ chars[i] = (char) EEPROM.read(index++);
}
return 1 + len;
@@ -417,7 +409,6 @@ static void store_settings() {
Serial.println("Settings saved");
}
-
static void setup_settings() {
if (!load_settings()) {
Serial.println("Could not load settings, storing defaults");
@@ -454,3 +445,4 @@ static void setup_settings() {}
// vim: set ft=arduino:
+