Tema SCPI Negru Rares-Razvan
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_TemperatureEx by
main.cpp@10:c7d53e4e0602, 2016-04-26 (annotated)
- Committer:
- tanasaro10
- Date:
- Tue Apr 26 20:15:41 2016 +0000
- Revision:
- 10:c7d53e4e0602
- Parent:
- 9:303d3628986a
- Child:
- 11:baafa4f7a15e
small updates added; new functionalities for push button; some bug fixes;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 1:1c058e553423 | 1 | /* |
RedBearLab | 0:cffe8ac1bdf0 | 2 | |
RedBearLab | 1:1c058e553423 | 3 | Copyright (c) 2012-2014 RedBearLab |
RedBearLab | 1:1c058e553423 | 4 | |
tanasaro10 | 9:303d3628986a | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
tanasaro10 | 9:303d3628986a | 6 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
tanasaro10 | 9:303d3628986a | 7 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
tanasaro10 | 9:303d3628986a | 8 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
RedBearLab | 1:1c058e553423 | 9 | subject to the following conditions: |
RedBearLab | 1:1c058e553423 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
RedBearLab | 1:1c058e553423 | 11 | |
tanasaro10 | 9:303d3628986a | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
tanasaro10 | 9:303d3628986a | 13 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
tanasaro10 | 9:303d3628986a | 14 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
tanasaro10 | 9:303d3628986a | 15 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
RedBearLab | 1:1c058e553423 | 16 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
RedBearLab | 1:1c058e553423 | 17 | |
RedBearLab | 1:1c058e553423 | 18 | */ |
RedBearLab | 1:1c058e553423 | 19 | |
RedBearLab | 1:1c058e553423 | 20 | /* |
RedBearLab | 1:1c058e553423 | 21 | * The application works with the BLEController iOS/Android App. |
RedBearLab | 1:1c058e553423 | 22 | * Type something from the Terminal to send |
RedBearLab | 1:1c058e553423 | 23 | * to the BLEController App or vice verse. |
RedBearLab | 1:1c058e553423 | 24 | * Characteristics received from App will print on Terminal. |
tanasaro10 | 9:303d3628986a | 25 | * Read read_me.md file for more informations about the extended feature |
RedBearLab | 1:1c058e553423 | 26 | */ |
tanasaro10 | 9:303d3628986a | 27 | |
tanasaro10 | 9:303d3628986a | 28 | |
RedBearLab | 2:4b66b69c7ecb | 29 | #include "ble/BLE.h" |
tanasaro10 | 8:f28ad4600b0f | 30 | #include <myData.h> |
tanasaro10 | 9:303d3628986a | 31 | #include <Gap.h> |
tanasaro10 | 9:303d3628986a | 32 | #include "ble_flash.h" |
tanasaro10 | 9:303d3628986a | 33 | #include "ble_flash.c" |
RedBearLab | 0:cffe8ac1bdf0 | 34 | |
RedBearLab | 0:cffe8ac1bdf0 | 35 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:cffe8ac1bdf0 | 36 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 37 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 38 | |
tanasaro10 | 9:303d3628986a | 39 | #define TXRX_BUF_LEN 20 /** For radio message transmission*/ |
tanasaro10 | 9:303d3628986a | 40 | |
tanasaro10 | 9:303d3628986a | 41 | #define MyASSERT(cond , serialpc, errVal) assert_error_app((bool)cond, serialpc, (uint16_t)errVal, __LINE__, __FILE__) |
RedBearLab | 0:cffe8ac1bdf0 | 42 | |
RedBearLab | 2:4b66b69c7ecb | 43 | BLE ble; |
RedBearLab | 0:cffe8ac1bdf0 | 44 | |
RedBearLab | 0:cffe8ac1bdf0 | 45 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:cffe8ac1bdf0 | 46 | |
RedBearLab | 0:cffe8ac1bdf0 | 47 | // The Nordic UART Service |
RedBearLab | 0:cffe8ac1bdf0 | 48 | static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 49 | static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 50 | static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; |
RedBearLab | 0:cffe8ac1bdf0 | 51 | static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; |
RedBearLab | 0:cffe8ac1bdf0 | 52 | |
tanasaro10 | 8:f28ad4600b0f | 53 | static const int8_t txPower = 0xCD; |
RedBearLab | 0:cffe8ac1bdf0 | 54 | |
RedBearLab | 0:cffe8ac1bdf0 | 55 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 56 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 57 | |
RedBearLab | 0:cffe8ac1bdf0 | 58 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 59 | static uint8_t rx_len=0; |
RedBearLab | 0:cffe8ac1bdf0 | 60 | |
tanasaro10 | 9:303d3628986a | 61 | static uint32_t gTimeInstant = 1; // TimerTick Resolution, in seconds |
tanasaro10 | 8:f28ad4600b0f | 62 | |
tanasaro10 | 10:c7d53e4e0602 | 63 | bool g_bIsConnected = false; |
tanasaro10 | 9:303d3628986a | 64 | bool g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 65 | bool g_bConnDisabled = false; |
tanasaro10 | 9:303d3628986a | 66 | bool g_LogActive = false; |
tanasaro10 | 9:303d3628986a | 67 | static myDataLog_t g_MyData; |
tanasaro10 | 9:303d3628986a | 68 | uint8_t g_MyDataIdx=0; |
tanasaro10 | 8:f28ad4600b0f | 69 | |
tanasaro10 | 9:303d3628986a | 70 | // pins connected for measuring |
tanasaro10 | 8:f28ad4600b0f | 71 | DigitalOut led(LED1); |
tanasaro10 | 8:f28ad4600b0f | 72 | PwmOut buzzer(p15); |
tanasaro10 | 9:303d3628986a | 73 | InterruptIn event(p29); // button |
tanasaro10 | 8:f28ad4600b0f | 74 | AnalogIn VP3(A3); |
tanasaro10 | 8:f28ad4600b0f | 75 | AnalogIn VP4(A4); |
tanasaro10 | 8:f28ad4600b0f | 76 | AnalogIn VP5(A5); |
tanasaro10 | 9:303d3628986a | 77 | AnalogIn* VP[3]= {&VP3,&VP4,&VP5}; |
tanasaro10 | 9:303d3628986a | 78 | #define NUM_OF_READINGS (4u) |
tanasaro10 | 9:303d3628986a | 79 | myPayload_t g_currMeasures; // last measurements |
tanasaro10 | 8:f28ad4600b0f | 80 | |
tanasaro10 | 8:f28ad4600b0f | 81 | Timeout timeout_err; // timeout for buzz on error |
tanasaro10 | 9:303d3628986a | 82 | Ticker periodicActions; |
tanasaro10 | 8:f28ad4600b0f | 83 | |
tanasaro10 | 8:f28ad4600b0f | 84 | mtime_manager_t g_myTimeVar; |
tanasaro10 | 8:f28ad4600b0f | 85 | mdate_manager_t g_myDateVar; |
tanasaro10 | 8:f28ad4600b0f | 86 | |
tanasaro10 | 9:303d3628986a | 87 | GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
RedBearLab | 0:cffe8ac1bdf0 | 88 | |
RedBearLab | 0:cffe8ac1bdf0 | 89 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
tanasaro10 | 9:303d3628986a | 90 | |
RedBearLab | 0:cffe8ac1bdf0 | 91 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
RedBearLab | 0:cffe8ac1bdf0 | 92 | |
RedBearLab | 0:cffe8ac1bdf0 | 93 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:cffe8ac1bdf0 | 94 | |
tanasaro10 | 9:303d3628986a | 95 | void sendRadioMsg(const uint8_t* buf, uint16_t length) |
tanasaro10 | 9:303d3628986a | 96 | { |
tanasaro10 | 9:303d3628986a | 97 | uint8_t retVal; |
tanasaro10 | 9:303d3628986a | 98 | retVal = ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, length); |
tanasaro10 | 9:303d3628986a | 99 | //pc.printf("Err=%d\r\n",retVal); |
tanasaro10 | 9:303d3628986a | 100 | MyASSERT((retVal!=0),&pc, retVal); |
tanasaro10 | 8:f28ad4600b0f | 101 | } |
tanasaro10 | 8:f28ad4600b0f | 102 | |
RedBearLab | 0:cffe8ac1bdf0 | 103 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
RedBearLab | 0:cffe8ac1bdf0 | 104 | { |
RedBearLab | 0:cffe8ac1bdf0 | 105 | pc.printf("Disconnected \r\n"); |
tanasaro10 | 9:303d3628986a | 106 | g_bIsConnected = false; |
tanasaro10 | 9:303d3628986a | 107 | g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 108 | pc.printf("R: %d\r",reason); |
tanasaro10 | 10:c7d53e4e0602 | 109 | if (reason != 0x16) { |
tanasaro10 | 10:c7d53e4e0602 | 110 | ble.startAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 111 | g_bIsAdvertising = true; |
tanasaro10 | 10:c7d53e4e0602 | 112 | } |
RedBearLab | 0:cffe8ac1bdf0 | 113 | } |
RedBearLab | 0:cffe8ac1bdf0 | 114 | |
tanasaro10 | 9:303d3628986a | 115 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
tanasaro10 | 9:303d3628986a | 116 | { |
tanasaro10 | 9:303d3628986a | 117 | pc.printf("Connected \r\n"); |
tanasaro10 | 9:303d3628986a | 118 | g_bIsConnected = true; |
tanasaro10 | 9:303d3628986a | 119 | g_bIsAdvertising = false; |
tanasaro10 | 8:f28ad4600b0f | 120 | } |
tanasaro10 | 8:f28ad4600b0f | 121 | |
tanasaro10 | 9:303d3628986a | 122 | void at_timeout_err() |
tanasaro10 | 9:303d3628986a | 123 | { |
tanasaro10 | 9:303d3628986a | 124 | // stop buzz |
tanasaro10 | 9:303d3628986a | 125 | buzz_int(&buzzer, 0,0); |
tanasaro10 | 9:303d3628986a | 126 | } |
tanasaro10 | 10:c7d53e4e0602 | 127 | void connectionUpdate(connection_update_t option) |
tanasaro10 | 10:c7d53e4e0602 | 128 | { |
tanasaro10 | 10:c7d53e4e0602 | 129 | if (g_bConnDisabled == false) { |
tanasaro10 | 10:c7d53e4e0602 | 130 | switch (option) { |
tanasaro10 | 10:c7d53e4e0602 | 131 | case eStartAdvertising: { |
tanasaro10 | 10:c7d53e4e0602 | 132 | if ((g_bIsConnected == false)&&(g_bIsAdvertising == false)) { |
tanasaro10 | 10:c7d53e4e0602 | 133 | pc.printf("Start Advertising\r"); |
tanasaro10 | 10:c7d53e4e0602 | 134 | ble.startAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 135 | g_bIsAdvertising = true; |
tanasaro10 | 10:c7d53e4e0602 | 136 | } |
tanasaro10 | 10:c7d53e4e0602 | 137 | break; |
tanasaro10 | 9:303d3628986a | 138 | } |
tanasaro10 | 10:c7d53e4e0602 | 139 | case eStopAdvertising: { |
tanasaro10 | 10:c7d53e4e0602 | 140 | if (g_bIsAdvertising == true) { |
tanasaro10 | 10:c7d53e4e0602 | 141 | pc.printf("Stop Advertising\r"); |
tanasaro10 | 10:c7d53e4e0602 | 142 | ble.stopAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 143 | g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 144 | } |
tanasaro10 | 10:c7d53e4e0602 | 145 | break; |
tanasaro10 | 9:303d3628986a | 146 | } |
tanasaro10 | 10:c7d53e4e0602 | 147 | case eDisconnect: { |
tanasaro10 | 10:c7d53e4e0602 | 148 | if (g_bIsConnected == true) { |
tanasaro10 | 10:c7d53e4e0602 | 149 | pc.printf("Close connection\r"); |
tanasaro10 | 10:c7d53e4e0602 | 150 | ble.disconnect((Gap::DisconnectionReason_t)0x12); |
tanasaro10 | 10:c7d53e4e0602 | 151 | } else if (g_bIsAdvertising == true) { |
tanasaro10 | 10:c7d53e4e0602 | 152 | pc.printf("Stop Advertising\r"); |
tanasaro10 | 10:c7d53e4e0602 | 153 | ble.stopAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 154 | g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 155 | } |
tanasaro10 | 10:c7d53e4e0602 | 156 | break; |
tanasaro10 | 9:303d3628986a | 157 | } |
tanasaro10 | 9:303d3628986a | 158 | } |
tanasaro10 | 8:f28ad4600b0f | 159 | } |
tanasaro10 | 9:303d3628986a | 160 | } |
tanasaro10 | 9:303d3628986a | 161 | void write_data_to_flash(uint32_t *tick) |
tanasaro10 | 10:c7d53e4e0602 | 162 | { |
tanasaro10 | 9:303d3628986a | 163 | uint32_t retVal=0; |
tanasaro10 | 9:303d3628986a | 164 | uint8_t page_num=0; |
tanasaro10 | 10:c7d53e4e0602 | 165 | |
tanasaro10 | 9:303d3628986a | 166 | if (g_MyDataIdx==0) { |
tanasaro10 | 9:303d3628986a | 167 | //initiate connection |
tanasaro10 | 9:303d3628986a | 168 | connectionUpdate(eStartAdvertising); |
tanasaro10 | 9:303d3628986a | 169 | // time and date used to initialize the g_MyData variable |
tanasaro10 | 9:303d3628986a | 170 | memcpy(&g_MyData.startData.date,&g_myDateVar.currentDate, sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 171 | memcpy(&g_MyData.startData.time,&g_myTimeVar.currentTime, sizeof(mtime_t)); |
tanasaro10 | 9:303d3628986a | 172 | memcpy(&g_MyData.startData.data,&g_currMeasures, sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 173 | } else { |
tanasaro10 | 9:303d3628986a | 174 | // it should be logged here the time difference from last record... |
tanasaro10 | 10:c7d53e4e0602 | 175 | g_MyData.myData[g_MyDataIdx-1].min = (uint16_t)(*tick*gTimeInstant / 60); |
tanasaro10 | 10:c7d53e4e0602 | 176 | g_MyData.myData[g_MyDataIdx-1].sec = (*tick*gTimeInstant% 60); |
tanasaro10 | 10:c7d53e4e0602 | 177 | memcpy(&g_MyData.myData[g_MyDataIdx-1].data,&g_currMeasures, sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 178 | } |
tanasaro10 | 9:303d3628986a | 179 | *tick = 0; |
tanasaro10 | 9:303d3628986a | 180 | |
tanasaro10 | 9:303d3628986a | 181 | if (g_MyDataIdx==(MAXBUFFER-5)) { |
tanasaro10 | 9:303d3628986a | 182 | //initiate disconnection |
tanasaro10 | 10:c7d53e4e0602 | 183 | connectionUpdate(eDisconnect); |
tanasaro10 | 9:303d3628986a | 184 | } |
tanasaro10 | 9:303d3628986a | 185 | |
tanasaro10 | 10:c7d53e4e0602 | 186 | if (g_MyDataIdx == MAXBUFFER) { |
tanasaro10 | 9:303d3628986a | 187 | // write2Flash the current page num |
tanasaro10 | 9:303d3628986a | 188 | page_num=flash_currPage(); |
tanasaro10 | 9:303d3628986a | 189 | // write2Flash the current page data |
tanasaro10 | 9:303d3628986a | 190 | retVal=ble_flash_page_write(page_num, (uint32_t*)&(g_MyData), 251u); |
tanasaro10 | 9:303d3628986a | 191 | pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myTimeVar.currentTime.min); |
tanasaro10 | 9:303d3628986a | 192 | flash_go_nextPage(); |
tanasaro10 | 9:303d3628986a | 193 | //save_flash_curr_pageNr(g_myDateVar.currentDate); |
tanasaro10 | 9:303d3628986a | 194 | } |
tanasaro10 | 10:c7d53e4e0602 | 195 | g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER+1); |
tanasaro10 | 8:f28ad4600b0f | 196 | } |
tanasaro10 | 8:f28ad4600b0f | 197 | |
tanasaro10 | 9:303d3628986a | 198 | |
tanasaro10 | 9:303d3628986a | 199 | void on_error_radioMsg() |
tanasaro10 | 9:303d3628986a | 200 | { |
tanasaro10 | 9:303d3628986a | 201 | char myBuf[TXRX_BUF_LEN]; |
tanasaro10 | 9:303d3628986a | 202 | |
tanasaro10 | 9:303d3628986a | 203 | sprintf(myBuf,"%s","WrongSyntax"); |
tanasaro10 | 9:303d3628986a | 204 | buzz_int(&buzzer,5,3); |
tanasaro10 | 9:303d3628986a | 205 | timeout_err.attach(&at_timeout_err, 2); |
tanasaro10 | 9:303d3628986a | 206 | sendRadioMsg((uint8_t*)&myBuf[0], 12); |
tanasaro10 | 8:f28ad4600b0f | 207 | } |
tanasaro10 | 8:f28ad4600b0f | 208 | |
tanasaro10 | 8:f28ad4600b0f | 209 | |
tanasaro10 | 9:303d3628986a | 210 | void flash_page_serial_dump(uint32_t* p_curr_addr) |
tanasaro10 | 10:c7d53e4e0602 | 211 | { |
tanasaro10 | 10:c7d53e4e0602 | 212 | myDataLogShort_t initialData; |
tanasaro10 | 10:c7d53e4e0602 | 213 | myDataL_t dataOut[2]; |
tanasaro10 | 10:c7d53e4e0602 | 214 | uint8_t i; |
tanasaro10 | 9:303d3628986a | 215 | |
tanasaro10 | 10:c7d53e4e0602 | 216 | p_curr_addr += 2; // skip the magic number and the word count |
tanasaro10 | 9:303d3628986a | 217 | memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); |
tanasaro10 | 9:303d3628986a | 218 | pc.printf("20%2d_%2d_%2d H:%2d P:%4x\r",initialData.startData.date.year, initialData.startData.date.month, initialData.startData.date.day, initialData.startData.time.hour, p_curr_addr); |
tanasaro10 | 9:303d3628986a | 219 | pc.printf("%2d:%2d;%3d;%3d;%3d \r",initialData.startData.time.min, initialData.startData.time.sec, initialData.startData.data.light, initialData.startData.data.gndV, initialData.startData.data.temp); |
tanasaro10 | 9:303d3628986a | 220 | pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",initialData.myData.min, initialData.myData.sec, initialData.myData.data.light, initialData.myData.data.gndV, initialData.myData.data.temp); |
tanasaro10 | 9:303d3628986a | 221 | p_curr_addr += 6; |
tanasaro10 | 10:c7d53e4e0602 | 222 | |
tanasaro10 | 9:303d3628986a | 223 | for (i=0; i<49; i++) { |
tanasaro10 | 9:303d3628986a | 224 | memcpy((uint32_t*)&dataOut, p_curr_addr, 5*sizeof(uint32_t)); |
tanasaro10 | 9:303d3628986a | 225 | pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",dataOut[0].min, dataOut[0].sec, dataOut[0].data.light, dataOut[0].data.gndV, dataOut[0].data.temp, i); |
tanasaro10 | 9:303d3628986a | 226 | pc.printf("%2d:%2d;%3d;%3d;%3d\r",dataOut[1].min, dataOut[1].sec, dataOut[1].data.light, dataOut[1].data.gndV, dataOut[1].data.temp); |
tanasaro10 | 9:303d3628986a | 227 | p_curr_addr += 5; |
tanasaro10 | 10:c7d53e4e0602 | 228 | } |
tanasaro10 | 9:303d3628986a | 229 | } |
tanasaro10 | 9:303d3628986a | 230 | |
tanasaro10 | 9:303d3628986a | 231 | int update_measurements() |
tanasaro10 | 9:303d3628986a | 232 | { |
tanasaro10 | 10:c7d53e4e0602 | 233 | int retVal; |
tanasaro10 | 10:c7d53e4e0602 | 234 | static myPayload_t prevMeasures=(myPayload_t) { |
tanasaro10 | 10:c7d53e4e0602 | 235 | 0, 0, 0, 0, 0 |
tanasaro10 | 10:c7d53e4e0602 | 236 | }; |
tanasaro10 | 9:303d3628986a | 237 | |
tanasaro10 | 9:303d3628986a | 238 | g_currMeasures = (myPayload_t) { |
tanasaro10 | 9:303d3628986a | 239 | VP[0]->read_u16(), VP[2]->read_u16(), VP[1]->read_u16(), led, 0 |
tanasaro10 | 9:303d3628986a | 240 | }; |
tanasaro10 | 9:303d3628986a | 241 | retVal = memcmp(&g_currMeasures,&prevMeasures,sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 242 | memcpy(&prevMeasures,&g_currMeasures,sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 243 | return retVal; |
tanasaro10 | 10:c7d53e4e0602 | 244 | } |
tanasaro10 | 10:c7d53e4e0602 | 245 | |
tanasaro10 | 9:303d3628986a | 246 | void at_eachInstant() |
tanasaro10 | 10:c7d53e4e0602 | 247 | { |
tanasaro10 | 9:303d3628986a | 248 | static uint32_t tick=0; |
tanasaro10 | 9:303d3628986a | 249 | int retVal; |
tanasaro10 | 10:c7d53e4e0602 | 250 | |
tanasaro10 | 8:f28ad4600b0f | 251 | // update time |
tanasaro10 | 9:303d3628986a | 252 | update_time(&g_myTimeVar, &g_myDateVar, gTimeInstant); |
tanasaro10 | 9:303d3628986a | 253 | |
tanasaro10 | 9:303d3628986a | 254 | //update measurements |
tanasaro10 | 9:303d3628986a | 255 | retVal = update_measurements(); |
tanasaro10 | 10:c7d53e4e0602 | 256 | |
tanasaro10 | 9:303d3628986a | 257 | // if there are changes in data save |
tanasaro10 | 9:303d3628986a | 258 | if ((retVal!=0)&&(g_LogActive==true)) { |
tanasaro10 | 10:c7d53e4e0602 | 259 | write_data_to_flash(&tick); |
tanasaro10 | 9:303d3628986a | 260 | } |
tanasaro10 | 9:303d3628986a | 261 | tick++; |
tanasaro10 | 9:303d3628986a | 262 | } |
tanasaro10 | 9:303d3628986a | 263 | |
tanasaro10 | 9:303d3628986a | 264 | // Radio commands decode |
tanasaro10 | 9:303d3628986a | 265 | void decode(uint8_t * buffer, uint16_t length) |
tanasaro10 | 9:303d3628986a | 266 | { |
tanasaro10 | 9:303d3628986a | 267 | uint16_t len; |
tanasaro10 | 9:303d3628986a | 268 | char myBuf[TXRX_BUF_LEN]; |
tanasaro10 | 9:303d3628986a | 269 | |
tanasaro10 | 9:303d3628986a | 270 | switch (buffer[0]) { |
tanasaro10 | 9:303d3628986a | 271 | case 'i': {// Analog Input Read Request |
tanasaro10 | 9:303d3628986a | 272 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 273 | case '0': { |
tanasaro10 | 9:303d3628986a | 274 | // display all inputs |
tanasaro10 | 9:303d3628986a | 275 | sprintf(myBuf,"All:%3d,%3d,%3d,%2d", g_currMeasures.light,g_currMeasures.gndV,g_currMeasures.temp,g_currMeasures.led_on); |
tanasaro10 | 9:303d3628986a | 276 | len = 18; |
tanasaro10 | 9:303d3628986a | 277 | break; |
tanasaro10 | 9:303d3628986a | 278 | } |
tanasaro10 | 9:303d3628986a | 279 | case '1': { |
tanasaro10 | 9:303d3628986a | 280 | sprintf(myBuf,"Input 1 = %3d", g_currMeasures.light); |
tanasaro10 | 9:303d3628986a | 281 | len = 13; |
tanasaro10 | 9:303d3628986a | 282 | break; |
tanasaro10 | 9:303d3628986a | 283 | } |
tanasaro10 | 9:303d3628986a | 284 | case '2': { |
tanasaro10 | 9:303d3628986a | 285 | sprintf(myBuf,"Input 2 = %3d", g_currMeasures.gndV); |
tanasaro10 | 9:303d3628986a | 286 | len = 13; |
tanasaro10 | 9:303d3628986a | 287 | break; |
tanasaro10 | 9:303d3628986a | 288 | } |
tanasaro10 | 9:303d3628986a | 289 | case '3': { |
tanasaro10 | 9:303d3628986a | 290 | sprintf(myBuf,"Input 3 = %3d", g_currMeasures.temp); |
tanasaro10 | 9:303d3628986a | 291 | len = 13; |
tanasaro10 | 9:303d3628986a | 292 | break; |
tanasaro10 | 9:303d3628986a | 293 | } |
tanasaro10 | 9:303d3628986a | 294 | case '4': { |
tanasaro10 | 9:303d3628986a | 295 | sprintf(myBuf,"Input 4 = %2d", g_currMeasures.led_on); |
tanasaro10 | 9:303d3628986a | 296 | len = 12; |
tanasaro10 | 9:303d3628986a | 297 | break; |
tanasaro10 | 9:303d3628986a | 298 | } |
tanasaro10 | 9:303d3628986a | 299 | default: { |
tanasaro10 | 9:303d3628986a | 300 | sprintf(myBuf,"All:%3d,%3d,%3d,%2d", g_currMeasures.light,g_currMeasures.gndV,g_currMeasures.temp,g_currMeasures.led_on); |
tanasaro10 | 9:303d3628986a | 301 | len = 18; |
tanasaro10 | 9:303d3628986a | 302 | break; |
tanasaro10 | 8:f28ad4600b0f | 303 | } |
tanasaro10 | 8:f28ad4600b0f | 304 | } |
tanasaro10 | 9:303d3628986a | 305 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 306 | break; |
tanasaro10 | 9:303d3628986a | 307 | } |
tanasaro10 | 9:303d3628986a | 308 | case 'l': {// toogle led |
tanasaro10 | 9:303d3628986a | 309 | led = ! led; |
tanasaro10 | 9:303d3628986a | 310 | if (led==0) { |
tanasaro10 | 9:303d3628986a | 311 | sprintf(myBuf,"%s","ON"); |
tanasaro10 | 9:303d3628986a | 312 | len = 2; |
tanasaro10 | 9:303d3628986a | 313 | } else { |
tanasaro10 | 9:303d3628986a | 314 | sprintf(myBuf,"%s","OFF"); |
tanasaro10 | 9:303d3628986a | 315 | len = 3; |
tanasaro10 | 9:303d3628986a | 316 | } |
tanasaro10 | 9:303d3628986a | 317 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 318 | break; |
tanasaro10 | 9:303d3628986a | 319 | } |
tanasaro10 | 9:303d3628986a | 320 | case 's': {// buzzer |
tanasaro10 | 10:c7d53e4e0602 | 321 | if (((buffer[1]>'9')||(buffer[1]<'0'))||((buffer[2]>'9')||(buffer[2]<'0'))) { |
tanasaro10 | 9:303d3628986a | 322 | MyASSERT(true,&pc, buffer[1]); // notify on serial interface |
tanasaro10 | 9:303d3628986a | 323 | on_error_radioMsg(); // notify on radio |
tanasaro10 | 9:303d3628986a | 324 | break; |
tanasaro10 | 9:303d3628986a | 325 | } else { |
tanasaro10 | 9:303d3628986a | 326 | buzz_int(&buzzer, (buffer[1]-'0'),(buffer[2]-'0')); |
tanasaro10 | 9:303d3628986a | 327 | sprintf(myBuf,"%s:%f","S",buzzer.read()); |
tanasaro10 | 9:303d3628986a | 328 | len= 7; |
tanasaro10 | 9:303d3628986a | 329 | } |
tanasaro10 | 9:303d3628986a | 330 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 331 | break; |
tanasaro10 | 9:303d3628986a | 332 | } |
tanasaro10 | 9:303d3628986a | 333 | case 't': {// time operations |
tanasaro10 | 9:303d3628986a | 334 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 335 | case 'i': {// time insert |
tanasaro10 | 9:303d3628986a | 336 | memcpy(myBuf,&buffer[2],2); |
tanasaro10 | 9:303d3628986a | 337 | g_myTimeVar.newTime.hour=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 9:303d3628986a | 338 | memcpy(myBuf,&buffer[4],2); |
tanasaro10 | 9:303d3628986a | 339 | g_myTimeVar.newTime.min=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 9:303d3628986a | 340 | memcpy(myBuf,&buffer[6],2); |
tanasaro10 | 9:303d3628986a | 341 | g_myTimeVar.newTime.sec=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 9:303d3628986a | 342 | g_myTimeVar.updateTime = true; |
tanasaro10 | 9:303d3628986a | 343 | sprintf(myBuf,"TimeInserted"); |
tanasaro10 | 9:303d3628986a | 344 | len= 12; |
tanasaro10 | 9:303d3628986a | 345 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 346 | break; |
tanasaro10 | 9:303d3628986a | 347 | } |
tanasaro10 | 9:303d3628986a | 348 | case 'g': {// time get |
tanasaro10 | 9:303d3628986a | 349 | sprintf(myBuf,"H:%2d:%2d:%2d",g_myTimeVar.currentTime.hour,g_myTimeVar.currentTime.min,g_myTimeVar.currentTime.sec); |
tanasaro10 | 9:303d3628986a | 350 | len = 11; |
tanasaro10 | 9:303d3628986a | 351 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 352 | break; |
tanasaro10 | 9:303d3628986a | 353 | } |
tanasaro10 | 9:303d3628986a | 354 | default: |
tanasaro10 | 9:303d3628986a | 355 | MyASSERT(true,&pc, buffer[1]); // notify on serial interface |
tanasaro10 | 9:303d3628986a | 356 | on_error_radioMsg(); // notify on radio |
tanasaro10 | 9:303d3628986a | 357 | } |
tanasaro10 | 9:303d3628986a | 358 | break; |
tanasaro10 | 8:f28ad4600b0f | 359 | } |
tanasaro10 | 9:303d3628986a | 360 | case 'd': {// date operations |
tanasaro10 | 9:303d3628986a | 361 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 362 | case 'i': { // date insert |
tanasaro10 | 9:303d3628986a | 363 | memcpy(myBuf,&buffer[2],2); |
tanasaro10 | 9:303d3628986a | 364 | g_myDateVar.newDate.year=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 9:303d3628986a | 365 | memcpy(myBuf,&buffer[4],2); |
tanasaro10 | 9:303d3628986a | 366 | g_myDateVar.newDate.month=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 9:303d3628986a | 367 | memcpy(myBuf,&buffer[6],2); |
tanasaro10 | 9:303d3628986a | 368 | g_myDateVar.newDate.day=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 9:303d3628986a | 369 | g_myDateVar.updateDate = true; |
tanasaro10 | 9:303d3628986a | 370 | sprintf(myBuf,"DateInserted"); |
tanasaro10 | 9:303d3628986a | 371 | len= 12; |
tanasaro10 | 9:303d3628986a | 372 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 373 | break; |
tanasaro10 | 9:303d3628986a | 374 | } |
tanasaro10 | 9:303d3628986a | 375 | case 'g': { // time get |
tanasaro10 | 9:303d3628986a | 376 | sprintf(myBuf,"D:20%2d:%2d:%2d",g_myDateVar.currentDate.year,g_myDateVar.currentDate.month,g_myDateVar.currentDate.day); |
tanasaro10 | 9:303d3628986a | 377 | len = 13; |
tanasaro10 | 9:303d3628986a | 378 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 379 | break; |
tanasaro10 | 9:303d3628986a | 380 | } |
tanasaro10 | 9:303d3628986a | 381 | default: |
tanasaro10 | 9:303d3628986a | 382 | MyASSERT(true,&pc, buffer[1]); // notify on serial interface |
tanasaro10 | 9:303d3628986a | 383 | on_error_radioMsg(); // notify on radio |
tanasaro10 | 9:303d3628986a | 384 | } |
tanasaro10 | 9:303d3628986a | 385 | break; |
tanasaro10 | 9:303d3628986a | 386 | } |
tanasaro10 | 9:303d3628986a | 387 | |
tanasaro10 | 9:303d3628986a | 388 | case 'f': {// file operations |
tanasaro10 | 9:303d3628986a | 389 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 390 | case '1': { |
tanasaro10 | 9:303d3628986a | 391 | //pc.printf("S Payload_t: %d \r\n",sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 392 | //pc.printf("S myDataL_t: %d \r\n",sizeof(myDataL_t)); |
tanasaro10 | 10:c7d53e4e0602 | 393 | |
tanasaro10 | 9:303d3628986a | 394 | sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage()); |
tanasaro10 | 9:303d3628986a | 395 | len = 18; |
tanasaro10 | 9:303d3628986a | 396 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 397 | break; |
tanasaro10 | 9:303d3628986a | 398 | } |
tanasaro10 | 10:c7d53e4e0602 | 399 | case '2': { // start measuring |
tanasaro10 | 9:303d3628986a | 400 | sprintf(myBuf,"Start Meas"); |
tanasaro10 | 9:303d3628986a | 401 | len = 12; |
tanasaro10 | 9:303d3628986a | 402 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 403 | g_LogActive = true; |
tanasaro10 | 9:303d3628986a | 404 | break; |
tanasaro10 | 9:303d3628986a | 405 | } |
tanasaro10 | 10:c7d53e4e0602 | 406 | case '3': { // stop measuring |
tanasaro10 | 9:303d3628986a | 407 | sprintf(myBuf,"Stop Meas"); |
tanasaro10 | 9:303d3628986a | 408 | len = 11; |
tanasaro10 | 9:303d3628986a | 409 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 410 | g_LogActive = false; |
tanasaro10 | 9:303d3628986a | 411 | break; |
tanasaro10 | 9:303d3628986a | 412 | } |
tanasaro10 | 9:303d3628986a | 413 | |
tanasaro10 | 9:303d3628986a | 414 | default: { |
tanasaro10 | 9:303d3628986a | 415 | // error |
tanasaro10 | 9:303d3628986a | 416 | } |
tanasaro10 | 9:303d3628986a | 417 | } |
tanasaro10 | 9:303d3628986a | 418 | break; |
tanasaro10 | 9:303d3628986a | 419 | } |
tanasaro10 | 9:303d3628986a | 420 | default: { |
tanasaro10 | 9:303d3628986a | 421 | MyASSERT(true,&pc, buffer[1]); // notify on serial interface |
tanasaro10 | 9:303d3628986a | 422 | on_error_radioMsg(); // notify on radio; |
tanasaro10 | 9:303d3628986a | 423 | } |
tanasaro10 | 8:f28ad4600b0f | 424 | } |
tanasaro10 | 8:f28ad4600b0f | 425 | } |
tanasaro10 | 8:f28ad4600b0f | 426 | |
tanasaro10 | 9:303d3628986a | 427 | // decode serial command that starts with x |
tanasaro10 | 9:303d3628986a | 428 | static void decode_s(uint8_t * buffer, uint16_t length) |
tanasaro10 | 9:303d3628986a | 429 | { |
tanasaro10 | 9:303d3628986a | 430 | uint8_t page_nr; |
tanasaro10 | 9:303d3628986a | 431 | char myBuf[5]; |
tanasaro10 | 9:303d3628986a | 432 | uint32_t * p_curr_addr; |
tanasaro10 | 10:c7d53e4e0602 | 433 | |
tanasaro10 | 9:303d3628986a | 434 | switch (buffer[0]) { |
tanasaro10 | 9:303d3628986a | 435 | case 'f': { // info about selected flash page |
tanasaro10 | 9:303d3628986a | 436 | if ((buffer[1]<='9')&&(buffer[1]>='0')) { |
tanasaro10 | 9:303d3628986a | 437 | memcpy(myBuf,&buffer[1],3); |
tanasaro10 | 10:c7d53e4e0602 | 438 | page_nr= atoi(myBuf); |
tanasaro10 | 10:c7d53e4e0602 | 439 | uint8_t p_word_count; |
tanasaro10 | 9:303d3628986a | 440 | |
tanasaro10 | 9:303d3628986a | 441 | pc.printf("buffer[1]: %c \r\n",buffer[1]); |
tanasaro10 | 9:303d3628986a | 442 | |
tanasaro10 | 9:303d3628986a | 443 | p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (flash_currPage() - page_nr)); |
tanasaro10 | 9:303d3628986a | 444 | pc.printf("page_addr: %x, pgNr = %d \r\n",p_curr_addr,(flash_currPage() - page_nr)); |
tanasaro10 | 9:303d3628986a | 445 | p_curr_addr += 1; |
tanasaro10 | 9:303d3628986a | 446 | pc.printf("page_addr: %x \r\n",p_curr_addr); |
tanasaro10 | 9:303d3628986a | 447 | p_word_count = (uint8_t)(*(p_curr_addr)); |
tanasaro10 | 10:c7d53e4e0602 | 448 | pc.printf("nr_of_words: %d \r\n",p_word_count); |
tanasaro10 | 9:303d3628986a | 449 | flash_page_serial_dump((p_curr_addr-1)); |
tanasaro10 | 9:303d3628986a | 450 | } |
tanasaro10 | 9:303d3628986a | 451 | break; |
tanasaro10 | 9:303d3628986a | 452 | } |
tanasaro10 | 9:303d3628986a | 453 | case 'd': { // full dump |
tanasaro10 | 10:c7d53e4e0602 | 454 | uint16_t page0; |
tanasaro10 | 10:c7d53e4e0602 | 455 | pc.printf("Full dump \r\n"); |
tanasaro10 | 10:c7d53e4e0602 | 456 | |
tanasaro10 | 9:303d3628986a | 457 | page0 = flash_currPage(); |
tanasaro10 | 9:303d3628986a | 458 | for (page_nr=1; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM+1); page_nr++) { |
tanasaro10 | 10:c7d53e4e0602 | 459 | if ((page0-page_nr)< MIN_PAGE_NUM) { |
tanasaro10 | 9:303d3628986a | 460 | page0 = MAX_PAGE_NUM + page_nr; |
tanasaro10 | 8:f28ad4600b0f | 461 | } |
tanasaro10 | 9:303d3628986a | 462 | p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (page0-page_nr)); |
tanasaro10 | 10:c7d53e4e0602 | 463 | flash_page_serial_dump(p_curr_addr); |
tanasaro10 | 9:303d3628986a | 464 | } |
tanasaro10 | 9:303d3628986a | 465 | break; |
tanasaro10 | 9:303d3628986a | 466 | } |
tanasaro10 | 10:c7d53e4e0602 | 467 | case 'g': { |
tanasaro10 | 10:c7d53e4e0602 | 468 | pc.printf("g_MyDataIdx= %d\r", g_MyDataIdx); |
tanasaro10 | 9:303d3628986a | 469 | break; |
tanasaro10 | 9:303d3628986a | 470 | } |
tanasaro10 | 9:303d3628986a | 471 | case 'c': { |
tanasaro10 | 10:c7d53e4e0602 | 472 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 473 | case 'a': { |
tanasaro10 | 9:303d3628986a | 474 | connectionUpdate(eStartAdvertising); |
tanasaro10 | 9:303d3628986a | 475 | break; |
tanasaro10 | 9:303d3628986a | 476 | } |
tanasaro10 | 9:303d3628986a | 477 | case 'c' : { |
tanasaro10 | 9:303d3628986a | 478 | connectionUpdate(eDisconnect); |
tanasaro10 | 9:303d3628986a | 479 | break; |
tanasaro10 | 8:f28ad4600b0f | 480 | } |
tanasaro10 | 9:303d3628986a | 481 | case 's' : { |
tanasaro10 | 9:303d3628986a | 482 | connectionUpdate(eStopAdvertising); |
tanasaro10 | 9:303d3628986a | 483 | break; |
tanasaro10 | 8:f28ad4600b0f | 484 | } |
tanasaro10 | 10:c7d53e4e0602 | 485 | default: |
tanasaro10 | 10:c7d53e4e0602 | 486 | pc.printf("Not recognized cmd !\r"); |
tanasaro10 | 10:c7d53e4e0602 | 487 | } |
tanasaro10 | 9:303d3628986a | 488 | break; |
tanasaro10 | 9:303d3628986a | 489 | } |
tanasaro10 | 9:303d3628986a | 490 | default: { |
tanasaro10 | 9:303d3628986a | 491 | // nothing |
tanasaro10 | 8:f28ad4600b0f | 492 | } |
tanasaro10 | 9:303d3628986a | 493 | } |
tanasaro10 | 8:f28ad4600b0f | 494 | } |
tanasaro10 | 9:303d3628986a | 495 | |
RedBearLab | 2:4b66b69c7ecb | 496 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
tanasaro10 | 9:303d3628986a | 497 | { |
tanasaro10 | 9:303d3628986a | 498 | uint8_t buf[TXRX_BUF_LEN+1]= {'R',':',0}; |
tanasaro10 | 9:303d3628986a | 499 | uint16_t bytesRead; |
tanasaro10 | 9:303d3628986a | 500 | |
tanasaro10 | 9:303d3628986a | 501 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { |
tanasaro10 | 6:a574229993b8 | 502 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), &buf[2], &bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 503 | memset(txPayload, 0, TXRX_BUF_LEN); |
tanasaro10 | 9:303d3628986a | 504 | memcpy(txPayload, &buf[2], bytesRead); |
tanasaro10 | 9:303d3628986a | 505 | if (txPayload[0] == 'x') { |
tanasaro10 | 9:303d3628986a | 506 | decode(&txPayload[1],bytesRead); |
tanasaro10 | 9:303d3628986a | 507 | } |
tanasaro10 | 9:303d3628986a | 508 | //echo back |
tanasaro10 | 9:303d3628986a | 509 | bytesRead+=2; |
tanasaro10 | 9:303d3628986a | 510 | sendRadioMsg(buf, bytesRead); |
tanasaro10 | 9:303d3628986a | 511 | |
tanasaro10 | 6:a574229993b8 | 512 | // print on PC monitor |
tanasaro10 | 9:303d3628986a | 513 | buf[bytesRead]='\r'; |
tanasaro10 | 9:303d3628986a | 514 | //buf[bytesRead+1]='\n'; |
tanasaro10 | 9:303d3628986a | 515 | pc.printf("%s",buf); |
RedBearLab | 0:cffe8ac1bdf0 | 516 | } |
RedBearLab | 0:cffe8ac1bdf0 | 517 | } |
RedBearLab | 0:cffe8ac1bdf0 | 518 | |
RedBearLab | 0:cffe8ac1bdf0 | 519 | void uartCB(void) |
tanasaro10 | 9:303d3628986a | 520 | { |
tanasaro10 | 9:303d3628986a | 521 | while(pc.readable()) { |
tanasaro10 | 9:303d3628986a | 522 | rx_buf[rx_len++] = pc.getc(); |
tanasaro10 | 9:303d3628986a | 523 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') { |
tanasaro10 | 9:303d3628986a | 524 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
tanasaro10 | 9:303d3628986a | 525 | if ((rx_buf[0]=='x')) { |
tanasaro10 | 10:c7d53e4e0602 | 526 | decode_s(&rx_buf[1],(rx_len-1)); // serial decode |
tanasaro10 | 9:303d3628986a | 527 | } |
tanasaro10 | 9:303d3628986a | 528 | rx_len= 0; |
RedBearLab | 0:cffe8ac1bdf0 | 529 | break; |
RedBearLab | 0:cffe8ac1bdf0 | 530 | } |
RedBearLab | 0:cffe8ac1bdf0 | 531 | } |
RedBearLab | 0:cffe8ac1bdf0 | 532 | } |
RedBearLab | 0:cffe8ac1bdf0 | 533 | |
tanasaro10 | 9:303d3628986a | 534 | void button() |
tanasaro10 | 9:303d3628986a | 535 | { |
tanasaro10 | 8:f28ad4600b0f | 536 | uint8_t buf[TXRX_BUF_LEN+1]; |
tanasaro10 | 8:f28ad4600b0f | 537 | buf[0]='B'; |
tanasaro10 | 10:c7d53e4e0602 | 538 | buf[1]='U'; |
tanasaro10 | 10:c7d53e4e0602 | 539 | buf[2]='T'; |
tanasaro10 | 8:f28ad4600b0f | 540 | buf[3]='N'; |
tanasaro10 | 10:c7d53e4e0602 | 541 | |
tanasaro10 | 9:303d3628986a | 542 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); |
tanasaro10 | 10:c7d53e4e0602 | 543 | g_bConnDisabled = !g_bConnDisabled; |
tanasaro10 | 10:c7d53e4e0602 | 544 | led = !led; |
tanasaro10 | 10:c7d53e4e0602 | 545 | if (g_bConnDisabled == true){ |
tanasaro10 | 10:c7d53e4e0602 | 546 | ble.disconnect((Gap::DisconnectionReason_t)0x12); |
tanasaro10 | 10:c7d53e4e0602 | 547 | g_bIsConnected = false; |
tanasaro10 | 10:c7d53e4e0602 | 548 | } else { |
tanasaro10 | 10:c7d53e4e0602 | 549 | connectionUpdate(eStartAdvertising); |
tanasaro10 | 10:c7d53e4e0602 | 550 | } |
tanasaro10 | 8:f28ad4600b0f | 551 | } |
tanasaro10 | 8:f28ad4600b0f | 552 | |
tanasaro10 | 9:303d3628986a | 553 | void g_varInit() |
tanasaro10 | 9:303d3628986a | 554 | { |
tanasaro10 | 9:303d3628986a | 555 | g_myDateVar.updateDate = false; |
tanasaro10 | 8:f28ad4600b0f | 556 | g_myTimeVar.updateTime = false; |
tanasaro10 | 9:303d3628986a | 557 | /* retreive latest date, time and page flash available */ |
tanasaro10 | 10:c7d53e4e0602 | 558 | search_latest_in_flash(&g_myDateVar.currentDate, &g_myTimeVar.currentTime ); |
tanasaro10 | 8:f28ad4600b0f | 559 | } |
tanasaro10 | 8:f28ad4600b0f | 560 | |
RedBearLab | 0:cffe8ac1bdf0 | 561 | int main(void) |
RedBearLab | 0:cffe8ac1bdf0 | 562 | { |
RedBearLab | 0:cffe8ac1bdf0 | 563 | ble.init(); |
tanasaro10 | 9:303d3628986a | 564 | g_varInit(); |
RedBearLab | 0:cffe8ac1bdf0 | 565 | ble.onDisconnection(disconnectionCallback); |
tanasaro10 | 9:303d3628986a | 566 | ble.onConnection(connectionCallback); |
tanasaro10 | 9:303d3628986a | 567 | ble.onDataWritten(WrittenHandler); |
tanasaro10 | 8:f28ad4600b0f | 568 | event.rise(&button); |
tanasaro10 | 10:c7d53e4e0602 | 569 | |
tanasaro10 | 6:a574229993b8 | 570 | pc.baud(19200); |
RedBearLab | 0:cffe8ac1bdf0 | 571 | pc.printf("SimpleChat Init \r\n"); |
tanasaro10 | 9:303d3628986a | 572 | |
RedBearLab | 0:cffe8ac1bdf0 | 573 | pc.attach( uartCB , pc.RxIrq); |
tanasaro10 | 9:303d3628986a | 574 | // setup advertising |
RedBearLab | 0:cffe8ac1bdf0 | 575 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:cffe8ac1bdf0 | 576 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:cffe8ac1bdf0 | 577 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
tanasaro10 | 9:303d3628986a | 578 | (const uint8_t *)"MyBleVT", sizeof("MyBleVT") - 1); |
RedBearLab | 0:cffe8ac1bdf0 | 579 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
tanasaro10 | 9:303d3628986a | 580 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
tanasaro10 | 9:303d3628986a | 581 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower)); |
tanasaro10 | 9:303d3628986a | 582 | ble.setTxPower(txPower); |
tanasaro10 | 9:303d3628986a | 583 | // 100ms; in multiples of 0.625ms. |
RedBearLab | 0:cffe8ac1bdf0 | 584 | ble.setAdvertisingInterval(160); |
tanasaro10 | 9:303d3628986a | 585 | /* |
tanasaro10 | 9:303d3628986a | 586 | // activate radio notifications - usefull for flashwrite |
tanasaro10 | 9:303d3628986a | 587 | void (*ptrFunc)(bool); |
tanasaro10 | 9:303d3628986a | 588 | ptrFunc = ble_flash_on_radio_active_evt; |
tanasaro10 | 9:303d3628986a | 589 | //needed for flash write |
tanasaro10 | 9:303d3628986a | 590 | //ble.onRadioNotification(ptrFunc); |
tanasaro10 | 10:c7d53e4e0602 | 591 | */ |
RedBearLab | 0:cffe8ac1bdf0 | 592 | ble.addService(uartService); |
tanasaro10 | 9:303d3628986a | 593 | ble.startAdvertising(); |
RedBearLab | 0:cffe8ac1bdf0 | 594 | pc.printf("Advertising Start \r\n"); |
tanasaro10 | 9:303d3628986a | 595 | periodicActions.attach(&at_eachInstant,gTimeInstant); |
tanasaro10 | 9:303d3628986a | 596 | while(1) { |
tanasaro10 | 9:303d3628986a | 597 | ble.waitForEvent(); |
RedBearLab | 0:cffe8ac1bdf0 | 598 | } |
RedBearLab | 0:cffe8ac1bdf0 | 599 | } |