/*! * \file uart.c * * \brief Target uart implementation * * \copyright Revised BSD License, see section \ref LICENSE. * * \code * * _ __ _ ________ _ * | |/ /(_)___ / ____/ /_ (_)___ * | // / __ \/ / / __ \/ / __ \ * / |/ / / / / /___/ / / / / /_/ / * /_/|_/_/_/ /_/\____/_/ /_/_/ .___/ * /_/ * (C) 2022-2025 XinChip * * \endcode * * \author ( XinChip ) Alex-J * * \author ( XinChip ) */ /*----------------------------------------------------------------------------------- INCLUDE HEADE FILES ------------------------------------------------------------------------------------*/ #include "uart.h" #include "xc_drv_uart.h" #include "timer.h" #include "RF433.h" #include "pwm.h" #include "xc_drv_pwm.h" #include "rgblight.h" #include "mode.h" #include "timer.h" #include "math.h" #include "timeslice.h" /*------------------------------------------------------------------------------------ Macros -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Global Variables -------------------------------------------------------------------------------------*/ uint8_t only_read_charactertics_buffer[5]; uint8_t only_read_charactertics_buffer_length = 5; ring_buffer_t uart0_ring_buff; uint8_t buffer[5] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA}; uint8_t uart0_buff[UART0_BUFFER_LEN]; extern bool buffer_complet_flag; // void uart_receive_cb(uint8_t *buff, uint16_t len) { // static uint8_t cnt = 0; // for (int i = 0; i < len; i++) { // // uart_user_ctl.r_buff[cnt++] = buff[i]; // // uart_user_ctl.r_len++; // ring_buffer_queue(&uart0_ring_buff, buff[i]); // } // // if (cnt == sizeof(uart_user_ctl.r_buff)) // { // //uart_user_ctl.rx_done = true; // cnt = 0; // } } /// void scan_uar_data(uint8_t *frame){ } extern uint8_t sss; uint8_t rx_date; void scan_uart(uint8_t *arr) { uint8_t uart_handle = UART0_IDX; buffer_complet_flag=false; // 组帧:帧头 + 数据 + 校验 uint8_t user_rx_buf[6]; // 1字节帧头 + 4字节数据 + 1字节校验位 user_rx_buf[0] = 0xAA; // 帧头标志 for (uint8_t i = 0; i < 4; i++) { user_rx_buf[1 + i] = arr[i]; // 填充数据 } // 校验位:简单累加校验 uint8_t checksum = 0; for (uint8_t i = 0; i < 5; i++) // 帧头和数据 { checksum += user_rx_buf[i]; } user_rx_buf[5] = checksum; // 校验位 sss=1; // printf("sss=%d\r\n",sss); switch (user_rx_buf[4]) { case 1: if(user_rx_buf[1]==0X01){ if(deviceStatus==POWEROFF){ Start_PWM(); } }else if(user_rx_buf[1]==0X00){ if(deviceStatus==POWERON){ Stop_PWM(); } } break; case 2: if(POWEROFF==deviceStatus)break; if(user_rx_buf[1] < 1U || user_rx_buf[1] > (EFFECT_MODE_MAX + 1U)) break; choose_mode_bh=0; choose_mode_falsg=user_rx_buf[1]-1U; startTimer(&timer1,1); break; case 5: break; case 3: if(POWEROFF==deviceStatus)break; if(Mode==mode0){ if(user_rx_buf[1] < BRIGHTNESS_MIN_PERCENT) user_rx_buf[1] = BRIGHTNESS_MIN_PERCENT; else if(user_rx_buf[1] > BRIGHTNESS_MAX_PERCENT) user_rx_buf[1] = BRIGHTNESS_MAX_PERCENT; Brightness = user_rx_buf[1]; } break; case 4: if(POWEROFF==deviceStatus)break; if(user_rx_buf[1] > SPEED_MAX_LEVEL) user_rx_buf[1] = SPEED_MAX_LEVEL; Speed =user_rx_buf[1]; break; case 6: if(POWEROFF==deviceStatus)break; temperature(user_rx_buf[1]); Mode=mode0; choose_mode_bh=0; choose_mode_falsg=CUSTOM_TEMPERATURE_MODE;//圆环模式 startTimer(&timer1,1); break; case 10: // if(POWEROFF==deviceStatus)break; if(user_rx_buf[1] ==0x00){ is_time=0; }else if(user_rx_buf[1] ==0x01){ is_time=1; } break; case 11: if(POWEROFF==deviceStatus)break; flag_1h=(unsigned long)(user_rx_buf[1] << 16) | (unsigned long )(user_rx_buf[2] << 8) | (unsigned long )user_rx_buf[3]; flag_1h=flag_1h*60000; //flag_1h=2000; break; case 9: break; } //set_TaskComps_timer(2,1000); } void scan_uart_ota(uint8_t *arr){ // uint8_t uart_handle = UART0_IDX; // buffer_complet_flag = false; // 清除标志 // xc_uart_send_data(uart_handle, arr,139); // 发送完整帧 } void scan_uart_usr(uint8_t *arr){ // uint8_t uart_handle = UART0_IDX; // buffer_complet_flag = false; // 清除标志 // xc_uart_send_data(uart_handle, arr,arr[1]); // 发送完整帧 }