//----------------------------------------------------
/**
	\file		temp_ds18b20.h
	\ingroup	hardware
	\ingroup	all

	\brief		Ovladac teplomeru DS18B20
				<<< ALL >>>

	\author		ConTeK s.r.o., Bc. Stanek Jan
	\version	01.10
	\date		24.04.2013
*/
//----------------------------------------------------
#ifndef _DS18B20_H_
#define _DS18B20_H_

#include "hal.h"
#include <stmachine.h>
#include "onewire.h"


#define DS_DEVICE_CNT_MAX	PARAM_DS18B20_MAX_COUNT		//!< Nejvyssi pocet spravovanych zarizeni

//----------------------------------------------------
//!	\name	Definice prikazu
//@{

	//Rom Commands
#define DS_READ_ROM 	OW_READ_ROM
#define DS_MATCH_ROM	OW_MATCH_ROM
#define DS_SKIP_ROM		OW_SKIP_ROM
	//Temperature commands
#define DS_CONVERT	0x44	//!< Convert temperature (result in scratchPad)
	//Scratchpad commands
#define	DS_READ		0xBE	//!< Read from scratchPad
#define	DS_WRITE	0x4E	//!< Write to scratchPad
	//EEPROM commands
#define DS_COPY		0x48	//!< Save stcratchPad to EEPROM
#define DS_RECALL	0xB8	//!< Load EEPROM to scratchPad
	//Other commands
#define DS_READ_PWR	0xB4	//!< Read supply mode
//@}

#define DS_FAMILY_ID 	0x28	//!< Device family = DS18B20

//----------------------------------------------------
//!	\name	Definice casovani AD prevodu [ms]
//@{
//! \note	Dle rozliseni prevodu
#define DS_CONV_TIME_12		750		//!< Cas prevodu v rozliseni 12 bitu
#define DS_CONV_TIME_11		375		//!< Cas prevodu v rozliseni 11 bitu
#define DS_CONV_TIME_10		188		//!< Cas prevodu v rozliseni 10 bitu
#define DS_CONV_TIME_09		94		//!< Cas prevodu v rozliseni  9 bitu
//@}

//----------------------------------------------------
//!	\name	Definice konfiguracniho registru
//@{
#define getDsConfig(_val) ((_val << 5) | 0x1F)	//!< Slozeni konfiguracniho slova

#define DS_RES_12_BITS		0x03	//!< Nastaveni rozliseni 12 bitu
#define DS_RES_11_BITS		0x02	//!< Nastaveni rozliseni 11 bitu
#define DS_RES_10_BITS		0x01	//!< Nastaveni rozliseni 10 bitu
#define DS_RES_09_BITS		0x00	//!< Nastaveni rozliseni  9 bitu
//@}

extern uint8_t owCRC8_table[256];	//!< CRC Tabulka (Dallas OneWire polynom)

//----------------------------------------------------
/**
	\brief  Kontext teplomeru DS18B20
*/
//----------------------------------------------------
typedef struct _DS18B20_CTX
{
	ONE_WIRE_DEVICE	*	dev;		//!< Interpretace zarizeni na sbernici
	int16_t				value;		//!< Hodnota ziskana ze zarizeni
}DS18B20_CTX;


struct _TEMPSENS_CTX;
//----------------------------------------------------
/**
	\brief  Definice rozhrani pro pristup do pameti
*/
//----------------------------------------------------
typedef struct _DS18B20_IFACE
{
	void (*getId)(struct _TEMPSENS_CTX * _ctx, uint8_t _index);		//!< Nacte ID z pameti do kontextu
	void (*saveId)(struct _TEMPSENS_CTX * _ctx, uint8_t _index);	//!< Ulozi ID z kontextu do pameti
	void (*setPresence)(uint8_t _index, bool_t _value);				//!< Nastavi priznak pritomnosti ulozeneho zarizeni
}DS18B20_IFACE;

//----------------------------------------------------
/**
	\brief  Kontext mereni teploty po 1-wire
*/
//----------------------------------------------------
typedef struct _TEMPSENS_CTX
{
	HAL_CTX		Parent;			//!< Rodic vrstvy

	STMACHINE	Stm;			//!< Stavovy automat mereni teplot

	uint8_t	actualID[8];		//!< Pole pro datovou vymenu s pameti

	DS18B20_CTX	sens[DS_DEVICE_CNT_MAX];	//!< Pole zpracovavanych teplomeru
	DS18B20_IFACE IFace;					//!< Pristup do pameti
	ONE_WIRE_CTX *	bus;					//!< Odkaz na kontext sbernice
	ONE_WIRE_CTX *	bus_tmp;				//!< Pomocna promenna (retezeni sbernic)

	uint8_t iterIdx;		//!< Pomocna promenna pri obsluze teplomeru
	bool_t	running;		//!< Priznak inicializace

}TEMPSENS_CTX;



//----------------------------------------------------
/**
	\brief  Inicializace teplomeru na 1-wire sbernici
	\param	_ctx	Kontext mereni teploty s DS18B20
	\param	_host	Pridruzena 1-wire sbernice (prvni polozka spojoveho seznamu)
	\return TRUE	Pokud inicializace probehla v poradku
*/
//----------------------------------------------------
bool_t DsTempSensInitialize(TEMPSENS_CTX * _ctx, ONE_WIRE_CTX * _host);


#endif //_DS18B20_H_
