Tema SCPI Negru Rares-Razvan

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_TemperatureEx by Valentin Tanasa

Revision:
11:baafa4f7a15e
Parent:
10:c7d53e4e0602
Child:
12:7772974713ac
--- 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));    
 }