/*
 * globales.h
 *
 * Created: 19/11/2016 11:00:15
 * Definición de tipos
 *  Author: José Emilio Ledo
 */ 


#ifndef GLOBALES_H_
#define GLOBALES_H_
#include "gpio.h"

#define LED_0         AVR32_PIN_PD21
#define LED_1         AVR32_PIN_PD27
#define LED_2         AVR32_PIN_PD28
#define LED_3         AVR32_PIN_PD29
#define LED_4         AVR32_PIN_PD30

#define MODE_KEY      AVR32_PIN_PC02
#define UP_KEY        AVR32_PIN_PC03
#define OK_KEY        AVR32_PIN_PC04
#define DWN_KEY       AVR32_PIN_PC05

#define PWM           AVR32_PIN_PC18
#define MUTE          AVR32_PIN_PD14

#define ADC_REF       3.0F
#define ADC_OFFSET    10
#define ADC_OFFSET_G2    20

/**************************************************************
* Comandos desde el PC ****************************************
***************************************************************/
#define SET_FREQ                 1
#define SET_DUTY                 2
#define SET_MAX_I                3
#define SET_MAX_T                4
#define SET_DELAY                5
#define SET_SAMPLES              6
#define SET_VOLUME               7
#define SET_ADC_GAIN             8

/**********************************************************
*Definicion de macros***************************
***********************************************************/
#define LED0_On()       gpio_set_pin_low(LED_0);
#define LED0_Off()      gpio_set_pin_high(LED_0);

#define LED1_On()       gpio_set_pin_low(LED_1);
#define LED1_Off()      gpio_set_pin_high(LED_1);

#define LED2_On()       gpio_set_pin_low(LED_2);
#define LED2_Off()      gpio_set_pin_high(LED_2);

#define LED3_On()       gpio_set_pin_low(LED_3);
#define LED3_Off()      gpio_set_pin_high(LED_3);

#define LED4_On()       gpio_set_pin_low(LED_4);
#define LED4_Off()      gpio_set_pin_high(LED_4);

#define MUTE_On()       gpio_set_pin_high(MUTE);
#define MUTE_Off()      gpio_set_pin_low(MUTE);

#define Mosfet_On()     gpio_set_pin_high(PWM);
#define Mosfet_Off()    gpio_set_pin_low(PWM);

#define KeyMode()       (gpio_pin_is_low(MODE_KEY))
#define KeyUp()         gpio_pin_is_low(UP_KEY)
#define KeyOk()         gpio_pin_is_low(OK_KEY)
#define KeyDwn()        gpio_pin_is_low(DWN_KEY)

extern uint8_t lcd_buffer_H[16];
extern uint8_t lcd_buffer_L[16];
extern uint8_t v_buffer_index;
extern uint16_t i_buffer_index;

typedef enum PWM_FREQ
{
	Hz_200,
	Hz_400,
	Hz_600,
	Hz_800
	}PWM_FREQ;
	
	
typedef enum PWM_DUTY
{
	Duty_10,
	Duty_20,
	Duty_30,
	Duty_40
	}PWM_DUTY;	
	
	

typedef struct bcd
{
	uint8_t dec_miles;
	uint8_t miles;
	uint8_t centenas;
	uint8_t decenas;
	uint8_t unidades;
}bcd;

extern bcd digitos;


typedef struct PARAMETROS
{
	PWM_FREQ pwmFreq;
	PWM_DUTY pwmDuty;
	uint16_t max_I;       //corriente maxima del mosfet
	uint16_t max_T;       //temperatura maxima del mosfet
	uint8_t sampleDelay; //retardo antes de muestrear la señal
	uint8_t samples;     //numero de lecturas por periodo
	uint8_t  volume;      //volume del sonido  (0....10)
	uint8_t adc_gain;
	uint8_t eep_writed;
	
	
}PARAMETROS;
extern PARAMETROS Params;

typedef struct FLAGS
{
	bool usb_datos_listos;
	bool adc_sample;
	bool muestrearV;
	bool dac_update;
	bool lcd_menu;
	bool visualizar;
	bool calcularI;
	
}FLAGS;

extern FLAGS flags;
extern float V;
extern int16_t I;
extern float T;
extern int16_t iArea;


//Convierte entero de 16 bits en caracteres ascii
extern void int_to_bcd(int16_t);
//Convierte byte de 8 bits en caracteres ascii
extern void char_to_bcd(uint8_t );
extern void send_to_lcd(char cmd);
extern void waves_init();
extern void Mosfet_gate_pulse_Off(void);
extern void Mosfet_gate_pulse_update(void);


#endif /* GLOBALES_H_ */