/*
Diplomova prace : Palubni pocitac

temp- api pro praci s teplot. cidly

Atmega128A 10 MHz XTAL

Autor: Ondrej Lutera

2011/2012

*/

#include "temp.h"
#include "uart.h"

temp_t tempSensors[MAX_SENSORS];
temp_t ee_tempSensors[MAX_SENSORS] EEMEM;
uint8_t eepromInit EEMEM;

static volatile unsigned char chnl = NOCH;

uint8_t findTempSensors(temp_t * sensors)
{
    uint8_t i;
    uint8_t id[OW_ROMCODE_SIZE];
    uint8_t diff, nSensors;

    nSensors = 0;

    ow_set_bus(&DS_PIN,&DS_PORT,&DS_DDR,DS);
    ow_parasite_enable();

    for( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE && nSensors < MAX_SENSORS ; )
    {
        DS18X20_find_sensor( &diff, &id[0] );

#ifdef CAR_DEBUG
        for (i = 0; i < 8; i++ )uart_puthex_byte(id[i]);
#endif

        if( diff == OW_PRESENCE_ERR )
        {

            return 0;
            break;
        }

        if( diff == OW_DATA_ERR )
        {

            return 0;
            break;
        }

        for (i=0; i<OW_ROMCODE_SIZE; i++)
            sensors[nSensors].id[i]=id[i];

        nSensors++;
    }

    return nSensors;
}



uint16_t initTempSensors(temp_t *newSensors, uint8_t readEE)
{

    temp_t sensors[MAX_SENSORS];
    uint16_t cnt,i,j,k;
    uint8_t comp;
    uint16_t found=0;

    memset(sensors,0,sizeof(temp_t)*MAX_SENSORS);

    if(readEE)
    {

        if(eeprom_read_byte(&eepromInit)!='Y')
        {

            eeprom_update_block(&sensors,&ee_tempSensors,sizeof(temp_t)*MAX_SENSORS);
            eeprom_write_byte(&eepromInit,'Y');

        }
        eeprom_read_block(&tempSensors,&ee_tempSensors,sizeof(temp_t)*MAX_SENSORS);

    }

    cnt=findTempSensors(sensors);

    for(k=0; k<MAX_SENSORS; k++)tempSensors[k].connected=FALSE;

    for(k=0; k<MAX_SENSORS; k++)
    {

        for(i=0; i<cnt; i++)
        {

            comp = 0;
            j = 0;

            if(sensors[i].connected==TRUE) continue;

            do
            {

                comp |=  sensors[i].id[j]^tempSensors[k].id[j];
                j++;

            }
            while(j<OW_ROMCODE_SIZE && !comp);

            if(comp==0)
            {
                tempSensors[k].connected = TRUE;
                sensors[i].connected = TRUE;
                found++;
                break;
            }

        }

    }

    uint8_t index=  0;

    for(i=0; i<cnt; i++)
    {

        if(sensors[i].connected==FALSE)
        {

            for(j=0; j<OW_ROMCODE_SIZE; j++) newSensors[index].id[j]=sensors[i].id[j];
            index++;
        }
    }

    return (found<<8|index);
}

uint8_t readTempSensor(uint8_t index,int16_t * retVal)
{

    if(index>=MAX_SENSORS || tempSensors[index].connected == FALSE) return DS18X20_ERROR;

    return DS18X20_read_decicelsius(tempSensors[index].id,retVal);

}

uint8_t startTempSensors()
{

    ow_parasite_enable();
    ow_set_bus(&DS_PIN,&DS_PORT,&DS_DDR,DS);

    return DS18X20_start_meas(DS18X20_POWER_PARASITE,NULL);

}

void clearTempSensor(uint8_t index)
{

    memset(&tempSensors[index],0,sizeof(temp_t));
    eeprom_update_block(&tempSensors[index],&ee_tempSensors[index],sizeof(temp_t));

}

void setTempSensor(uint8_t index,uint8_t * id)
{

    if(index>=MAX_SENSORS) return;

    memcpy(tempSensors[index].id,id,OW_ROMCODE_SIZE);
    eeprom_update_block(&tempSensors[index],&ee_tempSensors[index],sizeof(temp_t));

}

uint8_t tempSensorCon(uint8_t index)
{

    if(index<MAX_SENSORS) return tempSensors[index].connected;
    else return index;
}

char * getTempSensorText(uint8_t index)
{

    if(index<MAX_SENSORS) return tempSensors[index].text;
    else return NULL;
}

uint8_t setTempSensorText(uint8_t index, char * text)
{


    if(index<MAX_SENSORS)
    {

        if(strlen(text)<TEMP_TEXT_SIZE)
        {

            strncpy(tempSensors[index].text,text,TEMP_TEXT_SIZE);

            eeprom_update_block(&tempSensors[index],&ee_tempSensors[index],sizeof(temp_t));

            return 1;

        }

    }

    return 0;
}

void initTempSensorsData(void)
{

    memset(&tempSensors,0,sizeof(temp_t)*MAX_SENSORS);

    eeprom_update_block(&tempSensors,&ee_tempSensors,sizeof(temp_t)*MAX_SENSORS);

}

void updateTempSensorsData(void)
{

    eeprom_update_block(&tempSensors,&ee_tempSensors,sizeof(temp_t)*MAX_SENSORS);

}
