#include "bsp_air.h"
#include "bsp_spi.h"
#include "stdio.h"

static air_staus_struct air_staus;

void set_single_air_staus(air_num_enum air_num,air_run_action_enum air_run_action)
{
		switch((uint8_t)air_num)
		{
			case AIR_1:
				air_staus.value1 = air_run_action;
				break;
			case AIR_2:
				air_staus.value2 = air_run_action;
				break;
			case AIR_3:
				air_staus.value3 = air_run_action;
				break;
			case AIR_4:
				air_staus.value4 = air_run_action;
				break;
			case AIR_5:
				air_staus.value5 = air_run_action;
				break;
			case AIR_6:
				air_staus.value6 = air_run_action;
				break;
			case AIR_7:
				air_staus.value7 = air_run_action;
				break;
			case AIR_8:
				air_staus.value8 = air_run_action;
				break;
			case AIR_9:
				air_staus.value9 = air_run_action;
				break;
			case AIR_10:
				air_staus.value10 = air_run_action;
				break;	
		}
}

void set_all_air_staus(air_run_action_enum air_run_action)
{
		for(air_num_enum air_num = AIR_1; air_num < AIR_MAX_NUM; air_num++)
		{
			set_single_air_staus(air_num,air_run_action);
		}
}



static uint16_t CalChecksum(uint8_t * data, uint16_t len)
{
    uint16_t sum = 0;
    for(int i = 0; i < len; ++i)
    {
        sum += data[i];
    }
    return sum;
}


/*打包一帧*/
static uint8_t creat_air_frame(uint8_t device_id, uint8_t message_type, uint8_t error_flag, air_staus_struct air_staus, air_spi_frame_struct *tx_frame)
{
		tx_frame->device_id = device_id;
		tx_frame->message_type = message_type;
		tx_frame->error_flag = error_flag;
		tx_frame->value_1 = air_staus.value1;
		tx_frame->value_2 = air_staus.value2;
		tx_frame->value_3 = air_staus.value3;
		tx_frame->value_4 = air_staus.value4;
		tx_frame->value_5 = air_staus.value5;
		tx_frame->value_6 = air_staus.value6;
		tx_frame->value_7 = air_staus.value7;
		tx_frame->value_8 = air_staus.value8;
		tx_frame->value_9 = air_staus.value9;
		tx_frame->value_10 = air_staus.value10;
		
		tx_frame->check_sum = CalChecksum((uint8_t *)tx_frame,sizeof(air_spi_frame_struct) - 1);
		return 1;
}

/*发送并接受*/
static uint8_t air_frame_rev(air_spi_frame_struct *tx_frame, air_spi_frame_struct *rx_frame)
{
		 if(spi_tx_rx_buffer((uint8_t*)tx_frame,(uint8_t *)rx_frame,sizeof(air_spi_frame_struct)))
		 {
				if(rx_frame->device_id != 0x01)
				{
					return 0;
				}
				if(rx_frame->message_type != 0x01)
				{
					return 0;
				}
				if(rx_frame->check_sum != CalChecksum((uint8_t *)rx_frame, sizeof(air_spi_frame_struct) - 1))
				{
					return 0;
				}
		 }
		 return 1;
}

/*错误码处理*/
static void air_frame_error_handle(uint8_t error_flag)
{
		if(error_flag & AIR_PUMP_ERROR)
		{
			 printf("...");
		}
		if(error_flag & VALUE_JAM)
		{
				printf("...");
		}
		if(error_flag & OVER_VOLTAGE_ALARM)
		{
				printf("...");
		}
}

//100ms调用一次
void air_handle(void)
{
	static air_spi_frame_struct tx_frame;
	static air_spi_frame_struct rx_frame;
	static uint8_t out_time_cnt;
	creat_air_frame(0x01,0x00,0x00, air_staus, &tx_frame);
	if(air_frame_rev(&tx_frame, &rx_frame))
	{
			if(rx_frame.error_flag == 0)
			{
					out_time_cnt = 0;
			}
			else
			{
					air_frame_error_handle(rx_frame.error_flag);
					out_time_cnt++;
			}
				
	}
	else
	{
			out_time_cnt++;
	}
	
	if(out_time_cnt >= 30)
	{
			printf("通信超时");
			spi1_init();
			out_time_cnt = 0;
	}
	
}

