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
Diff: main.cpp
- Revision:
- 11:baafa4f7a15e
- Parent:
- 10:c7d53e4e0602
- Child:
- 12:7772974713ac
--- a/main.cpp Tue Apr 26 20:15:41 2016 +0000 +++ b/main.cpp Fri May 06 18:14:18 2016 +0000 @@ -27,9 +27,10 @@ #include "ble/BLE.h" +//#include "LowPowerTicker.h" #include <myData.h> #include <Gap.h> -#include "ble_flash.h" +//#include "ble_flash.h" #include "ble_flash.c" #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ @@ -52,7 +53,7 @@ static const int8_t txPower = 0xCD; -uint8_t txPayload[TXRX_BUF_LEN] = {0,}; +uint8_t txPayload[TXRX_BUF_LEN] = {0, p28}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; static uint8_t rx_buf[TXRX_BUF_LEN]; @@ -81,8 +82,7 @@ Timeout timeout_err; // timeout for buzz on error Ticker periodicActions; -mtime_manager_t g_myTimeVar; -mdate_manager_t g_myDateVar; +mdatetime_manager_t g_myDateTimeVar; GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); @@ -167,13 +167,12 @@ //initiate connection connectionUpdate(eStartAdvertising); // time and date used to initialize the g_MyData variable - memcpy(&g_MyData.startData.date,&g_myDateVar.currentDate, sizeof(date_t)); - memcpy(&g_MyData.startData.time,&g_myTimeVar.currentTime, sizeof(mtime_t)); + memcpy(&g_MyData.startData.datetime,&g_myDateTimeVar.currentDateTime, sizeof(mdate_time_t)); memcpy(&g_MyData.startData.data,&g_currMeasures, sizeof(myPayload_t)); } else { // it should be logged here the time difference from last record... - g_MyData.myData[g_MyDataIdx-1].min = (uint16_t)(*tick*gTimeInstant / 60); - g_MyData.myData[g_MyDataIdx-1].sec = (*tick*gTimeInstant% 60); + g_MyData.myData[g_MyDataIdx-1].minutes = (uint16_t)(*tick*gTimeInstant / 60); + g_MyData.myData[g_MyDataIdx-1].seconds = (*tick*gTimeInstant% 60); memcpy(&g_MyData.myData[g_MyDataIdx-1].data,&g_currMeasures, sizeof(myPayload_t)); } *tick = 0; @@ -188,9 +187,8 @@ page_num=flash_currPage(); // write2Flash the current page data retVal=ble_flash_page_write(page_num, (uint32_t*)&(g_MyData), 251u); - pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myTimeVar.currentTime.min); + pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myDateTimeVar.currentDateTime.minutes); flash_go_nextPage(); - //save_flash_curr_pageNr(g_myDateVar.currentDate); } g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER+1); } @@ -206,24 +204,25 @@ sendRadioMsg((uint8_t*)&myBuf[0], 12); } - void flash_page_serial_dump(uint32_t* p_curr_addr) { myDataLogShort_t initialData; + mdate_time_t * pdate; myDataL_t dataOut[2]; uint8_t i; p_curr_addr += 2; // skip the magic number and the word count memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); - pc.printf("20%2d_%2d_%2d H:%2d P:%4x\r",initialData.startData.date.year, initialData.startData.date.month, initialData.startData.date.day, initialData.startData.time.hour, p_curr_addr); - pc.printf("%2d:%2d;%3d;%3d;%3d \r",initialData.startData.time.min, initialData.startData.time.sec, initialData.startData.data.light, initialData.startData.data.gndV, initialData.startData.data.temp); - pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",initialData.myData.min, initialData.myData.sec, initialData.myData.data.light, initialData.myData.data.gndV, initialData.myData.data.temp); + pdate = &initialData.startData.datetime; + pc.printf("20%2d_%2d_%2d H:%2d P:%4x\r",pdate->year, pdate->month, pdate->day, pdate->hours, p_curr_addr); + pc.printf("%2d:%2d;%3d;%3d;%3d \r",pdate->minutes, pdate->seconds, initialData.startData.data.light, initialData.startData.data.gndV, initialData.startData.data.temp); + pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",initialData.myData.minutes, initialData.myData.seconds, initialData.myData.data.light, initialData.myData.data.gndV, initialData.myData.data.temp); p_curr_addr += 6; for (i=0; i<49; i++) { memcpy((uint32_t*)&dataOut, p_curr_addr, 5*sizeof(uint32_t)); - pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",dataOut[0].min, dataOut[0].sec, dataOut[0].data.light, dataOut[0].data.gndV, dataOut[0].data.temp, i); - pc.printf("%2d:%2d;%3d;%3d;%3d\r",dataOut[1].min, dataOut[1].sec, dataOut[1].data.light, dataOut[1].data.gndV, dataOut[1].data.temp); + pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",dataOut[0].minutes, dataOut[0].seconds, dataOut[0].data.light, dataOut[0].data.gndV, dataOut[0].data.temp, i); + pc.printf("%2d:%2d;%3d;%3d;%3d\r",dataOut[1].minutes, dataOut[1].seconds, dataOut[1].data.light, dataOut[1].data.gndV, dataOut[1].data.temp); p_curr_addr += 5; } } @@ -249,7 +248,7 @@ int retVal; // update time - update_time(&g_myTimeVar, &g_myDateVar, gTimeInstant); + update_time(&g_myDateTimeVar, gTimeInstant); //update measurements retVal = update_measurements(); @@ -330,51 +329,39 @@ sendRadioMsg((uint8_t *)myBuf, len); break; } - case 't': {// time operations + case 'd': + case 't': {// date /time operations switch (buffer[1]) { - case 'i': {// time insert - memcpy(myBuf,&buffer[2],2); - g_myTimeVar.newTime.hour=atoi(myBuf); // TODO check if it is a number - memcpy(myBuf,&buffer[4],2); - g_myTimeVar.newTime.min=atoi(myBuf); // TODO check if it is a number - memcpy(myBuf,&buffer[6],2); - g_myTimeVar.newTime.sec=atoi(myBuf); // TODO check if it is a number - g_myTimeVar.updateTime = true; - sprintf(myBuf,"TimeInserted"); - len= 12; - sendRadioMsg((uint8_t *)myBuf, len); - break; - } - case 'g': {// time get - sprintf(myBuf,"H:%2d:%2d:%2d",g_myTimeVar.currentTime.hour,g_myTimeVar.currentTime.min,g_myTimeVar.currentTime.sec); - len = 11; + case 'i': { // date insert + uint8_t i; + uint8_t * pdata = &g_myDateTimeVar.newDateTime.year; // to insert data + + if (buffer[0]=='t') { + sprintf(myBuf," TimeInserted"); + pdata +=3; + } else {sprintf(myBuf," DateInserted");} + len= 14; + + for (i=0;i<3;i++){ + memcpy(myBuf,&buffer[2+2*i],2); + *pdata=atoi(myBuf); // TODO check if it is a number + pdata= pdata+1; + } + g_myDateTimeVar.updateDateTime = true; + sendRadioMsg((uint8_t *)myBuf, len); break; } - default: - MyASSERT(true,&pc, buffer[1]); // notify on serial interface - on_error_radioMsg(); // notify on radio - } - break; - } - case 'd': {// date operations - switch (buffer[1]) { - case 'i': { // date insert - memcpy(myBuf,&buffer[2],2); - g_myDateVar.newDate.year=atoi(myBuf); // TODO check if it is a number - memcpy(myBuf,&buffer[4],2); - g_myDateVar.newDate.month=atoi(myBuf); // TODO check if it is a number - memcpy(myBuf,&buffer[6],2); - g_myDateVar.newDate.day=atoi(myBuf); // TODO check if it is a number - g_myDateVar.updateDate = true; - sprintf(myBuf,"DateInserted"); - len= 12; - sendRadioMsg((uint8_t *)myBuf, len); - break; - } - case 'g': { // time get - sprintf(myBuf,"D:20%2d:%2d:%2d",g_myDateVar.currentDate.year,g_myDateVar.currentDate.month,g_myDateVar.currentDate.day); - len = 13; + case 'g': { // time/date get + uint8_t * pdata1 = &g_myDateTimeVar.currentDateTime.year; // to get data + if (buffer[0]=='t') { + pdata1 +=3; + sprintf(myBuf,"H:%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2)); + len = 11; + }else { + sprintf(myBuf,"D:20%2d:%2d:%2d",*pdata1,*(pdata1+1),*(pdata1+2)); + len = 13; + } sendRadioMsg((uint8_t *)myBuf, len); break; } @@ -387,10 +374,7 @@ case 'f': {// file operations switch (buffer[1]) { - case '1': { - //pc.printf("S Payload_t: %d \r\n",sizeof(myPayload_t)); - //pc.printf("S myDataL_t: %d \r\n",sizeof(myDataL_t)); - + case '1': { sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage()); len = 18; sendRadioMsg((uint8_t *)myBuf, len); @@ -410,7 +394,6 @@ g_LogActive = false; break; } - default: { // error } @@ -455,7 +438,7 @@ pc.printf("Full dump \r\n"); page0 = flash_currPage(); - for (page_nr=1; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM+1); page_nr++) { + for (page_nr=0; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM); page_nr++) { if ((page0-page_nr)< MIN_PAGE_NUM) { page0 = MAX_PAGE_NUM + page_nr; } @@ -510,8 +493,7 @@ sendRadioMsg(buf, bytesRead); // print on PC monitor - buf[bytesRead]='\r'; - //buf[bytesRead+1]='\n'; + buf[bytesRead]='\r'; pc.printf("%s",buf); } } @@ -543,8 +525,13 @@ g_bConnDisabled = !g_bConnDisabled; led = !led; if (g_bConnDisabled == true){ - ble.disconnect((Gap::DisconnectionReason_t)0x12); - g_bIsConnected = false; + if (g_bIsConnected == true){ + ble.disconnect((Gap::DisconnectionReason_t)0x12); + g_bIsConnected = false; + } else if (g_bIsAdvertising == true) { + ble.stopAdvertising(); + g_bIsAdvertising = false; + } } else { connectionUpdate(eStartAdvertising); } @@ -552,10 +539,9 @@ void g_varInit() { - g_myDateVar.updateDate = false; - g_myTimeVar.updateTime = false; + g_myDateTimeVar.updateDateTime = true; /* retreive latest date, time and page flash available */ - search_latest_in_flash(&g_myDateVar.currentDate, &g_myTimeVar.currentTime ); + search_latest_in_flash(&g_myDateTimeVar.newDateTime); } int main(void)