Tema SCPI Negru Rares-Razvan

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_TemperatureEx by Valentin Tanasa

functions.cpp

Committer:
tanasaro10
Date:
2016-04-25
Revision:
9:303d3628986a

File content as of revision 9:303d3628986a:

#include "myData.h"


uint8_t eNrDaysPerMonth[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static uint8_t g_currPage = MAX_PAGE_NUM;  // current page in Flash to be write

uint8_t flash_currPage(){
    return g_currPage;    
}

uint8_t flash_go_nextPage(){
    g_currPage --;
    if (g_currPage < MIN_PAGE_NUM) {
        g_currPage = MAX_PAGE_NUM;
    }
    return g_currPage;    
}


uint8_t flash_prev_N_Page(uint8_t nr_of_pages){
    uint8_t retVal;
    retVal = g_currPage + nr_of_pages;
    
    if (retVal > MAX_PAGE_NUM ) {
        retVal = MIN_PAGE_NUM + (retVal % MAX_PAGE_NUM - 1);
    }
    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;
    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};
    
    sizeB=sizeof(date_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);
        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;                            
                        }
                }
                
            }
        }        
    }
   
    memcpy(outTime, &max_time, sizeB);
    memcpy(outDate, &max_date, 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;
    }
    
    if (mdata->year> 99u){
        memcpy(mdata,&lDate,sizeof(date_t));
    }
    //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;
    }
}


int buzz_int(PwmOut* buzzer, uint8_t period, uint8_t duty_cycle){
    int retVal = 0;
    if ((duty_cycle<10)&&(period<10)){
        if (period!=0) {        
            buzzer->period_ms(period);
            *buzzer = (10.0 - (float)duty_cycle)/9.0;        
        } else {
            *buzzer = 0;
        }
    } else {
        retVal=-1;
    }
    return retVal;
}

void assert_error_app(bool condition, Serial *pc, uint16_t error, uint16_t line, const char* file){
    if (condition) {
        pc->printf("App err = %d, line = %d, file = %s\r\n",error, line, file);
    }
}