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

#ifndef INC_TEMPERATURE_CONTROL_AND_MONITORING_SYSTEM_H_
#define INC_TEMPERATURE_CONTROL_AND_MONITORING_SYSTEM_H_

#endif /* INC_TEMPERATURE_CONTROL_AND_MONITORING_SYSTEM_H_ */

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


////////////////////////var definitions//////////////////////////////
//////////debug///////////
extern UART_HandleTypeDef huart2;
char buff[60];

//I2C Addresses of thermometers						 A2 A1 A0
#define TEMP_IN_UP_ADR		(uint8_t) 0x18<<1		// 0 0 0
#define TEMP_IN_DWN_ADR		(uint8_t) 0x1F<<1		// 1 1 1
#define TEMP_AMB_ADR	 	(uint8_t) 0x19<<1		// 0 0 1

////////flash memory handle////////
#define START_ADR  (uint32_t) 0x0801F800 //page 64
#define EMPTY_CELL (uint32_t) 0xFFFFFFFF

//////////menu//////////////
#define STATE_MAX 			(uint8_t) 2
#define MENU_COUNTDOWN_MAX 	(uint8_t) 15*4
temperature_t Tset_tmp;
temperature_t Hys_up_tmp;
temperature_t Hys_dwn_tmp;
int state;
uint16_t menu_countdown;
uint8_t change_flag;
uint8_t set_flag;

/////measured temperatures/////
typedef int32_t temperature_t;
#define TEMP_IN_UP_INDEX (uint8_t) 0
#define TEMP_IN_DWN_INDEX (uint8_t) 1
#define TEMP_IN_INDEX (uint8_t) 2
#define TEMP_AMB_INDEX (uint8_t) 3
temperature_t Temperatures[4];

////////////settings///////////
#define TEMP_SCALE	(uint16_t) 100
#define T_SET_DEF (temperature_t) 29*TEMP_SCALE
#define HYS_UP_DEF (temperature_t) 1*TEMP_SCALE
#define HYS_DWN_DEF (temperature_t) 1*TEMP_SCALE
#define  TEMP_SET_INDEX	0
#define  HYS_UP_INDEX	1
#define  HYS_DWN_INDEX	2
temperature_t T_Settings[3];


///////regulation//////////
uint8_t action_flag;

///action elements state control///
extern ADC_HandleTypeDef hadc1;
extern ADC_HandleTypeDef hadc2;
#define CURRENT_SCALE 1000
#define OFFSET 150
int tmp;
int raw_cooler;
int raw_heater;
int32_t U_heater;
int32_t I_heater;
int32_t U_cooler;
int32_t I_cooler;
uint8_t heater_state;
uint8_t cooler_state;

///////data logging///////////
extern UART_HandleTypeDef huart1;
#define SERVER_URL "/Temperature_control_and_monitoring_system/give_data?"


//////////SSID, PASSWORD, AND SERVER IP change here/////////////
#define SSID "SSID"
#define PASSWORD "PASSWORD"
#define SERVER_IP "192.168.1.166"


#define SERVER_PORT 5000
char data_out[200];

/////////////////////functions definitions/////////////////////////////
/**
 * based on settings, regulates temperature inside regulated area
 * @param temperature inside regulated area
 */
void Temp_regulation(temperature_t Temp_in);

/**
 * sets display, so that temperatures can be displayed
 */
void Set_temp_output();

/**
 * saves program settings on to last page of FLASH memory
 * @param pointer on array inside which temperature settings are stored
 */
void write_flash(temperature_t *T_settings);

/**
 * gets value of desired setting from FLASH memory
 * @param index of desired setting value
 * @return value of desired setting
 */
temperature_t read_flash(uint8_t value_sel);

/**
 * initializes wifi module and establishes connection with AP
 */
void wifi_init();

/**
 * takes all given values, temperature settings from flash and state of door and
 * seds it to server to log
 * @param pointer on array inside which currently measured temperatures are stored
 * @param measured current flowing through heater
 * @param measured current flowing through cooler
 */
void wifi_send_data(temperature_t *Temperatures,
					int32_t I_heater,
					int32_t I_cooler);

/**
 * turns on error LED, displays error message on display, and send it to debug UART,
 * then reinitializes display, for temperature displaying
 * @param number of error message (0-9)
 */
void error(uint8_t error);

/**
 * initializes all modules, checks temperature settings in FLASH memory, and sets
 * display for temperature displaying
 */
void Temperature_control_and_monitoring_system_init();

/**
 * executes the main Temperature control and monitoring system routine
 */
void Temperature_control_and_monitoring_system();
