aboutsummaryrefslogtreecommitdiff
path: root/app.cpp
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2014-12-23 13:38:57 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2014-12-27 22:39:07 +0100
commit7c0e1f7ffb750813a788940fdd55e9800c2f701d (patch)
treede7a200205b6d6725cdbd428dfedb0eac2a825a1 /app.cpp
downloadtrygvisio_soil_moisture-7c0e1f7ffb750813a788940fdd55e9800c2f701d.tar.gz
trygvisio_soil_moisture-7c0e1f7ffb750813a788940fdd55e9800c2f701d.tar.bz2
trygvisio_soil_moisture-7c0e1f7ffb750813a788940fdd55e9800c2f701d.tar.xz
trygvisio_soil_moisture-7c0e1f7ffb750813a788940fdd55e9800c2f701d.zip
o Initial import of trygvis.io soil moisture device.
Diffstat (limited to 'app.cpp')
-rw-r--r--app.cpp91
1 files changed, 91 insertions, 0 deletions
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 <Arduino.h>
+#include <HardwareSerial.h>
+
+// 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);
+}
+
+