From d317fdb0c737e6b1994da3f01c39937292c1e56f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 25 Jul 2015 05:12:59 +0200 Subject: o Adding a Debug class that delegates to Serial to control where the debug output goes. --- trygvisio_soil_moisture.ino | 167 ++++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 83 deletions(-) (limited to 'trygvisio_soil_moisture.ino') diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index 4d5bc9c..59b0cf6 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -9,6 +9,7 @@ #include "config.h" #include "services.h" #include "app.h" +#include "Debug.h" static void setup_rf(); static void show_pipes(); @@ -26,11 +27,11 @@ static boolean timing_change_done = false; void __ble_assert(const char *file, uint16_t line) { - Serial.print("ERROR "); - Serial.print(file); - Serial.print(": "); - Serial.print(line); - Serial.print("\n"); + Debug::print("ERROR "); + Debug::print(file); + Debug::print(": "); + Debug::print(line); + Debug::print("\n"); while(1); } @@ -77,7 +78,7 @@ void setup() { } static void setup_rf() { - Serial.println(F("setup_rf()")); + Debug::println(F("setup_rf()")); // Point ACI data structures to the the setup data that the nRFgo studio generated for the nRF8001 if (NULL != services_pipe_type_mapping) { aci_state.aci_setup_info.services_pipe_type_mapping = &services_pipe_type_mapping[0]; @@ -114,7 +115,7 @@ static void setup_rf() { // then we initialize the data structures required to setup the nRF8001 // 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")); + Debug::println(F("lib_aci_init done")); } static bool rf_started = false; @@ -131,21 +132,21 @@ static void aci_loop() { switch(aci_evt->evt_opcode) { case ACI_EVT_DEVICE_STARTED: - Serial.println(F("ACI_EVT_DEVICE_STARTED")); + Debug::println(F("ACI_EVT_DEVICE_STARTED")); aci_state.data_credit_total = aci_evt->params.device_started.credit_available; - Serial.print(F("aci_state.data_credit_total=")); - Serial.println(aci_state.data_credit_total, DEC); + Debug::print(F("aci_state.data_credit_total=")); + Debug::println(aci_state.data_credit_total, DEC); switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: - Serial.println(F("ACI_DEVICE_SETUP")); + Debug::println(F("ACI_DEVICE_SETUP")); rf_started = true; setup_required = true; break; case ACI_DEVICE_STANDBY: - Serial.println(F("ACI_DEVICE_STANDBY")); + Debug::println(F("ACI_DEVICE_STANDBY")); if (aci_evt->params.device_started.hw_error) { delay(20); // Magic number used to make sure the HW error event is handled correctly. } @@ -154,7 +155,7 @@ static void aci_loop() { // lib_aci_broadcast(10/* in seconds */, 0x0100 /* advertising interval 100ms */); // ret = lib_aci_open_adv_pipe(PIPE_BATTERY_BATTERY_LEVEL_BROADCAST); // ret = lib_aci_open_adv_pipe(PIPE_SOIL_MOISTURE_SOIL_MOISTURE_LEVEL_BROADCAST); - Serial.println(F("Advertising started")); + Debug::println(F("Advertising started")); } break; case ACI_DEVICE_INVALID: @@ -166,21 +167,21 @@ static void aci_loop() { break; case ACI_EVT_CMD_RSP: - // Serial.println(F("ACI_EVT_CMD_RSP")); - // Serial.print(F("aci_evt->params.cmd_rsp.cmd_opcode=")); - // Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); - // Serial.print(F("aci_evt->params.cmd_rsp.cmd_status=")); - // Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX); + // Debug::println(F("ACI_EVT_CMD_RSP")); + // Debug::print(F("aci_evt->params.cmd_rsp.cmd_opcode=")); + // Debug::println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); + // Debug::print(F("aci_evt->params.cmd_rsp.cmd_status=")); + // Debug::println(aci_evt->params.cmd_rsp.cmd_status, HEX); //If an ACI command response event comes with an error -> stop if (aci_evt->params.cmd_rsp.cmd_status != ACI_STATUS_SUCCESS) { //ACI ReadDynamicData and ACI WriteDynamicData will have status codes of //TRANSACTION_CONTINUE and TRANSACTION_COMPLETE //all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command// - // Serial.print(F("ACI Command ")); - // Serial.println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); - // Serial.print(F("Evt Cmd respone: Status ")); - // Serial.println(aci_evt->params.cmd_rsp.cmd_status, HEX); + // Debug::print(F("ACI Command ")); + // Debug::println(aci_evt->params.cmd_rsp.cmd_opcode, HEX); + // Debug::print(F("Evt Cmd respone: Status ")); + // Debug::println(aci_evt->params.cmd_rsp.cmd_status, HEX); } if (aci_evt->params.cmd_rsp.cmd_opcode == ACI_CMD_GET_DEVICE_VERSION) { //Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic @@ -189,9 +190,9 @@ static void aci_loop() { // sizeof(aci_evt_cmd_rsp_params_get_device_version_t)); } if (aci_evt->params.cmd_rsp.cmd_opcode == ACI_CMD_GET_TEMPERATURE) { - Serial.print("aci_evt->params.cmd_rsp.params.get_temperature="); - Serial.print(aci_evt->params.cmd_rsp.params.get_temperature.temperature_value, DEC); - Serial.println(); + Debug::print("aci_evt->params.cmd_rsp.params.get_temperature="); + Debug::print(aci_evt->params.cmd_rsp.params.get_temperature.temperature_value, DEC); + Debug::println(); int32_t t = aci_evt->params.cmd_rsp.params.get_temperature.temperature_value; @@ -228,11 +229,11 @@ static void aci_loop() { break; case ACI_EVT_CONNECTED: - Serial.println(F("ACI_EVT_CONNECTED")); + Debug::println(F("ACI_EVT_CONNECTED")); timing_change_done = false; aci_state.data_credit_available = aci_state.data_credit_total; - Serial.print(F("aci_state.data_credit_available=")); - Serial.println(aci_state.data_credit_available, DEC); + Debug::print(F("aci_state.data_credit_available=")); + Debug::println(aci_state.data_credit_available, DEC); // Get the device version of the nRF8001 and store it in the Hardware Revision String. // This will trigger a ACI_CMD_GET_DEVICE_VERSION. @@ -244,19 +245,19 @@ static void aci_loop() { break; case ACI_EVT_PIPE_STATUS: - Serial.println(F("ACI_EVT_PIPE_STATUS")); + Debug::println(F("ACI_EVT_PIPE_STATUS")); show_pipes(); break; case ACI_EVT_TIMING: - Serial.println(F("ACI_EVT_TIMING")); + Debug::println(F("ACI_EVT_TIMING")); break; case ACI_EVT_DISCONNECTED: - Serial.println(F("ACI_EVT_DISCONNECTED")); + Debug::println(F("ACI_EVT_DISCONNECTED")); lib_aci_connect(180/* in seconds */, 0x0100 /* advertising interval 100ms*/); - Serial.println(F("Advertising started")); + Debug::println(F("Advertising started")); sm_on_disconnect(); break; @@ -264,8 +265,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); +// Debug::print(F("ACI_EVT_DATA_RECEIVED: pipe_number=")); +// Debug::println(pipe_number, DEC); if (pipe_number == sm_pipe_rx) { on_soil_moisture_ctrl(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len); @@ -273,19 +274,19 @@ static void aci_loop() { break; case ACI_EVT_DATA_CREDIT: - Serial.println(F("ACI_EVT_DATA_CREDIT")); + Debug::println(F("ACI_EVT_DATA_CREDIT")); aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit; - Serial.print(F("aci_state.data_credit_available=")); - Serial.println(aci_state.data_credit_available, DEC); + Debug::print(F("aci_state.data_credit_available=")); + Debug::println(aci_state.data_credit_available, DEC); break; case ACI_EVT_PIPE_ERROR: - Serial.println(F("ACI_EVT_PIPE_ERROR")); + Debug::println(F("ACI_EVT_PIPE_ERROR")); //See the appendix in the nRF8001 Product Specication for details on the error codes - Serial.print(F("ACI Evt Pipe Error: Pipe #:")); - Serial.print(aci_evt->params.pipe_error.pipe_number, DEC); - Serial.print(F(" Pipe Error Code: 0x")); - Serial.println(aci_evt->params.pipe_error.error_code, HEX); + Debug::print(F("ACI Evt Pipe Error: Pipe #:")); + Debug::print(aci_evt->params.pipe_error.pipe_number, DEC); + Debug::print(F(" Pipe Error Code: 0x")); + Debug::println(aci_evt->params.pipe_error.error_code, HEX); // Increment the credit available as the data packet was not sent. // The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted @@ -296,40 +297,40 @@ static void aci_loop() { break; case ACI_EVT_HW_ERROR: - Serial.println(F("ACI_EVT_HW_ERROR")); - Serial.print(F("HW error: ")); - Serial.println(aci_evt->params.hw_error.line_num, DEC); + Debug::println(F("ACI_EVT_HW_ERROR")); + Debug::print(F("HW error: ")); + Debug::println(aci_evt->params.hw_error.line_num, DEC); for(uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++) { Serial.write(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20]; } - Serial.println(); + Debug::println(); lib_aci_connect(180/* in seconds */, 0x0050 /* advertising interval 50ms*/); - Serial.println(F("Advertising started")); + Debug::println(F("Advertising started")); break; case ACI_EVT_INVALID: - Serial.println(F("ACI_EVT_INVALID")); + Debug::println(F("ACI_EVT_INVALID")); break; case ACI_EVT_ECHO: - Serial.println(F("ACI_EVT_ECHO")); + Debug::println(F("ACI_EVT_ECHO")); break; case ACI_EVT_BOND_STATUS: - Serial.println(F("ACI_EVT_BOND_STATUS")); + Debug::println(F("ACI_EVT_BOND_STATUS")); break; case ACI_EVT_DATA_ACK: - Serial.println(F("ACI_EVT_DATA_ACK")); + Debug::println(F("ACI_EVT_DATA_ACK")); break; case ACI_EVT_DISPLAY_PASSKEY: - Serial.println(F("ACI_EVT_DISPLAY_PASSKEY")); + Debug::println(F("ACI_EVT_DISPLAY_PASSKEY")); break; case ACI_EVT_KEY_REQUEST: - Serial.println(F("ACI_EVT_KEY_REQUEST")); + Debug::println(F("ACI_EVT_KEY_REQUEST")); break; } } else { - // Serial.println(F("No ACI Events available")); + // Debug::println(F("No ACI Events available")); // No event in the ACI Event queue and if there is no event in the ACI command queue the arduino can go to sleep // Arduino can go to sleep now // Wakeup from sleep from the RDYN line @@ -341,8 +342,8 @@ static void aci_loop() { */ if (setup_required) { int ret = do_aci_setup(&aci_state); - Serial.print(F("do_aci_setup ret=")); - Serial.println(ret, DEC); + Debug::print(F("do_aci_setup ret=")); + Debug::println(ret, DEC); if (SETUP_SUCCESS == ret) { setup_required = false; } @@ -371,7 +372,7 @@ void loop() { if (!reset_attempted) { if (count == 3) { reset_attempted = true; - Serial.println(F("RF did not start, resetting RF")); + Debug::println(F("RF did not start, resetting RF")); // asm volatile ("jmp 0"); // lib_aci_pin_reset(); @@ -379,7 +380,7 @@ void loop() { count = 0; return; } else { - Serial.println(F("waiting for RF to start")); + Debug::println(F("waiting for RF to start")); } } /**/ @@ -393,7 +394,7 @@ void loop() { #ifdef USE_LOW_POWER_MODE == 1 #ifdef SM_DEBUG == 1 - Serial.println(F("Sleeping...")); + Debug::println(F("Sleeping...")); Serial.flush(); #endif // SM_DEBUG @@ -404,10 +405,10 @@ void loop() { static void show_pipes() { for (uint8_t i = 1; i <= NUMBER_OF_PIPES; i++) { uint8_t x = lib_aci_is_pipe_available(&aci_state, i); - Serial.print(F("pipe #")); - Serial.print(i, DEC); - Serial.print(F(", available=?")); - Serial.println(x, DEC); + Debug::print(F("pipe #")); + Debug::print(i, DEC); + Debug::print(F(", available=?")); + Debug::println(x, DEC); } } /* @@ -419,12 +420,12 @@ boolean tx_moisture(sm_res *res) { boolean available = lib_aci_is_pipe_available(&aci_state, pipe); - Serial.print(F("tx_soil_moisture, len=")); - Serial.println(len, DEC); - Serial.print(F("aci_state.data_credit_available=")); - Serial.println(aci_state.data_credit_available, DEC); - Serial.print(F("available=")); - Serial.println(available, DEC); + Debug::print(F("tx_soil_moisture, len=")); + Debug::println(len, DEC); + Debug::print(F("aci_state.data_credit_available=")); + Debug::println(aci_state.data_credit_available, DEC); + Debug::print(F("available=")); + Debug::println(available, DEC); if (available && aci_state.data_credit_available > 0) { status = lib_aci_send_data(pipe, data, len); @@ -438,8 +439,8 @@ boolean tx_moisture(sm_res *res) { void notify_battery_level(uint8_t value) { static const uint8_t pipe = PIPE_BATTERY_BATTERY_LEVEL_SET; - Serial.print(F("notify_battery_level, value=")); - Serial.println(value, DEC); + Debug::print(F("notify_battery_level, value=")); + Debug::println(value, DEC); value = value % 101; @@ -451,17 +452,17 @@ void notify_soil_moisture(const struct sm_res& res, uint8_t body_len) { uint8_t *data = (uint8_t *)&res; uint8_t len = SM_RES_HEADER_SIZE + body_len; -// Serial.print(F("notify_soil_moisture, code=")); -// Serial.print(res.code, DEC); -// Serial.print(F(", body_len=")); -// Serial.println(body_len, DEC); +// Debug::print(F("notify_soil_moisture, code=")); +// Debug::print(res.code, DEC); +// Debug::print(F(", body_len=")); +// Debug::println(body_len, DEC); -// Serial.print(F("aci_state.data_credit_available=")); -// Serial.println(aci_state.data_credit_available, DEC); +// Debug::print(F("aci_state.data_credit_available=")); +// Debug::println(aci_state.data_credit_available, DEC); bool available = lib_aci_is_pipe_available(&aci_state, sm_pipe_tx); -// Serial.print(F("pipe available=")); -// Serial.println(available, DEC); +// Debug::print(F("pipe available=")); +// Debug::println(available, DEC); // 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, sm_pipe_set, (uint8_t *)&res, len); @@ -472,23 +473,23 @@ void notify_soil_moisture(const struct sm_res& res, uint8_t body_len) { } #if SM_DEBUG == 1 -Serial.println("write_res"); +Debug::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."); + Debug::println("Not enough credits to send notification."); #endif return; } - boolean sent = lib_aci_send_data(pipe_tx, data, len); + boolean sent = lib_aci_send_data(sm_pipe_tx, data, len); if (sent) { aci_state.data_credit_available--; } else { #if SM_DEBUG == 1 - Serial.println("Sending failed"); + Debug::println("Sending failed"); #endif } } -- cgit v1.2.3