Tema SCPI Negru Rares-Razvan
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_TemperatureEx by
Diff: main.cpp
- Revision:
- 10:c7d53e4e0602
- Parent:
- 9:303d3628986a
- Child:
- 11:baafa4f7a15e
diff -r 303d3628986a -r c7d53e4e0602 main.cpp --- a/main.cpp Mon Apr 25 19:34:39 2016 +0000 +++ b/main.cpp Tue Apr 26 20:15:41 2016 +0000 @@ -60,8 +60,9 @@ static uint32_t gTimeInstant = 1; // TimerTick Resolution, in seconds -bool g_bIsConnected = false; +bool g_bIsConnected = false; bool g_bIsAdvertising = false; +bool g_bConnDisabled = false; bool g_LogActive = false; static myDataLog_t g_MyData; uint8_t g_MyDataIdx=0; @@ -104,6 +105,11 @@ pc.printf("Disconnected \r\n"); g_bIsConnected = false; g_bIsAdvertising = false; + pc.printf("R: %d\r",reason); + if (reason != 0x16) { + ble.startAdvertising(); + g_bIsAdvertising = true; + } } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) @@ -118,42 +124,45 @@ // stop buzz buzz_int(&buzzer, 0,0); } -void connectionUpdate(connection_update_t option){ - switch (option){ - case eStartAdvertising:{ - if ((g_bIsConnected == false)&&(g_bIsAdvertising == false)) { - pc.printf("Start Advertising\r"); - ble.startAdvertising(); - g_bIsAdvertising = true; +void connectionUpdate(connection_update_t option) +{ + if (g_bConnDisabled == false) { + switch (option) { + case eStartAdvertising: { + if ((g_bIsConnected == false)&&(g_bIsAdvertising == false)) { + pc.printf("Start Advertising\r"); + ble.startAdvertising(); + g_bIsAdvertising = true; + } + break; } - break; - } - case eStopAdvertising:{ - if (g_bIsAdvertising == true){ - pc.printf("Stop Advertising\r"); - ble.stopAdvertising(); - g_bIsAdvertising = false; + case eStopAdvertising: { + if (g_bIsAdvertising == true) { + pc.printf("Stop Advertising\r"); + ble.stopAdvertising(); + g_bIsAdvertising = false; + } + break; } - break; - } - case eDisconnect:{ - if (g_bIsConnected == true) { - pc.printf("Close connection\r"); - ble.disconnect((Gap::DisconnectionReason_t)0x12); - } else if (g_bIsAdvertising == true){ - pc.printf("Stop Advertising\r"); - ble.stopAdvertising(); - g_bIsAdvertising = false; + case eDisconnect: { + if (g_bIsConnected == true) { + pc.printf("Close connection\r"); + ble.disconnect((Gap::DisconnectionReason_t)0x12); + } else if (g_bIsAdvertising == true) { + pc.printf("Stop Advertising\r"); + ble.stopAdvertising(); + g_bIsAdvertising = false; + } + break; } - break; } } } void write_data_to_flash(uint32_t *tick) -{ +{ uint32_t retVal=0; uint8_t page_num=0; - + if (g_MyDataIdx==0) { //initiate connection connectionUpdate(eStartAdvertising); @@ -163,18 +172,18 @@ 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].min = (uint16_t)(*tick*gTimeInstant / 60); - g_MyData.myData[g_MyDataIdx].sec = (*tick*gTimeInstant% 60); - memcpy(&g_MyData.myData[g_MyDataIdx].data,&g_currMeasures, sizeof(myPayload_t)); + g_MyData.myData[g_MyDataIdx-1].min = (uint16_t)(*tick*gTimeInstant / 60); + g_MyData.myData[g_MyDataIdx-1].sec = (*tick*gTimeInstant% 60); + memcpy(&g_MyData.myData[g_MyDataIdx-1].data,&g_currMeasures, sizeof(myPayload_t)); } *tick = 0; if (g_MyDataIdx==(MAXBUFFER-5)) { //initiate disconnection - connectionUpdate(eDisconnect); + connectionUpdate(eDisconnect); } - if (g_MyDataIdx==(MAXBUFFER-1)) { + if (g_MyDataIdx == MAXBUFFER) { // write2Flash the current page num page_num=flash_currPage(); // write2Flash the current page data @@ -183,7 +192,7 @@ flash_go_nextPage(); //save_flash_curr_pageNr(g_myDateVar.currentDate); } - g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER); + g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER+1); } @@ -199,30 +208,32 @@ void flash_page_serial_dump(uint32_t* p_curr_addr) -{ -myDataLogShort_t initialData; -myDataL_t dataOut[2]; -uint8_t i; +{ + myDataLogShort_t initialData; + myDataL_t dataOut[2]; + uint8_t i; - p_curr_addr += 2; // skip the magic number and the word count + 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); 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); p_curr_addr += 5; - } + } } int update_measurements() { -int retVal; -static myPayload_t prevMeasures=(myPayload_t) {0, 0, 0, 0, 0 }; + int retVal; + static myPayload_t prevMeasures=(myPayload_t) { + 0, 0, 0, 0, 0 + }; g_currMeasures = (myPayload_t) { VP[0]->read_u16(), VP[2]->read_u16(), VP[1]->read_u16(), led, 0 @@ -230,22 +241,22 @@ retVal = memcmp(&g_currMeasures,&prevMeasures,sizeof(myPayload_t)); memcpy(&prevMeasures,&g_currMeasures,sizeof(myPayload_t)); return retVal; -} - +} + void at_eachInstant() -{ +{ static uint32_t tick=0; int retVal; - + // update time update_time(&g_myTimeVar, &g_myDateVar, gTimeInstant); //update measurements retVal = update_measurements(); - + // if there are changes in data save if ((retVal!=0)&&(g_LogActive==true)) { - write_data_to_flash(&tick); + write_data_to_flash(&tick); } tick++; } @@ -307,7 +318,7 @@ break; } case 's': {// buzzer - if (((buffer[1]>'9')||(buffer[1]<'0'))||((buffer[2]>'9')||(buffer[2]<'0'))){ + if (((buffer[1]>'9')||(buffer[1]<'0'))||((buffer[2]>'9')||(buffer[2]<'0'))) { MyASSERT(true,&pc, buffer[1]); // notify on serial interface on_error_radioMsg(); // notify on radio break; @@ -379,20 +390,20 @@ case '1': { //pc.printf("S Payload_t: %d \r\n",sizeof(myPayload_t)); //pc.printf("S myDataL_t: %d \r\n",sizeof(myDataL_t)); - + sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage()); len = 18; sendRadioMsg((uint8_t *)myBuf, len); break; } - case '2':{// start measuring + case '2': { // start measuring sprintf(myBuf,"Start Meas"); len = 12; sendRadioMsg((uint8_t *)myBuf, len); g_LogActive = true; break; } - case '3':{// stop measuring + case '3': { // stop measuring sprintf(myBuf,"Stop Meas"); len = 11; sendRadioMsg((uint8_t *)myBuf, len); @@ -419,13 +430,13 @@ uint8_t page_nr; char myBuf[5]; uint32_t * p_curr_addr; - + switch (buffer[0]) { case 'f': { // info about selected flash page if ((buffer[1]<='9')&&(buffer[1]>='0')) { memcpy(myBuf,&buffer[1],3); - page_nr= atoi(myBuf); - uint8_t p_word_count; + page_nr= atoi(myBuf); + uint8_t p_word_count; pc.printf("buffer[1]: %c \r\n",buffer[1]); @@ -434,31 +445,31 @@ p_curr_addr += 1; pc.printf("page_addr: %x \r\n",p_curr_addr); p_word_count = (uint8_t)(*(p_curr_addr)); - pc.printf("nr_of_words: %d \r\n",p_word_count); + pc.printf("nr_of_words: %d \r\n",p_word_count); flash_page_serial_dump((p_curr_addr-1)); } break; } case 'd': { // full dump - uint16_t page0; - pc.printf("Full dump \r\n"); - + uint16_t page0; + pc.printf("Full dump \r\n"); + page0 = flash_currPage(); for (page_nr=1; page_nr<=(MAX_PAGE_NUM-MIN_PAGE_NUM+1); page_nr++) { - if ((page0-page_nr)< MIN_PAGE_NUM){ + if ((page0-page_nr)< MIN_PAGE_NUM) { page0 = MAX_PAGE_NUM + page_nr; } p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * (page0-page_nr)); - flash_page_serial_dump(p_curr_addr); + flash_page_serial_dump(p_curr_addr); } break; } - case 'g':{ - pc.printf("g_MyDataIdx= %d\r", g_MyDataIdx); + case 'g': { + pc.printf("g_MyDataIdx= %d\r", g_MyDataIdx); break; } case 'c': { - switch (buffer[1]){ + switch (buffer[1]) { case 'a': { connectionUpdate(eStartAdvertising); break; @@ -471,8 +482,9 @@ connectionUpdate(eStopAdvertising); break; } - default: pc.printf("Not recognized cmd !\r"); - } + default: + pc.printf("Not recognized cmd !\r"); + } break; } default: { @@ -511,7 +523,7 @@ if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') { ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); if ((rx_buf[0]=='x')) { - decode_s(&rx_buf[1],(rx_len-1)); // serial decode + decode_s(&rx_buf[1],(rx_len-1)); // serial decode } rx_len= 0; break; @@ -523,10 +535,19 @@ { uint8_t buf[TXRX_BUF_LEN+1]; buf[0]='B'; - buf[1]=':'; - buf[2]='O'; + buf[1]='U'; + buf[2]='T'; buf[3]='N'; + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); + g_bConnDisabled = !g_bConnDisabled; + led = !led; + if (g_bConnDisabled == true){ + ble.disconnect((Gap::DisconnectionReason_t)0x12); + g_bIsConnected = false; + } else { + connectionUpdate(eStartAdvertising); + } } void g_varInit() @@ -534,7 +555,7 @@ g_myDateVar.updateDate = false; g_myTimeVar.updateTime = false; /* retreive latest date, time and page flash available */ - search_latest_in_flash(&g_myDateVar.currentDate, &g_myTimeVar.currentTime ); + search_latest_in_flash(&g_myDateVar.currentDate, &g_myTimeVar.currentTime ); } int main(void) @@ -545,7 +566,7 @@ ble.onConnection(connectionCallback); ble.onDataWritten(WrittenHandler); event.rise(&button); - + pc.baud(19200); pc.printf("SimpleChat Init \r\n"); @@ -567,7 +588,7 @@ ptrFunc = ble_flash_on_radio_active_evt; //needed for flash write //ble.onRadioNotification(ptrFunc); - */ + */ ble.addService(uartService); ble.startAdvertising(); pc.printf("Advertising Start \r\n");