/*
 * globales.h
 *
 * Created: 30/05/2018 13:22:15
 *  Author: Ledo
 */ 


#ifndef GLOBALES_H_
#define GLOBALES_H_

//System LED
#define SYS_LED                 PIO_PD31_IDX
#define SYS_LED_MASK            (1<<31)
//RS485 Bus Control
#define RS4_DE_PIN              PIO_PA29_IDX
#define RS4_DE_PIN_MASK         (1<<29)
//Transistors Digital Outputs
#define OUT0_PIN                PIO_PA0_IDX
#define OUT0_PIN_MASK           (1<<0)
#define OUT1_PIN                PIO_PA1_IDX
#define OUT1_PIN_MASK           (1<<1)
#define OUT2_PIN                PIO_PA2_IDX
#define OUT2_PIN_MASK           (1<<2)

//EQM402B Control Signals (J15-3 and J15-4)
#define MOTOR_CNTRL_PIN         PIO_PA16_IDX
#define MOTOR_CNTRL_PIN_MASK    (1<<16)
#define BRAKE_CNTRL_PIN         PIO_PA25_IDX
#define BRAKE_CNTRL_PIN_MASK    (1<<25)


//Memoria Flash serie Winbond
#define WB_P_PIN                PIO_PD23_IDX
#define WB_P_PIN_MASK           (1<<23)
#define WB_CS_PIN               PIO_PA11_IDX
#define WB_CS_PIN_MASK          (1<<11)

//AD7843 TP Controller 
#define AD7843_CS_PIN               PIO_PA9_IDX
#define AD7843_CS_PIN_MASK          (1<<9)
#define AD7843_TP_PEN_PIN           PIO_PA26_IDX
#define AD7843_TP_PEN_PIN_MASK      (1<<26)

//External SPI Chip Select
#define EXT_SPI_CS_PIN               PIO_PA10_IDX
#define EXT_SPI_CS_PIN_MASK          (1<<10)


/*** Display TFT *******************************/
#define DB0_PIN_MASK             (1<<0)
#define DB1_PIN_MASK             (1<<1)
#define DB2_PIN_MASK             (1<<2)
#define DB3_PIN_MASK             (1<<3)
#define DB4_PIN_MASK             (1<<4)
#define DB5_PIN_MASK             (1<<5)
#define DB6_PIN_MASK             (1<<6)
#define DB7_PIN_MASK             (1<<7)
#define DB8_PIN_MASK             (1<<8)
#define DB9_PIN_MASK             (1<<9)
#define DB10_PIN_MASK            (1<<10)
#define DB11_PIN_MASK            (1<<11)
#define DB12_PIN_MASK            (1<<12)
#define DB13_PIN_MASK            (1<<13)
#define DB14_PIN_MASK            (1<<14)
#define DB15_PIN_MASK            (1<<15)
#define DB16_PIN_MASK            (1<<16)
#define DB17_PIN_MASK            (1<<17)
#define TFT_DC_PIN_MASK          (1<<18)
#define TFT_CS_PIN_MASK          (1<<19)
#define TFT_RD_PIN_MASK          (1<<21)
#define TFT_WR_PIN_MASK          (1<<22)
#define TFT_IM0_PIN_MASK         (1<<25)
#define TFT_IM1_PIN_MASK         (1<<26)
#define TFT_BKL_PIN_MASK         (1<<27)
#define TFT_RST_PIN_MASK         (1<<28)


//*********************************
// COLORS
//*********************************
// VGA color palette
#define VGA_BLACK		0x0000
#define VGA_WHITE		0xFFFF
#define VGA_RED			0xF800
#define VGA_GREEN		0x0400
#define VGA_BLUE		0x001F
#define VGA_SILVER		0xC618
#define VGA_GRAY		0x8410
#define VGA_MAROON		0x8000
#define VGA_YELLOW		0xFFE0
#define VGA_OLIVE		0x8400
#define VGA_LIME		0x07E0
#define VGA_AQUA		0x07FF
#define VGA_TEAL		0x0410
#define VGA_NAVY		0x0010
#define VGA_FUCHSIA		0xF81F
#define VGA_PURPLE		0x8010
#define VGA_TRANSPARENT	0xFFFFFFFF



#define RS4_BUFFER_LENGHT      64
extern uint8_t rs4_rxbuffer[RS4_BUFFER_LENGHT];
extern uint8_t rs4_txbuffer[RS4_BUFFER_LENGHT];
extern uint8_t rs4_rx_index;
extern bool rs4_datos_listos;

