Tema SCPI Negru Rares-Razvan
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_TemperatureEx by
main.cpp@13:bd15c17003e7, 2016-10-24 (annotated)
- Committer:
- raresnegru22
- Date:
- Mon Oct 24 10:09:39 2016 +0000
- Revision:
- 13:bd15c17003e7
- Parent:
- 12:7772974713ac
Tema SCPI
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. |
tanasaro10 | 9:303d3628986a | 24 | * Read read_me.md file for more informations about the extended feature |
RedBearLab | 1:1c058e553423 | 25 | */ |
tanasaro10 | 9:303d3628986a | 26 | |
tanasaro10 | 9:303d3628986a | 27 | |
RedBearLab | 2:4b66b69c7ecb | 28 | #include "ble/BLE.h" |
tanasaro10 | 8:f28ad4600b0f | 29 | #include <myData.h> |
tanasaro10 | 9:303d3628986a | 30 | #include <Gap.h> |
tanasaro10 | 9:303d3628986a | 31 | #include "ble_flash.c" |
tanasaro10 | 12:7772974713ac | 32 | #include "BatteryService.h" |
tanasaro10 | 12:7772974713ac | 33 | |
tanasaro10 | 12:7772974713ac | 34 | BLE ble; |
RedBearLab | 0:cffe8ac1bdf0 | 35 | |
RedBearLab | 0:cffe8ac1bdf0 | 36 | #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ |
RedBearLab | 0:cffe8ac1bdf0 | 37 | #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 38 | #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ |
RedBearLab | 0:cffe8ac1bdf0 | 39 | |
tanasaro10 | 9:303d3628986a | 40 | #define TXRX_BUF_LEN 20 /** For radio message transmission*/ |
tanasaro10 | 9:303d3628986a | 41 | |
tanasaro10 | 9:303d3628986a | 42 | #define MyASSERT(cond , serialpc, errVal) assert_error_app((bool)cond, serialpc, (uint16_t)errVal, __LINE__, __FILE__) |
RedBearLab | 0:cffe8ac1bdf0 | 43 | |
tanasaro10 | 12:7772974713ac | 44 | Serial pc(USBTX, USBRX); |
RedBearLab | 0:cffe8ac1bdf0 | 45 | |
tanasaro10 | 12:7772974713ac | 46 | uint8_t batteryLevel=100; |
RedBearLab | 0:cffe8ac1bdf0 | 47 | |
RedBearLab | 0:cffe8ac1bdf0 | 48 | // The Nordic UART Service |
RedBearLab | 0:cffe8ac1bdf0 | 49 | 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 | 50 | 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 | 51 | 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 | 52 | 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 | 53 | |
tanasaro10 | 12:7772974713ac | 54 | static const int8_t txPower = 0x1E; |
tanasaro10 | 12:7772974713ac | 55 | BatteryService *batteryService = NULL; |
RedBearLab | 0:cffe8ac1bdf0 | 56 | |
tanasaro10 | 12:7772974713ac | 57 | uint8_t txPayload[TXRX_BUF_LEN] = {0,}; |
RedBearLab | 0:cffe8ac1bdf0 | 58 | uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; |
tanasaro10 | 12:7772974713ac | 59 | char myBuf[TXRX_BUF_LEN]; |
tanasaro10 | 12:7772974713ac | 60 | uint16_t len; |
RedBearLab | 0:cffe8ac1bdf0 | 61 | |
RedBearLab | 0:cffe8ac1bdf0 | 62 | static uint8_t rx_buf[TXRX_BUF_LEN]; |
RedBearLab | 0:cffe8ac1bdf0 | 63 | static uint8_t rx_len=0; |
RedBearLab | 0:cffe8ac1bdf0 | 64 | |
tanasaro10 | 9:303d3628986a | 65 | static uint32_t gTimeInstant = 1; // TimerTick Resolution, in seconds |
tanasaro10 | 8:f28ad4600b0f | 66 | |
tanasaro10 | 12:7772974713ac | 67 | |
tanasaro10 | 10:c7d53e4e0602 | 68 | bool g_bIsConnected = false; |
tanasaro10 | 9:303d3628986a | 69 | bool g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 70 | bool g_bConnDisabled = false; |
tanasaro10 | 9:303d3628986a | 71 | bool g_LogActive = false; |
tanasaro10 | 9:303d3628986a | 72 | static myDataLog_t g_MyData; |
tanasaro10 | 9:303d3628986a | 73 | uint8_t g_MyDataIdx=0; |
tanasaro10 | 8:f28ad4600b0f | 74 | |
tanasaro10 | 12:7772974713ac | 75 | |
tanasaro10 | 12:7772974713ac | 76 | |
tanasaro10 | 9:303d3628986a | 77 | // pins connected for measuring |
tanasaro10 | 12:7772974713ac | 78 | |
tanasaro10 | 8:f28ad4600b0f | 79 | DigitalOut led(LED1); |
tanasaro10 | 12:7772974713ac | 80 | // voltage for the termic resistor |
tanasaro10 | 12:7772974713ac | 81 | AnalogIn Vin(A5); |
tanasaro10 | 12:7772974713ac | 82 | |
tanasaro10 | 12:7772974713ac | 83 | myPayload_t g_currMeasures2; // last measurements |
tanasaro10 | 8:f28ad4600b0f | 84 | |
tanasaro10 | 8:f28ad4600b0f | 85 | Timeout timeout_err; // timeout for buzz on error |
tanasaro10 | 9:303d3628986a | 86 | Ticker periodicActions; |
tanasaro10 | 12:7772974713ac | 87 | bool bNewSample = false; |
tanasaro10 | 11:baafa4f7a15e | 88 | mdatetime_manager_t g_myDateTimeVar; |
tanasaro10 | 12:7772974713ac | 89 | uint8_t gBatteryValue=0; |
tanasaro10 | 9:303d3628986a | 90 | 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 | 91 | |
RedBearLab | 0:cffe8ac1bdf0 | 92 | GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
tanasaro10 | 9:303d3628986a | 93 | |
RedBearLab | 0:cffe8ac1bdf0 | 94 | GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; |
RedBearLab | 0:cffe8ac1bdf0 | 95 | |
RedBearLab | 0:cffe8ac1bdf0 | 96 | GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
RedBearLab | 0:cffe8ac1bdf0 | 97 | |
tanasaro10 | 12:7772974713ac | 98 | void at_timeout_err() |
tanasaro10 | 12:7772974713ac | 99 | { |
tanasaro10 | 12:7772974713ac | 100 | // stop buzz |
tanasaro10 | 12:7772974713ac | 101 | |
tanasaro10 | 12:7772974713ac | 102 | } |
tanasaro10 | 12:7772974713ac | 103 | |
tanasaro10 | 12:7772974713ac | 104 | void alarm(){ |
tanasaro10 | 12:7772974713ac | 105 | |
tanasaro10 | 12:7772974713ac | 106 | //timeout_err.attach(&at_timeout_err, 2); |
tanasaro10 | 12:7772974713ac | 107 | } |
tanasaro10 | 12:7772974713ac | 108 | |
tanasaro10 | 9:303d3628986a | 109 | void sendRadioMsg(const uint8_t* buf, uint16_t length) |
tanasaro10 | 9:303d3628986a | 110 | { |
tanasaro10 | 9:303d3628986a | 111 | uint8_t retVal; |
tanasaro10 | 9:303d3628986a | 112 | retVal = ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, length); |
tanasaro10 | 9:303d3628986a | 113 | //pc.printf("Err=%d\r\n",retVal); |
tanasaro10 | 9:303d3628986a | 114 | MyASSERT((retVal!=0),&pc, retVal); |
tanasaro10 | 8:f28ad4600b0f | 115 | } |
tanasaro10 | 8:f28ad4600b0f | 116 | |
RedBearLab | 0:cffe8ac1bdf0 | 117 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
RedBearLab | 0:cffe8ac1bdf0 | 118 | { |
RedBearLab | 0:cffe8ac1bdf0 | 119 | pc.printf("Disconnected \r\n"); |
tanasaro10 | 9:303d3628986a | 120 | g_bIsConnected = false; |
tanasaro10 | 9:303d3628986a | 121 | g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 122 | pc.printf("R: %d\r",reason); |
tanasaro10 | 10:c7d53e4e0602 | 123 | if (reason != 0x16) { |
tanasaro10 | 10:c7d53e4e0602 | 124 | ble.startAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 125 | g_bIsAdvertising = true; |
tanasaro10 | 10:c7d53e4e0602 | 126 | } |
RedBearLab | 0:cffe8ac1bdf0 | 127 | } |
RedBearLab | 0:cffe8ac1bdf0 | 128 | |
tanasaro10 | 9:303d3628986a | 129 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
tanasaro10 | 9:303d3628986a | 130 | { |
tanasaro10 | 9:303d3628986a | 131 | pc.printf("Connected \r\n"); |
tanasaro10 | 9:303d3628986a | 132 | g_bIsConnected = true; |
tanasaro10 | 9:303d3628986a | 133 | g_bIsAdvertising = false; |
tanasaro10 | 8:f28ad4600b0f | 134 | } |
tanasaro10 | 8:f28ad4600b0f | 135 | |
tanasaro10 | 10:c7d53e4e0602 | 136 | void connectionUpdate(connection_update_t option) |
tanasaro10 | 10:c7d53e4e0602 | 137 | { |
tanasaro10 | 10:c7d53e4e0602 | 138 | if (g_bConnDisabled == false) { |
tanasaro10 | 10:c7d53e4e0602 | 139 | switch (option) { |
tanasaro10 | 10:c7d53e4e0602 | 140 | case eStartAdvertising: { |
tanasaro10 | 10:c7d53e4e0602 | 141 | if ((g_bIsConnected == false)&&(g_bIsAdvertising == false)) { |
tanasaro10 | 10:c7d53e4e0602 | 142 | pc.printf("Start Advertising\r"); |
tanasaro10 | 10:c7d53e4e0602 | 143 | ble.startAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 144 | g_bIsAdvertising = true; |
tanasaro10 | 10:c7d53e4e0602 | 145 | } |
tanasaro10 | 10:c7d53e4e0602 | 146 | break; |
tanasaro10 | 9:303d3628986a | 147 | } |
tanasaro10 | 10:c7d53e4e0602 | 148 | case eStopAdvertising: { |
tanasaro10 | 10:c7d53e4e0602 | 149 | if (g_bIsAdvertising == true) { |
tanasaro10 | 10:c7d53e4e0602 | 150 | pc.printf("Stop Advertising\r"); |
tanasaro10 | 10:c7d53e4e0602 | 151 | ble.stopAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 152 | g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 153 | } |
tanasaro10 | 10:c7d53e4e0602 | 154 | break; |
tanasaro10 | 9:303d3628986a | 155 | } |
tanasaro10 | 10:c7d53e4e0602 | 156 | case eDisconnect: { |
tanasaro10 | 10:c7d53e4e0602 | 157 | if (g_bIsConnected == true) { |
tanasaro10 | 10:c7d53e4e0602 | 158 | pc.printf("Close connection\r"); |
tanasaro10 | 10:c7d53e4e0602 | 159 | ble.disconnect((Gap::DisconnectionReason_t)0x12); |
tanasaro10 | 10:c7d53e4e0602 | 160 | } else if (g_bIsAdvertising == true) { |
tanasaro10 | 10:c7d53e4e0602 | 161 | pc.printf("Stop Advertising\r"); |
tanasaro10 | 10:c7d53e4e0602 | 162 | ble.stopAdvertising(); |
tanasaro10 | 10:c7d53e4e0602 | 163 | g_bIsAdvertising = false; |
tanasaro10 | 10:c7d53e4e0602 | 164 | } |
tanasaro10 | 10:c7d53e4e0602 | 165 | break; |
tanasaro10 | 9:303d3628986a | 166 | } |
tanasaro10 | 9:303d3628986a | 167 | } |
tanasaro10 | 8:f28ad4600b0f | 168 | } |
tanasaro10 | 9:303d3628986a | 169 | } |
tanasaro10 | 12:7772974713ac | 170 | void write_data_to_flash(uint32_t *tick, myPayload_t * currMeasures) |
tanasaro10 | 10:c7d53e4e0602 | 171 | { |
tanasaro10 | 9:303d3628986a | 172 | uint8_t page_num=0; |
tanasaro10 | 10:c7d53e4e0602 | 173 | |
tanasaro10 | 9:303d3628986a | 174 | if (g_MyDataIdx==0) { |
tanasaro10 | 9:303d3628986a | 175 | //initiate connection |
tanasaro10 | 9:303d3628986a | 176 | connectionUpdate(eStartAdvertising); |
tanasaro10 | 9:303d3628986a | 177 | // time and date used to initialize the g_MyData variable |
tanasaro10 | 11:baafa4f7a15e | 178 | memcpy(&g_MyData.startData.datetime,&g_myDateTimeVar.currentDateTime, sizeof(mdate_time_t)); |
tanasaro10 | 12:7772974713ac | 179 | memcpy(&g_MyData.startData.data,currMeasures, sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 180 | } else { |
tanasaro10 | 9:303d3628986a | 181 | // it should be logged here the time difference from last record... |
tanasaro10 | 11:baafa4f7a15e | 182 | g_MyData.myData[g_MyDataIdx-1].minutes = (uint16_t)(*tick*gTimeInstant / 60); |
tanasaro10 | 11:baafa4f7a15e | 183 | g_MyData.myData[g_MyDataIdx-1].seconds = (*tick*gTimeInstant% 60); |
tanasaro10 | 12:7772974713ac | 184 | memcpy(&g_MyData.myData[g_MyDataIdx-1].data,currMeasures, sizeof(myPayload_t)); |
tanasaro10 | 9:303d3628986a | 185 | } |
tanasaro10 | 9:303d3628986a | 186 | *tick = 0; |
tanasaro10 | 9:303d3628986a | 187 | |
tanasaro10 | 12:7772974713ac | 188 | if (g_MyDataIdx==(MAXBUFFER-3)) { |
tanasaro10 | 9:303d3628986a | 189 | //initiate disconnection |
tanasaro10 | 10:c7d53e4e0602 | 190 | connectionUpdate(eDisconnect); |
tanasaro10 | 9:303d3628986a | 191 | } |
tanasaro10 | 9:303d3628986a | 192 | |
tanasaro10 | 10:c7d53e4e0602 | 193 | if (g_MyDataIdx == MAXBUFFER) { |
tanasaro10 | 9:303d3628986a | 194 | // write2Flash the current page num |
tanasaro10 | 12:7772974713ac | 195 | //connectionUpdate(eDisconnect); |
tanasaro10 | 12:7772974713ac | 196 | |
tanasaro10 | 9:303d3628986a | 197 | page_num=flash_currPage(); |
tanasaro10 | 9:303d3628986a | 198 | // write2Flash the current page data |
tanasaro10 | 12:7772974713ac | 199 | ble_flash_page_write(page_num, (uint32_t*)&(g_MyData), 251u); |
tanasaro10 | 12:7772974713ac | 200 | memset(&g_MyData,0,sizeof(myDataLog_t)); |
tanasaro10 | 12:7772974713ac | 201 | //pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myDateTimeVar.currentDateTime.minutes); |
tanasaro10 | 9:303d3628986a | 202 | flash_go_nextPage(); |
tanasaro10 | 9:303d3628986a | 203 | } |
tanasaro10 | 10:c7d53e4e0602 | 204 | g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER+1); |
tanasaro10 | 8:f28ad4600b0f | 205 | } |
tanasaro10 | 8:f28ad4600b0f | 206 | |
tanasaro10 | 9:303d3628986a | 207 | |
tanasaro10 | 9:303d3628986a | 208 | void on_error_radioMsg() |
tanasaro10 | 9:303d3628986a | 209 | { |
tanasaro10 | 9:303d3628986a | 210 | sprintf(myBuf,"%s","WrongSyntax"); |
tanasaro10 | 12:7772974713ac | 211 | //buzz_int(&buzzer,5,3); |
tanasaro10 | 12:7772974713ac | 212 | //timeout_err.attach(&at_timeout_err, 2); |
tanasaro10 | 9:303d3628986a | 213 | sendRadioMsg((uint8_t*)&myBuf[0], 12); |
tanasaro10 | 8:f28ad4600b0f | 214 | } |
tanasaro10 | 8:f28ad4600b0f | 215 | |
tanasaro10 | 9:303d3628986a | 216 | void flash_page_serial_dump(uint32_t* p_curr_addr) |
tanasaro10 | 10:c7d53e4e0602 | 217 | { |
tanasaro10 | 10:c7d53e4e0602 | 218 | myDataLogShort_t initialData; |
tanasaro10 | 11:baafa4f7a15e | 219 | mdate_time_t * pdate; |
tanasaro10 | 10:c7d53e4e0602 | 220 | myDataL_t dataOut[2]; |
tanasaro10 | 10:c7d53e4e0602 | 221 | uint8_t i; |
tanasaro10 | 9:303d3628986a | 222 | |
tanasaro10 | 10:c7d53e4e0602 | 223 | p_curr_addr += 2; // skip the magic number and the word count |
tanasaro10 | 9:303d3628986a | 224 | memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); |
tanasaro10 | 11:baafa4f7a15e | 225 | pdate = &initialData.startData.datetime; |
tanasaro10 | 11:baafa4f7a15e | 226 | pc.printf("20%2d_%2d_%2d H:%2d P:%4x\r",pdate->year, pdate->month, pdate->day, pdate->hours, p_curr_addr); |
tanasaro10 | 12:7772974713ac | 227 | pc.printf("%2d:%2d;%3d;%3d;%3d;\r",pdate->minutes, pdate->seconds, initialData.startData.data.x, initialData.startData.data.y, initialData.startData.data.z); |
tanasaro10 | 12:7772974713ac | 228 | pc.printf("%2d:%2d;%3d;%3d;%3d;%3d\r",initialData.myData.minutes, initialData.myData.seconds, initialData.myData.data.x, initialData.myData.data.y, initialData.myData.data.z,initialData.myData.data.temp); |
tanasaro10 | 9:303d3628986a | 229 | p_curr_addr += 6; |
tanasaro10 | 10:c7d53e4e0602 | 230 | |
tanasaro10 | 9:303d3628986a | 231 | for (i=0; i<49; i++) { |
tanasaro10 | 9:303d3628986a | 232 | memcpy((uint32_t*)&dataOut, p_curr_addr, 5*sizeof(uint32_t)); |
tanasaro10 | 12:7772974713ac | 233 | pc.printf("%2d:%2d;%3d;%3d;%3d;%3d\r",dataOut[0].minutes, dataOut[0].seconds, dataOut[0].data.x, dataOut[0].data.y, dataOut[0].data.z,dataOut[0].data.temp); |
tanasaro10 | 12:7772974713ac | 234 | pc.printf("%2d:%2d;%3d;%3d;%3d;%3d\r",dataOut[1].minutes, dataOut[1].seconds, dataOut[1].data.x, dataOut[1].data.y, dataOut[1].data.z,dataOut[1].data.temp); |
tanasaro10 | 9:303d3628986a | 235 | p_curr_addr += 5; |
tanasaro10 | 10:c7d53e4e0602 | 236 | } |
tanasaro10 | 9:303d3628986a | 237 | } |
tanasaro10 | 9:303d3628986a | 238 | |
tanasaro10 | 12:7772974713ac | 239 | int sign(int nr){ |
tanasaro10 | 12:7772974713ac | 240 | int retVal=0; |
tanasaro10 | 12:7772974713ac | 241 | if (nr>0) retVal=1; |
tanasaro10 | 12:7772974713ac | 242 | else if (nr<0) retVal=-1; |
tanasaro10 | 12:7772974713ac | 243 | |
tanasaro10 | 12:7772974713ac | 244 | return retVal; |
tanasaro10 | 12:7772974713ac | 245 | } |
tanasaro10 | 12:7772974713ac | 246 | |
tanasaro10 | 12:7772974713ac | 247 | float read_real_value(void){ |
tanasaro10 | 12:7772974713ac | 248 | uint32_t wrk,reg0,reg1,reg2; |
tanasaro10 | 12:7772974713ac | 249 | reg0 = NRF_ADC->ENABLE; // save register value |
tanasaro10 | 12:7772974713ac | 250 | reg1 = NRF_ADC->CONFIG; // save register value |
tanasaro10 | 12:7772974713ac | 251 | reg2 = NRF_ADC->RESULT; // save register value |
tanasaro10 | 12:7772974713ac | 252 | NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; |
tanasaro10 | 12:7772974713ac | 253 | NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | |
tanasaro10 | 12:7772974713ac | 254 | (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | |
tanasaro10 | 12:7772974713ac | 255 | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | |
tanasaro10 | 12:7772974713ac | 256 | (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | |
tanasaro10 | 12:7772974713ac | 257 | (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); |
tanasaro10 | 12:7772974713ac | 258 | NRF_ADC->EVENTS_END = 0; |
tanasaro10 | 12:7772974713ac | 259 | NRF_ADC->TASKS_START = 1; |
tanasaro10 | 12:7772974713ac | 260 | while (!NRF_ADC->EVENTS_END) {;} |
tanasaro10 | 12:7772974713ac | 261 | wrk = NRF_ADC->RESULT; // 10 bit result |
tanasaro10 | 12:7772974713ac | 262 | NRF_ADC->ENABLE = reg0; // recover register value |
tanasaro10 | 12:7772974713ac | 263 | NRF_ADC->CONFIG = reg1; // recover register value |
tanasaro10 | 12:7772974713ac | 264 | NRF_ADC->RESULT = reg2; // recover register value |
tanasaro10 | 12:7772974713ac | 265 | NRF_ADC->EVENTS_END = 0; |
tanasaro10 | 12:7772974713ac | 266 | return ((float)wrk / 1024 * 1.2 * 3.0); |
tanasaro10 | 12:7772974713ac | 267 | } |
tanasaro10 | 9:303d3628986a | 268 | |
tanasaro10 | 12:7772974713ac | 269 | uint8_t read100(void) |
tanasaro10 | 12:7772974713ac | 270 | { |
tanasaro10 | 12:7772974713ac | 271 | float wrk_vdd, v0p= 2.7 ,v100p= 3.3; |
tanasaro10 | 12:7772974713ac | 272 | wrk_vdd = read_real_value(); |
tanasaro10 | 12:7772974713ac | 273 | /*if (wrk_vdd <= v0p){ |
tanasaro10 | 12:7772974713ac | 274 | return 0; |
tanasaro10 | 12:7772974713ac | 275 | } else if (wrk_vdd >= v100p){ |
tanasaro10 | 12:7772974713ac | 276 | led = 0; |
tanasaro10 | 12:7772974713ac | 277 | return 100; |
tanasaro10 | 12:7772974713ac | 278 | } */ |
tanasaro10 | 12:7772974713ac | 279 | led = 1; |
tanasaro10 | 12:7772974713ac | 280 | wrk_vdd = (wrk_vdd - v0p) / (v100p - v0p); |
tanasaro10 | 12:7772974713ac | 281 | return (uint8_t)(wrk_vdd * 100); |
tanasaro10 | 10:c7d53e4e0602 | 282 | } |
tanasaro10 | 10:c7d53e4e0602 | 283 | |
tanasaro10 | 12:7772974713ac | 284 | void update_measurements() |
tanasaro10 | 12:7772974713ac | 285 | { |
tanasaro10 | 12:7772974713ac | 286 | uint32_t resistance; |
tanasaro10 | 12:7772974713ac | 287 | int32_t tempValue; |
tanasaro10 | 12:7772974713ac | 288 | |
tanasaro10 | 12:7772974713ac | 289 | // use Vin.read() o read the voltage from the AI5 pin |
tanasaro10 | 12:7772974713ac | 290 | // fell free to the fill code in this function .... |
raresnegru22 | 13:bd15c17003e7 | 291 | |
raresnegru22 | 13:bd15c17003e7 | 292 | //Let us consider the input voltage ranges from 0-3.3v.That means that the 0 voltage input from AI5 means the temperature of the resistor |
raresnegru22 | 13:bd15c17003e7 | 293 | //is -55 degrees,and a 3v3 input in AI5 means that the termistor is at 125 degrees. |
raresnegru22 | 13:bd15c17003e7 | 294 | //The number of values that can be inputed from 0 to 3v3 is 180 values. |
raresnegru22 | 13:bd15c17003e7 | 295 | //That means that each 0.018V increase on the input means that the temperature rised with 1 degree,and every decrease |
raresnegru22 | 13:bd15c17003e7 | 296 | //of 0.018V means that the temperature went down with 1 degree Celsius. |
raresnegru22 | 13:bd15c17003e7 | 297 | //Let us now read the voltage from the AI5 pin |
raresnegru22 | 13:bd15c17003e7 | 298 | resistance=Vin.read(); |
raresnegru22 | 13:bd15c17003e7 | 299 | //Based on my calculations,if the voltage is under 1.01V,the temperature is negative.With that in mind |
raresnegru22 | 13:bd15c17003e7 | 300 | //We will separate negative from positive values. |
raresnegru22 | 13:bd15c17003e7 | 301 | if(resistance<=1.01) |
raresnegru22 | 13:bd15c17003e7 | 302 | { |
raresnegru22 | 13:bd15c17003e7 | 303 | if(resistance<0.018) |
raresnegru22 | 13:bd15c17003e7 | 304 | { |
raresnegru22 | 13:bd15c17003e7 | 305 | tempValue=-55; |
raresnegru22 | 13:bd15c17003e7 | 306 | } |
raresnegru22 | 13:bd15c17003e7 | 307 | tempValue=-55+resistance/0.018; |
raresnegru22 | 13:bd15c17003e7 | 308 | |
raresnegru22 | 13:bd15c17003e7 | 309 | } |
raresnegru22 | 13:bd15c17003e7 | 310 | tempValue=0+resistance/0.018; |
raresnegru22 | 13:bd15c17003e7 | 311 | |
raresnegru22 | 13:bd15c17003e7 | 312 | |
tanasaro10 | 12:7772974713ac | 313 | |
tanasaro10 | 12:7772974713ac | 314 | |
tanasaro10 | 12:7772974713ac | 315 | |
tanasaro10 | 12:7772974713ac | 316 | |
tanasaro10 | 12:7772974713ac | 317 | // print on the bluetooth communication the value of Temperature computed in Celsius degrees |
tanasaro10 | 12:7772974713ac | 318 | sprintf(myBuf,"T:%3d.%2d;%d;\r", tempValue/100,tempValue%100,resistance); |
tanasaro10 | 12:7772974713ac | 319 | len = 20; |
tanasaro10 | 12:7772974713ac | 320 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 12:7772974713ac | 321 | } |
tanasaro10 | 12:7772974713ac | 322 | |
tanasaro10 | 12:7772974713ac | 323 | |
tanasaro10 | 9:303d3628986a | 324 | void at_eachInstant() |
tanasaro10 | 10:c7d53e4e0602 | 325 | { |
tanasaro10 | 9:303d3628986a | 326 | static uint32_t tick=0; |
tanasaro10 | 12:7772974713ac | 327 | //const uint8_t sizeB= 3*sizeof(uint16_t); |
tanasaro10 | 12:7772974713ac | 328 | //static myPayload_t prevMeasures2=(myPayload_t) { 0, 0, 0, 0}; |
tanasaro10 | 10:c7d53e4e0602 | 329 | |
tanasaro10 | 8:f28ad4600b0f | 330 | // update time |
tanasaro10 | 11:baafa4f7a15e | 331 | update_time(&g_myDateTimeVar, gTimeInstant); |
tanasaro10 | 12:7772974713ac | 332 | //sendRadioMsg("Tick\r", 5); |
tanasaro10 | 12:7772974713ac | 333 | batteryService->updateBatteryLevel(read100()); |
tanasaro10 | 9:303d3628986a | 334 | //update measurements |
tanasaro10 | 12:7772974713ac | 335 | if (g_bIsConnected==true){ |
tanasaro10 | 12:7772974713ac | 336 | update_measurements(); |
tanasaro10 | 12:7772974713ac | 337 | } |
tanasaro10 | 12:7772974713ac | 338 | if (g_LogActive==true){ |
tanasaro10 | 9:303d3628986a | 339 | // if there are changes in data save |
tanasaro10 | 12:7772974713ac | 340 | //g_currMeasures2.temp = Temp.read_u16(); |
tanasaro10 | 12:7772974713ac | 341 | //if (memcmp(&prevMeasures2, &g_currMeasures2, sizeB)!=0) |
tanasaro10 | 12:7772974713ac | 342 | if (bNewSample == true) |
tanasaro10 | 12:7772974713ac | 343 | //if (update_measurements()==1) |
tanasaro10 | 12:7772974713ac | 344 | { |
tanasaro10 | 12:7772974713ac | 345 | bNewSample = false; |
tanasaro10 | 12:7772974713ac | 346 | write_data_to_flash(&tick, &g_currMeasures2); |
tanasaro10 | 12:7772974713ac | 347 | //memcpy(&prevMeasures2,&g_currMeasures2,sizeB); |
tanasaro10 | 12:7772974713ac | 348 | // |
tanasaro10 | 12:7772974713ac | 349 | //write_data_to_flash(&tick, &g_currMeasures2); |
tanasaro10 | 12:7772974713ac | 350 | } |
tanasaro10 | 12:7772974713ac | 351 | tick++; |
tanasaro10 | 9:303d3628986a | 352 | } |
tanasaro10 | 9:303d3628986a | 353 | } |
tanasaro10 | 9:303d3628986a | 354 | |
tanasaro10 | 12:7772974713ac | 355 | |
tanasaro10 | 9:303d3628986a | 356 | // Radio commands decode |
tanasaro10 | 9:303d3628986a | 357 | void decode(uint8_t * buffer, uint16_t length) |
tanasaro10 | 9:303d3628986a | 358 | { |
tanasaro10 | 9:303d3628986a | 359 | switch (buffer[0]) { |
tanasaro10 | 12:7772974713ac | 360 | case 'r': {// Read Operations |
tanasaro10 | 9:303d3628986a | 361 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 362 | case '0': { |
tanasaro10 | 9:303d3628986a | 363 | // display all inputs |
tanasaro10 | 12:7772974713ac | 364 | sprintf(myBuf,"Id1:%d,%d,%d,%d\r\n", g_currMeasures2.x,g_currMeasures2.y,g_currMeasures2.z,g_currMeasures2.temp); |
tanasaro10 | 12:7772974713ac | 365 | len = 20; |
tanasaro10 | 9:303d3628986a | 366 | break; |
tanasaro10 | 9:303d3628986a | 367 | } |
tanasaro10 | 9:303d3628986a | 368 | case '1': { |
tanasaro10 | 12:7772974713ac | 369 | sprintf(myBuf,"Id2:%d,%d,%d\r\n", g_currMeasures2.x,g_currMeasures2.y,g_currMeasures2.z); |
tanasaro10 | 12:7772974713ac | 370 | len = 20; |
tanasaro10 | 9:303d3628986a | 371 | break; |
tanasaro10 | 9:303d3628986a | 372 | } |
tanasaro10 | 9:303d3628986a | 373 | case '2': { |
tanasaro10 | 12:7772974713ac | 374 | sprintf(myBuf,"V:%2.3f\r\n", read_real_value()); |
tanasaro10 | 12:7772974713ac | 375 | len = 11; |
tanasaro10 | 9:303d3628986a | 376 | break; |
tanasaro10 | 9:303d3628986a | 377 | } |
tanasaro10 | 9:303d3628986a | 378 | case '3': { |
tanasaro10 | 12:7772974713ac | 379 | sprintf(myBuf,"T = %3d\r\n", g_currMeasures2.temp); |
tanasaro10 | 12:7772974713ac | 380 | len = 10; |
tanasaro10 | 9:303d3628986a | 381 | break; |
tanasaro10 | 9:303d3628986a | 382 | } |
tanasaro10 | 12:7772974713ac | 383 | case '4':{ |
tanasaro10 | 12:7772974713ac | 384 | // sprintf(myBuf,"F1=%2d\r\n",FILTER_COEF1); |
tanasaro10 | 12:7772974713ac | 385 | len = 7; |
tanasaro10 | 12:7772974713ac | 386 | break; |
tanasaro10 | 12:7772974713ac | 387 | } |
tanasaro10 | 12:7772974713ac | 388 | case '5':{ |
tanasaro10 | 12:7772974713ac | 389 | //sprintf(myBuf,"Acc=%2d\r\n",gAccAmp); |
tanasaro10 | 12:7772974713ac | 390 | len = 8; |
tanasaro10 | 9:303d3628986a | 391 | break; |
tanasaro10 | 9:303d3628986a | 392 | } |
tanasaro10 | 9:303d3628986a | 393 | default: { |
tanasaro10 | 12:7772974713ac | 394 | sprintf(myBuf,"Nothing \r\n"); |
tanasaro10 | 12:7772974713ac | 395 | len = 10; |
tanasaro10 | 9:303d3628986a | 396 | break; |
tanasaro10 | 8:f28ad4600b0f | 397 | } |
tanasaro10 | 8:f28ad4600b0f | 398 | } |
tanasaro10 | 9:303d3628986a | 399 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 400 | break; |
tanasaro10 | 9:303d3628986a | 401 | } |
tanasaro10 | 9:303d3628986a | 402 | case 'l': {// toogle led |
tanasaro10 | 9:303d3628986a | 403 | led = ! led; |
tanasaro10 | 9:303d3628986a | 404 | if (led==0) { |
tanasaro10 | 9:303d3628986a | 405 | sprintf(myBuf,"%s","ON"); |
tanasaro10 | 9:303d3628986a | 406 | len = 2; |
tanasaro10 | 9:303d3628986a | 407 | } else { |
tanasaro10 | 9:303d3628986a | 408 | sprintf(myBuf,"%s","OFF"); |
tanasaro10 | 9:303d3628986a | 409 | len = 3; |
tanasaro10 | 9:303d3628986a | 410 | } |
tanasaro10 | 9:303d3628986a | 411 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 412 | break; |
tanasaro10 | 12:7772974713ac | 413 | } |
tanasaro10 | 12:7772974713ac | 414 | case 'i':{ // Insert data values |
tanasaro10 | 12:7772974713ac | 415 | switch (buffer[1]){ |
tanasaro10 | 12:7772974713ac | 416 | case 'f':{ |
tanasaro10 | 12:7772974713ac | 417 | memcpy(myBuf,&buffer[2],2); |
tanasaro10 | 12:7772974713ac | 418 | //FILTER_COEF1=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 12:7772974713ac | 419 | break; |
tanasaro10 | 12:7772974713ac | 420 | } |
tanasaro10 | 12:7772974713ac | 421 | case 'a':{ |
tanasaro10 | 12:7772974713ac | 422 | memcpy(myBuf,&buffer[2],2); |
tanasaro10 | 12:7772974713ac | 423 | //gAccAmp = atoi(myBuf); |
tanasaro10 | 12:7772974713ac | 424 | break; |
tanasaro10 | 12:7772974713ac | 425 | } |
tanasaro10 | 12:7772974713ac | 426 | |
tanasaro10 | 12:7772974713ac | 427 | default: { |
tanasaro10 | 12:7772974713ac | 428 | // on_error_radioMsg(); // notify on radio |
tanasaro10 | 12:7772974713ac | 429 | } |
tanasaro10 | 9:303d3628986a | 430 | } |
tanasaro10 | 9:303d3628986a | 431 | break; |
tanasaro10 | 9:303d3628986a | 432 | } |
tanasaro10 | 11:baafa4f7a15e | 433 | case 'd': |
tanasaro10 | 11:baafa4f7a15e | 434 | case 't': {// date /time operations |
tanasaro10 | 9:303d3628986a | 435 | switch (buffer[1]) { |
tanasaro10 | 11:baafa4f7a15e | 436 | case 'i': { // date insert |
tanasaro10 | 11:baafa4f7a15e | 437 | uint8_t i; |
tanasaro10 | 11:baafa4f7a15e | 438 | uint8_t * pdata = &g_myDateTimeVar.newDateTime.year; // to insert data |
tanasaro10 | 11:baafa4f7a15e | 439 | |
tanasaro10 | 11:baafa4f7a15e | 440 | if (buffer[0]=='t') { |
tanasaro10 | 11:baafa4f7a15e | 441 | sprintf(myBuf," TimeInserted"); |
tanasaro10 | 11:baafa4f7a15e | 442 | pdata +=3; |
tanasaro10 | 11:baafa4f7a15e | 443 | } else {sprintf(myBuf," DateInserted");} |
tanasaro10 | 11:baafa4f7a15e | 444 | len= 14; |
tanasaro10 | 11:baafa4f7a15e | 445 | |
tanasaro10 | 11:baafa4f7a15e | 446 | for (i=0;i<3;i++){ |
tanasaro10 | 11:baafa4f7a15e | 447 | memcpy(myBuf,&buffer[2+2*i],2); |
tanasaro10 | 11:baafa4f7a15e | 448 | *pdata=atoi(myBuf); // TODO check if it is a number |
tanasaro10 | 11:baafa4f7a15e | 449 | pdata= pdata+1; |
tanasaro10 | 11:baafa4f7a15e | 450 | } |
tanasaro10 | 11:baafa4f7a15e | 451 | g_myDateTimeVar.updateDateTime = true; |
tanasaro10 | 11:baafa4f7a15e | 452 | |
tanasaro10 | 9:303d3628986a | 453 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 454 | break; |
tanasaro10 | 9:303d3628986a | 455 | } |
tanasaro10 | 11:baafa4f7a15e | 456 | case 'g': { // time/date get |
tanasaro10 | 11:baafa4f7a15e | 457 | uint8_t * pdata1 = &g_myDateTimeVar.currentDateTime.year; // to get data |
tanasaro10 | 11:baafa4f7a15e | 458 | if (buffer[0]=='t') { |
tanasaro10 | 11:baafa4f7a15e | 459 | pdata1 +=3; |
tanasaro10 | 11:baafa4f7a15e | 460 | sprintf(myBuf,"H:%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2)); |
tanasaro10 | 11:baafa4f7a15e | 461 | len = 11; |
tanasaro10 | 11:baafa4f7a15e | 462 | }else { |
tanasaro10 | 11:baafa4f7a15e | 463 | sprintf(myBuf,"D:20%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2)); |
tanasaro10 | 11:baafa4f7a15e | 464 | len = 13; |
tanasaro10 | 11:baafa4f7a15e | 465 | } |
tanasaro10 | 9:303d3628986a | 466 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 467 | break; |
tanasaro10 | 9:303d3628986a | 468 | } |
tanasaro10 | 9:303d3628986a | 469 | default: |
tanasaro10 | 9:303d3628986a | 470 | MyASSERT(true,&pc, buffer[1]); // notify on serial interface |
tanasaro10 | 9:303d3628986a | 471 | on_error_radioMsg(); // notify on radio |
tanasaro10 | 9:303d3628986a | 472 | } |
tanasaro10 | 9:303d3628986a | 473 | break; |
tanasaro10 | 9:303d3628986a | 474 | } |
tanasaro10 | 9:303d3628986a | 475 | |
tanasaro10 | 9:303d3628986a | 476 | case 'f': {// file operations |
tanasaro10 | 9:303d3628986a | 477 | switch (buffer[1]) { |
tanasaro10 | 11:baafa4f7a15e | 478 | case '1': { |
tanasaro10 | 9:303d3628986a | 479 | sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage()); |
tanasaro10 | 9:303d3628986a | 480 | len = 18; |
tanasaro10 | 9:303d3628986a | 481 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 482 | break; |
tanasaro10 | 9:303d3628986a | 483 | } |
tanasaro10 | 10:c7d53e4e0602 | 484 | case '2': { // start measuring |
tanasaro10 | 9:303d3628986a | 485 | sprintf(myBuf,"Start Meas"); |
tanasaro10 | 9:303d3628986a | 486 | len = 12; |
tanasaro10 | 9:303d3628986a | 487 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 488 | g_LogActive = true; |
tanasaro10 | 12:7772974713ac | 489 | |
tanasaro10 | 9:303d3628986a | 490 | break; |
tanasaro10 | 9:303d3628986a | 491 | } |
tanasaro10 | 10:c7d53e4e0602 | 492 | case '3': { // stop measuring |
tanasaro10 | 12:7772974713ac | 493 | //measureSampling.detach(); |
tanasaro10 | 9:303d3628986a | 494 | sprintf(myBuf,"Stop Meas"); |
tanasaro10 | 9:303d3628986a | 495 | len = 11; |
tanasaro10 | 9:303d3628986a | 496 | sendRadioMsg((uint8_t *)myBuf, len); |
tanasaro10 | 9:303d3628986a | 497 | g_LogActive = false; |
tanasaro10 | 12:7772974713ac | 498 | ble_flash_page_write(flash_currPage(), (uint32_t*)&(g_MyData), 251u); |
tanasaro10 | 12:7772974713ac | 499 | memset(&g_MyData,0,sizeof(myDataLog_t)); |
tanasaro10 | 12:7772974713ac | 500 | flash_go_nextPage(); |
tanasaro10 | 12:7772974713ac | 501 | break; |
tanasaro10 | 12:7772974713ac | 502 | } |
tanasaro10 | 12:7772974713ac | 503 | case '4':{ |
tanasaro10 | 12:7772974713ac | 504 | break; |
tanasaro10 | 12:7772974713ac | 505 | } |
tanasaro10 | 12:7772974713ac | 506 | case '5':{ // read one measure |
tanasaro10 | 12:7772974713ac | 507 | |
tanasaro10 | 9:303d3628986a | 508 | break; |
tanasaro10 | 9:303d3628986a | 509 | } |
tanasaro10 | 9:303d3628986a | 510 | default: { |
tanasaro10 | 9:303d3628986a | 511 | // error |
tanasaro10 | 9:303d3628986a | 512 | } |
tanasaro10 | 9:303d3628986a | 513 | } |
tanasaro10 | 9:303d3628986a | 514 | break; |
tanasaro10 | 9:303d3628986a | 515 | } |
tanasaro10 | 9:303d3628986a | 516 | default: { |
tanasaro10 | 9:303d3628986a | 517 | MyASSERT(true,&pc, buffer[1]); // notify on serial interface |
tanasaro10 | 9:303d3628986a | 518 | on_error_radioMsg(); // notify on radio; |
tanasaro10 | 9:303d3628986a | 519 | } |
tanasaro10 | 8:f28ad4600b0f | 520 | } |
tanasaro10 | 8:f28ad4600b0f | 521 | } |
tanasaro10 | 8:f28ad4600b0f | 522 | |
tanasaro10 | 9:303d3628986a | 523 | // decode serial command that starts with x |
tanasaro10 | 9:303d3628986a | 524 | static void decode_s(uint8_t * buffer, uint16_t length) |
tanasaro10 | 9:303d3628986a | 525 | { |
tanasaro10 | 9:303d3628986a | 526 | uint8_t page_nr; |
tanasaro10 | 9:303d3628986a | 527 | char myBuf[5]; |
tanasaro10 | 9:303d3628986a | 528 | uint32_t * p_curr_addr; |
tanasaro10 | 10:c7d53e4e0602 | 529 | |
tanasaro10 | 9:303d3628986a | 530 | switch (buffer[0]) { |
tanasaro10 | 9:303d3628986a | 531 | case 'f': { // info about selected flash page |
tanasaro10 | 9:303d3628986a | 532 | if ((buffer[1]<='9')&&(buffer[1]>='0')) { |
tanasaro10 | 9:303d3628986a | 533 | memcpy(myBuf,&buffer[1],3); |
tanasaro10 | 10:c7d53e4e0602 | 534 | page_nr= atoi(myBuf); |
tanasaro10 | 10:c7d53e4e0602 | 535 | uint8_t p_word_count; |
tanasaro10 | 9:303d3628986a | 536 | |
tanasaro10 | 9:303d3628986a | 537 | pc.printf("buffer[1]: %c \r\n",buffer[1]); |
tanasaro10 | 9:303d3628986a | 538 | |
tanasaro10 | 9:303d3628986a | 539 | p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (flash_currPage() - page_nr)); |
tanasaro10 | 9:303d3628986a | 540 | pc.printf("page_addr: %x, pgNr = %d \r\n",p_curr_addr,(flash_currPage() - page_nr)); |
tanasaro10 | 9:303d3628986a | 541 | p_curr_addr += 1; |
tanasaro10 | 9:303d3628986a | 542 | pc.printf("page_addr: %x \r\n",p_curr_addr); |
tanasaro10 | 9:303d3628986a | 543 | p_word_count = (uint8_t)(*(p_curr_addr)); |
tanasaro10 | 10:c7d53e4e0602 | 544 | pc.printf("nr_of_words: %d \r\n",p_word_count); |
tanasaro10 | 9:303d3628986a | 545 | flash_page_serial_dump((p_curr_addr-1)); |
tanasaro10 | 9:303d3628986a | 546 | } |
tanasaro10 | 9:303d3628986a | 547 | break; |
tanasaro10 | 9:303d3628986a | 548 | } |
tanasaro10 | 9:303d3628986a | 549 | case 'd': { // full dump |
tanasaro10 | 10:c7d53e4e0602 | 550 | uint16_t page0; |
tanasaro10 | 10:c7d53e4e0602 | 551 | pc.printf("Full dump \r\n"); |
tanasaro10 | 10:c7d53e4e0602 | 552 | |
tanasaro10 | 9:303d3628986a | 553 | page0 = flash_currPage(); |
tanasaro10 | 11:baafa4f7a15e | 554 | for (page_nr=0; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM); page_nr++) { |
tanasaro10 | 10:c7d53e4e0602 | 555 | if ((page0-page_nr)< MIN_PAGE_NUM) { |
tanasaro10 | 9:303d3628986a | 556 | page0 = MAX_PAGE_NUM + page_nr; |
tanasaro10 | 8:f28ad4600b0f | 557 | } |
tanasaro10 | 9:303d3628986a | 558 | p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (page0-page_nr)); |
tanasaro10 | 10:c7d53e4e0602 | 559 | flash_page_serial_dump(p_curr_addr); |
tanasaro10 | 9:303d3628986a | 560 | } |
tanasaro10 | 9:303d3628986a | 561 | break; |
tanasaro10 | 9:303d3628986a | 562 | } |
tanasaro10 | 10:c7d53e4e0602 | 563 | case 'g': { |
tanasaro10 | 10:c7d53e4e0602 | 564 | pc.printf("g_MyDataIdx= %d\r", g_MyDataIdx); |
tanasaro10 | 9:303d3628986a | 565 | break; |
tanasaro10 | 9:303d3628986a | 566 | } |
tanasaro10 | 9:303d3628986a | 567 | case 'c': { |
tanasaro10 | 10:c7d53e4e0602 | 568 | switch (buffer[1]) { |
tanasaro10 | 9:303d3628986a | 569 | case 'a': { |
tanasaro10 | 9:303d3628986a | 570 | connectionUpdate(eStartAdvertising); |
tanasaro10 | 9:303d3628986a | 571 | break; |
tanasaro10 | 9:303d3628986a | 572 | } |
tanasaro10 | 9:303d3628986a | 573 | case 'c' : { |
tanasaro10 | 9:303d3628986a | 574 | connectionUpdate(eDisconnect); |
tanasaro10 | 9:303d3628986a | 575 | break; |
tanasaro10 | 8:f28ad4600b0f | 576 | } |
tanasaro10 | 9:303d3628986a | 577 | case 's' : { |
tanasaro10 | 9:303d3628986a | 578 | connectionUpdate(eStopAdvertising); |
tanasaro10 | 9:303d3628986a | 579 | break; |
tanasaro10 | 8:f28ad4600b0f | 580 | } |
tanasaro10 | 10:c7d53e4e0602 | 581 | default: |
tanasaro10 | 10:c7d53e4e0602 | 582 | pc.printf("Not recognized cmd !\r"); |
tanasaro10 | 10:c7d53e4e0602 | 583 | } |
tanasaro10 | 9:303d3628986a | 584 | break; |
tanasaro10 | 9:303d3628986a | 585 | } |
tanasaro10 | 9:303d3628986a | 586 | default: { |
tanasaro10 | 9:303d3628986a | 587 | // nothing |
tanasaro10 | 8:f28ad4600b0f | 588 | } |
tanasaro10 | 9:303d3628986a | 589 | } |
tanasaro10 | 8:f28ad4600b0f | 590 | } |
tanasaro10 | 9:303d3628986a | 591 | |
RedBearLab | 2:4b66b69c7ecb | 592 | void WrittenHandler(const GattWriteCallbackParams *Handler) |
tanasaro10 | 9:303d3628986a | 593 | { |
tanasaro10 | 9:303d3628986a | 594 | uint8_t buf[TXRX_BUF_LEN+1]= {'R',':',0}; |
tanasaro10 | 9:303d3628986a | 595 | uint16_t bytesRead; |
tanasaro10 | 9:303d3628986a | 596 | |
tanasaro10 | 9:303d3628986a | 597 | if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { |
tanasaro10 | 6:a574229993b8 | 598 | ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), &buf[2], &bytesRead); |
RedBearLab | 0:cffe8ac1bdf0 | 599 | memset(txPayload, 0, TXRX_BUF_LEN); |
tanasaro10 | 9:303d3628986a | 600 | memcpy(txPayload, &buf[2], bytesRead); |
tanasaro10 | 9:303d3628986a | 601 | if (txPayload[0] == 'x') { |
tanasaro10 | 9:303d3628986a | 602 | decode(&txPayload[1],bytesRead); |
tanasaro10 | 9:303d3628986a | 603 | } |
tanasaro10 | 9:303d3628986a | 604 | //echo back |
tanasaro10 | 9:303d3628986a | 605 | bytesRead+=2; |
tanasaro10 | 9:303d3628986a | 606 | sendRadioMsg(buf, bytesRead); |
tanasaro10 | 9:303d3628986a | 607 | |
tanasaro10 | 6:a574229993b8 | 608 | // print on PC monitor |
tanasaro10 | 11:baafa4f7a15e | 609 | buf[bytesRead]='\r'; |
tanasaro10 | 9:303d3628986a | 610 | pc.printf("%s",buf); |
RedBearLab | 0:cffe8ac1bdf0 | 611 | } |
RedBearLab | 0:cffe8ac1bdf0 | 612 | } |
RedBearLab | 0:cffe8ac1bdf0 | 613 | |
RedBearLab | 0:cffe8ac1bdf0 | 614 | void uartCB(void) |
tanasaro10 | 9:303d3628986a | 615 | { |
tanasaro10 | 9:303d3628986a | 616 | while(pc.readable()) { |
tanasaro10 | 9:303d3628986a | 617 | rx_buf[rx_len++] = pc.getc(); |
tanasaro10 | 9:303d3628986a | 618 | if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') { |
tanasaro10 | 9:303d3628986a | 619 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); |
tanasaro10 | 9:303d3628986a | 620 | if ((rx_buf[0]=='x')) { |
tanasaro10 | 10:c7d53e4e0602 | 621 | decode_s(&rx_buf[1],(rx_len-1)); // serial decode |
tanasaro10 | 9:303d3628986a | 622 | } |
tanasaro10 | 9:303d3628986a | 623 | rx_len= 0; |
RedBearLab | 0:cffe8ac1bdf0 | 624 | break; |
RedBearLab | 0:cffe8ac1bdf0 | 625 | } |
RedBearLab | 0:cffe8ac1bdf0 | 626 | } |
RedBearLab | 0:cffe8ac1bdf0 | 627 | } |
RedBearLab | 0:cffe8ac1bdf0 | 628 | |
tanasaro10 | 9:303d3628986a | 629 | void button() |
tanasaro10 | 9:303d3628986a | 630 | { |
tanasaro10 | 8:f28ad4600b0f | 631 | uint8_t buf[TXRX_BUF_LEN+1]; |
tanasaro10 | 8:f28ad4600b0f | 632 | buf[0]='B'; |
tanasaro10 | 10:c7d53e4e0602 | 633 | buf[1]='U'; |
tanasaro10 | 10:c7d53e4e0602 | 634 | buf[2]='T'; |
tanasaro10 | 8:f28ad4600b0f | 635 | buf[3]='N'; |
tanasaro10 | 10:c7d53e4e0602 | 636 | |
tanasaro10 | 9:303d3628986a | 637 | ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); |
tanasaro10 | 10:c7d53e4e0602 | 638 | g_bConnDisabled = !g_bConnDisabled; |
tanasaro10 | 10:c7d53e4e0602 | 639 | led = !led; |
tanasaro10 | 10:c7d53e4e0602 | 640 | if (g_bConnDisabled == true){ |
tanasaro10 | 11:baafa4f7a15e | 641 | if (g_bIsConnected == true){ |
tanasaro10 | 11:baafa4f7a15e | 642 | ble.disconnect((Gap::DisconnectionReason_t)0x12); |
tanasaro10 | 11:baafa4f7a15e | 643 | g_bIsConnected = false; |
tanasaro10 | 11:baafa4f7a15e | 644 | } else if (g_bIsAdvertising == true) { |
tanasaro10 | 11:baafa4f7a15e | 645 | ble.stopAdvertising(); |
tanasaro10 | 11:baafa4f7a15e | 646 | g_bIsAdvertising = false; |
tanasaro10 | 11:baafa4f7a15e | 647 | } |
tanasaro10 | 10:c7d53e4e0602 | 648 | } else { |
tanasaro10 | 10:c7d53e4e0602 | 649 | connectionUpdate(eStartAdvertising); |
tanasaro10 | 10:c7d53e4e0602 | 650 | } |
tanasaro10 | 8:f28ad4600b0f | 651 | } |
tanasaro10 | 8:f28ad4600b0f | 652 | |
tanasaro10 | 9:303d3628986a | 653 | void g_varInit() |
tanasaro10 | 9:303d3628986a | 654 | { |
tanasaro10 | 11:baafa4f7a15e | 655 | g_myDateTimeVar.updateDateTime = true; |
tanasaro10 | 9:303d3628986a | 656 | /* retreive latest date, time and page flash available */ |
tanasaro10 | 11:baafa4f7a15e | 657 | search_latest_in_flash(&g_myDateTimeVar.newDateTime); |
tanasaro10 | 8:f28ad4600b0f | 658 | } |
tanasaro10 | 8:f28ad4600b0f | 659 | |
RedBearLab | 0:cffe8ac1bdf0 | 660 | int main(void) |
RedBearLab | 0:cffe8ac1bdf0 | 661 | { |
RedBearLab | 0:cffe8ac1bdf0 | 662 | ble.init(); |
tanasaro10 | 9:303d3628986a | 663 | g_varInit(); |
RedBearLab | 0:cffe8ac1bdf0 | 664 | ble.onDisconnection(disconnectionCallback); |
tanasaro10 | 9:303d3628986a | 665 | ble.onConnection(connectionCallback); |
tanasaro10 | 9:303d3628986a | 666 | ble.onDataWritten(WrittenHandler); |
tanasaro10 | 12:7772974713ac | 667 | //event.rise(&button); |
tanasaro10 | 12:7772974713ac | 668 | |
tanasaro10 | 12:7772974713ac | 669 | |
tanasaro10 | 12:7772974713ac | 670 | //event.rise(&accInt1); |
tanasaro10 | 6:a574229993b8 | 671 | pc.baud(19200); |
RedBearLab | 0:cffe8ac1bdf0 | 672 | pc.printf("SimpleChat Init \r\n"); |
tanasaro10 | 12:7772974713ac | 673 | |
tanasaro10 | 12:7772974713ac | 674 | //mma1 = Adafruit_MMA8451(); |
tanasaro10 | 12:7772974713ac | 675 | //mma2 = Adafruit_MMA8451(); |
tanasaro10 | 12:7772974713ac | 676 | //init_Acc(); |
tanasaro10 | 12:7772974713ac | 677 | |
RedBearLab | 0:cffe8ac1bdf0 | 678 | pc.attach( uartCB , pc.RxIrq); |
tanasaro10 | 9:303d3628986a | 679 | // setup advertising |
RedBearLab | 0:cffe8ac1bdf0 | 680 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
RedBearLab | 0:cffe8ac1bdf0 | 681 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
RedBearLab | 0:cffe8ac1bdf0 | 682 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
tanasaro10 | 12:7772974713ac | 683 | (const uint8_t *)"BleTp", sizeof("BleTp") - 1); |
RedBearLab | 0:cffe8ac1bdf0 | 684 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
tanasaro10 | 9:303d3628986a | 685 | (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
tanasaro10 | 9:303d3628986a | 686 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower)); |
tanasaro10 | 9:303d3628986a | 687 | ble.setTxPower(txPower); |
tanasaro10 | 9:303d3628986a | 688 | // 100ms; in multiples of 0.625ms. |
tanasaro10 | 12:7772974713ac | 689 | ble.setAdvertisingInterval(320); |
tanasaro10 | 9:303d3628986a | 690 | /* |
tanasaro10 | 9:303d3628986a | 691 | // activate radio notifications - usefull for flashwrite |
tanasaro10 | 9:303d3628986a | 692 | void (*ptrFunc)(bool); |
tanasaro10 | 9:303d3628986a | 693 | ptrFunc = ble_flash_on_radio_active_evt; |
tanasaro10 | 9:303d3628986a | 694 | //needed for flash write |
tanasaro10 | 9:303d3628986a | 695 | //ble.onRadioNotification(ptrFunc); |
tanasaro10 | 10:c7d53e4e0602 | 696 | */ |
RedBearLab | 0:cffe8ac1bdf0 | 697 | ble.addService(uartService); |
tanasaro10 | 12:7772974713ac | 698 | batteryService = new BatteryService(ble, batteryLevel); |
tanasaro10 | 9:303d3628986a | 699 | ble.startAdvertising(); |
tanasaro10 | 12:7772974713ac | 700 | pc.printf("Advertising Start \r\n"); |
tanasaro10 | 12:7772974713ac | 701 | periodicActions.attach(&at_eachInstant,gTimeInstant); |
tanasaro10 | 12:7772974713ac | 702 | gBatteryValue = read100(); |
tanasaro10 | 12:7772974713ac | 703 | |
tanasaro10 | 9:303d3628986a | 704 | while(1) { |
tanasaro10 | 9:303d3628986a | 705 | ble.waitForEvent(); |
RedBearLab | 0:cffe8ac1bdf0 | 706 | } |
RedBearLab | 0:cffe8ac1bdf0 | 707 | } |