
#include "includes.h"


extern tUChar inputTable[MAX_SENSORS][INPUT_TABLE_RAM_SIZE];

tUInt findSensor( GUINTRECORD adr );
tUInt AddInputTable(tUInt offset, tUChar* data);
tUInt InitInputTable(void);



// **********************************************************************************
// tabulky sensoru, podle typu zarizeni
// brano z definice prekladu - nyni je to zarizeni EDK4/EHS4
// je potreba aby to bylo univerzalni, dopredu nevime kolik sensoru je
// posledni polozka je rezervovana na ulozeni binarniho stavu pri zpracovani funkce
// **********************************************************************************
/*const unsigned char sensorTable[MAX_SENSORS][SENSOR_ITEMS]  =
 {
	 // PORADI, TYP SENSORU, VELICINA					
	 { SENSOR_POS_VOLTAGE, SENSOR_TYPE_VOLTAGE, SENSOR_CHAN_1, },
	 
	 { SENSOR_POS_TEMPERATURE, SENSOR_TYPE_TEMPERATURE, SENSOR_CHAN_1, },
//	 { SENSOR_POS_TEMPERATURE, SENSOR_TYPE_TEMPERATURE, SENSOR_CHAN_2, },
	 
//	 { SENSOR_POS_WIND, SENSOR_TYPE_WIND, ADDR_IO_WIND_MS, }  
//	 { SENSOR_POS_WIND, SENSOR_TYPE_WIND, ADDR_IO_WIND_KMH, }  

//	 { SENSOR_POS_HUMIDITY, SENSOR_TYPE_HUMIDITY, ADDR_IO_WIND_MS, }  
//	 { SENSOR_POS_HUMIDITY, SENSOR_TYPE_HUMIDITY, ADDR_IO_WIND_KMH, }  

//	 { SENSOR_POS_LUX, SENSOR_TYPE_LUX, ADDR_IO_WIND_MS, }  
//	 { SENSOR_POS_LUX, SENSOR_TYPE_LUX, ADDR_IO_WIND_KMH, }  
	 
	 { NULL, NULL,NULL, }
 };
*/



// **********************************************************************************
// tabulky sensoru, podle typu zarizeni
// brano z definice prekladu - nyni je to zarizeni EDK4/EHS4
// je potreba aby to bylo univerzalni, dopredu nevime kolik sensoru je
// posledni polozka je rezervovana na ulozeni binarniho stavu pri zpracovani funkce
// **********************************************************************************
const unsigned char sensorTable[MAX_SENSORS][SENSOR_ITEMS]  =
 {
	 // PORADI, TYP SENSORU, VELICINA					
	 { SENSOR_POS_VOLTAGE, SENSOR_TYPE_VOLTAGE, SENSOR_CHAN_1, },
#ifndef GR_EXT8
	 { SENSOR_POS_TEMPERATURE, SENSOR_TYPE_TEMPERATURE, SENSOR_CHAN_1, },
#endif
	 { NULL, NULL,NULL, }
 };


// ************************************************************************* 
// findSensor
// overi pritomnost sensoru podle adresz ADDR_MODUL
// ************************************************************************* 
tUInt findSensor( GUINTRECORD adr )
{
  tUChar i;	
  tUInt found;

   
  i = 0;
  found = 0xFF;		// default stav pro nenalezeno
  
  do {
	   if(sensorTable[i][0] == adr.VALUE.HI && sensorTable[i][2] == adr.VALUE.LO)	// sensor je v tabulce
		{
		  found = i;		// nalezeno, vraci offset sensoru, pro nasledny pristup do tabulky inputTable
		   break;
		}
	   i++;
	 }
   while ( sensorTable[i][0] != NULL);		// prochazet do konce tabulky

 return found;		// navratova hodnota
}

// ************************************************************************* 
// AddInputTable
// ulozi nastaveni pro danou adresu sensoru 
// ************************************************************************* 
tUInt AddInputTable(tUInt offset, tUChar* data)
{
  my_memcpy( (tUChar*)&inputTable[offset], (tUChar*)&data[0], 8 );	// ulozit data
  
  // ulozi konfiguraci - obsah inputTable[] do dataflash
  Write_InputConfig();
  
}




// **********************************************************************************
// InitInputTable
// smazani tabulky InputTable, pri inicializaci zarizeni
// *********************************************************************************
tUInt InitInputTable(void)
{
  tUInt i;
  
  // inicializovat tabulku InputTable v RAM
  for (i=0; i<MAX_SENSORS; i++)
   {
   	 my_memset( &inputTable[i][0], 0, INPUT_TABLE_RAM_SIZE);	// smazat cely obsah 
     inputTable[i][FLAG_IO] = FCE_DELTA;					// default je vzdy nastavena DELTA
    
#ifndef GR_EXT8
	 if ( sensorTable[i][0] == SENSOR_POS_TEMPERATURE)
   	   inputTable[i][DELTA] = DEFAULT_DELTA_TEMPERATURE;	// default DELTA pro teplomer
	else
#endif
	  if ( sensorTable[i][0] == SENSOR_POS_VOLTAGE)		
	   inputTable[i][DELTA] = DEFAULT_DELTA_VOLTAGE;		// default DELTA pro VOLTMETR
/*
	 else if ( sensorTable[i][0] == SENSOR_POS_CURRENT)		
	   inputTable[i][DELTA] = DEFAULT_DELTA_CURRENT;		

	 else if ( sensorTable[i][0] == SENSOR_POS_LUXMETER)		
	   inputTable[i][DELTA] = DEFAULT_DELTA_LUX;		

	 else if ( sensorTable[i][0] == SENSOR_POS_WIND)		
	   inputTable[i][DELTA] = DEFAULT_DELTA_WIND_KMH;		

	 else if ( sensorTable[i][0] == SENSOR_POS_HUMIDITY)		
	   inputTable[i][DELTA] = DEFAULT_DELTA_HUMIDITY;		
*/
	
   }
   
   // zapat celou InputTable do DataFlash
   Write_InputConfig();
}

