This program is given as a sample exercise. It has all the functionality to be used on a BLE Nano device and to connect to SimpleChat application for Android/ iOS from RebBearLab. The aim of the exercise is to read a voltage and then to convert as good as possible the appropriate temperature in Celsius degrees. AI5 pin is considered for reading the voltage for the termistor. The ADC of AI5 is called every second. The function to be updated : update_measurements() from main.cpp file.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_DataLogger_with_Chat by Valentin Tanasa

Committer:
tanasaro10
Date:
Sat Oct 08 10:30:15 2016 +0000
Revision:
12:7772974713ac
Parent:
11:baafa4f7a15e
Version ready for use to exercise.

Who changed what in which revision?

UserRevisionLine numberNew 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 ....
tanasaro10 12:7772974713ac 291 // aditionally you can declare globals variables at the begining of the file or in myData.h
tanasaro10 12:7772974713ac 292
tanasaro10 12:7772974713ac 293
tanasaro10 12:7772974713ac 294
tanasaro10 12:7772974713ac 295
tanasaro10 12:7772974713ac 296
tanasaro10 12:7772974713ac 297 // print on the bluetooth communication the value of Temperature computed in Celsius degrees
tanasaro10 12:7772974713ac 298 sprintf(myBuf,"T:%3d.%2d;%d;\r", tempValue/100,tempValue%100,resistance);
tanasaro10 12:7772974713ac 299 len = 20;
tanasaro10 12:7772974713ac 300 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 12:7772974713ac 301 }
tanasaro10 12:7772974713ac 302
tanasaro10 12:7772974713ac 303
tanasaro10 9:303d3628986a 304 void at_eachInstant()
tanasaro10 10:c7d53e4e0602 305 {
tanasaro10 9:303d3628986a 306 static uint32_t tick=0;
tanasaro10 12:7772974713ac 307 //const uint8_t sizeB= 3*sizeof(uint16_t);
tanasaro10 12:7772974713ac 308 //static myPayload_t prevMeasures2=(myPayload_t) { 0, 0, 0, 0};
tanasaro10 10:c7d53e4e0602 309
tanasaro10 8:f28ad4600b0f 310 // update time
tanasaro10 11:baafa4f7a15e 311 update_time(&g_myDateTimeVar, gTimeInstant);
tanasaro10 12:7772974713ac 312 //sendRadioMsg("Tick\r", 5);
tanasaro10 12:7772974713ac 313 batteryService->updateBatteryLevel(read100());
tanasaro10 9:303d3628986a 314 //update measurements
tanasaro10 12:7772974713ac 315 if (g_bIsConnected==true){
tanasaro10 12:7772974713ac 316 update_measurements();
tanasaro10 12:7772974713ac 317 }
tanasaro10 12:7772974713ac 318 if (g_LogActive==true){
tanasaro10 9:303d3628986a 319 // if there are changes in data save
tanasaro10 12:7772974713ac 320 //g_currMeasures2.temp = Temp.read_u16();
tanasaro10 12:7772974713ac 321 //if (memcmp(&prevMeasures2, &g_currMeasures2, sizeB)!=0)
tanasaro10 12:7772974713ac 322 if (bNewSample == true)
tanasaro10 12:7772974713ac 323 //if (update_measurements()==1)
tanasaro10 12:7772974713ac 324 {
tanasaro10 12:7772974713ac 325 bNewSample = false;
tanasaro10 12:7772974713ac 326 write_data_to_flash(&tick, &g_currMeasures2);
tanasaro10 12:7772974713ac 327 //memcpy(&prevMeasures2,&g_currMeasures2,sizeB);
tanasaro10 12:7772974713ac 328 //
tanasaro10 12:7772974713ac 329 //write_data_to_flash(&tick, &g_currMeasures2);
tanasaro10 12:7772974713ac 330 }
tanasaro10 12:7772974713ac 331 tick++;
tanasaro10 9:303d3628986a 332 }
tanasaro10 9:303d3628986a 333 }
tanasaro10 9:303d3628986a 334
tanasaro10 12:7772974713ac 335
tanasaro10 9:303d3628986a 336 // Radio commands decode
tanasaro10 9:303d3628986a 337 void decode(uint8_t * buffer, uint16_t length)
tanasaro10 9:303d3628986a 338 {
tanasaro10 9:303d3628986a 339 switch (buffer[0]) {
tanasaro10 12:7772974713ac 340 case 'r': {// Read Operations
tanasaro10 9:303d3628986a 341 switch (buffer[1]) {
tanasaro10 9:303d3628986a 342 case '0': {
tanasaro10 9:303d3628986a 343 // display all inputs
tanasaro10 12:7772974713ac 344 sprintf(myBuf,"Id1:%d,%d,%d,%d\r\n", g_currMeasures2.x,g_currMeasures2.y,g_currMeasures2.z,g_currMeasures2.temp);
tanasaro10 12:7772974713ac 345 len = 20;
tanasaro10 9:303d3628986a 346 break;
tanasaro10 9:303d3628986a 347 }
tanasaro10 9:303d3628986a 348 case '1': {
tanasaro10 12:7772974713ac 349 sprintf(myBuf,"Id2:%d,%d,%d\r\n", g_currMeasures2.x,g_currMeasures2.y,g_currMeasures2.z);
tanasaro10 12:7772974713ac 350 len = 20;
tanasaro10 9:303d3628986a 351 break;
tanasaro10 9:303d3628986a 352 }
tanasaro10 9:303d3628986a 353 case '2': {
tanasaro10 12:7772974713ac 354 sprintf(myBuf,"V:%2.3f\r\n", read_real_value());
tanasaro10 12:7772974713ac 355 len = 11;
tanasaro10 9:303d3628986a 356 break;
tanasaro10 9:303d3628986a 357 }
tanasaro10 9:303d3628986a 358 case '3': {
tanasaro10 12:7772974713ac 359 sprintf(myBuf,"T = %3d\r\n", g_currMeasures2.temp);
tanasaro10 12:7772974713ac 360 len = 10;
tanasaro10 9:303d3628986a 361 break;
tanasaro10 9:303d3628986a 362 }
tanasaro10 12:7772974713ac 363 case '4':{
tanasaro10 12:7772974713ac 364 // sprintf(myBuf,"F1=%2d\r\n",FILTER_COEF1);
tanasaro10 12:7772974713ac 365 len = 7;
tanasaro10 12:7772974713ac 366 break;
tanasaro10 12:7772974713ac 367 }
tanasaro10 12:7772974713ac 368 case '5':{
tanasaro10 12:7772974713ac 369 //sprintf(myBuf,"Acc=%2d\r\n",gAccAmp);
tanasaro10 12:7772974713ac 370 len = 8;
tanasaro10 9:303d3628986a 371 break;
tanasaro10 9:303d3628986a 372 }
tanasaro10 9:303d3628986a 373 default: {
tanasaro10 12:7772974713ac 374 sprintf(myBuf,"Nothing \r\n");
tanasaro10 12:7772974713ac 375 len = 10;
tanasaro10 9:303d3628986a 376 break;
tanasaro10 8:f28ad4600b0f 377 }
tanasaro10 8:f28ad4600b0f 378 }
tanasaro10 9:303d3628986a 379 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 380 break;
tanasaro10 9:303d3628986a 381 }
tanasaro10 9:303d3628986a 382 case 'l': {// toogle led
tanasaro10 9:303d3628986a 383 led = ! led;
tanasaro10 9:303d3628986a 384 if (led==0) {
tanasaro10 9:303d3628986a 385 sprintf(myBuf,"%s","ON");
tanasaro10 9:303d3628986a 386 len = 2;
tanasaro10 9:303d3628986a 387 } else {
tanasaro10 9:303d3628986a 388 sprintf(myBuf,"%s","OFF");
tanasaro10 9:303d3628986a 389 len = 3;
tanasaro10 9:303d3628986a 390 }
tanasaro10 9:303d3628986a 391 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 392 break;
tanasaro10 12:7772974713ac 393 }
tanasaro10 12:7772974713ac 394 case 'i':{ // Insert data values
tanasaro10 12:7772974713ac 395 switch (buffer[1]){
tanasaro10 12:7772974713ac 396 case 'f':{
tanasaro10 12:7772974713ac 397 memcpy(myBuf,&buffer[2],2);
tanasaro10 12:7772974713ac 398 //FILTER_COEF1=atoi(myBuf); // TODO check if it is a number
tanasaro10 12:7772974713ac 399 break;
tanasaro10 12:7772974713ac 400 }
tanasaro10 12:7772974713ac 401 case 'a':{
tanasaro10 12:7772974713ac 402 memcpy(myBuf,&buffer[2],2);
tanasaro10 12:7772974713ac 403 //gAccAmp = atoi(myBuf);
tanasaro10 12:7772974713ac 404 break;
tanasaro10 12:7772974713ac 405 }
tanasaro10 12:7772974713ac 406
tanasaro10 12:7772974713ac 407 default: {
tanasaro10 12:7772974713ac 408 // on_error_radioMsg(); // notify on radio
tanasaro10 12:7772974713ac 409 }
tanasaro10 9:303d3628986a 410 }
tanasaro10 9:303d3628986a 411 break;
tanasaro10 9:303d3628986a 412 }
tanasaro10 11:baafa4f7a15e 413 case 'd':
tanasaro10 11:baafa4f7a15e 414 case 't': {// date /time operations
tanasaro10 9:303d3628986a 415 switch (buffer[1]) {
tanasaro10 11:baafa4f7a15e 416 case 'i': { // date insert
tanasaro10 11:baafa4f7a15e 417 uint8_t i;
tanasaro10 11:baafa4f7a15e 418 uint8_t * pdata = &g_myDateTimeVar.newDateTime.year; // to insert data
tanasaro10 11:baafa4f7a15e 419
tanasaro10 11:baafa4f7a15e 420 if (buffer[0]=='t') {
tanasaro10 11:baafa4f7a15e 421 sprintf(myBuf," TimeInserted");
tanasaro10 11:baafa4f7a15e 422 pdata +=3;
tanasaro10 11:baafa4f7a15e 423 } else {sprintf(myBuf," DateInserted");}
tanasaro10 11:baafa4f7a15e 424 len= 14;
tanasaro10 11:baafa4f7a15e 425
tanasaro10 11:baafa4f7a15e 426 for (i=0;i<3;i++){
tanasaro10 11:baafa4f7a15e 427 memcpy(myBuf,&buffer[2+2*i],2);
tanasaro10 11:baafa4f7a15e 428 *pdata=atoi(myBuf); // TODO check if it is a number
tanasaro10 11:baafa4f7a15e 429 pdata= pdata+1;
tanasaro10 11:baafa4f7a15e 430 }
tanasaro10 11:baafa4f7a15e 431 g_myDateTimeVar.updateDateTime = true;
tanasaro10 11:baafa4f7a15e 432
tanasaro10 9:303d3628986a 433 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 434 break;
tanasaro10 9:303d3628986a 435 }
tanasaro10 11:baafa4f7a15e 436 case 'g': { // time/date get
tanasaro10 11:baafa4f7a15e 437 uint8_t * pdata1 = &g_myDateTimeVar.currentDateTime.year; // to get data
tanasaro10 11:baafa4f7a15e 438 if (buffer[0]=='t') {
tanasaro10 11:baafa4f7a15e 439 pdata1 +=3;
tanasaro10 11:baafa4f7a15e 440 sprintf(myBuf,"H:%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2));
tanasaro10 11:baafa4f7a15e 441 len = 11;
tanasaro10 11:baafa4f7a15e 442 }else {
tanasaro10 11:baafa4f7a15e 443 sprintf(myBuf,"D:20%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2));
tanasaro10 11:baafa4f7a15e 444 len = 13;
tanasaro10 11:baafa4f7a15e 445 }
tanasaro10 9:303d3628986a 446 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 447 break;
tanasaro10 9:303d3628986a 448 }
tanasaro10 9:303d3628986a 449 default:
tanasaro10 9:303d3628986a 450 MyASSERT(true,&pc, buffer[1]); // notify on serial interface
tanasaro10 9:303d3628986a 451 on_error_radioMsg(); // notify on radio
tanasaro10 9:303d3628986a 452 }
tanasaro10 9:303d3628986a 453 break;
tanasaro10 9:303d3628986a 454 }
tanasaro10 9:303d3628986a 455
tanasaro10 9:303d3628986a 456 case 'f': {// file operations
tanasaro10 9:303d3628986a 457 switch (buffer[1]) {
tanasaro10 11:baafa4f7a15e 458 case '1': {
tanasaro10 9:303d3628986a 459 sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage());
tanasaro10 9:303d3628986a 460 len = 18;
tanasaro10 9:303d3628986a 461 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 462 break;
tanasaro10 9:303d3628986a 463 }
tanasaro10 10:c7d53e4e0602 464 case '2': { // start measuring
tanasaro10 9:303d3628986a 465 sprintf(myBuf,"Start Meas");
tanasaro10 9:303d3628986a 466 len = 12;
tanasaro10 9:303d3628986a 467 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 468 g_LogActive = true;
tanasaro10 12:7772974713ac 469
tanasaro10 9:303d3628986a 470 break;
tanasaro10 9:303d3628986a 471 }
tanasaro10 10:c7d53e4e0602 472 case '3': { // stop measuring
tanasaro10 12:7772974713ac 473 //measureSampling.detach();
tanasaro10 9:303d3628986a 474 sprintf(myBuf,"Stop Meas");
tanasaro10 9:303d3628986a 475 len = 11;
tanasaro10 9:303d3628986a 476 sendRadioMsg((uint8_t *)myBuf, len);
tanasaro10 9:303d3628986a 477 g_LogActive = false;
tanasaro10 12:7772974713ac 478 ble_flash_page_write(flash_currPage(), (uint32_t*)&(g_MyData), 251u);
tanasaro10 12:7772974713ac 479 memset(&g_MyData,0,sizeof(myDataLog_t));
tanasaro10 12:7772974713ac 480 flash_go_nextPage();
tanasaro10 12:7772974713ac 481 break;
tanasaro10 12:7772974713ac 482 }
tanasaro10 12:7772974713ac 483 case '4':{
tanasaro10 12:7772974713ac 484 break;
tanasaro10 12:7772974713ac 485 }
tanasaro10 12:7772974713ac 486 case '5':{ // read one measure
tanasaro10 12:7772974713ac 487
tanasaro10 9:303d3628986a 488 break;
tanasaro10 9:303d3628986a 489 }
tanasaro10 9:303d3628986a 490 default: {
tanasaro10 9:303d3628986a 491 // error
tanasaro10 9:303d3628986a 492 }
tanasaro10 9:303d3628986a 493 }
tanasaro10 9:303d3628986a 494 break;
tanasaro10 9:303d3628986a 495 }
tanasaro10 9:303d3628986a 496 default: {
tanasaro10 9:303d3628986a 497 MyASSERT(true,&pc, buffer[1]); // notify on serial interface
tanasaro10 9:303d3628986a 498 on_error_radioMsg(); // notify on radio;
tanasaro10 9:303d3628986a 499 }
tanasaro10 8:f28ad4600b0f 500 }
tanasaro10 8:f28ad4600b0f 501 }
tanasaro10 8:f28ad4600b0f 502
tanasaro10 9:303d3628986a 503 // decode serial command that starts with x
tanasaro10 9:303d3628986a 504 static void decode_s(uint8_t * buffer, uint16_t length)
tanasaro10 9:303d3628986a 505 {
tanasaro10 9:303d3628986a 506 uint8_t page_nr;
tanasaro10 9:303d3628986a 507 char myBuf[5];
tanasaro10 9:303d3628986a 508 uint32_t * p_curr_addr;
tanasaro10 10:c7d53e4e0602 509
tanasaro10 9:303d3628986a 510 switch (buffer[0]) {
tanasaro10 9:303d3628986a 511 case 'f': { // info about selected flash page
tanasaro10 9:303d3628986a 512 if ((buffer[1]<='9')&&(buffer[1]>='0')) {
tanasaro10 9:303d3628986a 513 memcpy(myBuf,&buffer[1],3);
tanasaro10 10:c7d53e4e0602 514 page_nr= atoi(myBuf);
tanasaro10 10:c7d53e4e0602 515 uint8_t p_word_count;
tanasaro10 9:303d3628986a 516
tanasaro10 9:303d3628986a 517 pc.printf("buffer[1]: %c \r\n",buffer[1]);
tanasaro10 9:303d3628986a 518
tanasaro10 9:303d3628986a 519 p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (flash_currPage() - page_nr));
tanasaro10 9:303d3628986a 520 pc.printf("page_addr: %x, pgNr = %d \r\n",p_curr_addr,(flash_currPage() - page_nr));
tanasaro10 9:303d3628986a 521 p_curr_addr += 1;
tanasaro10 9:303d3628986a 522 pc.printf("page_addr: %x \r\n",p_curr_addr);
tanasaro10 9:303d3628986a 523 p_word_count = (uint8_t)(*(p_curr_addr));
tanasaro10 10:c7d53e4e0602 524 pc.printf("nr_of_words: %d \r\n",p_word_count);
tanasaro10 9:303d3628986a 525 flash_page_serial_dump((p_curr_addr-1));
tanasaro10 9:303d3628986a 526 }
tanasaro10 9:303d3628986a 527 break;
tanasaro10 9:303d3628986a 528 }
tanasaro10 9:303d3628986a 529 case 'd': { // full dump
tanasaro10 10:c7d53e4e0602 530 uint16_t page0;
tanasaro10 10:c7d53e4e0602 531 pc.printf("Full dump \r\n");
tanasaro10 10:c7d53e4e0602 532
tanasaro10 9:303d3628986a 533 page0 = flash_currPage();
tanasaro10 11:baafa4f7a15e 534 for (page_nr=0; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM); page_nr++) {
tanasaro10 10:c7d53e4e0602 535 if ((page0-page_nr)< MIN_PAGE_NUM) {
tanasaro10 9:303d3628986a 536 page0 = MAX_PAGE_NUM + page_nr;
tanasaro10 8:f28ad4600b0f 537 }
tanasaro10 9:303d3628986a 538 p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (page0-page_nr));
tanasaro10 10:c7d53e4e0602 539 flash_page_serial_dump(p_curr_addr);
tanasaro10 9:303d3628986a 540 }
tanasaro10 9:303d3628986a 541 break;
tanasaro10 9:303d3628986a 542 }
tanasaro10 10:c7d53e4e0602 543 case 'g': {
tanasaro10 10:c7d53e4e0602 544 pc.printf("g_MyDataIdx= %d\r", g_MyDataIdx);
tanasaro10 9:303d3628986a 545 break;
tanasaro10 9:303d3628986a 546 }
tanasaro10 9:303d3628986a 547 case 'c': {
tanasaro10 10:c7d53e4e0602 548 switch (buffer[1]) {
tanasaro10 9:303d3628986a 549 case 'a': {
tanasaro10 9:303d3628986a 550 connectionUpdate(eStartAdvertising);
tanasaro10 9:303d3628986a 551 break;
tanasaro10 9:303d3628986a 552 }
tanasaro10 9:303d3628986a 553 case 'c' : {
tanasaro10 9:303d3628986a 554 connectionUpdate(eDisconnect);
tanasaro10 9:303d3628986a 555 break;
tanasaro10 8:f28ad4600b0f 556 }
tanasaro10 9:303d3628986a 557 case 's' : {
tanasaro10 9:303d3628986a 558 connectionUpdate(eStopAdvertising);
tanasaro10 9:303d3628986a 559 break;
tanasaro10 8:f28ad4600b0f 560 }
tanasaro10 10:c7d53e4e0602 561 default:
tanasaro10 10:c7d53e4e0602 562 pc.printf("Not recognized cmd !\r");
tanasaro10 10:c7d53e4e0602 563 }
tanasaro10 9:303d3628986a 564 break;
tanasaro10 9:303d3628986a 565 }
tanasaro10 9:303d3628986a 566 default: {
tanasaro10 9:303d3628986a 567 // nothing
tanasaro10 8:f28ad4600b0f 568 }
tanasaro10 9:303d3628986a 569 }
tanasaro10 8:f28ad4600b0f 570 }
tanasaro10 9:303d3628986a 571
RedBearLab 2:4b66b69c7ecb 572 void WrittenHandler(const GattWriteCallbackParams *Handler)
tanasaro10 9:303d3628986a 573 {
tanasaro10 9:303d3628986a 574 uint8_t buf[TXRX_BUF_LEN+1]= {'R',':',0};
tanasaro10 9:303d3628986a 575 uint16_t bytesRead;
tanasaro10 9:303d3628986a 576
tanasaro10 9:303d3628986a 577 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) {
tanasaro10 6:a574229993b8 578 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), &buf[2], &bytesRead);
RedBearLab 0:cffe8ac1bdf0 579 memset(txPayload, 0, TXRX_BUF_LEN);
tanasaro10 9:303d3628986a 580 memcpy(txPayload, &buf[2], bytesRead);
tanasaro10 9:303d3628986a 581 if (txPayload[0] == 'x') {
tanasaro10 9:303d3628986a 582 decode(&txPayload[1],bytesRead);
tanasaro10 9:303d3628986a 583 }
tanasaro10 9:303d3628986a 584 //echo back
tanasaro10 9:303d3628986a 585 bytesRead+=2;
tanasaro10 9:303d3628986a 586 sendRadioMsg(buf, bytesRead);
tanasaro10 9:303d3628986a 587
tanasaro10 6:a574229993b8 588 // print on PC monitor
tanasaro10 11:baafa4f7a15e 589 buf[bytesRead]='\r';
tanasaro10 9:303d3628986a 590 pc.printf("%s",buf);
RedBearLab 0:cffe8ac1bdf0 591 }
RedBearLab 0:cffe8ac1bdf0 592 }
RedBearLab 0:cffe8ac1bdf0 593
RedBearLab 0:cffe8ac1bdf0 594 void uartCB(void)
tanasaro10 9:303d3628986a 595 {
tanasaro10 9:303d3628986a 596 while(pc.readable()) {
tanasaro10 9:303d3628986a 597 rx_buf[rx_len++] = pc.getc();
tanasaro10 9:303d3628986a 598 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') {
tanasaro10 9:303d3628986a 599 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
tanasaro10 9:303d3628986a 600 if ((rx_buf[0]=='x')) {
tanasaro10 10:c7d53e4e0602 601 decode_s(&rx_buf[1],(rx_len-1)); // serial decode
tanasaro10 9:303d3628986a 602 }
tanasaro10 9:303d3628986a 603 rx_len= 0;
RedBearLab 0:cffe8ac1bdf0 604 break;
RedBearLab 0:cffe8ac1bdf0 605 }
RedBearLab 0:cffe8ac1bdf0 606 }
RedBearLab 0:cffe8ac1bdf0 607 }
RedBearLab 0:cffe8ac1bdf0 608
tanasaro10 9:303d3628986a 609 void button()
tanasaro10 9:303d3628986a 610 {
tanasaro10 8:f28ad4600b0f 611 uint8_t buf[TXRX_BUF_LEN+1];
tanasaro10 8:f28ad4600b0f 612 buf[0]='B';
tanasaro10 10:c7d53e4e0602 613 buf[1]='U';
tanasaro10 10:c7d53e4e0602 614 buf[2]='T';
tanasaro10 8:f28ad4600b0f 615 buf[3]='N';
tanasaro10 10:c7d53e4e0602 616
tanasaro10 9:303d3628986a 617 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4);
tanasaro10 10:c7d53e4e0602 618 g_bConnDisabled = !g_bConnDisabled;
tanasaro10 10:c7d53e4e0602 619 led = !led;
tanasaro10 10:c7d53e4e0602 620 if (g_bConnDisabled == true){
tanasaro10 11:baafa4f7a15e 621 if (g_bIsConnected == true){
tanasaro10 11:baafa4f7a15e 622 ble.disconnect((Gap::DisconnectionReason_t)0x12);
tanasaro10 11:baafa4f7a15e 623 g_bIsConnected = false;
tanasaro10 11:baafa4f7a15e 624 } else if (g_bIsAdvertising == true) {
tanasaro10 11:baafa4f7a15e 625 ble.stopAdvertising();
tanasaro10 11:baafa4f7a15e 626 g_bIsAdvertising = false;
tanasaro10 11:baafa4f7a15e 627 }
tanasaro10 10:c7d53e4e0602 628 } else {
tanasaro10 10:c7d53e4e0602 629 connectionUpdate(eStartAdvertising);
tanasaro10 10:c7d53e4e0602 630 }
tanasaro10 8:f28ad4600b0f 631 }
tanasaro10 8:f28ad4600b0f 632
tanasaro10 9:303d3628986a 633 void g_varInit()
tanasaro10 9:303d3628986a 634 {
tanasaro10 11:baafa4f7a15e 635 g_myDateTimeVar.updateDateTime = true;
tanasaro10 9:303d3628986a 636 /* retreive latest date, time and page flash available */
tanasaro10 11:baafa4f7a15e 637 search_latest_in_flash(&g_myDateTimeVar.newDateTime);
tanasaro10 8:f28ad4600b0f 638 }
tanasaro10 8:f28ad4600b0f 639
RedBearLab 0:cffe8ac1bdf0 640 int main(void)
RedBearLab 0:cffe8ac1bdf0 641 {
RedBearLab 0:cffe8ac1bdf0 642 ble.init();
tanasaro10 9:303d3628986a 643 g_varInit();
RedBearLab 0:cffe8ac1bdf0 644 ble.onDisconnection(disconnectionCallback);
tanasaro10 9:303d3628986a 645 ble.onConnection(connectionCallback);
tanasaro10 9:303d3628986a 646 ble.onDataWritten(WrittenHandler);
tanasaro10 12:7772974713ac 647 //event.rise(&button);
tanasaro10 12:7772974713ac 648
tanasaro10 12:7772974713ac 649
tanasaro10 12:7772974713ac 650 //event.rise(&accInt1);
tanasaro10 6:a574229993b8 651 pc.baud(19200);
RedBearLab 0:cffe8ac1bdf0 652 pc.printf("SimpleChat Init \r\n");
tanasaro10 12:7772974713ac 653
tanasaro10 12:7772974713ac 654 //mma1 = Adafruit_MMA8451();
tanasaro10 12:7772974713ac 655 //mma2 = Adafruit_MMA8451();
tanasaro10 12:7772974713ac 656 //init_Acc();
tanasaro10 12:7772974713ac 657
RedBearLab 0:cffe8ac1bdf0 658 pc.attach( uartCB , pc.RxIrq);
tanasaro10 9:303d3628986a 659 // setup advertising
RedBearLab 0:cffe8ac1bdf0 660 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 661 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 662 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
tanasaro10 12:7772974713ac 663 (const uint8_t *)"BleTp", sizeof("BleTp") - 1);
RedBearLab 0:cffe8ac1bdf0 664 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
tanasaro10 9:303d3628986a 665 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
tanasaro10 9:303d3628986a 666 //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower));
tanasaro10 9:303d3628986a 667 ble.setTxPower(txPower);
tanasaro10 9:303d3628986a 668 // 100ms; in multiples of 0.625ms.
tanasaro10 12:7772974713ac 669 ble.setAdvertisingInterval(320);
tanasaro10 9:303d3628986a 670 /*
tanasaro10 9:303d3628986a 671 // activate radio notifications - usefull for flashwrite
tanasaro10 9:303d3628986a 672 void (*ptrFunc)(bool);
tanasaro10 9:303d3628986a 673 ptrFunc = ble_flash_on_radio_active_evt;
tanasaro10 9:303d3628986a 674 //needed for flash write
tanasaro10 9:303d3628986a 675 //ble.onRadioNotification(ptrFunc);
tanasaro10 10:c7d53e4e0602 676 */
RedBearLab 0:cffe8ac1bdf0 677 ble.addService(uartService);
tanasaro10 12:7772974713ac 678 batteryService = new BatteryService(ble, batteryLevel);
tanasaro10 9:303d3628986a 679 ble.startAdvertising();
tanasaro10 12:7772974713ac 680 pc.printf("Advertising Start \r\n");
tanasaro10 12:7772974713ac 681 periodicActions.attach(&at_eachInstant,gTimeInstant);
tanasaro10 12:7772974713ac 682 gBatteryValue = read100();
tanasaro10 12:7772974713ac 683
tanasaro10 9:303d3628986a 684 while(1) {
tanasaro10 9:303d3628986a 685 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 686 }
RedBearLab 0:cffe8ac1bdf0 687 }