From 7c0e1f7ffb750813a788940fdd55e9800c2f701d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 23 Dec 2014 13:38:57 +0100 Subject: o Initial import of trygvis.io soil moisture device. --- app.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 app.cpp (limited to 'app.cpp') diff --git a/app.cpp b/app.cpp new file mode 100644 index 0000000..909a097 --- /dev/null +++ b/app.cpp @@ -0,0 +1,91 @@ +#include "app.h" + +#include +#include + +// http://bleaklow.com/2010/09/05/progmem_and_gcc_bug_34734.html +#undef PROGMEM +#define PROGMEM __attribute__((section(".progmem.data"))) + +#define SENSOR_COUNT 1 + +// See http://redbearlab.com/blendmicro/ +struct sm_sensor { + uint8_t pin; + uint8_t value; +} gauges[SENSOR_COUNT] = { + { 9, 0}, +}; + +void on_loop() { + static unsigned long last = 0, now; + + static uint8_t x = 10; + + now = millis(); + if (now - last > 3000) { + last = now; + sm_res data; + data.len = sizeof(struct sm_get_sensor_level_res); + data.code = SM_CMD_GET_SENSOR_LEVEL; + data.get_sensor_level.level = x; + x++; + if(x == 0) { + x = 10; + } + + if(x % 2 == 0) { + notify_soil_moisture(&data); + } else { + notify_battery_level(x); + } + } +} + +void on_soil_moisture(uint8_t *data, uint8_t len) { + Serial.print(F("on_soil_moisture, data[0]=")); + Serial.print(data[0], HEX); + Serial.print(F(", data[1]=")); + Serial.println(data[1], HEX); + + struct sm_req *cmd = (struct sm_req *) data; + struct sm_res res; + + res.code = cmd->code; + + Serial.print("code="); + Serial.println(cmd->code, DEC); + + switch(cmd->code) { + case SM_CMD_GET_SENSOR_COUNT: + res.len = sizeof(struct sm_get_sensor_count_res); + res.get_sensor_count.count = SENSOR_COUNT; + break; + case SM_CMD_SET_SENSOR_THRESHOLD: +// res.len = sizeof(struct fsp_set_gauge_res); +// if (cmd->set_gauge.gauge < GAUGE_COUNT) { +// analogWrite(gauges[cmd->set_gauge.gauge].pin, cmd->set_gauge.value); +// gauges[cmd->set_gauge.gauge].value = cmd->set_gauge.value; +// } + + break; + case SM_CMD_GET_SENSOR_LEVEL: +// res.len = sizeof(struct fsp_get_gauge_res); +// if (cmd->get_gauge.gauge < GAUGE_COUNT) { +// res.get_gauge.gauge = cmd->get_gauge.gauge; +// res.get_gauge.value = gauges[cmd->get_gauge.gauge].value; +// } else { +// res.code = FSP_CMD_FAIL; +// res.len = 0; +// } + break; + default: + res.code = SM_CMD_FAIL; + res.len = 0; + break; + } + +// tx_soil_moisture(&res); +} + + -- cgit v1.2.3