/******************************************************************************
* File Name		:	LED.c
* Date				: 2021/05/26
* Description	:	
******************************************************************************/
#ifndef _LCD_h_
#define _LCD_h_

/*_____ I N C L U D E S ____________________________________________________*/
#include "sonix_user.h"
#include <SN8F5754.H>

/*_____ D E C L A R A T I O N S ____________________________________________*/

typedef struct
{
  uint8_t sec;             
  uint8_t min;
	uint8_t hour;
	
}ClockTypeDef;
extern ClockTypeDef clock;

typedef struct
{
	u8 port;
  u8 seg1;              
  u8 seg2;
	u8 seg3;
	u8 seg4;
	u8 ledl;
	u8 ledh;
}LEDTypeDef;
extern LEDTypeDef Mix;

extern idata uint8_t r_lcd_buf[];
extern code uint8_t r_table[];
/*_____ D E F I N I T I O N S ______________________________________________*/


//==========================================================================//
//																																					//
//				Modify following Settings for changing PINs layout								//
//																																					//
//==========================================================================//

#define  	MATRIXLED1_IO_INPUT_SCHMITT_OFF		{	P3M &= ~0xE;	P2M &= ~0x01;	P1M &= ~0xE1;\
																							P3CON |= 0xE; P2CON |= 0x01;	P1CON |= 0xE1;}

#define	PIN1_IO_Mode		P0M		
#define	PIN1_IO_BIT			BIT_0
#define	IO_PIN1_OUT			P00

#define	PIN2_IO_Mode		P0M		
#define	PIN2_IO_BIT			BIT_1
#define	IO_PIN2_OUT			P01

#define	PIN3_IO_Mode		P0M		
#define	PIN3_IO_BIT			BIT_2
#define	IO_PIN3_OUT			P02

#define	PIN4_IO_Mode		P0M		
#define	PIN4_IO_BIT			BIT_3
#define	IO_PIN4_OUT			P03

#define	PIN5_IO_Mode		P1M		
#define	PIN5_IO_BIT			BIT_0
#define	IO_PIN5_OUT			P10

#define	PIN6_IO_Mode		P1M		
#define	PIN6_IO_BIT			BIT_1
#define	IO_PIN6_OUT			P11

#define	PIN7_IO_Mode		P1M		
#define	PIN7_IO_BIT			BIT_2
#define	IO_PIN7_OUT			P12

#define	PIN8_IO_Mode		P1M		
#define	PIN8_IO_BIT			BIT_3
#define	IO_PIN8_OUT			P13


#define	LED_DISPALY_BUF1		r_lcd_buf[0] //** r_lcd_buf[1] as 7-SEG1
#define	LED_DISPALY_BUF2		r_lcd_buf[1] //** r_lcd_buf[2] as 7-SEG2

//==========================================================================//
//																																					//
//							End of Settings for changing PINs layout										//
//																																					//
//==========================================================================//



//==========================================================================//
//			 											Fixed Settings																//
//==========================================================================//
#define PIN0_OUT				IOM_PIN1_OUT; IO_PIN1_OUT
#define PIN1_OUT				IOM_PIN2_OUT; IO_PIN2_OUT
#define PIN2_OUT				IOM_PIN3_OUT; IO_PIN3_OUT
#define PIN3_OUT				IOM_PIN4_OUT; IO_PIN4_OUT
#define PIN4_OUT				IOM_PIN5_OUT; IO_PIN5_OUT
#define PIN5_OUT				IOM_PIN6_OUT; IO_PIN6_OUT
#define PIN6_OUT				IOM_PIN7_OUT; IO_PIN7_OUT
#define PIN7_OUT				IOM_PIN8_OUT; IO_PIN8_OUT