/**************************************************************
* MACROS
**************************************************************/
#define MOTOR_ON() pio_set_pin_high(MOTOR_CNTRL_PIN);
#define MOTOR_OFF() pio_set_pin_low(MOTOR_CNTRL_PIN);
#define BRAKE_ON() pio_set_pin_high(BRAKE_CNTRL_PIN);
#define BRAKE_OFF() pio_set_pin_low(BRAKE_CNTRL_PIN);

typedef enum FONTYPE
{
	font8x12,
	font16x16,
	font7seg,
	font48,
	font7_96_144
}FONTYPE;

typedef struct _current_font
{
	uint8_t* font;
	uint16_t x_size;
	uint16_t y_size;
	uint16_t offset;
	uint16_t numchars;
}_current_font;
extern _current_font cfont;


/// Structure to store time and date
typedef struct m41t00_time_t
{
	uint8_t sec;            ///< Seconds:       0  to 59
	uint8_t min;            ///< Minutes:       0  to 59
	uint8_t hour;           ///< Hours:         0  to 23
	uint8_t day_of_week;    ///< Day of Week:   1  to 7
	uint8_t day_of_month;   ///< Days of Month: 1  to 31 (depending on month)
	uint8_t month;          ///< Months:        1  to 12
	uint8_t year;           ///< Years:         00 to 99
	uint8_t creg;
} m41t00_time_t;

extern m41t00_time_t decoded_time;
extern m41t00_time_t time;

typedef struct ENCODER
{
	volatile bool direction;
	volatile uint8_t pulse_counter;
	volatile bool pulse;
	
	}ENCODER;
extern ENCODER Encoder;	

typedef struct FLAGS
{
	volatile bool sys_led_toogle;
	bool bitmap;
	}FLAGS;
extern FLAGS Flags;

typedef enum CAN_MOTOR_CMD
{
	Motor_On = 1,
	Motor_Off,
	Rpm_set,
	Power_set,
	Dir_left,
	Dir_right,
	Brake_On,
	Brake_Off,
	Set_params,
	Read_vars
}CAN_MOTOR_CMD;

typedef enum CAN_COOLER_CMD
{
	Set_Kp_A = 1,
	Set_Ki_A,
	Set_Kd_A,
	Enable_disable_A,
	Set_T_setpoint_A,
	Set_Kp_B,
	Set_Ki_B,
	Set_Kd_B,
	Enable_disable_B,
	Set_T_setpoint_B,
	Set_Kp_C,
	Set_Ki_C,
	Set_Kd_C,
	Enable_disable_C,
	Set_T_setpoint_C,
	Enable_all,
	Disable_all,
	Leer_T,
	Leer_Set,
	LeerPID_A,
	LeerPID_B,
	LeerPID_C
	
}CAN_COOLER_CMD;

typedef enum CAN_IO_CMD
{
	Outs_set = 1,
	Outs_clr,
	Outs_update_all,
	Read_IO_vars
}CAN_IO_CMD;

/* Mapeado de las entradas digitales del módulo CAN_IO*/
typedef enum INPUTS
{
	CAN_INPUT0 = (1 << 0),
	CAN_INPUT1 = (1 << 1),
	CAN_INPUT2 = (1 << 2),
	CAN_INPUT3 = (1 << 3),
	CAN_INPUT4 = (1 << 4),
	CAN_INPUT5 = (1 << 5),
	CAN_INPUT6 = (1 << 6),
	CAN_INPUT7 = (1 << 7),
	CAN_INPUT8 = (1 << 8),
	CAN_INPUT9 = (1 << 9),
	CAN_INPUT10 = (1 << 10),
	CAN_INPUT11 = (1 << 11),
	CAN_INPUT12 = (1 << 12),
	CAN_INPUT13 = (1 << 13),
	CAN_INPUT14 = (1 << 14),
	CAN_INPUT15 = (1 << 15)
	
}INPUTS;

/* Mapeado de las salidas digitales del módulo CAN_IO*/
typedef enum OUTS
{
	CAN_OUTPUT0 = (1 << 0),
	CAN_OUTPUT1 = (1 << 1),
	CAN_OUTPUT2 = (1 << 2),
	CAN_OUTPUT3 = (1 << 3),
	CAN_OUTPUT4 = (1 << 4),
	CAN_OUTPUT5 = (1 << 5),
	CAN_OUTPUT6 = (1 << 6),
	CAN_OUTPUT7 = (1 << 7),
	CAN_OUTPUT8 = (1 << 8),
	CAN_OUTPUT9 = (1 << 9),
	CAN_OUTPUT10 = (1 << 10),
	CAN_OUTPUT11 = (1 << 11),
	CAN_OUTPUT12 = (1 << 12),
	CAN_OUTPUT13 = (1 << 13),
	CAN_OUTPUT14 = (1 << 14),
	CAN_OUTPUT15 = (1 << 15),
	CAN_OUTPUT16 = (1 << 16),
	CAN_OUTPUT17 = (1 << 17)
	
}OUTPUTS;

