#ifndef _ds2418_H_
#define _ds2418_H_

	#include "main.h"
 // #include "stm32f4xx_hal_i2c.h"
/* DS2482 DRIVER **************************************************************************************************************************************/
	#define POLL_LIMIT		0x30	// 0x30 is the minimum poll limit

	#define DS2482_CMD_DRST		0xF0	// Device Reset
	#define DS2482_CMD_SRP		0xE1	// Set Read Pointer
	#define DS2482_CMD_WCFG		0xD2	// Write Configuration
	#define DS2482_CMD_1WRS		0xB4	// 1-Wire Reset
	#define DS2482_CMD_1WWB		0xA5	// 1-Wire Write Byte
	#define DS2482_CMD_1WRB		0x96	// 1-Wire Read Byte
	#define DS2482_CMD_1WSB		0x87	// 1-Wire Single Bit
	#define DS2482_CMD_1WT		0x78	// 1-Wire Triplet

	#define DS2482_STATUS_1WB	0x01	// Status 1-Wire Busy
	#define DS2482_STATUS_PPD	0x02	// Status Presence Pulse Detect
	#define DS2482_STATUS_SD	0x04	// Status Short Detected
	#define DS2482_STATUS_LL	0x08	// Status 1-Wire Logic Level
	#define DS2482_STATUS_RST	0x10	// Status Device Reset
	#define DS2482_STATUS_SBR	0x20	// Status Single Bit Result
	#define DS2482_STATUS_TSB	0x40	// Status Triplet Second Bit
	#define DS2482_STATUS_DIR	0x80	// Status Branch Direction Taken

	#define DS2482_CFG_APU		0x01	// Config Active Pull-Up
	#define DS2482_CFG_SPU		0x04	// Config Strong Pull-Up
	#define DS2482_CFG_1WS		0x08	// Config 1-Wire Speed

	#define DS2482_READPTR_SR	0xF0	// Status Register
	#define DS2482_READPTR_RDR	0xE1	// Read Data Register
	#define DS2482_READPTR_CSR	0xD2	// Channel Selection Register
	#define DS2482_READPTR_CR	0xC3	// Configuration Register
	
	/* OW DRIVER **************************************************************************************************************************************/
	#define OW_MATCH_ROM			0x55
	#define OW_SKIP_ROM				0xCC
	#define OW_SEARCH_ROM			0xF0
	#define OW_CONVERT_T			0x44
	#define OW_READ_SCRATCHPAD 0xBE

	#define OW_SEARCH_FIRST		0xFF	// start new search
	#define OW_PRESENCE_ERR		0xFF
	#define OW_DATA_ERR				0xFE
	#define OW_LAST_DEVICE		0x00	// last device found

	#define OW_ROMCODE_SIZE		8
	
	#define OW_CONVERT_T		0x44
	#define	OW_DEVICE_COUNT		9

	/* OW CRC **************************************************************************************************************************************/
	#define CRC8INIT    0x00
	#define CRC8POLY    0x18              //0X18 = X^8+X^5+X^4+X^0
	
	extern uint8_t OW_SEARCH_BUFFER[OW_DEVICE_COUNT][8];
	
	uint8_t ds2482_detect(I2C_HandleTypeDef *hi2c);  
	uint8_t ds2482_reset(I2C_HandleTypeDef *hi2c);
	uint8_t ds2482_write_config(I2C_HandleTypeDef *hi2c, uint8_t config);
	
	uint8_t OW_Reset(I2C_HandleTypeDef *hi2c); 
	uint8_t OW_WriteByte(I2C_HandleTypeDef *hi2c, uint8_t SEND_BYTE);
	uint8_t OW_TouchBit(I2C_HandleTypeDef *hi2c, uint8_t SEND_BYTE);
	uint8_t OW_ReadByte(I2C_HandleTypeDef *hi2c);
	void OW_WriteBit(I2C_HandleTypeDef *hi2c, uint8_t SEND_BYTE);
	
	static void OW_InternCommand(uint8_t command, uint8_t *id, uint8_t reset, I2C_HandleTypeDef *hi2c);
	
	void OW_Search(I2C_HandleTypeDef *hi2c); 
	void OW_Command(uint8_t command, uint8_t *id, uint8_t reset, I2C_HandleTypeDef *hi2c);

	float Read_ds18b20(uint8_t *Serial_Number, I2C_HandleTypeDef *hi2c, float T_Old);
	void ConvertTemperature_ds18b20(I2C_HandleTypeDef *hi2c);
		
	uint8_t crc8( uint8_t* data, uint16_t number_of_bytes_in_data );
  uint8_t OW_Checksum(uint8_t *addr, uint8_t len) ;
	uint8_t OW_CRC_Byte(uint8_t crc, uint8_t Data);
	uint8_t OW_Status(I2C_HandleTypeDef *hi2c); 
	
#endif
