/*
Diplomova prace : Palubni pocitac

helper_func.c  - pomocne funkce pro main

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/

#include "helper_func.h"

/*------------------ funkce pro main menu LCD apod ----------*/

void loadAvgData(char * path,char * file, avg_t * data,uint8_t trackIndex,uint8_t findTrack)
{
    if(data && path)
    {

        data_t firstData;
        data_t measData;

        if(findTrack)   /* pokud hleda v ramci jedne trate tak vola tyto fce */
        {

            getTrackMeas(path,file,&firstData,trackIndex,0);
            getTrackMeas(path,file,&measData,trackIndex,1);

        }
        else   /* zde ziskame z dane lokace nejstarsi data + nejnovejsi a udelame rozdil tedy ziskame data pro prumery */
        {
            if(!file)
            {
                findMeas(path,&measData,1);
                findMeas(path,&firstData,0);
            }
            else
            {

                struct fat_file_struct * fd;

                uint8_t state;

                fd = openFile(path,file,&state);

                if(state==OLD_FILE)
                {

                    readMeas(&firstData,0,FAT_SEEK_SET,fd);
                    readMeas(&measData,-1,FAT_SEEK_END,fd);

                }

                fat_close_file(fd);

            }


        }

        diffMeasData(&measData,&firstData);

        data->km = RetMeters(&measData.meas.TotImp)/100;
        data->kmh = RetSpeed(&measData.meas.TotImp,&measData.meas.TotTime);
        data->fCons = RetFuelCons(&measData.meas.TotImp,&measData.meas.TotInj);
        data->liters = GetFuel(&measData.meas.TotInj);
        data->rpm = RetRpm(&measData.meas.TotRpm,&measData.meas.TotTime);

        getPcfFormat(measData.meas.TotTime,&data->time);

    }
}
void redrawWaitScreen(void)
{

    dog_StartPage();
    do
    {

        drawFrame();
        drawWait(56,43);
    }
    while(dog_NextPage());

}
void redrawLogTxt(DOG_PSTR_P text)
{

    dog_StartPage();
    do
    {
        drawFrame();
        dog_DrawStrP(10,30,font_6x12,text);


    }
    while(dog_NextPage());

}

pcf_t * getPcfFormat(tm_t sec,pcf_t * time)
{


    time->year = sec / (365UL*24*60*60);
    sec %=(365UL*24*60*60);
    time->month = sec/(30UL*24*60*60);
    sec%=(30UL*24*60*60);
    time->day = sec/(24UL*60*60);
    sec%=(24UL*60*60);
    time->hour = sec/(60UL*60);
    sec%=(60UL*60);
    time->min = sec/60UL;
    sec%=60UL;
    time->sec = sec;

    return time;
}

void diffMeasData(data_t * inOut,const data_t * in)
{

    if(inOut && in)
    {

        cm_t * mOut = &inOut->meas;
        const cm_t * mIn = &in->meas;

        if(mOut->TotInj >= mIn->TotInj)
        {

            /*  uint8_t dec = 0;

              if(mOut->TotInj_imp >= mIn->TotInj_imp)
              {

                  mOut->TotInj_imp-=mIn->TotInj_imp;

              }
              else if(mOut->TotInj_min > 0)
              {


                  mOut->TotInj_imp = mIn->TotInj_imp - mOut->TotInj_imp;
                  mOut->TotInj_imp = (tm_t)3000000UL-mOut->TotInj_imp;
                  dec = 1;

              }*/

            mOut->TotInj-=mIn->TotInj;

            //if(dec)mOut->TotInj_min--;

        }
        else
        {
            mOut->TotInj = 0;


        }

        if(mOut->TotTime > mIn->TotTime)mOut->TotTime-=mIn->TotTime;
        else mOut->TotTime = 0;

        if(mOut->TotImp > mIn->TotImp)mOut->TotImp -=mIn->TotImp;
        else mOut->TotImp = 0;

    }
}

