Tema SCPI Negru Rares-Razvan
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_TemperatureEx by
functions.cpp@9:303d3628986a, 2016-04-25 (annotated)
- Committer:
- tanasaro10
- Date:
- Mon Apr 25 19:34:39 2016 +0000
- Revision:
- 9:303d3628986a
Major rework on the code and extended functionality added. Detailed explanations are given in read_me.md file.
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:303d3628986a | 30 | void save_flash_curr_pageNr(date_t mdata){ |
tanasaro10 | 9:303d3628986a | 31 | uint8_t data2write[4]; |
tanasaro10 | 9:303d3628986a | 32 | memcpy(&data2write,&mdata,sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 33 | data2write[3] = g_currPage; |
tanasaro10 | 9:303d3628986a | 34 | ble_flash_word_write((((uint32_t *)((uint16_t)256 * 255)) + 254),*(uint32_t*)data2write); |
tanasaro10 | 9:303d3628986a | 35 | } |
tanasaro10 | 9:303d3628986a | 36 | |
tanasaro10 | 9:303d3628986a | 37 | void search_latest_in_flash(date_t * outDate, mtime_t * outTime){ |
tanasaro10 | 9:303d3628986a | 38 | uint8_t page_nr, sizeB; |
tanasaro10 | 9:303d3628986a | 39 | uint16_t max_page=(uint16_t)MAX_PAGE_NUM+1u; |
tanasaro10 | 9:303d3628986a | 40 | int retVal; |
tanasaro10 | 9:303d3628986a | 41 | myDataLogShort_t initialData; |
tanasaro10 | 9:303d3628986a | 42 | date_t max_date={16,1,1}, inv_date={255,255,255}; |
tanasaro10 | 9:303d3628986a | 43 | mtime_t max_time={0,0,0}, inv_time={255,255,255}; |
tanasaro10 | 9:303d3628986a | 44 | |
tanasaro10 | 9:303d3628986a | 45 | sizeB=sizeof(date_t); |
tanasaro10 | 9:303d3628986a | 46 | uint32_t* p_curr_addr; |
tanasaro10 | 9:303d3628986a | 47 | |
tanasaro10 | 9:303d3628986a | 48 | for (page_nr = MAX_PAGE_NUM; page_nr>= MIN_PAGE_NUM; page_nr --){ |
tanasaro10 | 9:303d3628986a | 49 | p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * page_nr); |
tanasaro10 | 9:303d3628986a | 50 | p_curr_addr += 2; // skip the magic number and the word count |
tanasaro10 | 9:303d3628986a | 51 | memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); |
tanasaro10 | 9:303d3628986a | 52 | retVal = memcmp(&initialData.startData.date, &inv_date, sizeB); |
tanasaro10 | 9:303d3628986a | 53 | if (retVal!=0) { |
tanasaro10 | 9:303d3628986a | 54 | retVal = memcmp(&initialData.startData.time, &inv_time, sizeB); |
tanasaro10 | 9:303d3628986a | 55 | if (retVal !=0) { |
tanasaro10 | 9:303d3628986a | 56 | retVal = memcmp(&initialData.startData.date, &max_date, sizeB); |
tanasaro10 | 9:303d3628986a | 57 | if (retVal >=0) { |
tanasaro10 | 9:303d3628986a | 58 | if (retVal>0){ |
tanasaro10 | 9:303d3628986a | 59 | memcpy(&max_date, &initialData.startData.date, sizeB); |
tanasaro10 | 9:303d3628986a | 60 | max_time.hour = 0; |
tanasaro10 | 9:303d3628986a | 61 | max_time.min = 0; |
tanasaro10 | 9:303d3628986a | 62 | max_time.sec = 0; |
tanasaro10 | 9:303d3628986a | 63 | } |
tanasaro10 | 9:303d3628986a | 64 | retVal = memcmp(&initialData.startData.time, &max_time, sizeB); |
tanasaro10 | 9:303d3628986a | 65 | if (retVal >=0) { |
tanasaro10 | 9:303d3628986a | 66 | memcpy(&max_time, &initialData.startData.time, sizeB); |
tanasaro10 | 9:303d3628986a | 67 | max_page= page_nr; |
tanasaro10 | 9:303d3628986a | 68 | } |
tanasaro10 | 9:303d3628986a | 69 | } |
tanasaro10 | 9:303d3628986a | 70 | |
tanasaro10 | 9:303d3628986a | 71 | } |
tanasaro10 | 9:303d3628986a | 72 | } |
tanasaro10 | 9:303d3628986a | 73 | } |
tanasaro10 | 9:303d3628986a | 74 | |
tanasaro10 | 9:303d3628986a | 75 | memcpy(outTime, &max_time, sizeB); |
tanasaro10 | 9:303d3628986a | 76 | memcpy(outDate, &max_date, sizeB); |
tanasaro10 | 9:303d3628986a | 77 | g_currPage= (uint8_t)(max_page-1u); |
tanasaro10 | 9:303d3628986a | 78 | } |
tanasaro10 | 9:303d3628986a | 79 | |
tanasaro10 | 9:303d3628986a | 80 | void load_flash_curr_pageNr(date_t *mdata){ |
tanasaro10 | 9:303d3628986a | 81 | uint8_t l_currPage[4]; |
tanasaro10 | 9:303d3628986a | 82 | date_t lDate; |
tanasaro10 | 9:303d3628986a | 83 | memcpy(&lDate,mdata,sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 84 | memcpy(&l_currPage,( ((uint32_t *)(BLE_FLASH_PAGE_SIZE * 255)) + 254),sizeof(uint32_t)); |
tanasaro10 | 9:303d3628986a | 85 | |
tanasaro10 | 9:303d3628986a | 86 | g_currPage = (l_currPage[3]); |
tanasaro10 | 9:303d3628986a | 87 | memcpy(mdata,&l_currPage,sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 88 | |
tanasaro10 | 9:303d3628986a | 89 | if (g_currPage< MIN_PAGE_NUM){ |
tanasaro10 | 9:303d3628986a | 90 | g_currPage = MAX_PAGE_NUM; |
tanasaro10 | 9:303d3628986a | 91 | } |
tanasaro10 | 9:303d3628986a | 92 | |
tanasaro10 | 9:303d3628986a | 93 | if (mdata->year> 99u){ |
tanasaro10 | 9:303d3628986a | 94 | memcpy(mdata,&lDate,sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 95 | } |
tanasaro10 | 9:303d3628986a | 96 | //ble_flash_word_write((uint32_t *)(BLE_FLASH_PAGE_SIZE * MAX_PAGE_NUM),(uint32_t)g_currPage); |
tanasaro10 | 9:303d3628986a | 97 | } |
tanasaro10 | 9:303d3628986a | 98 | |
tanasaro10 | 9:303d3628986a | 99 | void update_time(mtime_manager_t* myTimeVar, mdate_manager_t* myDateVar, uint16_t tseconds){ |
tanasaro10 | 9:303d3628986a | 100 | if (myTimeVar->updateTime ==false){ |
tanasaro10 | 9:303d3628986a | 101 | myTimeVar->newTime.sec = (myTimeVar->currentTime.sec + tseconds)% 60; |
tanasaro10 | 9:303d3628986a | 102 | myTimeVar->newTime.min = (myTimeVar->currentTime.min + ((tseconds + myTimeVar->currentTime.sec) / 60))%60; |
tanasaro10 | 9:303d3628986a | 103 | if (myTimeVar->newTime.min< myTimeVar->currentTime.min ) { |
tanasaro10 | 9:303d3628986a | 104 | myTimeVar->currentTime.hour++; |
tanasaro10 | 9:303d3628986a | 105 | } |
tanasaro10 | 9:303d3628986a | 106 | myTimeVar->newTime.hour = (myTimeVar->currentTime.hour + (tseconds / 3600+myTimeVar->newTime.min/60))%24; |
tanasaro10 | 9:303d3628986a | 107 | if (myTimeVar->newTime.hour < myTimeVar->currentTime.hour){ |
tanasaro10 | 9:303d3628986a | 108 | memcpy(&myDateVar->newDate,&myDateVar->currentDate,sizeof(date_t));// fill with default date data |
tanasaro10 | 9:303d3628986a | 109 | |
tanasaro10 | 9:303d3628986a | 110 | myDateVar->newDate.day = (myDateVar->currentDate.day + 1)%(eNrDaysPerMonth[myDateVar->currentDate.month+1]+1); |
tanasaro10 | 9:303d3628986a | 111 | if (myDateVar->newDate.day < myDateVar->currentDate.day ){ |
tanasaro10 | 9:303d3628986a | 112 | myDateVar->newDate.month = (myDateVar->currentDate.month+ 1)%13+1; |
tanasaro10 | 9:303d3628986a | 113 | if (myDateVar->newDate.month< myDateVar->currentDate.month){ |
tanasaro10 | 9:303d3628986a | 114 | myDateVar->newDate.year = (myDateVar->currentDate.year+ 1); |
tanasaro10 | 9:303d3628986a | 115 | } |
tanasaro10 | 9:303d3628986a | 116 | } |
tanasaro10 | 9:303d3628986a | 117 | memcpy(&myDateVar->currentDate,&myDateVar->newDate, sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 118 | } |
tanasaro10 | 9:303d3628986a | 119 | memcpy(&myTimeVar->currentTime,&myTimeVar->newTime, sizeof(mtime_t)); |
tanasaro10 | 9:303d3628986a | 120 | } else { |
tanasaro10 | 9:303d3628986a | 121 | memcpy(&myTimeVar->currentTime,&myTimeVar->newTime, sizeof(mtime_t)); |
tanasaro10 | 9:303d3628986a | 122 | myTimeVar->updateTime =false; |
tanasaro10 | 9:303d3628986a | 123 | } |
tanasaro10 | 9:303d3628986a | 124 | if (myDateVar->updateDate ==true){ // there is a new Date ? |
tanasaro10 | 9:303d3628986a | 125 | memcpy(&myDateVar->currentDate,&myDateVar->newDate, sizeof(date_t)); |
tanasaro10 | 9:303d3628986a | 126 | myDateVar->updateDate =true; |
tanasaro10 | 9:303d3628986a | 127 | } |
tanasaro10 | 9:303d3628986a | 128 | } |
tanasaro10 | 9:303d3628986a | 129 | |
tanasaro10 | 9:303d3628986a | 130 | |
tanasaro10 | 9:303d3628986a | 131 | int buzz_int(PwmOut* buzzer, uint8_t period, uint8_t duty_cycle){ |
tanasaro10 | 9:303d3628986a | 132 | int retVal = 0; |
tanasaro10 | 9:303d3628986a | 133 | if ((duty_cycle<10)&&(period<10)){ |
tanasaro10 | 9:303d3628986a | 134 | if (period!=0) { |
tanasaro10 | 9:303d3628986a | 135 | buzzer->period_ms(period); |
tanasaro10 | 9:303d3628986a | 136 | *buzzer = (10.0 - (float)duty_cycle)/9.0; |
tanasaro10 | 9:303d3628986a | 137 | } else { |
tanasaro10 | 9:303d3628986a | 138 | *buzzer = 0; |
tanasaro10 | 9:303d3628986a | 139 | } |
tanasaro10 | 9:303d3628986a | 140 | } else { |
tanasaro10 | 9:303d3628986a | 141 | retVal=-1; |
tanasaro10 | 9:303d3628986a | 142 | } |
tanasaro10 | 9:303d3628986a | 143 | return retVal; |
tanasaro10 | 9:303d3628986a | 144 | } |
tanasaro10 | 9:303d3628986a | 145 | |
tanasaro10 | 9:303d3628986a | 146 | void assert_error_app(bool condition, Serial *pc, uint16_t error, uint16_t line, const char* file){ |
tanasaro10 | 9:303d3628986a | 147 | if (condition) { |
tanasaro10 | 9:303d3628986a | 148 | pc->printf("App err = %d, line = %d, file = %s\r\n",error, line, file); |
tanasaro10 | 9:303d3628986a | 149 | } |
tanasaro10 | 9:303d3628986a | 150 | } |