// // Created by 20420 on 2024/12/4. // #include "openmv.h" #include "string.h" #include "bsp_usart.h" #include "stdlib.h" #include "daemon.h" #include "bsp_log.h" #define OPENMV_SIZE 10u //接收openmv发送的数组 // openmv数据 static Openmv_Information openmv_ctrl[2]; //[0]:当前数据TEMP,[1]:上一次的数据LAST.用于按键持续按下和切换的判断 static uint8_t open_init_flag = 0; // 遥控器初始化标志位 //openmv的串口实例 static USARTInstance *openmv_usart_instance; static DaemonInstance *openmv_daemon_instance; static void sbus_to_openmv(const uint8_t *sbus_buf) { openmv_ctrl[0].cx = sbus_buf[0]; openmv_ctrl[0].cy = sbus_buf[1]; openmv_ctrl[0].cw = sbus_buf[2]; openmv_ctrl[0].ch = sbus_buf[3]; } static void OpenmvRxCallback() { DaemonReload(openmv_daemon_instance); // 先喂狗 sbus_to_openmv(openmv_usart_instance->recv_buff); // 进行openmv协议解析 } static void OpenmvLostCallback(void *id) { memset(openmv_ctrl, 0, sizeof(openmv_ctrl)); // 清空遥控器数据 USARTServiceInit(openmv_usart_instance); // 尝试重新启动接收 LOGWARNING("[rc] openmv lost"); } Openmv_Information *OpenmvInit(UART_HandleTypeDef *openmv_usart_handle)//rc_usart_handle { USART_Init_Config_s conf; conf.module_callback = OpenmvRxCallback; conf.usart_handle = openmv_usart_handle; conf.recv_buff_size = OPENMV_SIZE; openmv_usart_instance = USARTRegister(&conf); // 进行守护进程的注册,用于定时检查遥控器是否正常工作 Daemon_Init_Config_s daemon_conf = { .reload_count = 10, // 100ms未收到数据视为离线,遥控器的接收频率实际上是1000/14Hz(大约70Hz) .callback = OpenmvLostCallback, .owner_id = NULL, // 只有1个遥控器,不需要owner_id }; openmv_daemon_instance = DaemonRegister(&daemon_conf); open_init_flag = 1; return openmv_ctrl; } uint8_t OpenmvIsOnline() { if (open_init_flag) return DaemonIsOnline(openmv_daemon_instance); return 0; }