/*
 * File:   CH455.c
 * Author: JiangXiaowei
 */
 
#include "ch455g.h"		// 修改该文件以适应硬件环境/单片机型号等
#include "delay.h"
#include "../globalDef.h"
#include "uart_conf.h"
//定义
#define I2C_DELAY 10

#define CH455H_LED_NUM 16


uint32_t ch455h_Time_duty[CH455H_LED_NUM];                         //占空比
uint8_t  ch455h_Time_en[CH455H_LED_NUM];                           //使能
uint8_t  ch455h_Time_en_process;                      //全局使能

extern OS_SEM i2c_sem;

static uint16_t CH455H_LED_VALUE=0x0000;

void CH455_Init(void)
{
	
	
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//return;
	
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB时钟
 
	
	//stm32f103中PB3、PB4、PA15为JTAG引脚，若要作为普通IO口来使用的话，要禁用JTAG功能。
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);  
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//普通输出模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
	GPIO_Init(GPIOB, &GPIO_InitStructure);
  
	GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11); 	
	
	CH455_Write( CH455_7SEG_ON );		//八段式
	
	//EXTI_MY_INIT();
	
}
void CH455H_SCL_D_OUT(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//普通输出模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	delay_us(I2C_DELAY);
}

void CH455H_SDA_D_OUT(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;//普通输出模式
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	delay_us(I2C_DELAY);
}

void CH455H_SDA_D_IN(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	delay_us(I2C_DELAY);
}

/*
 * 函数功能：IIC产生起始信号函数
 * 输入参数：无
 * 输出参数：无
 */
void CH455_I2c_Start(void)
{
    CH455_SDA_D_OUT;
    CH455_SCL_D_OUT;
    CH455_SCL = high;   //SCL = high;
    //GPIO_SetBits(GPIOB,GPIO_Pin_7);
  
    delay_us(I2C_DELAY);
    CH455_SDA = high;   //SDA = high;
    delay_us(I2C_DELAY);
    CH455_SDA = low;    //SDA = low;
    delay_us(I2C_DELAY);
    CH455_SCL = low;    //SCL = low;
    //GPIO_ResetBits(GPIOB,GPIO_Pin_7);
  
    delay_us(I2C_DELAY);
}
 
/*
 * 函数功能：IIC产生停止信号
 * 输入参数：无
 * 输出参数：无
 */
void CH455_I2c_Stop(void)
{
    CH455_SDA_D_OUT;
    CH455_SCL = low;     //SCL = low;
    delay_us(I2C_DELAY);
    CH455_SDA = low;     //SDA = low;
    delay_us(I2C_DELAY);
    CH455_SCL = high;    //SCL = high;
    delay_us(I2C_DELAY);
    CH455_SDA = high;    //SDA = high;
    delay_us(I2C_DELAY);
}

/*
 * 函数功能：IIC发送一个字节
 * 输入参数：一个字节
 * 输出参数：无
 */
void CH455_I2c_WrByte(unsigned char IIC_Byte)
{
    unsigned char i;
    CH455_SDA_D_OUT;
    for(i = 0; i < 8; i++)
    {
        if(IIC_Byte & 0x80)
            CH455_SDA = high;   //SDA=high;
        else
            CH455_SDA = low;   //SDA=low;
        delay_us(I2C_DELAY);
        CH455_SCL = high;      //SCL=high;
        delay_us(I2C_DELAY);
        delay_us(I2C_DELAY);
        CH455_SCL = low;       //SCL=low;
        delay_us(I2C_DELAY);
        IIC_Byte<<=1;
    }
    delay_us(I2C_DELAY);
    CH455_SDA = high;          //SDA=1;
		
    delay_us(I2C_DELAY);
    CH455_SCL = high;          //SCL=1;
		
    delay_us(I2C_DELAY);
    CH455_SCL = low;           //SCL=0;
		
    delay_us(I2C_DELAY);
}

unsigned char  CH455_I2c_RdByte( void )		//读一个字节数据
{    
    unsigned char i, bytedata;
    CH455_SDA_D_IN;     //将数据设置为输入模式
    delay_us(I2C_DELAY);
    CH455_SDA = 1;      //数据线拉高
    delay_us(I2C_DELAY);
    CH455_SCL = 0;
    delay_us(I2C_DELAY);
    for(i = 0; i < 8; i++)      //读8位数据
    {
        CH455_SCL = 1;
        delay_us(I2C_DELAY);
        bytedata <<= 1;
        delay_us(I2C_DELAY);
        bytedata |= (CH455_SDA_IN);
        delay_us(I2C_DELAY);
        CH455_SCL = 0;
        delay_us(I2C_DELAY);
    }
    CH455_SDA_D_OUT;            //数据线设置回输出模式
    CH455_SDA = high;          //SDA=1;
    delay_us(I2C_DELAY);
    CH455_SCL = high;          //SCL=1;
    delay_us(I2C_DELAY);
    CH455_SCL = low;           //SCL=0;
    delay_us(I2C_DELAY);
    return(bytedata);//返回数据
}
 
void CH455_Write( UINT16 cmd )	//写命令
{
	CH455_I2c_Start();               //启动总线
   	CH455_I2c_WrByte(((UINT8)(cmd>>7)&CH455_I2C_MASK)|CH455_I2C_ADDR);
	//CH455_I2c_WrByte(((UINT8)(cmd)&CH455_I2C_MASK)|CH455_I2C_ADDR);
   	CH455_I2c_WrByte((UINT8)cmd);     //发送数据
  	CH455_I2c_Stop();                 //结束总线 
}
 