#define	IOM_PIN1_IN			PIN1_IO_Mode &= (~PIN1_IO_BIT)
#define	IOM_PIN1_OUT		PIN1_IO_Mode |= PIN1_IO_BIT
#define	IOM_PIN2_IN			PIN2_IO_Mode &= (~PIN2_IO_BIT)
#define	IOM_PIN2_OUT		PIN2_IO_Mode |= PIN2_IO_BIT
#define	IOM_PIN3_IN			PIN3_IO_Mode &= (~PIN3_IO_BIT)
#define	IOM_PIN3_OUT		PIN3_IO_Mode |= PIN3_IO_BIT
#define	IOM_PIN4_IN			PIN4_IO_Mode &= (~PIN4_IO_BIT)
#define	IOM_PIN4_OUT		PIN4_IO_Mode |= PIN4_IO_BIT
#define	IOM_PIN5_IN			PIN5_IO_Mode &= (~PIN5_IO_BIT)
#define	IOM_PIN5_OUT		PIN5_IO_Mode |= PIN5_IO_BIT
#define	IOM_PIN6_IN			PIN6_IO_Mode &= (~PIN6_IO_BIT)
#define	IOM_PIN6_OUT		PIN6_IO_Mode |= PIN6_IO_BIT
#define	IOM_PIN7_IN			PIN7_IO_Mode &= (~PIN7_IO_BIT)
#define	IOM_PIN7_OUT		PIN7_IO_Mode |= PIN7_IO_BIT
#define	IOM_PIN8_IN			PIN8_IO_Mode &= (~PIN8_IO_BIT)
#define	IOM_PIN8_OUT		PIN8_IO_Mode |= PIN8_IO_BIT


#define		LED_MATRIX1_SEG2_A_ON			{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM7); PIN7_OUT=1; PIN3_OUT=!(LED_DISPALY_BUF2 & A);} 
#define		LED_MATRIX1_SEG2_B_ON			{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM7); PIN7_OUT=1; PIN2_OUT=!(LED_DISPALY_BUF2 & B);} 
#define		LED_MATRIX1_SEG2_C_ON			{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM7); PIN7_OUT=1; PIN1_OUT=!(LED_DISPALY_BUF2 & C);} 
#define		LED_MATRIX1_SEG2_D_ON			{COMCTL =~(mskCOMCTL_COM0|mskCOMCTL_COM6); PIN6_OUT=1; PIN0_OUT=!(LED_DISPALY_BUF2 & D);} 		
#define		LED_MATRIX1_SEG2_E_ON			{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM6); PIN6_OUT=1; PIN1_OUT=!(LED_DISPALY_BUF2 & E);}		
#define		LED_MATRIX1_SEG2_F_ON			{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM6); PIN6_OUT=1; PIN3_OUT=!(LED_DISPALY_BUF2 & F);} 	
#define		LED_MATRIX1_SEG2_G_ON			{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM6); PIN6_OUT=1; PIN2_OUT=!(LED_DISPALY_BUF2 & G);} 

#define		LED_MATRIX1_SEG2_A_OFF		{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM7); PIN7_OUT =!(LED_DISPALY_BUF2 & A); PIN3_OUT=1;} 
#define		LED_MATRIX1_SEG2_B_OFF		{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM7); PIN7_OUT =!(LED_DISPALY_BUF2 & B); PIN2_OUT=1;} 
#define		LED_MATRIX1_SEG2_C_OFF		{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM7); PIN7_OUT =!(LED_DISPALY_BUF2 & C); PIN1_OUT=1;} 
#define		LED_MATRIX1_SEG2_D_OFF		{COMCTL =~(mskCOMCTL_COM0|mskCOMCTL_COM6); PIN6_OUT =!(LED_DISPALY_BUF2 & D); PIN0_OUT=1;} 		
#define		LED_MATRIX1_SEG2_E_OFF		{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM6); PIN6_OUT =!(LED_DISPALY_BUF2 & E); PIN1_OUT=1;}		
#define		LED_MATRIX1_SEG2_F_OFF		{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM6); PIN6_OUT =!(LED_DISPALY_BUF2 & F); PIN3_OUT=1;} 	
#define		LED_MATRIX1_SEG2_G_OFF		{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM6); PIN6_OUT =!(LED_DISPALY_BUF2 & G); PIN2_OUT=1;} 

