From d015c05c7a141f183647fe0cd64c332b7af23800 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 2 Dec 2014 20:06:26 +0100 Subject: o Splitting out the app code into its own file. o Understanding how pipes work, getting a TX part of a pipe to work. --- fiken_status_panel.ino | 111 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 22 deletions(-) (limited to 'fiken_status_panel.ino') diff --git a/fiken_status_panel.ino b/fiken_status_panel.ino index 22f7f4a..35b3d40 100644 --- a/fiken_status_panel.ino +++ b/fiken_status_panel.ino @@ -1,8 +1,10 @@ #include +#include #include #include #include "services.h" +#include "app.h" static services_pipe_type_mapping_t services_pipe_type_mapping[NUMBER_OF_PIPES] = SERVICES_PIPE_TYPE_MAPPING_CONTENT; static hal_aci_data_t setup_msgs[NB_SETUP_MESSAGES] PROGMEM = SETUP_MESSAGES_CONTENT; @@ -40,6 +42,8 @@ void setup() { #if defined (__AVR_ATmega32U4__) while(!Serial) { } + + delay(1000); #elif defined(__PIC32MX__) delay(1000); #endif @@ -84,9 +88,12 @@ void setup() { Serial.println(F("lib_aci_init done")); } +static bool rf_started = false; static bool setup_required = false; static void aci_loop() { + uint8_t pipe_number; + // We enter the if statement only when there is a ACI event available to be processed if (lib_aci_event_get(&aci_state, &aci_data)) { aci_evt_t * aci_evt; @@ -96,9 +103,14 @@ static void aci_loop() { case ACI_EVT_DEVICE_STARTED: Serial.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); + switch(aci_evt->params.device_started.device_mode) { case ACI_DEVICE_SETUP: Serial.println(F("ACI_DEVICE_SETUP")); + rf_started = true; setup_required = true; break; @@ -149,6 +161,8 @@ static void aci_loop() { Serial.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); // Get the device version of the nRF8001 and store it in the Hardware Revision String. // This will trigger a ACI_CMD_GET_DEVICE_VERSION. @@ -157,6 +171,7 @@ static void aci_loop() { case ACI_EVT_PIPE_STATUS: Serial.println(F("ACI_EVT_PIPE_STATUS")); + show_pipes(); break; case ACI_EVT_TIMING: @@ -170,20 +185,34 @@ static void aci_loop() { break; 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(aci_evt->params.data_received.rx_data.pipe_number, DEC); - if (aci_evt->params.data_received.rx_data.pipe_number == PIPE_FIKEN_STATUS_PANEL_GAUGE_RX_ACK) { - if (aci_evt->len != PIPE_FIKEN_STATUS_PANEL_GAUGE_RX_ACK_MAX_SIZE) { + Serial.println(pipe_number, DEC); + + if (pipe_number == PIPE_FIKEN_STATUS_PANEL_GAUGE_DATA_RX_ACK) { + if (aci_evt->len != PIPE_FIKEN_STATUS_PANEL_GAUGE_DATA_RX_ACK_MAX_SIZE) { break; } - set_gauge_req(aci_evt->params.data_received.rx_data.aci_data); - lib_aci_send_ack(&aci_state, PIPE_FIKEN_STATUS_PANEL_GAUGE_RX_ACK); + on_gauge_data(aci_evt->params.data_received.rx_data.aci_data, aci_evt->len - 2); + lib_aci_send_ack(&aci_state, PIPE_FIKEN_STATUS_PANEL_GAUGE_DATA_RX_ACK); + } + + if (pipe_number == PIPE_FIKEN_STATUS_PANEL_GAUGE_CONTROL_RX) { + uint8_t len = aci_evt->len - 2; + // if (aci_evt->len != PIPE_FIKEN_STATUS_PANEL_GAUGE_CONTROL_RX_MAX_SIZE) { + // break; + // } + + on_gauge_ctrl(aci_evt->params.data_received.rx_data.aci_data, len); } break; case ACI_EVT_DATA_CREDIT: Serial.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); break; case ACI_EVT_PIPE_ERROR: @@ -197,8 +226,7 @@ static void aci_loop() { // 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 // for the credit. - if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) - { + if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code) { aci_state.data_credit_available++; } break; @@ -208,8 +236,7 @@ static void aci_loop() { Serial.print(F("HW error: ")); Serial.println(aci_evt->params.hw_error.line_num, DEC); - for(uint8_t counter = 0; counter <= (aci_evt->len - 3); counter++) - { + 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(); @@ -258,13 +285,6 @@ static void aci_loop() { } } -void set_gauge_req(uint8_t *data) { - Serial.print(F("set_gauge_req, channel=")); - Serial.print(data[0], DEC); - Serial.print(F(", value=")); - Serial.println(data[1], DEC); -} - static uint8_t value = 0; void loop() { static unsigned long last = 0, now; @@ -275,15 +295,62 @@ void loop() { Serial.write(Serial.read()); } - if (!setup_required) { - now = millis(); - if (now - last > 3000) { + now = millis(); + if (now - last > 3000) { + last = now; + + if (!rf_started) { + static int count = 0; + count++; + if(count == 3) { + Serial.println(F("RF did not start, resetting")); + + asm volatile ("jmp 0"); + + count = 0; + return; + } else { + Serial.println(F("waiting for RF to start")); + } + } + else if (!setup_required) { value++; - lib_aci_set_local_data(&aci_state, PIPE_FIKEN_STATUS_PANEL_GAUGE_SET, &value, 1); - last = now; + lib_aci_set_local_data(&aci_state, PIPE_FIKEN_STATUS_PANEL_GAUGE_DATA_SET, &value, 1); Serial.print(F("value=")); - Serial.println(value, DEC); + Serial.println(value, HEX); + + show_pipes(); + } + } +} + +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); + } +} + +bool send_ctrl(uint8_t *data, uint8_t len) { + bool status = false; + + Serial.print(F("send_ctrl, 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(lib_aci_is_pipe_available(&aci_state, PIPE_FIKEN_STATUS_PANEL_GAUGE_CONTROL_TX), DEC); + + if (lib_aci_is_pipe_available(&aci_state, PIPE_FIKEN_STATUS_PANEL_GAUGE_CONTROL_TX) && + aci_state.data_credit_available >= 1) { + status = lib_aci_send_data(PIPE_FIKEN_STATUS_PANEL_GAUGE_CONTROL_TX, data, len); + if (status) { + aci_state.data_credit_available--; } } + return status; } -- cgit v1.2.3