UINT8 CH455_Read( void )		//读取按键
{
	UINT8 keycode;
   	CH455_I2c_Start();                //启动总线
   	CH455_I2c_WrByte(((UINT8)(CH455_GET_KEY>>7)&CH455_I2C_MASK)|0x01|CH455_I2C_ADDR);
   	keycode=CH455_I2c_RdByte();      //读取数据
	CH455_I2c_Stop();                //结束总线
	return keycode;
}


void CH455H_LED_test(void){
	static uint16_t led=0;
  CH455_Write( CH455_DIG0 | ((led>>0) & 0x0F));
  CH455_Write( CH455_DIG1 | ((led>>4) & 0x0F));
  CH455_Write( CH455_DIG2 | ((led>>8) & 0x0F));
  CH455_Write( CH455_DIG3 | ((led>>12) & 0x0F));
	led++;
}
/*-----------------------------------------------------------
 * 16 12 8 4
 * 15 11 7 3
 * 14 10 6 2 
 * 13  9 5 1
 *------------------------------------------------------------*/
void CH455H_LED_test00(short led){
	//static uint16_t led=0;
	
		OS_ERR      err;

	
		OSSemPend(&i2c_sem, 0, OS_OPT_PEND_BLOCKING, NULL, &err);

    led = (((led & 0xaaaa) >> 1) | ((led & 0x5555) << 1));
    led = (((led & 0xcccc) >> 2) | ((led & 0x3333) << 2));
    led = (((led & 0xf0f0) >> 4) | ((led & 0x0f0f) << 4));
    led = (((led & 0xff00) >> 8) | ((led & 0x00ff) << 8));
	
    CH455_Write( CH455_DIG0 | ((led>>0) & 0x0F));
    CH455_Write( CH455_DIG1 | ((led>>4) & 0x0F));
    CH455_Write( CH455_DIG2 | ((led>>8) & 0x0F));
    CH455_Write( CH455_DIG3 | ((led>>12) & 0x0F));
	//led++;
		    OSSemPost(&i2c_sem, OS_OPT_POST_ALL, &err);

}


void CH455H_LED_test2(void){
	static uint16_t led=0;
  CH455_Write( CH455_DIG0 | ((led>>0) & 0x0F));
  CH455_Write( CH455_DIG1 | ((led>>4) & 0x0F));
  CH455_Write( CH455_DIG2 | ((led>>8) & 0x0F));
  CH455_Write( CH455_DIG3 | ((led>>12) & 0x0F));
	led=~led;
}


void CH455H_LED_test3(void){
	static uint16_t led=0;
  CH455_Write( CH455_DIG0 | ((led>>0) & 0x0F));
	led++;
	led&=0x000F;
}
extern PtTest g_ptTest;

/*--------------------------------------------------
 *  上电显示版本号 持续显示0.5s
 * -------------------------------------------------*/
void led_display_version(void){

	if(g_ptTest.bTestStart == 1) return;
    CH455H_LED_test00(g_firmwareMsg.gatewayMsg.verMsg.gate_appVr);
    delay_ms(1500);
    CH455H_LED_test00(0);
    delay_ms(1500);
}

void set_led_status(uint8_t index,uint8_t status)
{
	//return;
    if(g_ptTest.bTestStart == 1) return;
    switch(index){
    case 0  :g_ledStatus.led16.led.led01 = status; break;
    case 1  :g_ledStatus.led16.led.led02 = status; break;
    case 2  :g_ledStatus.led16.led.led03 = status; break;
    case 3  :g_ledStatus.led16.led.led04 = status; break;
    case 4  :g_ledStatus.led16.led.led05 = status; break;
    case 5  :g_ledStatus.led16.led.led06 = status; break;
    case 6  :g_ledStatus.led16.led.led07 = status; break;
    case 7  :g_ledStatus.led16.led.led08 = status; break;
    case 8  :g_ledStatus.led16.led.led09 = status; break;
    case 9  :g_ledStatus.led16.led.led10 = status; break;
    case 10 :g_ledStatus.led16.led.led11 = status; break;
    case 11 :g_ledStatus.led16.led.led12 = status; break;
    case 12 :g_ledStatus.led16.led.led13 = status; break;
    case 13 :g_ledStatus.led16.led.led14 = status; break;
    case 14 :g_ledStatus.led16.led.led15 = status; break;
    case 15 :g_ledStatus.led16.led.led16 = status; break;
    default : break;
    }

    CH455H_LED_test00(g_ledStatus.led16.status);
}
void set_led_update_status(uint16_t status)
{
	//return;
    if(g_ptTest.bTestStart == 1) return;
    CH455H_LED_test00(status);
}
void set_led_no_init_sn(uint16_t status)
{
   // return;
    if(g_ptTest.bTestStart == 1) return;
     CH455H_LED_test00(status);
}
/* 显示lora信道 编号 */

void set_led_lora_index(uint16_t status)
{
    if(g_ptTest.bTestStart == 1) return;
   // if(g_ledStatus.status != LED_DISP_LORA ) return;
     CH455H_LED_test00(status);
}