#define		LED_MATRIX1_SEG1_A_ON			{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM5); PIN5_OUT=1; PIN3_OUT=!(LED_DISPALY_BUF1 & A);} 
#define		LED_MATRIX1_SEG1_B_ON			{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM5); PIN5_OUT=1; PIN2_OUT=!(LED_DISPALY_BUF1 & B);} 
#define		LED_MATRIX1_SEG1_C_ON			{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM5); PIN5_OUT=1; PIN1_OUT=!(LED_DISPALY_BUF1 & C);} 
#define		LED_MATRIX1_SEG1_D_ON			{COMCTL =~(mskCOMCTL_COM0|mskCOMCTL_COM4); PIN4_OUT=1; PIN0_OUT=!(LED_DISPALY_BUF1 & D);} 		
#define		LED_MATRIX1_SEG1_E_ON			{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM4); PIN4_OUT=1; PIN1_OUT=!(LED_DISPALY_BUF1 & E);}		
#define		LED_MATRIX1_SEG1_F_ON			{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM4); PIN4_OUT=1; PIN3_OUT=!(LED_DISPALY_BUF1 & F);} 	
#define		LED_MATRIX1_SEG1_G_ON			{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM4); PIN4_OUT=1; PIN2_OUT=!(LED_DISPALY_BUF1 & G);} 

#define		LED_MATRIX1_SEG1_A_OFF		{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM5); PIN5_OUT=!(LED_DISPALY_BUF1 & A); PIN3_OUT=1;} 
#define		LED_MATRIX1_SEG1_B_OFF		{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM5); PIN5_OUT=!(LED_DISPALY_BUF1 & B); PIN2_OUT=1;} 
#define		LED_MATRIX1_SEG1_C_OFF		{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM5); PIN5_OUT=!(LED_DISPALY_BUF1 & C); PIN1_OUT=1;} 
#define		LED_MATRIX1_SEG1_D_OFF		{COMCTL =~(mskCOMCTL_COM0|mskCOMCTL_COM4); PIN4_OUT=!(LED_DISPALY_BUF1 & D); PIN0_OUT=1;} 		
#define		LED_MATRIX1_SEG1_E_OFF		{COMCTL =~(mskCOMCTL_COM1|mskCOMCTL_COM4); PIN4_OUT=!(LED_DISPALY_BUF1 & E); PIN1_OUT=1;}		
#define		LED_MATRIX1_SEG1_F_OFF		{COMCTL =~(mskCOMCTL_COM3|mskCOMCTL_COM4); PIN4_OUT=!(LED_DISPALY_BUF1 & F); PIN3_OUT=1;} 	
#define		LED_MATRIX1_SEG1_G_OFF		{COMCTL =~(mskCOMCTL_COM2|mskCOMCTL_COM4); PIN4_OUT=!(LED_DISPALY_BUF1 & G); PIN2_OUT=1;} 

//==========================================================================//
//			 											Const Defined																	//
//==========================================================================//
#define	BIT_0		(0x01U<<0)
#define	BIT_1		(0x01U<<1)
#define	BIT_2		(0x01U<<2)
#define	BIT_3		(0x01U<<3)
#define	BIT_4		(0x01U<<4)
#define	BIT_5		(0x01U<<5)
#define	BIT_6		(0x01U<<6)
#define	BIT_7		(0x01U<<7)

#define A				0X01
#define B				0X02
#define C				0X04
#define D				0X08
#define E				0X10
#define F				0X20
#define G				0X40
#define H1			0X01
#define H2			0X02
#define H3			0X04



/*_____ M A C R O S ________________________________________________________*/

/*_____ F U N C T I O N S __________________________________________________*/
void led_ctr(u16 led ,bit on);
void Dis_on_off(bit flag);
void TemperatureDisplay(void);
void LEDMatrixDriver(void);
#endif