/*

 乐迪遥控器

*/
/* Includes ----------------------------------------------------------------- */
#include "LD_remote.h"

#include <string.h>

#include "bsp_usart.h"
#include "error_detect.h"
#include "LD_remote.h"







//static void sbus_to_rc(volatile const uint8_t *sbus_buf, RC_ctrl_t *rc_ctrl);

//RC_ctrl_t rc_ctrl;
//static uint8_t sbus_rx_buf[2][RC_FRAME_LENGTH];
//uint8_t remote_ready = 0;//Ò£¿ØÆ÷×¼±¸Íê³É 

//void remote_control_init(void)
//{
//	RC_init(sbus_rx_buf[0], sbus_rx_buf[1], RC_FRAME_LENGTH);
//}

// int map_int(int x, int in_min, int in_max, int out_min, int out_max)		
//{
//	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
//}
////´®¿ÚÖÐ¶Ï
//void USART3_IRQHandler(void)
//{
//	//have received data  ???
//	if(huart3.Instance->SR & UART_FLAG_RXNE)
//	{
//		__HAL_UART_CLEAR_FEFLAG(&huart3);
//	}
//	else if(USART3->SR & UART_FLAG_IDLE)
//	{
//	
//    static uint16_t this_time_rx_len = 0;
//		__HAL_UART_CLEAR_PEFLAG(&huart3);
//		
//		if( (hdma_usart3_rx.Instance->CR & DMA_SxCR_CT) == RESET)
//		{
//			//current memory buffer used is memory0
//			
//			//disable dma to change dma register
//			__HAL_DMA_DISABLE(&hdma_usart3_rx);
//			
//			//get received data length, length = set_data_length - remain_length 
//			this_time_rx_len = SBUS_RX_BUF_NUM - hdma_usart3_rx.Instance->NDTR;

//			//reset set_data_length
//			hdma_usart3_rx.Instance->NDTR = SBUS_RX_BUF_NUM;
//			
//			//change memory0 to memory1
//			hdma_usart3_rx.Instance->CR |= DMA_SxCR_CT;
//			
//			//enable dma
//			__HAL_DMA_ENABLE(&hdma_usart3_rx);
//			
//			//1 frame length is correct data
//			if(this_time_rx_len == RC_FRAME_LENGTH)
//			{
//				sbus_to_rc(sbus_rx_buf[0], &rc_ctrl);
//			}
//		}
//			else
//			{
//				__HAL_DMA_DISABLE(&hdma_usart3_rx);
//				
//				this_time_rx_len = SBUS_RX_BUF_NUM - hdma_usart3_rx.Instance->NDTR;
//				
//				hdma_usart3_rx.Instance->NDTR = SBUS_RX_BUF_NUM;
//				
//				//change memory1 to memory0
//				DMA1_Stream1->CR &= ~(DMA_SxCR_CT);
//				
//				__HAL_DMA_ENABLE(&hdma_usart3_rx);
//				
//				if(this_time_rx_len == RC_FRAME_LENGTH)
//				{
//					sbus_to_rc(sbus_rx_buf[1], &rc_ctrl);
//				}
//			}
//	}
//}

//static void sbus_to_rc(volatile const uint8_t *sbus_buf, RC_ctrl_t *rc_ctrl)
//{
//    if (sbus_buf == NULL || rc_ctrl == NULL)
//    {
//        return;
//    }
//		
//		rc_ctrl->ch[0] = (sbus_buf[1] | (sbus_buf[2] << 8)) & 0x07ff;        //!< Channel 0
//    rc_ctrl->ch[1] = ((sbus_buf[2] >> 3) | (sbus_buf[3] << 5)) & 0x07ff; //!< Channel 1
//    rc_ctrl->ch[2] = ((sbus_buf[3] >> 6) | (sbus_buf[4] << 2) |          //!< Channel 2
//                         (sbus_buf[5] << 10)) &0x07ff;
//    rc_ctrl->ch[3] = ((sbus_buf[5] >> 1) | (sbus_buf[6] << 7)) & 0x07ff; //!< Channel 3
//    
//		rc_ctrl->sw[0] = ((int16_t)sbus_buf[6] >> 4 | ((int16_t)sbus_buf[7] << 4 )) & 0x07FF;                  //!< Switch left
//    rc_ctrl->sw[1] = ((int16_t)sbus_buf[7] >> 7 | ((int16_t)sbus_buf[8] << 1 ) | (int16_t)sbus_buf[9] << 9 ) & 0x07FF;                       //!< Switch right
//    rc_ctrl->sw[2] = ((int16_t)sbus_buf[9] >> 2 | ((int16_t)sbus_buf[10] << 6 )) & 0x07FF;;                    //!< Mouse X axis
//    rc_ctrl->sw[3] = ((int16_t)sbus_buf[10] >> 5 | ((int16_t)sbus_buf[11] << 3 )) & 0x07FF;                    //!< Mouse Y axis
//    rc_ctrl->sw[4] = ((int16_t)sbus_buf[12] << 0 | ((int16_t)sbus_buf[13] << 8 )) & 0x07FF;                  //!< Mouse Z axis
//    rc_ctrl->sw[5] = ((int16_t)sbus_buf[13] >> 3 | ((int16_t)sbus_buf[14] << 5 )) & 0x07FF;                                  //!< Mouse Left Is Press ?
//    rc_ctrl->sw[6] = ((int16_t)sbus_buf[14] >> 6 | ((int16_t)sbus_buf[15] << 2 ) | (int16_t)sbus_buf[16] << 10 ) & 0x07FF;                                  //!< Mouse Right Is Press ?
//    rc_ctrl->sw[7] = ((int16_t)sbus_buf[16] >> 1 | ((int16_t)sbus_buf[17] << 7 )) & 0x07FF;                    //!< KeyBoard value

