/*
 * MCP9808_handle.h
 *
 *      Author: Martin Mitter
 */

////////////////////////includes/////////////////////////////////////
#include "main.h"
#include <string.h>
#include <stdio.h>
#include "i2c-lcd.h"

////////////////////////var definitions//////////////////////////////
extern I2C_HandleTypeDef hi2c2;

#define REG_TEMP		 	(uint8_t) 0b00000101	// adress of register, which holds the temperature
#define REG_RES				(uint8_t) 0b00001000	// adress of register, used to set the resolution

#define REG_RES_5		 	(uint8_t) 0b00000000	// data for register, used to set the resolution to 0,5 °C/b	res=0
#define REG_RES_25		 	(uint8_t) 0b00000001	// data for register, used to set the resolution to 0,25 °C/b	res=1
#define REG_RES_125		 	(uint8_t) 0b00000010	// data for register, used to set the resolution to 0,125 °C/b	res=2
#define REG_RES_0625		(uint8_t) 0b00000011	// data for register, used to set the resolution to 0,0625 °C/b	res=3

#define TEMP_SCALE	(uint16_t) 100
HAL_StatusTypeDef ret;

typedef int32_t temperature_t;
temperature_t Temperature;
uint8_t flag_zero;
uint8_t buf[2];

#define DEGREE_CELS	"\xDF\x43"
char text[17];

/////////////////////functions definitions/////////////////////////////
/**
 * sets desired temperature resolution of MCP9808 thermometer
 * @param I2C address of thermometer
 * @param chose desired resolution (0-3(finest))
 */
void thermometer_set_res (uint8_t adr, uint8_t res);

/**
 * gets measured temperature from MCP9808 thermometer
 * @param I2C address of thermometer
 * @return measured value in °C, 666*TEMP_SCALE in case of problem
 */
temperature_t get_temp (uint8_t adr);

/**
 * displays error message on display, turn on error LED
 */
void Tx_error();

/**
 * displays error message on display, turn on error LED
 */
void Rx_error();

/**
 * displays temperature value on display in format +XX,XX°C
 * @param temperature value
 * @param chose row of the display to display temperature (0-1)
 * @param chose column of the display from which to start displaying temperature (0-8)
 */
void print_temp(temperature_t Temp, uint8_t row, uint8_t column);
