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 +++++++++++++++++++++++++++++++++++---------- app.h | 4 +++- trygvisio_soil_moisture.ino | 13 ++++++----- 3 files changed, 55 insertions(+), 19 deletions(-) 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: diff --git a/app.h b/app.h index f1b829a..e0ef6e9 100644 --- a/app.h +++ b/app.h @@ -93,7 +93,7 @@ struct sm_req { struct sm_res { // header uint8_t code; - + // body union { struct sm_get_sensor_count_res get_sensor_count; @@ -109,6 +109,7 @@ struct sm_res { void sm_loop(); void sm_setup(); void sm_on_connect(); +void sm_on_disconnect(); //boolean tx_soil_moisture(struct sm_res& res); void notify_soil_moisture(const struct sm_res& res, uint8_t body_len); @@ -127,3 +128,4 @@ void write_res(struct sm_res const& res); #endif +// vim: set ft=arduino: diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index f908e4a..a8a7d1f 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -97,7 +97,7 @@ static void setup_rf() { // The second parameter is for turning debug printing on for the ACI Commands and Events so they be printed on the Serial lib_aci_init(&aci_state, false); Serial.println(F("lib_aci_init done")); - + sm_setup(); } @@ -184,7 +184,7 @@ static void aci_loop() { // Get the device version of the nRF8001 and store it in the Hardware Revision String. // This will trigger a ACI_CMD_GET_DEVICE_VERSION. lib_aci_device_version(); - + sm_on_connect(); break; @@ -202,6 +202,8 @@ static void aci_loop() { lib_aci_connect(180/* in seconds */, 0x0100 /* advertising interval 100ms*/); Serial.println(F("Advertising started")); + + sm_on_disconnect(); break; case ACI_EVT_DATA_RECEIVED: @@ -209,7 +211,7 @@ static void aci_loop() { Serial.print(F("ACI_EVT_DATA_RECEIVED: pipe_number=")); Serial.println(pipe_number, DEC); - + if (pipe_number == pipe_rx) { on_soil_moisture_ctrl(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len); } @@ -312,11 +314,10 @@ void loop() { count++; if (!reset_attempted) { - if (count == 3) { reset_attempted = true; Serial.println(F("RF did not start, resetting RF")); - + // asm volatile ("jmp 0"); // lib_aci_pin_reset(); setup_rf(); @@ -375,7 +376,7 @@ void notify_battery_level(uint8_t value) { Serial.print(F("notify_battery_level, value=")); Serial.println(value, DEC); - + value = value % 101; lib_aci_send_data(pipe, &value, 1); -- cgit v1.2.3