//		rc_ctrl->ch[0] -= RC_CH_VALUE_OFFSET;
//    rc_ctrl->ch[1] -= RC_CH_VALUE_OFFSET;
//    rc_ctrl->ch[2] -= RC_CH_VALUE_OFFSET;
//    rc_ctrl->ch[3] -= RC_CH_VALUE_OFFSET;
//    
//		//µ÷ÕûÒ¡¸ËÖÐÖµÊ¹Ò£¿ØÆ÷Î¢µ÷Á¿Îª0Ê±Ò¡¸ËÖÐÖµ½Ó½ü0£¬ÓÍÃÅ³ýÍâ
//		//µÚÒ»´Îµ÷³µÇ°ÏÈÈ·ÈÏÍ¨µÀÖµÊÇ·ñÕýÈ·£¡£¡Èç²»·ûºÏ¸ù¾ÝÊµ¼ÊÍ¨µÀÖµ½øÐÐÐÞ¸Ä
//		//ÐÞ¸Ä´Ë´¦¼°omni_chassis.cÖÐÍ¨µÀÖµ²¿·Ö
//		rc_ctrl->ch[0] += 24;	//y(-694,693)×óÓÒ
//		rc_ctrl->ch[1] = -rc_ctrl->ch[1]-24;	//x(-693,694)Ç°ºó
//		rc_ctrl->ch[2] = -rc_ctrl->ch[2]+764;		//m(95,1482)ÓÍÃÅ
//		rc_ctrl->ch[3] += 24;	//w(-694,693)Ðý×ª
//		
//		rc_ctrl->ch[1] = map_int(rc_ctrl->ch[1],-693,694,-700,700);		//xÓ³Éäµ½(-700,700)
//		rc_ctrl->ch[0] = map_int(rc_ctrl->ch[0],-694,693,-700,700);		//yÓ³Éäµ½(-700,700)
//		rc_ctrl->ch[3] = 0.5*(rc_ctrl->ch[3]);		//w
//		
//		//ËÀÇø(-30,30)
//		if(rc_ctrl->ch[0]>-30&&rc_ctrl->ch[0]<30) rc_ctrl->ch[0]=0;
//		if(rc_ctrl->ch[1]>-30&&rc_ctrl->ch[1]<30) rc_ctrl->ch[1]=0;
//		if(rc_ctrl->ch[2]>-30&&rc_ctrl->ch[2]<30) rc_ctrl->ch[2]=0;
//		if(rc_ctrl->ch[3]>-30&&rc_ctrl->ch[3]<30) rc_ctrl->ch[3]=0;
//    remote_ready = 1;
//}                                                                            																																					



//int map(int x, int in_min, int in_max, int out_min, int out_max)		//Ó³Éäº¯Êý
//{
//	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
//}


///*



//  																																	
//306																							  					  	  306
// sw[]																						 	 					  		sw[7]
//1694																							 					  		1694
// 
//306																														    	306
//sw[6]                                                              sw[4]
//1694                                                               1694

//306         306                                        306        306   
//sw[0]      sw[2]    sw[1]:306-1694   sw[5]:306-1694  sw[]1000   sw[3] 
//1694       1694                                         1694       1694
//                                      710
//               688                   1425
//                |                      |
//                |                      |
//   54    -616------ch[3]770   -354---------ch[0] 339  0
//                |                      |
//                |                      |
//	             ch[2]                 ch[1]
//               _699                   38   
//*/
