/*
 * msp_MK32.c
 *
 *  Created on: Oct 26, 2023
 *      Author: shiya
 */
#include "MSP/msp_MK32.h"

int MK32_state=0;
UART_HandleTypeDef* Huart_MK32;
int32_t* Mk32_P_butV;
int32_t* Mk32_P_RxIndex;

SP_MSP_MK32_Button* P_MK32;
uint8_t MK32_butt_data[25];

//int32_t *P_butV Size = 17,对应16个通道以及1个发送器的在线状态
//UART_HandleTypeDef* huart::SBUS接口对应的串口硬件指针
//int32_t *P_butV::返回值，16个按键通道+1个遥控器在线状态返回值
//int32_t *P_RxIndex::接收到的串口数据包索引，累加
uint8_t GF_MSP_MK32_Init(UART_HandleTypeDef* huart,SP_MSP_MK32_Button* _MK32)
{
	MK32_state=0;
	Huart_MK32=huart;
	GF_BSP_Interrupt_Add_CallBack(DF_BSP_InterCall_E28_SBUS_RxFifo0Callback, GF_MSP_MK32_Unscramble_Callback);
	HAL_UART_Receive_IT(Huart_MK32,MK32_butt_data,1);
	P_MK32 = _MK32;
	return 1;
}

//
/**
  * @brief  Rocker MK32 decoding
  * @param  Cache value of keys
  * @retval Key decoding value
  */

void Sbus_Data_Count(uint8_t *buf, int32_t  *But_Value)
{
	int16_t	CH[16];
    int Start_byte=-1;

	CH[0]  = ((buf[Start_byte+1]	|buf[Start_byte+2]<<8)                  		& 0x07FF);
	CH[1]  = ((buf[Start_byte+2]>>3 |buf[Start_byte+3]<<5)             				& 0x07FF);
	CH[2]  = ((buf[Start_byte+3]>>6 |buf[Start_byte+4]<<2 |buf[Start_byte+5]<<10)  	& 0x07FF);
	CH[3]  = ((buf[Start_byte+5]>>1 |buf[Start_byte+6]<<7)             				& 0x07FF);
	CH[4]  = ((buf[Start_byte+6]>>4 |buf[Start_byte+7]<<4)              			& 0x07FF);
	CH[5]  = ((buf[Start_byte+7]>>7 |buf[Start_byte+8]<<1 |buf[Start_byte+9]<<9)   	& 0x07FF);
	CH[6]  = ((buf[Start_byte+9]>>2 |buf[Start_byte+10]<<6)             			& 0x07FF);
	CH[7]  = ((buf[Start_byte+10]>>5|buf[Start_byte+11]<<3)             			& 0x07FF);
	CH[8]  = ((buf[Start_byte+12]   |buf[Start_byte+13]<<8)             			& 0x07FF);
	CH[9]  = ((buf[Start_byte+13]>>3|buf[Start_byte+14]<<5)             			& 0x07FF);
	CH[10] = ((buf[Start_byte+14]>>6|buf[Start_byte+15]<<2|buf[Start_byte+16]<<10) 	& 0x07FF);
	CH[11] = ((buf[Start_byte+16]>>1|buf[Start_byte+17]<<7)             			& 0x07FF);
	CH[12] = ((buf[Start_byte+17]>>4|buf[Start_byte+18]<<4)             			& 0x07FF);
	CH[13] = ((buf[Start_byte+18]>>7|buf[Start_byte+19]<<1|buf[Start_byte+20]<<9)  	& 0x07FF);
	CH[14] = ((buf[Start_byte+20]>>2|buf[Start_byte+21]<<6)             			& 0x07FF);
	CH[15] = ((buf[Start_byte+21]>>5|buf[Start_byte+22]<<3)             			& 0x07FF);

	//按键数值：1050为中间值，272-1712
	for(int i=0;i<16;i++)
	{
		//But_Value[i+1]=  (int32_t)(((CH[i]-272)*0.625+1050-1500)*2.22222);

		But_Value[i+1]=  (int32_t)((CH[i]-992)*1.388889);
	}

	//遥控器地面站在线状态
	if(buf[22]==0) But_Value[17] = 1;
	else But_Value[17] = 0;

	But_Value[0]++;
}


void  GF_MSP_MK32_Unscramble_Callback()
{
	switch(MK32_state)
	{
	case 0:
		if(MK32_butt_data[0]==0X0f)
		{
			MK32_state=1;
			HAL_UART_Receive_IT(Huart_MK32,MK32_butt_data,24);
		}
		else
		{
			MK32_state=0;
			HAL_UART_Receive_IT(Huart_MK32,MK32_butt_data,1);
		}
		break;
	case 1:
		if(MK32_butt_data[23]==0X00)
		{
			MK32_state=0;
			Sbus_Data_Count(MK32_butt_data, (int32_t*)(P_MK32));
			HAL_UART_Receive_IT(Huart_MK32,MK32_butt_data,1);
		}
		else
		{
			MK32_state=0;
			HAL_UART_Receive_IT(Huart_MK32,MK32_butt_data,1);
		}
		break;

	default:
		MK32_state=0;

		HAL_UART_Receive_IT(Huart_MK32,MK32_butt_data,1);
		break;
	}
}
