From 80646adbb985215b34aac9b73402752a29cd2ef8 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 18 Jan 2015 23:10:07 +0100 Subject: o Making the device blink when connected. --- app.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 12 deletions(-) (limited to 'app.cpp') diff --git a/app.cpp b/app.cpp index 1a96192..65b4b73 100644 --- a/app.cpp +++ b/app.cpp @@ -8,6 +8,7 @@ #define PROGMEM __attribute__((section(".progmem.data"))) #define SENSOR_COUNT 2 +#define LED_PIN 13 // See http://redbearlab.com/blendmicro/ for pins. struct sm_sensor { @@ -27,45 +28,76 @@ struct sm_sensor { static unsigned long next_update[SENSOR_COUNT]; -void sm_on_connect() { - // Disable all updates until enabled. - for (int i = 0; i < SENSOR_COUNT; i++) { - next_update[i] = 0; - } -} +// How often the connected light should toggle, in ms +#define BLINK_WAIT 500 +static boolean connected; +static boolean blink; +static unsigned long last_blink; void sm_setup() { for (int i = 0; i < SENSOR_COUNT; i++) { struct sm_sensor s = sensors[i]; - + pinMode(s.a_pin, INPUT); pinMode(s.d1_pin, OUTPUT); pinMode(s.d2_pin, OUTPUT); } + + pinMode(LED_PIN, OUTPUT); +} + +void sm_on_connect() { + // Disable all updates until enabled. + for (int i = 0; i < SENSOR_COUNT; i++) { + next_update[i] = 0; + } + + connected = true; + blink = true; + digitalWrite(LED_PIN, HIGH); + last_blink = millis(); +} + +void sm_on_disconnect() { + connected = false; + digitalWrite(LED_PIN, LOW); } void sm_loop() { static unsigned long last = 0, now; - + static uint8_t x = 10; now = millis(); - + + if (now - last_blink > BLINK_WAIT) { + last_blink = now; + if (connected) { + blink = !blink; + digitalWrite(LED_PIN, _blink ? HIGH : LOW); + } + } + 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); s.value = analogRead(s.a_pin); + + if (i > 0) { + Serial.print(", "); + } Serial.print("#"); Serial.print(i, DEC); Serial.print(" = "); Serial.print(s.value, DEC); - Serial.println(); } + + Serial.println(); } // TODO: implement proper notification @@ -293,8 +325,9 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { if (res.code == SM_CMD_FAIL) { body_len = 0; - } + } notify_soil_moisture(res, body_len); } +// vim: set ft=arduino: -- cgit v1.2.3