Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_TemperatureEx by
main.cpp
00001 /* 00002 00003 Copyright (c) 2012-2014 RedBearLab 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 subject to the following conditions: 00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 00018 */ 00019 00020 /* 00021 * The application works with the BLEController iOS/Android App. 00022 * Type something from the Terminal to send 00023 * to the BLEController App or vice verse. 00024 * Read read_me.md file for more informations about the extended feature 00025 */ 00026 00027 00028 #include "ble/BLE.h" 00029 #include <myData.h> 00030 #include <Gap.h> 00031 #include "ble_flash.c" 00032 #include "BatteryService.h" 00033 00034 BLE ble; 00035 00036 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00037 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00038 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00039 00040 #define TXRX_BUF_LEN 20 /** For radio message transmission*/ 00041 00042 #define MyASSERT(cond , serialpc, errVal) assert_error_app((bool)cond, serialpc, (uint16_t)errVal, __LINE__, __FILE__) 00043 00044 Serial pc(USBTX, USBRX); 00045 00046 uint8_t batteryLevel=100; 00047 00048 // The Nordic UART Service 00049 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00050 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00051 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00052 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00053 00054 static const int8_t txPower = 0x1E; 00055 BatteryService *batteryService = NULL; 00056 00057 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00058 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00059 char myBuf[TXRX_BUF_LEN]; 00060 uint16_t len; 00061 00062 static uint8_t rx_buf[TXRX_BUF_LEN]; 00063 static uint8_t rx_len=0; 00064 00065 static uint32_t gTimeInstant = 1; // TimerTick Resolution, in seconds 00066 00067 00068 bool g_bIsConnected = false; 00069 bool g_bIsAdvertising = false; 00070 bool g_bConnDisabled = false; 00071 bool g_LogActive = false; 00072 static myDataLog_t g_MyData; 00073 uint8_t g_MyDataIdx=0; 00074 00075 00076 00077 // pins connected for measuring 00078 00079 DigitalOut led(LED1); 00080 // voltage for the termic resistor 00081 AnalogIn Vin(A5); 00082 00083 myPayload_t g_currMeasures2; // last measurements 00084 00085 Timeout timeout_err; // timeout for buzz on error 00086 Ticker periodicActions; 00087 bool bNewSample = false; 00088 mdatetime_manager_t g_myDateTimeVar; 00089 uint8_t gBatteryValue=0; 00090 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00091 00092 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00093 00094 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00095 00096 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00097 00098 void at_timeout_err() 00099 { 00100 // stop buzz 00101 00102 } 00103 00104 void alarm(){ 00105 00106 //timeout_err.attach(&at_timeout_err, 2); 00107 } 00108 00109 void sendRadioMsg(const uint8_t* buf, uint16_t length) 00110 { 00111 uint8_t retVal; 00112 retVal = ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, length); 00113 //pc.printf("Err=%d\r\n",retVal); 00114 MyASSERT((retVal!=0),&pc, retVal); 00115 } 00116 00117 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00118 { 00119 pc.printf("Disconnected \r\n"); 00120 g_bIsConnected = false; 00121 g_bIsAdvertising = false; 00122 pc.printf("R: %d\r",reason); 00123 if (reason != 0x16) { 00124 ble.startAdvertising(); 00125 g_bIsAdvertising = true; 00126 } 00127 } 00128 00129 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00130 { 00131 pc.printf("Connected \r\n"); 00132 g_bIsConnected = true; 00133 g_bIsAdvertising = false; 00134 } 00135 00136 void connectionUpdate(connection_update_t option) 00137 { 00138 if (g_bConnDisabled == false) { 00139 switch (option) { 00140 case eStartAdvertising: { 00141 if ((g_bIsConnected == false)&&(g_bIsAdvertising == false)) { 00142 pc.printf("Start Advertising\r"); 00143 ble.startAdvertising(); 00144 g_bIsAdvertising = true; 00145 } 00146 break; 00147 } 00148 case eStopAdvertising: { 00149 if (g_bIsAdvertising == true) { 00150 pc.printf("Stop Advertising\r"); 00151 ble.stopAdvertising(); 00152 g_bIsAdvertising = false; 00153 } 00154 break; 00155 } 00156 case eDisconnect: { 00157 if (g_bIsConnected == true) { 00158 pc.printf("Close connection\r"); 00159 ble.disconnect((Gap::DisconnectionReason_t)0x12); 00160 } else if (g_bIsAdvertising == true) { 00161 pc.printf("Stop Advertising\r"); 00162 ble.stopAdvertising(); 00163 g_bIsAdvertising = false; 00164 } 00165 break; 00166 } 00167 } 00168 } 00169 } 00170 void write_data_to_flash(uint32_t *tick, myPayload_t * currMeasures) 00171 { 00172 uint8_t page_num=0; 00173 00174 if (g_MyDataIdx==0) { 00175 //initiate connection 00176 connectionUpdate(eStartAdvertising); 00177 // time and date used to initialize the g_MyData variable 00178 memcpy(&g_MyData.startData.datetime,&g_myDateTimeVar.currentDateTime, sizeof(mdate_time_t)); 00179 memcpy(&g_MyData.startData.data,currMeasures, sizeof(myPayload_t)); 00180 } else { 00181 // it should be logged here the time difference from last record... 00182 g_MyData.myData[g_MyDataIdx-1].minutes = (uint16_t)(*tick*gTimeInstant / 60); 00183 g_MyData.myData[g_MyDataIdx-1].seconds = (*tick*gTimeInstant% 60); 00184 memcpy(&g_MyData.myData[g_MyDataIdx-1].data,currMeasures, sizeof(myPayload_t)); 00185 } 00186 *tick = 0; 00187 00188 if (g_MyDataIdx==(MAXBUFFER-3)) { 00189 //initiate disconnection 00190 connectionUpdate(eDisconnect); 00191 } 00192 00193 if (g_MyDataIdx == MAXBUFFER) { 00194 // write2Flash the current page num 00195 //connectionUpdate(eDisconnect); 00196 00197 page_num=flash_currPage(); 00198 // write2Flash the current page data 00199 ble_flash_page_write(page_num, (uint32_t*)&(g_MyData), 251u); 00200 memset(&g_MyData,0,sizeof(myDataLog_t)); 00201 //pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myDateTimeVar.currentDateTime.minutes); 00202 flash_go_nextPage(); 00203 } 00204 g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER+1); 00205 } 00206 00207 00208 void on_error_radioMsg() 00209 { 00210 sprintf(myBuf,"%s","WrongSyntax"); 00211 //buzz_int(&buzzer,5,3); 00212 //timeout_err.attach(&at_timeout_err, 2); 00213 sendRadioMsg((uint8_t*)&myBuf[0], 12); 00214 } 00215 00216 void flash_page_serial_dump(uint32_t* p_curr_addr) 00217 { 00218 myDataLogShort_t initialData; 00219 mdate_time_t * pdate; 00220 myDataL_t dataOut[2]; 00221 uint8_t i; 00222 00223 p_curr_addr += 2; // skip the magic number and the word count 00224 memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); 00225 pdate = &initialData.startData.datetime; 00226 pc.printf("20%2d_%2d_%2d H:%2d P:%4x\r",pdate->year, pdate->month, pdate->day, pdate->hours, p_curr_addr); 00227 pc.printf("%2d:%2d;%3d;%3d;%3d;\r",pdate->minutes, pdate->seconds, initialData.startData.data.x, initialData.startData.data.y, initialData.startData.data.z); 00228 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); 00229 p_curr_addr += 6; 00230 00231 for (i=0; i<49; i++) { 00232 memcpy((uint32_t*)&dataOut, p_curr_addr, 5*sizeof(uint32_t)); 00233 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); 00234 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); 00235 p_curr_addr += 5; 00236 } 00237 } 00238 00239 int sign(int nr){ 00240 int retVal=0; 00241 if (nr>0) retVal=1; 00242 else if (nr<0) retVal=-1; 00243 00244 return retVal; 00245 } 00246 00247 float read_real_value(void){ 00248 uint32_t wrk,reg0,reg1,reg2; 00249 reg0 = NRF_ADC->ENABLE; // save register value 00250 reg1 = NRF_ADC->CONFIG; // save register value 00251 reg2 = NRF_ADC->RESULT; // save register value 00252 NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; 00253 NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | 00254 (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | 00255 (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | 00256 (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | 00257 (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); 00258 NRF_ADC->EVENTS_END = 0; 00259 NRF_ADC->TASKS_START = 1; 00260 while (!NRF_ADC->EVENTS_END) {;} 00261 wrk = NRF_ADC->RESULT; // 10 bit result 00262 NRF_ADC->ENABLE = reg0; // recover register value 00263 NRF_ADC->CONFIG = reg1; // recover register value 00264 NRF_ADC->RESULT = reg2; // recover register value 00265 NRF_ADC->EVENTS_END = 0; 00266 return ((float)wrk / 1024 * 1.2 * 3.0); 00267 } 00268 00269 uint8_t read100(void) 00270 { 00271 float wrk_vdd, v0p= 2.7 ,v100p= 3.3; 00272 wrk_vdd = read_real_value(); 00273 /*if (wrk_vdd <= v0p){ 00274 return 0; 00275 } else if (wrk_vdd >= v100p){ 00276 led = 0; 00277 return 100; 00278 } */ 00279 led = 1; 00280 wrk_vdd = (wrk_vdd - v0p) / (v100p - v0p); 00281 return (uint8_t)(wrk_vdd * 100); 00282 } 00283 00284 void update_measurements() 00285 { 00286 uint32_t resistance; 00287 int32_t tempValue; 00288 00289 // use Vin.read() o read the voltage from the AI5 pin 00290 // fell free to the fill code in this function .... 00291 00292 //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 00293 //is -55 degrees,and a 3v3 input in AI5 means that the termistor is at 125 degrees. 00294 //The number of values that can be inputed from 0 to 3v3 is 180 values. 00295 //That means that each 0.018V increase on the input means that the temperature rised with 1 degree,and every decrease 00296 //of 0.018V means that the temperature went down with 1 degree Celsius. 00297 //Let us now read the voltage from the AI5 pin 00298 resistance=Vin.read(); 00299 //Based on my calculations,if the voltage is under 1.01V,the temperature is negative.With that in mind 00300 //We will separate negative from positive values. 00301 if(resistance<=1.01) 00302 { 00303 if(resistance<0.018) 00304 { 00305 tempValue=-55; 00306 } 00307 tempValue=-55+resistance/0.018; 00308 00309 } 00310 tempValue=0+resistance/0.018; 00311 00312 00313 00314 00315 00316 00317 // print on the bluetooth communication the value of Temperature computed in Celsius degrees 00318 sprintf(myBuf,"T:%3d.%2d;%d;\r", tempValue/100,tempValue%100,resistance); 00319 len = 20; 00320 sendRadioMsg((uint8_t *)myBuf, len); 00321 } 00322 00323 00324 void at_eachInstant() 00325 { 00326 static uint32_t tick=0; 00327 //const uint8_t sizeB= 3*sizeof(uint16_t); 00328 //static myPayload_t prevMeasures2=(myPayload_t) { 0, 0, 0, 0}; 00329 00330 // update time 00331 update_time(&g_myDateTimeVar, gTimeInstant); 00332 //sendRadioMsg("Tick\r", 5); 00333 batteryService->updateBatteryLevel(read100()); 00334 //update measurements 00335 if (g_bIsConnected==true){ 00336 update_measurements(); 00337 } 00338 if (g_LogActive==true){ 00339 // if there are changes in data save 00340 //g_currMeasures2.temp = Temp.read_u16(); 00341 //if (memcmp(&prevMeasures2, &g_currMeasures2, sizeB)!=0) 00342 if (bNewSample == true) 00343 //if (update_measurements()==1) 00344 { 00345 bNewSample = false; 00346 write_data_to_flash(&tick, &g_currMeasures2); 00347 //memcpy(&prevMeasures2,&g_currMeasures2,sizeB); 00348 // 00349 //write_data_to_flash(&tick, &g_currMeasures2); 00350 } 00351 tick++; 00352 } 00353 } 00354 00355 00356 // Radio commands decode 00357 void decode(uint8_t * buffer, uint16_t length) 00358 { 00359 switch (buffer[0]) { 00360 case 'r': {// Read Operations 00361 switch (buffer[1]) { 00362 case '0': { 00363 // display all inputs 00364 sprintf(myBuf,"Id1:%d,%d,%d,%d\r\n", g_currMeasures2.x,g_currMeasures2.y,g_currMeasures2.z,g_currMeasures2.temp); 00365 len = 20; 00366 break; 00367 } 00368 case '1': { 00369 sprintf(myBuf,"Id2:%d,%d,%d\r\n", g_currMeasures2.x,g_currMeasures2.y,g_currMeasures2.z); 00370 len = 20; 00371 break; 00372 } 00373 case '2': { 00374 sprintf(myBuf,"V:%2.3f\r\n", read_real_value()); 00375 len = 11; 00376 break; 00377 } 00378 case '3': { 00379 sprintf(myBuf,"T = %3d\r\n", g_currMeasures2.temp); 00380 len = 10; 00381 break; 00382 } 00383 case '4':{ 00384 // sprintf(myBuf,"F1=%2d\r\n",FILTER_COEF1); 00385 len = 7; 00386 break; 00387 } 00388 case '5':{ 00389 //sprintf(myBuf,"Acc=%2d\r\n",gAccAmp); 00390 len = 8; 00391 break; 00392 } 00393 default: { 00394 sprintf(myBuf,"Nothing \r\n"); 00395 len = 10; 00396 break; 00397 } 00398 } 00399 sendRadioMsg((uint8_t *)myBuf, len); 00400 break; 00401 } 00402 case 'l': {// toogle led 00403 led = ! led; 00404 if (led==0) { 00405 sprintf(myBuf,"%s","ON"); 00406 len = 2; 00407 } else { 00408 sprintf(myBuf,"%s","OFF"); 00409 len = 3; 00410 } 00411 sendRadioMsg((uint8_t *)myBuf, len); 00412 break; 00413 } 00414 case 'i':{ // Insert data values 00415 switch (buffer[1]){ 00416 case 'f':{ 00417 memcpy(myBuf,&buffer[2],2); 00418 //FILTER_COEF1=atoi(myBuf); // TODO check if it is a number 00419 break; 00420 } 00421 case 'a':{ 00422 memcpy(myBuf,&buffer[2],2); 00423 //gAccAmp = atoi(myBuf); 00424 break; 00425 } 00426 00427 default: { 00428 // on_error_radioMsg(); // notify on radio 00429 } 00430 } 00431 break; 00432 } 00433 case 'd': 00434 case 't': {// date /time operations 00435 switch (buffer[1]) { 00436 case 'i': { // date insert 00437 uint8_t i; 00438 uint8_t * pdata = &g_myDateTimeVar.newDateTime.year; // to insert data 00439 00440 if (buffer[0]=='t') { 00441 sprintf(myBuf," TimeInserted"); 00442 pdata +=3; 00443 } else {sprintf(myBuf," DateInserted");} 00444 len= 14; 00445 00446 for (i=0;i<3;i++){ 00447 memcpy(myBuf,&buffer[2+2*i],2); 00448 *pdata=atoi(myBuf); // TODO check if it is a number 00449 pdata= pdata+1; 00450 } 00451 g_myDateTimeVar.updateDateTime = true; 00452 00453 sendRadioMsg((uint8_t *)myBuf, len); 00454 break; 00455 } 00456 case 'g': { // time/date get 00457 uint8_t * pdata1 = &g_myDateTimeVar.currentDateTime.year; // to get data 00458 if (buffer[0]=='t') { 00459 pdata1 +=3; 00460 sprintf(myBuf,"H:%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2)); 00461 len = 11; 00462 }else { 00463 sprintf(myBuf,"D:20%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2)); 00464 len = 13; 00465 } 00466 sendRadioMsg((uint8_t *)myBuf, len); 00467 break; 00468 } 00469 default: 00470 MyASSERT(true,&pc, buffer[1]); // notify on serial interface 00471 on_error_radioMsg(); // notify on radio 00472 } 00473 break; 00474 } 00475 00476 case 'f': {// file operations 00477 switch (buffer[1]) { 00478 case '1': { 00479 sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage()); 00480 len = 18; 00481 sendRadioMsg((uint8_t *)myBuf, len); 00482 break; 00483 } 00484 case '2': { // start measuring 00485 sprintf(myBuf,"Start Meas"); 00486 len = 12; 00487 sendRadioMsg((uint8_t *)myBuf, len); 00488 g_LogActive = true; 00489 00490 break; 00491 } 00492 case '3': { // stop measuring 00493 //measureSampling.detach(); 00494 sprintf(myBuf,"Stop Meas"); 00495 len = 11; 00496 sendRadioMsg((uint8_t *)myBuf, len); 00497 g_LogActive = false; 00498 ble_flash_page_write(flash_currPage(), (uint32_t*)&(g_MyData), 251u); 00499 memset(&g_MyData,0,sizeof(myDataLog_t)); 00500 flash_go_nextPage(); 00501 break; 00502 } 00503 case '4':{ 00504 break; 00505 } 00506 case '5':{ // read one measure 00507 00508 break; 00509 } 00510 default: { 00511 // error 00512 } 00513 } 00514 break; 00515 } 00516 default: { 00517 MyASSERT(true,&pc, buffer[1]); // notify on serial interface 00518 on_error_radioMsg(); // notify on radio; 00519 } 00520 } 00521 } 00522 00523 // decode serial command that starts with x 00524 static void decode_s(uint8_t * buffer, uint16_t length) 00525 { 00526 uint8_t page_nr; 00527 char myBuf[5]; 00528 uint32_t * p_curr_addr; 00529 00530 switch (buffer[0]) { 00531 case 'f': { // info about selected flash page 00532 if ((buffer[1]<='9')&&(buffer[1]>='0')) { 00533 memcpy(myBuf,&buffer[1],3); 00534 page_nr= atoi(myBuf); 00535 uint8_t p_word_count; 00536 00537 pc.printf("buffer[1]: %c \r\n",buffer[1]); 00538 00539 p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (flash_currPage() - page_nr)); 00540 pc.printf("page_addr: %x, pgNr = %d \r\n",p_curr_addr,(flash_currPage() - page_nr)); 00541 p_curr_addr += 1; 00542 pc.printf("page_addr: %x \r\n",p_curr_addr); 00543 p_word_count = (uint8_t)(*(p_curr_addr)); 00544 pc.printf("nr_of_words: %d \r\n",p_word_count); 00545 flash_page_serial_dump((p_curr_addr-1)); 00546 } 00547 break; 00548 } 00549 case 'd': { // full dump 00550 uint16_t page0; 00551 pc.printf("Full dump \r\n"); 00552 00553 page0 = flash_currPage(); 00554 for (page_nr=0; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM); page_nr++) { 00555 if ((page0-page_nr)< MIN_PAGE_NUM) { 00556 page0 = MAX_PAGE_NUM + page_nr; 00557 } 00558 p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (page0-page_nr)); 00559 flash_page_serial_dump(p_curr_addr); 00560 } 00561 break; 00562 } 00563 case 'g': { 00564 pc.printf("g_MyDataIdx= %d\r", g_MyDataIdx); 00565 break; 00566 } 00567 case 'c': { 00568 switch (buffer[1]) { 00569 case 'a': { 00570 connectionUpdate(eStartAdvertising); 00571 break; 00572 } 00573 case 'c' : { 00574 connectionUpdate(eDisconnect); 00575 break; 00576 } 00577 case 's' : { 00578 connectionUpdate(eStopAdvertising); 00579 break; 00580 } 00581 default: 00582 pc.printf("Not recognized cmd !\r"); 00583 } 00584 break; 00585 } 00586 default: { 00587 // nothing 00588 } 00589 } 00590 } 00591 00592 void WrittenHandler(const GattWriteCallbackParams *Handler) 00593 { 00594 uint8_t buf[TXRX_BUF_LEN+1]= {'R',':',0}; 00595 uint16_t bytesRead; 00596 00597 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) { 00598 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), &buf[2], &bytesRead); 00599 memset(txPayload, 0, TXRX_BUF_LEN); 00600 memcpy(txPayload, &buf[2], bytesRead); 00601 if (txPayload[0] == 'x') { 00602 decode(&txPayload[1],bytesRead); 00603 } 00604 //echo back 00605 bytesRead+=2; 00606 sendRadioMsg(buf, bytesRead); 00607 00608 // print on PC monitor 00609 buf[bytesRead]='\r'; 00610 pc.printf("%s",buf); 00611 } 00612 } 00613 00614 void uartCB(void) 00615 { 00616 while(pc.readable()) { 00617 rx_buf[rx_len++] = pc.getc(); 00618 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') { 00619 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 00620 if ((rx_buf[0]=='x')) { 00621 decode_s(&rx_buf[1],(rx_len-1)); // serial decode 00622 } 00623 rx_len= 0; 00624 break; 00625 } 00626 } 00627 } 00628 00629 void button() 00630 { 00631 uint8_t buf[TXRX_BUF_LEN+1]; 00632 buf[0]='B'; 00633 buf[1]='U'; 00634 buf[2]='T'; 00635 buf[3]='N'; 00636 00637 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); 00638 g_bConnDisabled = !g_bConnDisabled; 00639 led = !led; 00640 if (g_bConnDisabled == true){ 00641 if (g_bIsConnected == true){ 00642 ble.disconnect((Gap::DisconnectionReason_t)0x12); 00643 g_bIsConnected = false; 00644 } else if (g_bIsAdvertising == true) { 00645 ble.stopAdvertising(); 00646 g_bIsAdvertising = false; 00647 } 00648 } else { 00649 connectionUpdate(eStartAdvertising); 00650 } 00651 } 00652 00653 void g_varInit() 00654 { 00655 g_myDateTimeVar.updateDateTime = true; 00656 /* retreive latest date, time and page flash available */ 00657 search_latest_in_flash(&g_myDateTimeVar.newDateTime); 00658 } 00659 00660 int main(void) 00661 { 00662 ble.init(); 00663 g_varInit(); 00664 ble.onDisconnection(disconnectionCallback); 00665 ble.onConnection(connectionCallback); 00666 ble.onDataWritten(WrittenHandler); 00667 //event.rise(&button); 00668 00669 00670 //event.rise(&accInt1); 00671 pc.baud(19200); 00672 pc.printf("SimpleChat Init \r\n"); 00673 00674 //mma1 = Adafruit_MMA8451(); 00675 //mma2 = Adafruit_MMA8451(); 00676 //init_Acc(); 00677 00678 pc.attach( uartCB , pc.RxIrq); 00679 // setup advertising 00680 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00681 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00682 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00683 (const uint8_t *)"BleTp", sizeof("BleTp") - 1); 00684 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00685 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00686 //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower)); 00687 ble.setTxPower(txPower); 00688 // 100ms; in multiples of 0.625ms. 00689 ble.setAdvertisingInterval(320); 00690 /* 00691 // activate radio notifications - usefull for flashwrite 00692 void (*ptrFunc)(bool); 00693 ptrFunc = ble_flash_on_radio_active_evt; 00694 //needed for flash write 00695 //ble.onRadioNotification(ptrFunc); 00696 */ 00697 ble.addService(uartService); 00698 batteryService = new BatteryService(ble, batteryLevel); 00699 ble.startAdvertising(); 00700 pc.printf("Advertising Start \r\n"); 00701 periodicActions.attach(&at_eachInstant,gTimeInstant); 00702 gBatteryValue = read100(); 00703 00704 while(1) { 00705 ble.waitForEvent(); 00706 } 00707 }
Generated on Wed Jul 27 2022 00:41:33 by
1.7.2
