/**
  ******************************************************************************
  * @file    ds18b20_driver.h
  * @brief   This file contains all the constants parameters for the DS18B20
  * 		 1-Wire Digital Thermometer
  ******************************************************************************
  * @attention
  * Usage:
  *		Uncomment LL Driver for HAL driver
  *
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef DS18B20_H
#define DS18B20_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "onewire.h"

/* I/O Port ------------------------------------------------------------------*/
#define DS_Pin        GPIO_PIN_0
#define DS_GPIO_Port  GPIOB

/* Data Structure ------------------------------------------------------------*/
#define DS18B20_DEVICE_AMOUNT		30

/* Register ------------------------------------------------------------------*/
#define DS18B20_CMD_CONVERT				    0x44
#define DS18B20_CMD_ALARM_SEARCH		  0xEC
#define DS18B20_CMD_READSCRATCHPAD		0xBE
#define DS18B20_CMD_WRITESCRATCHPAD		0x4E
#define DS18B20_CMD_COPYSCRATCHPAD		0x48
/* Data Structure ------------------------------------------------------------*/
#define DS18B20_FAMILY_CODE				0x28


#define DS18B20_SERIAL_NUMBER_LEN_BYTES                                    6
#define DS18B20_SERIAL_NUMBER_OFFSET_BYTES                                 1

#define DS18B20_SCRATCHPAD_T_LSB_BYTE_IDX                                  0
#define DS18B20_SCRATCHPAD_T_MSB_BYTE_IDX                                  1
#define DS18B20_SCRATCHPAD_T_LIMIT_H_BYTE_IDX                              2
#define DS18B20_SCRATCHPAD_T_LIMIT_L_BYTE_IDX                              3
#define DS18B20_SCRATCHPAD_CONFIG_BYTE_IDX                                 4
#define DS18B20_SCRATCHPAD_USER_BYTE_3_IDX                                 6
#define DS18B20_SCRATCHPAD_USER_BYTE_4_IDX                                 7
#define DS18B20_SCRATCHPAD_CRC_IDX                                         8

/* Bits locations for resolution */
#define DS18B20_RESOLUTION_R1			6
#define DS18B20_RESOLUTION_R0			5

#define DS18B20_DECIMAL_STEP_12BIT		0.0625
#define DS18B20_DECIMAL_STEP_11BIT		0.125
#define DS18B20_DECIMAL_STEP_10BIT		0.25
#define DS18B20_DECIMAL_STEP_9BIT		  0.5

#define DS18B20_DELAY_MS_9_BITS                                            94
#define DS18B20_DELAY_MS_10_BITS                                           188
#define DS18B20_DELAY_MS_11_BITS                                           375
#define DS18B20_DELAY_MS_12_BITS                                           750
#define DS18B20_DELAY_MS_MAX                                               DS18B20_DELAY_MS_12_BITS


/* DS18B20 Resolutions */
typedef enum {
	DS18B20_RESOLUTION_9BITS	= 0x1F,
	DS18B20_RESOLUTION_10BITS	= 0x3F,
	DS18B20_RESOLUTION_11BITS	= 0x5F,
	DS18B20_RESOLUTION_12BITS	= 0x7F
} DS18B20_Res_t;

typedef struct
{
	uint8_t 		  DevAddr[DS18B20_DEVICE_AMOUNT][8];
} DS18B20_Drv_t;
extern DS18B20_Drv_t *DS;;
extern OneWire_t OW;

/* External Function ---------------------------------------------------------*/
HAL_StatusTypeDef DS18B20_Search(DS18B20_Drv_t *DS, OneWire_t *OW);
HAL_StatusTypeDef DS18B20_StartConvT(OneWire_t* OW, uint8_t *ROM);
HAL_StatusTypeDef DS18B20_StartConvTAll(OneWire_t* OW);
HAL_StatusTypeDef DS18B20_CalcTemperature(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad, float *destination);
HAL_StatusTypeDef DS18B20_ReadScratchpad(OneWire_t* OW, uint8_t *ROM, uint8_t *Scratchpad);
HAL_StatusTypeDef DS18B20_WaitForEndConvertion(OneWire_t* OW);
HAL_StatusTypeDef DS18B20_WaitForEndConvertion_NonBlocking(OneWire_t* OW);
HAL_StatusTypeDef DS18B20_SetTempAlarm(OneWire_t* OW, uint8_t *ROM, int8_t Low,
		int8_t High);
HAL_StatusTypeDef DS18B20_WriteUserBytes(OneWire_t* OW, uint8_t *ROM, int16_t UserBytes12,
		int16_t UserBytes34, uint8_t UserBytesMask);
uint8_t DS18B20_AlarmSearch(DS18B20_Drv_t *DS, OneWire_t* OW);

HAL_StatusTypeDef DS18B20_SetResolution(OneWire_t* OW, uint8_t *ROM,
		DS18B20_Res_t Resolution);
HAL_StatusTypeDef DS18B20_IsValidAddress(uint8_t *ROM);
#ifdef __cplusplus
}
#endif

#endif /* DS18B20_H */
