#include <stdio.h>

#include "GT5DL32A3W.h"
#include "FONT_LIB.h"

#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"

static const char *LOG_TAG = "FONTLIB";


static void task_delay_ms(uint32_t ms_count)
{
    vTaskDelay(ms_count / portTICK_PERIOD_MS);
}

void gt_font_pin_init()
{
    gpio_config_t fontLib_pin_cfg = {};

    fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
    fontLib_pin_cfg.mode = GPIO_MODE_OUTPUT;
    fontLib_pin_cfg.pin_bit_mask =FONT_OUTPUT_PIN_SEL;
    fontLib_pin_cfg.pull_down_en = 0;
    fontLib_pin_cfg.pull_up_en = 0;
    gpio_config(&fontLib_pin_cfg);

    fontLib_pin_cfg.intr_type = GPIO_INTR_DISABLE;
    fontLib_pin_cfg.mode = GPIO_MODE_INPUT;
    fontLib_pin_cfg.pin_bit_mask =FONT_INPUT_PIN_SEL;
    fontLib_pin_cfg.pull_down_en = 0;
    fontLib_pin_cfg.pull_up_en = 1;
    gpio_config(&fontLib_pin_cfg);    
}

void font_init()
{
    gt_font_pin_init();

    FONT_CLK_1;
	FONT_SDA_1;
	FONT_CS_HIGHT;

	font_exit_sleep(); //退出睡眠

	//task_delay_ms(500); 

#if 1
    uint8_t id = GT_Font_Init();
    while(id==0)
    {
	  	ESP_LOGE(LOG_TAG,"font fail");
		task_delay_ms(500);
      	id = GT_Font_Init();
    }
#endif
	ESP_LOGI(LOG_TAG,"font OK");

}
void font_spi_write(unsigned char value)
{
  int i=0,data=value;   

  gpio_set_level(FONT_CLK_PIN,LOW_LEVEL);
   
  for(i=0;i<8;i++)
  {
    if(data&0x0080)
    {
       FONT_SDA_1;
    }
      
    else
    {
       FONT_SDA_0;
    }
    //ets_delay_us(1);
    gpio_set_level(FONT_CLK_PIN,HIGH_LEVEL);
    //ets_delay_us(1);
    gpio_set_level(FONT_CLK_PIN,LOW_LEVEL);
    data=((data<<1)&0xff);
  }
     
}


void Send_Byte(unsigned char out)
{	
	unsigned char i=0;

	for(i=0;i<8;i++)
	{
	  FONT_CLK_0;  
	  if(((out<<i)&0x80)==0)
		  FONT_SDA_0;   
	  else
		  FONT_SDA_1;
	  FONT_CLK_1;
  }
}



/*******************************************************************************/
// Get data sub-pro (STM8,STM32等双向口)    SPI接收点阵数据的算法               /
/*******************************************************************************/
unsigned char Get_Byte(void)         
{
	unsigned char i;
	unsigned char read_dat= 0,MISO = 0;

	FONT_CLK_1;
	for(i=0;i<8;i++)
	{
		FONT_CLK_0;
        MISO = FONT_RD_SDA;
		read_dat = read_dat<<1;
		if(MISO)				
			read_dat|=0x01;
		else
			read_dat&=0xfe;
		FONT_CLK_1;
	}
	return(read_dat);
}





unsigned char gt_read_data(unsigned char* sendbuf , unsigned char sendlen , unsigned char* receivebuf, unsigned int receivelen)
{

    unsigned int i;
    FONT_CS_LOW;
    for(i = 0; i < sendlen;i++)
    {
        Send_Byte(sendbuf[i] );
    }
    for(i = 0; i < receivelen;i++)
    {
        receivebuf[i] = Get_Byte();
    }

    FONT_CS_HIGHT;
    return 1;
}



