diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-04 23:43:14 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-04 23:43:14 +0100 |
commit | 08f269afd4926edca9cdfd00c3d15d419e6c87a4 (patch) | |
tree | 75554752ddc85f55c8a1be36ea7e4ae0191d7300 /app.cpp | |
parent | 2f50ae8864aa2ba9e5807432eb878552389a9cb2 (diff) | |
download | trygvisio_soil_moisture-08f269afd4926edca9cdfd00c3d15d419e6c87a4.tar.gz trygvisio_soil_moisture-08f269afd4926edca9cdfd00c3d15d419e6c87a4.tar.bz2 trygvisio_soil_moisture-08f269afd4926edca9cdfd00c3d15d419e6c87a4.tar.xz trygvisio_soil_moisture-08f269afd4926edca9cdfd00c3d15d419e6c87a4.zip |
o Adding an operation to set the wanted update interval.
Useful for debugging to stream values and later devices where this can
control how often the values are broadcasted.
Diffstat (limited to 'app.cpp')
-rw-r--r-- | app.cpp | 46 |
1 files changed, 39 insertions, 7 deletions
@@ -9,24 +9,37 @@ #define SENSOR_COUNT 1 -// See http://redbearlab.com/blendmicro/ +// See http://redbearlab.com/blendmicro/ for pins. struct sm_sensor { uint8_t pin; uint8_t value; uint16_t warning_value; + uint16_t update_interval; uint8_t name_length; uint8_t name[SENSOR_NAME_LEN]; } sensors[SENSOR_COUNT] = { - {9, 0, 10, 9, 'sensor #1'}, + // Pin, Value, Warning Value, Update Interval, Length of Name, Name + { 9, 0, 10, 0, 9, 'sensor #1'}, }; -void on_loop() { +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; + } +} + +void sm_on_loop() { static unsigned long last = 0, now; static uint8_t x = 10; now = millis(); - if (now - last > 3000) { + + // TODO: implement proper notification + if (false || now - last > 3000) { last = now; struct sm_res res; @@ -43,6 +56,7 @@ void on_loop() { // } else { // notify_battery_level(x); // } + notify_soil_moisture(res, sizeof(sm_get_value_res)); } } @@ -97,6 +111,12 @@ void write_req(struct sm_req const& req) { Serial.print("SM_CMD_GET_SENSOR_NAME"); break; + case SM_CMD_SET_UPDATE_INTERVAL: + Serial.print("SM_CMD_SET_UPDATE_INTERVAL"); + Serial.print(": interval_in_seconds="); + Serial.print(req.set_update_interval.interval_in_seconds, DEC); + break; + default: Serial.print("Unknown command!"); } @@ -141,6 +161,10 @@ void write_res(struct sm_res const& res) { write_name(res.get_sensor_name.name, res.get_sensor_name.length); break; + case SM_CMD_SET_UPDATE_INTERVAL: + Serial.print("SM_CMD_SET_UPDATE_INTERVAL"); + break; + default: Serial.print("Unknown command!"); } @@ -215,7 +239,6 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { case SM_CMD_GET_SENSOR_NAME: body_len = sizeof(sm_get_sensor_name_res); sensor = req->get_sensor_name.sensor; - Serial.print("sensor="); Serial.print(sensor, DEC); if (sensor < SENSOR_COUNT) { res.get_sensor_name.length = sensors[sensor].name_length; @@ -225,13 +248,22 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { } break; + case SM_CMD_SET_UPDATE_INTERVAL: + body_len = sizeof(sm_set_update_interval_res); + sensor = req->set_update_interval.sensor; + + if (sensor < SENSOR_COUNT) { + sensors[sensor].update_interval = req->set_update_interval.interval_in_seconds; + } else { + res.code = SM_CMD_FAIL; + } + break; + default: res.code = SM_CMD_FAIL; break; } - Serial.println(); - if (res.code == SM_CMD_FAIL) { body_len = 0; } |