This program is designed to work with RedBearLab BLE Controller App - Chat App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal. This Program scans some analog and digital inputs and save them in flash with timestamp. A RTC has been implemented. Using chat application (available for smart phones) some commands can be send to BLE Nano in order to perform some tasks. Other types of commands can be send via serial interface from a PC, especially data extraction from flash.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_SimpleChat_VT by
nRF51822_SimpleChat Extended With Data Measurements & Logger
This repo is derived from the initial SimpleChat Application and is build for BLE Nano Device, used in peripheral mode. This Application scan and measures some inputs and save them in flash with timestamp. A RTC has been implemented. Using chat application (available for smart phones) some commands can be send to BLE Nano in order to perform some tasks. Other types of commands can be send via serial interface from a PC, especially data extraction from flash.
Usage/Description:
After flashing or power on, the ble device will start to advertise and also its local time counter starts. While initializing, application will search through all flash pages, last data previosly written. From this pages will retrieve the last data and time available.
A Timer Tick (period set to 1 second) is used for updating RTC and for data measurements. When the application starts the data will not be saved in flash until a command is given to start (xf2). When started, it is recommended after connecting to ble device to set the time and date;
After the date and time updated, activate measurement logging (xf2).
Please be aware that for 5-6 seconds, each 100 seconds the connection is stopped by the application. This is due to fact that writing to flash is safe when radio connection is off. When g_MyDataIdx is between 95..100, connection should remain closed.
The Application can use somewhere around 100 flash pages (100K). This means, if data is sampled each second, the pages will start to overwrite after 100*100 seconds = 2.7 hours. Anyway the data is inserted into logger when it is different compared to previous measurement. Therefore the time the flash memory can log is bigger than 2.7 hours.
When retrieving the data log via serial, the format is as follows:
- page starts with:
2016_ 4_22 H: 7 => year, month, day, and hour
26:51;363; 1; 15 => minutes: seconds; A3,A4,A5 (no info about digital inputs) - following rows up to the next page:
0: 5;364; 1; 15 => minutes: seconds - time delta (time elapsed since last measurement); A3, A4, A5;
Hardware Connections
The BLE Nano Device is connected to some peripheral components as follows:
- Light Sensor ( A3 input)
- Temperature Sensor (A4 input)
- Gnd Voltage (or anything else) (A5 input)
- Buzzer (D6 output)
- Push Button (D5 input)
- [Optional]: MKUSB 20 Board (for Voltage Supply and for Serial Communication with a PC)
Supported Commands
- From RedBear Chat Application following messages are valid:
- Any message that not start with 'x' is echoed with 'R:' as prefix, and also is send via Serial Interface to PC
- Any message that start with 'x' is interpreted as command:
- xi0 / xi* except {1,2,3,4} => prints the analog inputs and one digital input (the led status) as uint16 values
- xi1 /xi2 / xi3 / xi4 =>prints the uint16 value for light, temp, gndV, led status
- xl => toggle led status
- xs[0-9][0-9] => buzzer; depending on digits, a digital pwm signal is send to buzzer; to deactivate: xs00
- xtg => returns the current time
- xti[0-9]{6} => insert time - exp: xti181004 means: 18H10M and 4 seconds
- xdg => returns the current date
- xdi[0-9]{6} => insert date - exp: xti160424 means: 2016 Y, 04 Month, 24 Day
- xf1 => prints the value of g_MyDataIdx (a value now between 0..99) representing the steps until a new page will be written in flash with new data; It also print the current flash page that will be written (between 155 and 255)
- xf2 => activate measurements logging
- xf3 => deactivate measurements logging
- x* => for invalid syntax a sound for 3 seconds is activated, with a message error
- From PC serial interface (with MKUSB 20 Board), following commands apply:
- xf[0-9] => request printing on serial of a specific flash page ( 0 - current page; 1 - previous page, etc)
- xd => request dump of all recorded logs (starting with current page)
- xg => prints the value of g_MyDataIdx (a value now between 0..99) representing the steps until a new page will be written in flash with new data;
- xca => open Radio Advertising (should be used when the Advertising is Off)
- xcc => Close Radio connection (should be used when Connection is in Connected state)
- xcs => Stop Radio Advertising (should be used when Advertising is active)
Other Commands:
- button push => closes the connection/advertising and turn off the led. If push again reset the previous conditions;
References
The mbed BLE API is meant to be used in projects on developer.mbed.org. Please see examples and sample project files there. A good starting point are these pages:
- [mbed BLE Homepage](http://developer.mbed.org/teams/Bluetooth-Low-Energy/) for all things BLE
- [mbed BLE Getting Started Guide](http://developer.mbed.org/forum/team-63-Bluetooth-Low-Energy-community/topic/5262/) a wonderful primer on using BLE with mbed
- [mbed BLE API page](http://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/docs/tip/) for the API in generated by doxygen
For this Application:
- [redBEar Ble Nano] (http://redbearlab.com/blenano/)
- [Project Blog - to be updated](http://tanasaro10.blogspot.ro/)
Revision 11:baafa4f7a15e, committed 2016-05-06
- Comitter:
- tanasaro10
- Date:
- Fri May 06 18:14:18 2016 +0000
- Parent:
- 10:c7d53e4e0602
- Commit message:
- minor updates
Changed in this revision
diff -r c7d53e4e0602 -r baafa4f7a15e main.cpp --- 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)
diff -r c7d53e4e0602 -r baafa4f7a15e myData.h --- a/myData.h Tue Apr 26 20:15:41 2016 +0000 +++ b/myData.h Fri May 06 18:14:18 2016 +0000 @@ -7,31 +7,23 @@ #define MAX_PAGE_NUM (NRF_FICR->CODESIZE - 1) // 255 #define MIN_PAGE_NUM 155u -typedef struct{ - uint8_t hour; - uint8_t min; - uint8_t sec; -} mtime_t; typedef struct{ - mtime_t currentTime; - mtime_t newTime; - bool updateTime; // true if currentTime needs to be updated with newTime; -} mtime_manager_t; - - -typedef struct { uint8_t year; // 20_XX, 2016 => 16; uint8_t month; // 1..12 uint8_t day; // 1..31 -}date_t; + uint8_t hours; + uint8_t minutes; + uint8_t seconds; +} mdate_time_t; typedef struct{ - date_t currentDate; - date_t newDate; - bool updateDate; // true if currentDate needs to be updated with newDate; -} mdate_manager_t; + mdate_time_t currentDateTime; + mdate_time_t newDateTime; + bool updateDateTime; // true if currentTime needs to be updated with newTime; +} mdatetime_manager_t; + typedef enum { eStartAdvertising =0, @@ -50,14 +42,13 @@ typedef struct { myPayload_t data; - uint8_t sec; - uint8_t min; + uint8_t seconds; + uint8_t minutes; } myDataL_t; typedef struct { - myPayload_t data; - mtime_t time; - date_t date; + mdate_time_t datetime; // 6 bytes length + myPayload_t data; // 8 bytes lenth } myData_t; typedef struct { @@ -79,21 +70,14 @@ /* Return page-N (thas has been written in Flash) */ uint8_t flash_prev_N_Page(uint8_t nr_of_pages); -/* save the current page Nr to flash*/ -void save_flash_curr_pageNr(date_t mdata); - -/* load from flash the current PageNr*/ -void load_flash_curr_pageNr(date_t* mdata); - /*search the latest page written and extract the page_nr, date, and time*/ -void search_latest_in_flash(date_t * outDate, mtime_t * outTime); +void search_latest_in_flash(mdate_time_t * outDateTime); /* Update current date and time with tseconds and also if there is a new date/time given */ -void update_time(mtime_manager_t* myTimeVar, mdate_manager_t* myDateVar, uint16_t tseconds); +void update_time(mdatetime_manager_t* myDateTimeVar, uint16_t tseconds); /* Update buzzer with period in ms =0..9 and duty cycle = 0..9*/ int buzz_int(PwmOut* buzzer, uint8_t period, uint8_t duty_cycle); /* Print on serial some error from main app*/ void assert_error_app(bool condition, Serial *pc, uint16_t error, uint16_t line, const char* file); -
diff -r c7d53e4e0602 -r baafa4f7a15e myFunctions.cpp --- a/myFunctions.cpp Tue Apr 26 20:15:41 2016 +0000 +++ b/myFunctions.cpp Fri May 06 18:14:18 2016 +0000 @@ -27,104 +27,60 @@ return retVal; } -void save_flash_curr_pageNr(date_t mdata){ - uint8_t data2write[4]; - memcpy(&data2write,&mdata,sizeof(date_t)); - data2write[3] = g_currPage; - ble_flash_word_write((((uint32_t *)((uint16_t)256 * 255)) + 254),*(uint32_t*)data2write); -} - -void search_latest_in_flash(date_t * outDate, mtime_t * outTime){ - uint8_t page_nr, sizeB; +void search_latest_in_flash(mdate_time_t * outDateTime){ + uint8_t page_nr, sizeB,temp[6]; uint16_t max_page=(uint16_t)MAX_PAGE_NUM+1u; int retVal; - myDataLogShort_t initialData; - date_t max_date={16,1,1}, inv_date={255,255,255}; - mtime_t max_time={0,0,0}, inv_time={255,255,255}; + mdate_time_t max_datetime={16,1,1,0,0,0}, inv_datetime={255,255,255,255,255,255}; - sizeB=sizeof(date_t); + sizeB=sizeof(mdate_time_t); uint32_t* p_curr_addr; for (page_nr = MAX_PAGE_NUM; page_nr>= MIN_PAGE_NUM; page_nr --){ p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * page_nr); p_curr_addr += 2; // skip the magic number and the word count - memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); - retVal = memcmp(&initialData.startData.date, &inv_date, sizeB); + + memcpy(temp, p_curr_addr, sizeB); + retVal = memcmp(&temp, &inv_datetime, sizeB); if (retVal!=0) { - retVal = memcmp(&initialData.startData.time, &inv_time, sizeB); - if (retVal !=0) { - retVal = memcmp(&initialData.startData.date, &max_date, sizeB); - if (retVal >=0) { - if (retVal>0){ - memcpy(&max_date, &initialData.startData.date, sizeB); - max_time.hour = 0; - max_time.min = 0; - max_time.sec = 0; - } - retVal = memcmp(&initialData.startData.time, &max_time, sizeB); - if (retVal >=0) { - memcpy(&max_time, &initialData.startData.time, sizeB); - max_page= page_nr; - } + retVal = memcmp(&temp, &max_datetime, sizeB); + if (retVal >0) { + memcpy(&max_datetime, &temp, sizeB); + max_page= page_nr; } - - } - } + } } - memcpy(outTime, &max_time, sizeB); - memcpy(outDate, &max_date, sizeB); + memcpy(outDateTime, &max_datetime, sizeB); g_currPage= (uint8_t)(max_page-1u); } -void load_flash_curr_pageNr(date_t *mdata){ - uint8_t l_currPage[4]; - date_t lDate; - memcpy(&lDate,mdata,sizeof(date_t)); - memcpy(&l_currPage,( ((uint32_t *)(BLE_FLASH_PAGE_SIZE * 255)) + 254),sizeof(uint32_t)); - - g_currPage = (l_currPage[3]); - memcpy(mdata,&l_currPage,sizeof(date_t)); - - if (g_currPage< MIN_PAGE_NUM){ - g_currPage = MAX_PAGE_NUM; +void update_time(mdatetime_manager_t* myDateTimeVar, uint16_t tseconds){ + //memcpy(&myDateTimeVar->newDateTime, &myDateTimeVar->currentDateTime, sizeof(mdate_time_t)); + if (myDateTimeVar->updateDateTime ==false){ + myDateTimeVar->newDateTime.seconds = (myDateTimeVar->currentDateTime.seconds + tseconds)% 60; + myDateTimeVar->newDateTime.minutes = (myDateTimeVar->currentDateTime.minutes + ((tseconds + myDateTimeVar->currentDateTime.seconds) / 60))%60; + if (myDateTimeVar->newDateTime.minutes< myDateTimeVar->currentDateTime.minutes ) { + myDateTimeVar->currentDateTime.hours++; + } + myDateTimeVar->newDateTime.hours = (myDateTimeVar->currentDateTime.hours + (tseconds / 3600+myDateTimeVar->newDateTime.minutes/60))%24; + if (myDateTimeVar->newDateTime.hours < myDateTimeVar->currentDateTime.hours){ + myDateTimeVar->newDateTime.day = (myDateTimeVar->currentDateTime.day + 1)%(eNrDaysPerMonth[myDateTimeVar->currentDateTime.month+1]+1); + if (myDateTimeVar->newDateTime.day < myDateTimeVar->currentDateTime.day ){ + myDateTimeVar->newDateTime.month = (myDateTimeVar->currentDateTime.month+ 1)%13+1; + if (myDateTimeVar->newDateTime.month< myDateTimeVar->currentDateTime.month){ + myDateTimeVar->newDateTime.year = (myDateTimeVar->currentDateTime.year+ 1); + } + } + } + } else { + myDateTimeVar->updateDateTime =false; } - if (mdata->year> 99u){ - memcpy(mdata,&lDate,sizeof(date_t)); + if (myDateTimeVar->updateDateTime ==true){ // there is a new Date ? + myDateTimeVar->updateDateTime =true; } - //ble_flash_word_write((uint32_t *)(BLE_FLASH_PAGE_SIZE * MAX_PAGE_NUM),(uint32_t)g_currPage); -} - -void update_time(mtime_manager_t* myTimeVar, mdate_manager_t* myDateVar, uint16_t tseconds){ - if (myTimeVar->updateTime ==false){ - myTimeVar->newTime.sec = (myTimeVar->currentTime.sec + tseconds)% 60; - myTimeVar->newTime.min = (myTimeVar->currentTime.min + ((tseconds + myTimeVar->currentTime.sec) / 60))%60; - if (myTimeVar->newTime.min< myTimeVar->currentTime.min ) { - myTimeVar->currentTime.hour++; - } - myTimeVar->newTime.hour = (myTimeVar->currentTime.hour + (tseconds / 3600+myTimeVar->newTime.min/60))%24; - if (myTimeVar->newTime.hour < myTimeVar->currentTime.hour){ - memcpy(&myDateVar->newDate,&myDateVar->currentDate,sizeof(date_t));// fill with default date data - - myDateVar->newDate.day = (myDateVar->currentDate.day + 1)%(eNrDaysPerMonth[myDateVar->currentDate.month+1]+1); - if (myDateVar->newDate.day < myDateVar->currentDate.day ){ - myDateVar->newDate.month = (myDateVar->currentDate.month+ 1)%13+1; - if (myDateVar->newDate.month< myDateVar->currentDate.month){ - myDateVar->newDate.year = (myDateVar->currentDate.year+ 1); - } - } - memcpy(&myDateVar->currentDate,&myDateVar->newDate, sizeof(date_t)); - } - memcpy(&myTimeVar->currentTime,&myTimeVar->newTime, sizeof(mtime_t)); - } else { - memcpy(&myTimeVar->currentTime,&myTimeVar->newTime, sizeof(mtime_t)); - myTimeVar->updateTime =false; - } - if (myDateVar->updateDate ==true){ // there is a new Date ? - memcpy(&myDateVar->currentDate,&myDateVar->newDate, sizeof(date_t)); - myDateVar->updateDate =true; - } + memcpy(&myDateTimeVar->currentDateTime,&myDateTimeVar->newDateTime, sizeof(mdate_time_t)); }