From dea9cb49271631d4def70d21db42a85d1d58f269 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 23 Jul 2015 15:37:39 +0200 Subject: o Fixing a bug where the reponse code wasn't properly set. --- .gitignore | 1 + app.cpp | 8 +++++++- config.h | 29 ++++++++++++++++++++++++++++- trygvisio_soil_moisture.ino | 35 ++++++++++++++++++++++++++--------- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 414487d..7e86e35 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build-*/ +config.h diff --git a/app.cpp b/app.cpp index cadcc30..d4f880d 100644 --- a/app.cpp +++ b/app.cpp @@ -134,7 +134,7 @@ void sm_loop() { } } -#ifdef SM_DEBUG +#ifdef SM_DEBUG == 1 static void write_name(uint8_t const* name, uint8_t len) { for(int i = 0; i < len; i++) { @@ -271,6 +271,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { switch(req->code) { case SM_CMD_GET_SENSOR_COUNT: body_len = sizeof(sm_get_sensor_count_res); + res.code = req->code; res.get_sensor_count.count = SENSOR_COUNT; break; @@ -279,6 +280,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { sensor = req->get_value.sensor; if (sensor < SENSOR_COUNT) { + res.code = req->code; // TODO: update the sensor's value res.get_value.value = sensors[sensor].value; } else { @@ -291,6 +293,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { sensor = req->set_warning_value.sensor; if (sensor < SENSOR_COUNT) { + res.code = req->code; sensors[sensor].warning_value = req->set_warning_value.warning_value; } else { res.code = SM_CMD_FAIL; @@ -302,6 +305,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { sensor = req->get_warning_value.sensor; if (sensor < SENSOR_COUNT) { + res.code = req->code; res.get_warning_value.warning_value = sensors[sensor].warning_value; } else { res.code = SM_CMD_FAIL; @@ -313,6 +317,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { sensor = req->set_sensor_name.sensor; if (sensor < SENSOR_COUNT) { + res.code = req->code; sensors[sensor].name_length = min(req->set_sensor_name.length, SENSOR_NAME_LEN); memcpy(sensors[sensor].name, req->set_sensor_name.name, sensors[sensor].name_length); } else { @@ -325,6 +330,7 @@ void on_soil_moisture_ctrl(uint8_t *data, uint8_t len) { sensor = req->get_sensor_name.sensor; if (sensor < SENSOR_COUNT) { + res.code = req->code; res.get_sensor_name.length = sensors[sensor].name_length; memcpy(res.get_sensor_name.name, sensors[sensor].name, SENSOR_NAME_LEN); } else { diff --git a/config.h b/config.h index 6ad7fb6..3f3271f 100644 --- a/config.h +++ b/config.h @@ -30,6 +30,9 @@ values are commented out, but some have default values */ +/////////////////////////////////////////////////////////////////////////////// +// Board version +// // You have to set this to match your board. The build will fail if // you don't set it. // @@ -39,8 +42,12 @@ values are commented out, but some have default values // // Default: not set, you have to set it // #define SM_BOARD_VERSION ... -#define SM_BOARD_VERSION 2 +#define SM_BOARD_VERSION 1 + +/////////////////////////////////////////////////////////////////////////////// +// Wait for serial before starting +// // If enabled the code won't start until you open the serial port // monitor or otherwise connect to the serial port. // @@ -54,6 +61,9 @@ values are commented out, but some have default values // Default: 1 // #define WAIT_FOR_SERIAL_BEFORE_STARING ... +/////////////////////////////////////////////////////////////////////////////// +// Persistent configuration +// // If enabled the settings for each sensor will be persisted in EEPROM // // Values: @@ -62,6 +72,19 @@ values are commented out, but some have default values // Default: 0 // #define PERSISTENT_CONFIGURATION_SUPPORT ... +/////////////////////////////////////////////////////////////////////////////// +// Use low power mode +// +// If enabled the device will try to use as little power as possible. +// +// Not fully functional yet so it is disabled by default. +// +// Values: +// 0: disabled +// 1: enabled +// Default: 0 +// #define USE_LOW_POWER_MODE ... + /* Default Values Section @@ -82,4 +105,8 @@ Do not touch stuff in this section, it only sets default values. #define SM_DEBUG 1 #endif +#ifndef LOW_POWER +#define LOW_POWER 0 +#endif + #endif diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index 8185754..729f5da 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -225,8 +225,8 @@ static void aci_loop() { case ACI_EVT_DATA_RECEIVED: pipe_number = aci_evt->params.data_received.rx_data.pipe_number; - Serial.print(F("ACI_EVT_DATA_RECEIVED: pipe_number=")); - Serial.println(pipe_number, DEC); +// 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); @@ -352,12 +352,14 @@ void loop() { sm_loop(); -#ifdef SM_DEBUG +#ifdef USE_LOW_POWER_MODE == 1 +#ifdef SM_DEBUG == 1 Serial.println(F("Sleeping...")); Serial.flush(); #endif // SM_DEBUG go_to_sleep(); +#endif // USE_LOW_POWER_MODE } static void show_pipes() { @@ -425,14 +427,29 @@ void notify_soil_moisture(const struct sm_res& res, uint8_t body_len) { // This should probably be an explicit part of the API, but for now it makes it easier to implement a synchronous interface. lib_aci_set_local_data(&aci_state, pipe_set, (uint8_t *)&res, len); - if (available && aci_state.data_credit_available > 0) { - boolean sent = lib_aci_send_data(pipe_tx, data, len); - if (sent) { - aci_state.data_credit_available--; + // There is no need to lod messages that won't be sent. + if (!available) { + return; + } #if SM_DEBUG == 1 - write_res(res); +Serial.println("write_res"); + write_res(res); +#endif + + if (aci_state.data_credit_available = 0) { +#if SM_DEBUG == 1 + Serial.println("Not enough credits to send notification."); +#endif + return; + } + + boolean sent = lib_aci_send_data(pipe_tx, data, len); + if (sent) { + aci_state.data_credit_available--; + } else { +#if SM_DEBUG == 1 + Serial.println("Sending failed"); #endif - } } } -- cgit v1.2.3