/*******************************************************************************/
//                    Send address sub-pro (STM8,STM32，51)                                     /
/*******************************************************************************/
void SPI_Address(unsigned char AddH,unsigned char AddM,unsigned char AddL) 
{
	Send_Byte(AddH);
	Send_Byte(AddM);
	Send_Byte(AddL);
}
/*******************************************************************************/
//                    Get N bytes sub-pro  (STM8,STM32，51)                                    //
/*******************************************************************************/
//客户自己实现，从address地址读取len个字节的数据并存入到DZ_Data数组当中
unsigned char r_dat_bat(unsigned long address,unsigned long DataLen,unsigned char *pBuff)
{
    unsigned long i;
    unsigned char addrHigh;
    unsigned char addrMid;
    unsigned char addrLow;
    addrHigh=address>>16;
    addrMid=address>>8;
    addrLow=(unsigned char)address;
  
	FONT_CS_LOW;          //片选选中字库芯片
	Send_Byte(0x03);	//普通读取首先送0X03,然后发送地址高八位addrHigh，中八位addrMid，低八位addrLow。
	SPI_Address(addrHigh,addrMid,addrLow);	
	for(i=0;i<DataLen;i++)
	*(pBuff+i)=Get_Byte();
	FONT_CS_HIGHT;
	return 0;
}

//客户自己实现，从address地址读取一个字节的数据并返回该数据
unsigned char r_dat(unsigned long address){
	unsigned char buff;
	unsigned char addrHigh;
	unsigned char addrMid;
	unsigned char addrLow;
	addrHigh=address>>16;
	addrMid=address>>8;
	addrLow=(unsigned char)address;
	
	FONT_CS_LOW;
	Send_Byte(0x03);
	SPI_Address(addrHigh,addrMid,addrLow);
	buff = Get_Byte();
	FONT_CS_HIGHT;
	return buff;
}

/******************************************************
客户自己实现, 库文件函数内部需要调用该函数匹配芯片ID号
根据说明文件或头文件是否需要, 没有就不需要实现
******************************************************/
unsigned char CheckID(unsigned char CMD, unsigned long address,
	unsigned long byte_long,unsigned char *p_arr)
{
	unsigned long j;
	FONT_CS_LOW;
	Send_Byte(CMD);
	Send_Byte((unsigned char)((address)>>16)); 
	Send_Byte((unsigned char)((address)>>8));   
	Send_Byte((unsigned char)address); 
	for(j=0;j<byte_long;j++)
	{
			p_arr[j]=Get_Byte();
	}
	FONT_CS_HIGHT;
	return 1;
}


void font_into_sleep()
{

	unsigned long j;
	FONT_CS_LOW;
	Send_Byte(0xB9);
	FONT_CS_HIGHT;
	//return 1;

}


void font_exit_sleep()
{

	unsigned long j;
	FONT_CS_LOW;
	Send_Byte(0xAB);
	FONT_CS_HIGHT;

}


void font_soft_rst()
{
		FONT_CS_LOW;
		Send_Byte(0x66);
		FONT_CS_HIGHT;
		task_delay_ms(10);
		FONT_CS_LOW;
		Send_Byte(0x99);
		FONT_CS_HIGHT;
		task_delay_ms(10);
}
// unsigned char test_ASCII_GetData(unsigned char asc,unsigned long ascii_kind,unsigned char *DZ_Data)
// {
//     return ASCII_GetData(  asc,  ascii_kind,  DZ_Data);
// }
// void test_spi()
// {
//     FONT_CS_LOW;
// 	Send_Byte(0x78);
//     FONT_CS_HIGHT;


//     FONT_CS_LOW;
// 	Send_Byte(0x56);
//     FONT_CS_HIGHT;

//     FONT_CS_LOW;
// 	Send_Byte(0x9f);

//     for(int i=0;i<3;i++)
// 	{
// 	   uint8_t id = Get_Byte();
// 	}
//     FONT_CS_HIGHT;

// }

