/*!
 * \file hal.h
 *
 * Author: Christos Choutouridis AEM: 8997
 * email : <cchoutou@ece.auth.gr>
 *
 */
#ifndef DRIVERS_HAL_H_
#define DRIVERS_HAL_H_

#include <thermostat_shield.h>
#include <alcd.h>
#include <jiffies.h>
#include <onewire_uart.h>
#include <deque08.h>
#include <thermostat.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif


int hal_hw_init (void);

/*
 * ========== LCD ===========
 */
void lcd_init (void);
void lcd_enable (uint8_t en) ;
int lcd_putchar (char c);
int lcd_puts (const char *s);

/*
 * ========== Led interface ==========
 */
void led_red (uint8_t en);
void led_green (uint8_t en);


/*
 * ========= Relay ===========
 */
void relay(uint8_t on);

/*
 * ========= Temperature ===========
 */
// OneWire commands
#define SKIPROM         0xCC  // Skip ROM matching transition
#define STARTCONV       0x44  // Tells device to take a temperature reading and put it on the scratchpad
#define COPYSCRATCH     0x48  // Copy EEPROM
#define READSCRATCH     0xBE  // Read EEPROM
#define WRITESCRATCH    0x4E  // Write to EEPROM
#define RECALLSCRATCH   0xB8  // Reload from last known
#define READPOWERSUPPLY 0xB4  // Determine if device needs parasite power
#define ALARMSEARCH     0xEC  // Query bus for devices with an alarm condition

// Device resolution
#define TEMP_9_BIT  0x1F //  9 bit
#define TEMP_10_BIT 0x3F // 10 bit
#define TEMP_11_BIT 0x5F // 11 bit
#define TEMP_12_BIT 0x7F // 12 bit
#define IS_TEMP_RESOLUTION(x) \
   ((x == TEMP_9_BIT) || (x == TEMP_10_BIT) || (x == TEMP_11_BIT) || (x == TEMP_12_BIT))

int temp_init (uint8_t resolution);
float temp_read (void);

/*
 * ========= Proximity ===========
 */
#define PROX_TIME_MAX      25    // [msec]
#define PROX_READINGS      7     // How many readings for averaging
#define PROX_MAX_DISTANSE  450   // [cm]

typedef struct {
   float_t  readings[7];
   int      iter;
} proximity_t;

void proximity_init (proximity_t* p);
float_t proximity (proximity_t* p);


/*
 * Public data types / classes
 */
extern alcd_t        alcd;
extern ow_uart_t     ow;
extern proximity_t   prox;

#ifdef __cplusplus
}
#endif

#endif /* DRIVERS_HAL_H_ */