uint8_t checkKeys(uint8_t oldPos,uint8_t max,uint8_t min,key_t * k)
{

    while(k->key_up && oldPos > min)
    {
        oldPos--;
        k->key_up--;

    }

    while(k->key_down && oldPos < max)
    {
        oldPos++;
        k->key_down--;

    }

    if(oldPos>max)oldPos=max;
    if(oldPos<min)oldPos=min;

    k->key_up=0;
    k->key_down=0;

    return oldPos;

}

/* --------------- funkce pro vypocty ----------------------*/

inline speed_t GetAvgSpeed(void)
{
    cm_t * data = getActiveUserMeas();
    return RetSpeed(&data->AvgImp,&data->ActTime);

}

inline speed_t GetTripSpeed(void)
{
    cm_t * data = getActiveUserMeas();
    return RetSpeed(&data->TripImp,&data->TripTime);

}

inline speed_t GetTotSpeed(void)
{
    cm_t * data = getActiveUserMeas();
    return RetSpeed(&data->TotImp,&data->TotTime);

}

inline tm_t GetCurrTime(void)
{
    cm_t * data = getActiveUserMeas();
    return data->ActTime;
}

inline tm_t GetTripTime(void)
{
    cm_t * data = getActiveUserMeas();
    return data->TripTime;
}

inline m_t GetActMeters(void)
{

    odo_t imp = GetImpCnt();

    return RetMeters(&imp);

}

inline fuel_t GetCurrFuelCons(void)
{

    inj_t ticks = GetInjCnt();
    odo_t imp = GetImpCnt();

    return RetFuelCons(&imp,&ticks);

}

inline rpmmin_t GetCurrRpm(void)
{
    static rpmmin_t lastRpmMin=0;
    rpm_t rpm = GetRpmCnt();
    rpm*=4; /* nasobime na rovnou 1s */
    tm_t t = 1;

    rpmmin_t r =  (RetRpm(&rpm,&t)/10)*10;

    if( r > lastRpmMin+10 || ( (lastRpmMin>10) && r < (lastRpmMin-10) ) )
    {
        lastRpmMin = r;
        return r;
    }
    else return lastRpmMin;
}

inline rpmmin_t GetTripRpm(void)
{

    cm_t * data = getActiveUserMeas();
    return RetRpm(&data->TripRpm,&data->TripTime);
}

inline rpmmin_t GetTotRpm(void)
{
    cm_t * data = getActiveUserMeas();
    return RetRpm(&data->TotRpm,&data->TotTime);
}

inline speed_t GetCurrSpeed(void)
{

    odo_t data = GetImpCnt();
    tm_t t = 1;

    return RetSpeed(&data,&t);

}

inline fuel_t GetAvgFuelCons(void)
{
    cm_t * data = getActiveUserMeas();
    return RetFuelCons(&data->AvgImp,&data->AvgInj);

}

inline fuel_t GetTripFuelCons(void)
{
    cm_t * data = getActiveUserMeas();
    return RetFuelCons(&data->TripImp,&data->TripInj);

}

inline fuel_t GetTotFuelCons(void)
{
    cm_t * data = getActiveUserMeas();
    return RetFuelCons(&data->TotImp,&data->TotInj);

}

inline m_t GetCurrMeters(void)
{
    cm_t * data = getActiveUserMeas();
    return RetMeters(&data->AvgImp);

}

inline m_t GetTripMeters(void)
{
    cm_t * data = getActiveUserMeas();
    return RetMeters(&data->TripImp);

}

inline m_t GetTotMeters(void)
{
    cm_t * data = getActiveUserMeas();
    return RetMeters(&data->TotImp);

}

inline fuel_t GetCurrFuel(void)
{
    cm_t * data = getActiveUserMeas();
    return GetFuel(&data->AvgInj);

}

inline fuel_t GetTripFuel(void)
{
    cm_t * data = getActiveUserMeas();
    return GetFuel(&data->TripInj);

}

inline fuel_t GetCurrFuelConsLh(void)
{

    return RetFuelConsLh();

}

uint16_t GetMaxDis(void)
{

    fuel_t f= GetAvgFuelCons();

    if(!f) return 0;

    uint16_t dis = ((uint16_t)GetTankL() * (uint16_t)100)/GetAvgFuelCons();

    return (dis*10);

}