/* Union para convertir Int32 to byte array */
typedef union UL2B
{
	uint32_t data32;
	uint8_t bytes[4];
}UL2B;
extern  UL2B ul2byte;
extern  UL2B ul2byteH;

typedef struct INPUTS_STATE
{
	bool bCAN_IN0;
	bool bCAN_IN1;
	bool bCAN_IN2;
	bool bCAN_IN3;
	bool bCAN_IN4;
	bool bCAN_IN5;
	bool bCAN_IN6;
	bool bCAN_IN7;
	bool bCAN_IN8;
	bool bCAN_IN9;
	bool bCAN_IN10;
	bool bCAN_IN11;
	bool bCAN_IN12;
	bool bCAN_IN13;
	bool bCAN_IN14;
	bool bCAN_IN15;
	
	uint8_t PuertoA;
	uint8_t PuertoB;

}INPUTS_STATE;
extern volatile INPUTS_STATE InputsState;

typedef struct OUTPUTS_STATE
{
	bool bCAN_OUT0;
	bool bCAN_OUT1;
	bool bCAN_OUT2;
	bool bCAN_OUT3;
	bool bCAN_OUT4;
	bool bCAN_OUT5;
	bool bCAN_OUT6;
	bool bCAN_OUT7;
	bool bCAN_OUT8;
	bool bCAN_OUT9;
	bool bCAN_OUT10;
	bool bCAN_OUT11;
	bool bCAN_OUT12;
	bool bCAN_OUT13;
	bool bCAN_OUT14;
	bool bCAN_OUT15;
	bool bCAN_OUT16;
	bool bCAN_OUT17;
	
	uint8_t PuertoC;
	uint8_t PuertoD;
	uint8_t PuertoR;
	
}OUTPUTS_STATE;
extern OUTPUTS_STATE OutsStateSet;
extern volatile OUTPUTS_STATE OutsStateRead;

typedef struct MOTOR
{
	bool Enable;
	bool Brake;
	bool Dir;
	volatile uint8_t Speed;
	uint8_t Power;
	uint16_t T;
	bool Hall;
	bool CanBus_Ok;
}MOTOR;

extern MOTOR MotorRead;

typedef struct COOLERS
{
	bool enable;
	uint16_t Kp;
	uint16_t Ki;
	uint16_t Kd;
	int16_t Tset;//ºC x 100
	int16_t Tactual; //ºC x 100
	bool CanBus_Ok;
}COOLERS;

extern COOLERS ReactorRead;
extern COOLERS IntercambiadorRead;
extern COOLERS SecadorRead;
extern const uint8_t array[85800];
extern uint16_t adc_value;

extern uint16_t  POINT_COLOR;
extern uint16_t  BACK_COLOR;

extern void    twi0_init_m41t00(void);
extern void    twi_read_rtc(void);
extern void    Time_adjust(uint8_t min,uint8_t hora, uint8_t dia);
extern void    configure_usart0(void);
extern void    Usart0_sendBytes(uint8_t bytes);

extern void CAN_Loop(void);
extern void Can_init(void);
extern void send_cmd2_motor(CAN_MOTOR_CMD cmd,uint8_t data);
extern void send_cmd2_cooler(CAN_COOLER_CMD cmd,uint16_t data);
extern void send_cmd2_IO(CAN_IO_CMD cmd, uint32_t mask);
extern uint32_t Salidas(void);

extern void LCD_Init(void);
extern void DrawTestPage(uint8_t *str);
extern void NT35510_printChar(uint8_t c, int x, int y,uint16_t fc, uint16_t bc);
extern void NT35510_print(char *st, int x, int y, int deg, uint16_t fc, uint16_t bc);
extern void NT35510_printNumI(long num, int x, int y, int length, char filler, uint16_t fc, uint16_t bc);
extern void NT35510_printNumF(double num, uint8_t lenght, uint8_t dec, char filler, int x, int y, uint16_t fc, uint16_t bc);
extern void NT35510_drawBitmap_from_w25q(int x, int y, int sx, int sy,uint32_t p, int scale);
extern void LCD_ShowNum(uint16_t x,uint16_t y,uint32_t num,uint8_t len,uint8_t Size);
extern void LCD_setFont(FONTYPE fontype);
#endif /* GLOBALES_H_ */