Tema SCPI Negru Rares-Razvan

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_TemperatureEx 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
tanasaro10 9:303d3628986a 1 #include "myData.h"
tanasaro10 9:303d3628986a 2
tanasaro10 9:303d3628986a 3
tanasaro10 9:303d3628986a 4 uint8_t eNrDaysPerMonth[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
tanasaro10 9:303d3628986a 5 static uint8_t g_currPage = MAX_PAGE_NUM; // current page in Flash to be write
tanasaro10 9:303d3628986a 6
tanasaro10 9:303d3628986a 7 uint8_t flash_currPage(){
tanasaro10 9:303d3628986a 8 return g_currPage;
tanasaro10 9:303d3628986a 9 }
tanasaro10 9:303d3628986a 10
tanasaro10 9:303d3628986a 11 uint8_t flash_go_nextPage(){
tanasaro10 9:303d3628986a 12 g_currPage --;
tanasaro10 9:303d3628986a 13 if (g_currPage < MIN_PAGE_NUM) {
tanasaro10 9:303d3628986a 14 g_currPage = MAX_PAGE_NUM;
tanasaro10 9:303d3628986a 15 }
tanasaro10 9:303d3628986a 16 return g_currPage;
tanasaro10 9:303d3628986a 17 }
tanasaro10 9:303d3628986a 18
tanasaro10 9:303d3628986a 19
tanasaro10 9:303d3628986a 20 uint8_t flash_prev_N_Page(uint8_t nr_of_pages){
tanasaro10 9:303d3628986a 21 uint8_t retVal;
tanasaro10 9:303d3628986a 22 retVal = g_currPage + nr_of_pages;
tanasaro10 9:303d3628986a 23
tanasaro10 9:303d3628986a 24 if (retVal > MAX_PAGE_NUM ) {
tanasaro10 9:303d3628986a 25 retVal = MIN_PAGE_NUM + (retVal % MAX_PAGE_NUM - 1);
tanasaro10 9:303d3628986a 26 }
tanasaro10 9:303d3628986a 27 return retVal;
tanasaro10 9:303d3628986a 28 }
tanasaro10 9:303d3628986a 29
tanasaro10 11:baafa4f7a15e 30 void search_latest_in_flash(mdate_time_t * outDateTime){
tanasaro10 11:baafa4f7a15e 31 uint8_t page_nr, sizeB,temp[6];
tanasaro10 9:303d3628986a 32 uint16_t max_page=(uint16_t)MAX_PAGE_NUM+1u;
tanasaro10 9:303d3628986a 33 int retVal;
tanasaro10 11:baafa4f7a15e 34 mdate_time_t max_datetime={16,1,1,0,0,0}, inv_datetime={255,255,255,255,255,255};
tanasaro10 9:303d3628986a 35
tanasaro10 11:baafa4f7a15e 36 sizeB=sizeof(mdate_time_t);
tanasaro10 9:303d3628986a 37 uint32_t* p_curr_addr;
tanasaro10 9:303d3628986a 38
tanasaro10 9:303d3628986a 39 for (page_nr = MAX_PAGE_NUM; page_nr>= MIN_PAGE_NUM; page_nr --){
tanasaro10 9:303d3628986a 40 p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * page_nr);
tanasaro10 9:303d3628986a 41 p_curr_addr += 2; // skip the magic number and the word count
tanasaro10 11:baafa4f7a15e 42
tanasaro10 11:baafa4f7a15e 43 memcpy(temp, p_curr_addr, sizeB);
tanasaro10 11:baafa4f7a15e 44 retVal = memcmp(&temp, &inv_datetime, sizeB);
tanasaro10 9:303d3628986a 45 if (retVal!=0) {
tanasaro10 11:baafa4f7a15e 46 retVal = memcmp(&temp, &max_datetime, sizeB);
tanasaro10 11:baafa4f7a15e 47 if (retVal >0) {
tanasaro10 11:baafa4f7a15e 48 memcpy(&max_datetime, &temp, sizeB);
tanasaro10 11:baafa4f7a15e 49 max_page= page_nr;
tanasaro10 9:303d3628986a 50 }
tanasaro10 11:baafa4f7a15e 51 }
tanasaro10 9:303d3628986a 52 }
tanasaro10 9:303d3628986a 53
tanasaro10 11:baafa4f7a15e 54 memcpy(outDateTime, &max_datetime, sizeB);
tanasaro10 9:303d3628986a 55 g_currPage= (uint8_t)(max_page-1u);
tanasaro10 9:303d3628986a 56 }
tanasaro10 9:303d3628986a 57
tanasaro10 11:baafa4f7a15e 58 void update_time(mdatetime_manager_t* myDateTimeVar, uint16_t tseconds){
tanasaro10 11:baafa4f7a15e 59 //memcpy(&myDateTimeVar->newDateTime, &myDateTimeVar->currentDateTime, sizeof(mdate_time_t));
tanasaro10 11:baafa4f7a15e 60 if (myDateTimeVar->updateDateTime ==false){
tanasaro10 11:baafa4f7a15e 61 myDateTimeVar->newDateTime.seconds = (myDateTimeVar->currentDateTime.seconds + tseconds)% 60;
tanasaro10 11:baafa4f7a15e 62 myDateTimeVar->newDateTime.minutes = (myDateTimeVar->currentDateTime.minutes + ((tseconds + myDateTimeVar->currentDateTime.seconds) / 60))%60;
tanasaro10 11:baafa4f7a15e 63 if (myDateTimeVar->newDateTime.minutes< myDateTimeVar->currentDateTime.minutes ) {
tanasaro10 11:baafa4f7a15e 64 myDateTimeVar->currentDateTime.hours++;
tanasaro10 11:baafa4f7a15e 65 }
tanasaro10 11:baafa4f7a15e 66 myDateTimeVar->newDateTime.hours = (myDateTimeVar->currentDateTime.hours + (tseconds / 3600+myDateTimeVar->newDateTime.minutes/60))%24;
tanasaro10 11:baafa4f7a15e 67 if (myDateTimeVar->newDateTime.hours < myDateTimeVar->currentDateTime.hours){
tanasaro10 12:7772974713ac 68 myDateTimeVar->newDateTime.day = (myDateTimeVar->currentDateTime.day + 1)%(eNrDaysPerMonth[myDateTimeVar->currentDateTime.month-1]+1);
tanasaro10 12:7772974713ac 69 if (myDateTimeVar->newDateTime.day == 0){
tanasaro10 12:7772974713ac 70 myDateTimeVar->newDateTime.day ++;
tanasaro10 12:7772974713ac 71 }
tanasaro10 11:baafa4f7a15e 72 if (myDateTimeVar->newDateTime.day < myDateTimeVar->currentDateTime.day ){
tanasaro10 12:7772974713ac 73 myDateTimeVar->newDateTime.month = (myDateTimeVar->currentDateTime.month+ 1)%13;
tanasaro10 12:7772974713ac 74 if (myDateTimeVar->newDateTime.month ==0){
tanasaro10 12:7772974713ac 75 myDateTimeVar->newDateTime.month++;
tanasaro10 12:7772974713ac 76 }
tanasaro10 11:baafa4f7a15e 77 if (myDateTimeVar->newDateTime.month< myDateTimeVar->currentDateTime.month){
tanasaro10 11:baafa4f7a15e 78 myDateTimeVar->newDateTime.year = (myDateTimeVar->currentDateTime.year+ 1);
tanasaro10 11:baafa4f7a15e 79 }
tanasaro10 11:baafa4f7a15e 80 }
tanasaro10 11:baafa4f7a15e 81 }
tanasaro10 11:baafa4f7a15e 82 } else {
tanasaro10 11:baafa4f7a15e 83 myDateTimeVar->updateDateTime =false;
tanasaro10 9:303d3628986a 84 }
tanasaro10 9:303d3628986a 85
tanasaro10 11:baafa4f7a15e 86 if (myDateTimeVar->updateDateTime ==true){ // there is a new Date ?
tanasaro10 11:baafa4f7a15e 87 myDateTimeVar->updateDateTime =true;
tanasaro10 9:303d3628986a 88 }
tanasaro10 11:baafa4f7a15e 89 memcpy(&myDateTimeVar->currentDateTime,&myDateTimeVar->newDateTime, sizeof(mdate_time_t));
tanasaro10 9:303d3628986a 90 }
tanasaro10 9:303d3628986a 91
tanasaro10 9:303d3628986a 92
tanasaro10 9:303d3628986a 93 int buzz_int(PwmOut* buzzer, uint8_t period, uint8_t duty_cycle){
tanasaro10 9:303d3628986a 94 int retVal = 0;
tanasaro10 9:303d3628986a 95 if ((duty_cycle<10)&&(period<10)){
tanasaro10 9:303d3628986a 96 if (period!=0) {
tanasaro10 9:303d3628986a 97 buzzer->period_ms(period);
tanasaro10 9:303d3628986a 98 *buzzer = (10.0 - (float)duty_cycle)/9.0;
tanasaro10 9:303d3628986a 99 } else {
tanasaro10 9:303d3628986a 100 *buzzer = 0;
tanasaro10 9:303d3628986a 101 }
tanasaro10 9:303d3628986a 102 } else {
tanasaro10 9:303d3628986a 103 retVal=-1;
tanasaro10 9:303d3628986a 104 }
tanasaro10 9:303d3628986a 105 return retVal;
tanasaro10 9:303d3628986a 106 }
tanasaro10 9:303d3628986a 107
tanasaro10 9:303d3628986a 108 void assert_error_app(bool condition, Serial *pc, uint16_t error, uint16_t line, const char* file){
tanasaro10 9:303d3628986a 109 if (condition) {
tanasaro10 9:303d3628986a 110 pc->printf("App err = %d, line = %d, file = %s\r\n",error, line, file);
tanasaro10 9:303d3628986a 111 }
tanasaro10 9:303d3628986a 112 }