From 5de7792f952a422c3828bca93ed0fdeb85e9709f Mon Sep 17 00:00:00 2001 From: dgjames <1943357252@qq.com> Date: Fri, 17 Jan 2025 16:46:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E9=A9=B1=E5=8A=A8=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=90=8D=E5=AD=97,matrix=E5=8F=AF=E4=BB=A5=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config | 3 +- .vscode/keil-assistant.log | 4 + board/ports/led_matrix/drv_matrix_led.c | 60 +++---- board/ports/led_matrix/drv_matrix_led.h | 11 +- my_error/README.md | 2 +- my_pro/RGB.c | 216 ------------------------ my_pro/RGB.h | 47 ------ my_pro/myproject.c | 78 +++++++-- rtconfig.h | 2 + 9 files changed, 107 insertions(+), 316 deletions(-) delete mode 100644 my_pro/RGB.c delete mode 100644 my_pro/RGB.h diff --git a/.config b/.config index 1f899c5..91d9b3a 100644 --- a/.config +++ b/.config @@ -1630,7 +1630,8 @@ CONFIG_RS485_UART_DEVICE_NAME="uart6" CONFIG_BSP_USING_SRAM=y CONFIG_BSP_USING_ONBOARD_LCD=y CONFIG_BSP_USING_ONBOARD_LCD_PWM_BL=y -# CONFIG_BSP_USING_ONBOARD_LED_MATRIX is not set +CONFIG_BSP_USING_ONBOARD_LED_MATRIX=y +CONFIG_BSP_USING_LED_MATRIX_RS485_DEMO=y # CONFIG_BSP_USING_LVGL is not set CONFIG_BSP_USING_SPI_FLASH=y # CONFIG_BSP_USING_EEPROM is not set diff --git a/.vscode/keil-assistant.log b/.vscode/keil-assistant.log index 32bcfc5..79e3cc8 100644 --- a/.vscode/keil-assistant.log +++ b/.vscode/keil-assistant.log @@ -14,3 +14,7 @@ [info] Log at : 2025/1/16|15:40:17|GMT+0800 +[info] Log at : 2025/1/17|15:45:42|GMT+0800 + +[info] Log at : 2025/1/17|15:52:06|GMT+0800 + diff --git a/board/ports/led_matrix/drv_matrix_led.c b/board/ports/led_matrix/drv_matrix_led.c index 7659717..ef15e17 100644 --- a/board/ports/led_matrix/drv_matrix_led.c +++ b/board/ports/led_matrix/drv_matrix_led.c @@ -45,11 +45,11 @@ const uint8_t tile[] = {2, 7}; // 常见颜色定义 -const RGBColor_TypeDef DARK = {0, 0, 0}; -const RGBColor_TypeDef GREEN = {255, 0, 0}; -const RGBColor_TypeDef RED = {0, 255, 0}; -const RGBColor_TypeDef BLUE = {0, 0, 255}; -const RGBColor_TypeDef WHITE = {255, 255, 255}; +const RGBColor_TypeDef LED_DARK = {0, 0, 0}; +const RGBColor_TypeDef LED_GREEN = {255, 0, 0}; +const RGBColor_TypeDef LED_RED = {0, 255, 0}; +const RGBColor_TypeDef LED_BLUE = {0, 0, 255}; +const RGBColor_TypeDef LED_WHITE = {255, 255, 255}; const RGBColor_TypeDef LT_RED = {0, 32, 0}; const RGBColor_TypeDef LT_GREEN = {32, 0, 0}; const RGBColor_TypeDef LT_BLUE = {0, 0, 32}; @@ -247,16 +247,16 @@ void led_matrix_fill_test(uint8_t index) void led_matrix_test1() { rt_memset(RGB_Data, 0x00, sizeof(RGB_Data)); - Set_LEDColor(0, RED); - Set_LEDColor(1, GREEN); - Set_LEDColor(2, BLUE); - Set_LEDColor(3, RED); - Set_LEDColor(4, GREEN); - Set_LEDColor(5, BLUE); - Set_LEDColor(6, RED); - Set_LEDColor(7, GREEN); - Set_LEDColor(8, BLUE); - Set_LEDColor(9, WHITE); + Set_LEDColor(0, LED_RED); + Set_LEDColor(1, LED_GREEN); + Set_LEDColor(2, LED_BLUE); + Set_LEDColor(3, LED_RED); + Set_LEDColor(4, LED_GREEN); + Set_LEDColor(5, LED_BLUE); + Set_LEDColor(6, LED_RED); + Set_LEDColor(7, LED_GREEN); + Set_LEDColor(8, LED_BLUE); + Set_LEDColor(9, LED_WHITE); // led_matrix_rst(); RGB_Reflash(); } @@ -265,22 +265,22 @@ MSH_CMD_EXPORT(led_matrix_test1, Test led matrix on board) void led_matrix_test2() { rt_memset(RGB_Data, 0x00, sizeof(RGB_Data)); - Set_LEDColor(0, BLUE); - Set_LEDColor(1, RED); - Set_LEDColor(2, GREEN); - Set_LEDColor(3, BLUE); - Set_LEDColor(4, RED); - Set_LEDColor(5, GREEN); - Set_LEDColor(6, BLUE); - Set_LEDColor(7, RED); - Set_LEDColor(8, GREEN); - Set_LEDColor(9, RED); + Set_LEDColor(0, LED_BLUE); + Set_LEDColor(1, LED_RED); + Set_LEDColor(2, LED_GREEN); + Set_LEDColor(3, LED_BLUE); + Set_LEDColor(4, LED_RED); + Set_LEDColor(5, LED_GREEN); + Set_LEDColor(6, LED_BLUE); + Set_LEDColor(7, LED_RED); + Set_LEDColor(8, LED_GREEN); + Set_LEDColor(9, LED_RED); - Set_LEDColor(14, GREEN); - Set_LEDColor(15, GREEN); - Set_LEDColor(16, BLUE); - Set_LEDColor(17, RED); - Set_LEDColor(18, WHITE); + Set_LEDColor(14, LED_GREEN); + Set_LEDColor(15, LED_GREEN); + Set_LEDColor(16, LED_BLUE); + Set_LEDColor(17, LED_RED); + Set_LEDColor(18, LED_WHITE); RGB_Reflash(); } diff --git a/board/ports/led_matrix/drv_matrix_led.h b/board/ports/led_matrix/drv_matrix_led.h index 87b8992..d506590 100644 --- a/board/ports/led_matrix/drv_matrix_led.h +++ b/board/ports/led_matrix/drv_matrix_led.h @@ -9,12 +9,11 @@ typedef struct RGBColor_TypeDef uint8_t B; } RGBColor_TypeDef; // 颜色结构体 -extern const RGBColor_TypeDef DARK; -extern const RGBColor_TypeDef GREEN; -extern const RGBColor_TypeDef RED; -extern const RGBColor_TypeDef BLUE; -extern const RGBColor_TypeDef WHITE; - +extern const RGBColor_TypeDef LED_DARK; +extern const RGBColor_TypeDef LED_GREEN; +extern const RGBColor_TypeDef LED_RED; +extern const RGBColor_TypeDef LED_BLUE; +extern const RGBColor_TypeDef LED_WHITE; extern void Set_LEDColor(uint16_t LedId, RGBColor_TypeDef Color); extern void RGB_Reflash(void); extern void led_matrix_rst(); diff --git a/my_error/README.md b/my_error/README.md index 1fba172..9ef7ab2 100644 --- a/my_error/README.md +++ b/my_error/README.md @@ -174,5 +174,5 @@ void lv_port_indev_init(void) ``` ### LED WS2812B -1. menuconfig 配置 pwm +1. menuconfig 配置 pwm/maxrix 驱动 使能 2. msh 报错 msh export 了两次复制的时候 diff --git a/my_pro/RGB.c b/my_pro/RGB.c deleted file mode 100644 index b70fa7e..0000000 --- a/my_pro/RGB.c +++ /dev/null @@ -1,216 +0,0 @@ -#include "RGB.h" -#include "stdlib.h" -// #include "tim.h" -#include -#include -#include - -/*Some Static Colors------------------------------*/ -const RGB_Color_TypeDef RED = {255,0,0}; //红色 -const RGB_Color_TypeDef GREEN = {0,255,0}; //绿色 -const RGB_Color_TypeDef BLUE = {0,0,255}; //深蓝色 -const RGB_Color_TypeDef SKY = {0,255,255}; //天蓝色 -const RGB_Color_TypeDef MAGENTA = {255,0,220}; //粉色 -const RGB_Color_TypeDef YELLOW = {128,216,0}; //黄色 -const RGB_Color_TypeDef OEANGE = {127,106,0}; //橘色 -const RGB_Color_TypeDef BLACK = {0,0,0}; //无颜色 -const RGB_Color_TypeDef WHITE = {255,255,255}; //白色 - -uint32_t period, pulse, dir; - -//将好看的颜色封装成数组,便于集中管理和访问 -RGB_Color_TypeDef table[16] = -{ - {254,67,101}, - {76,0,10}, - {249,15,173}, - {128,0,32}, - {158,46,36}, - {184,206,142}, - {227,23,13}, - {178,34,34}, - {255,99,71}, - {99,38,18}, - {255,97,0}, - {21,161,201}, - {56,94,15}, - {50,205,50}, - {160,32,240}, - {218,60,90} -}; - //这些是好看的颜色 -const RGB_Color_TypeDef color1 = {254,67,101}; -//const RGB_Color_TypeDef color2 = {76,0,10}; -//const RGB_Color_TypeDef color3 = {249,15,173}; -//const RGB_Color_TypeDef color4 = {128,0,32}; -//const RGB_Color_TypeDef color5 = {158,46,36}; -//const RGB_Color_TypeDef color6 = {184,206,142}; -//const RGB_Color_TypeDef color7 = {227,23,13}; -//const RGB_Color_TypeDef color8 = {178,34,34}; -//const RGB_Color_TypeDef color9 = {255,99,71}; -//const RGB_Color_TypeDef color10 ={99,38,18}; -//const RGB_Color_TypeDef color11= {255,97,0}; -//const RGB_Color_TypeDef color12= {21,161,201}; -//const RGB_Color_TypeDef color13= {56,94,15}; -//const RGB_Color_TypeDef color14= {50,205,50}; -//const RGB_Color_TypeDef color15= {160,32,240}; -//const RGB_Color_TypeDef color16= {218,60,90}; - - -/*二维数组存放最终PWM输出数组,每一行24个数据代表一个LED,最后一行24个0用于复位*/ -uint32_t Pixel_Buf[Pixel_NUM+1][24]; - - -#define PWM_DEV_NAME "pwm3" /* PWM设备名称 */ -#define PWM_DEV_CHANNEL 2 /* PWM通道 */ - -struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */ - -int RGB_Init(void) -{ - period = 1250; /* 周期为1.25us,单位为纳秒ns */ - dir = 1; /* PWM脉冲宽度值的增减方向 */ - pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */ - - /* 查找设备 */ - pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME); - if (pwm_dev == RT_NULL) - { - rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME); - return RT_ERROR; - } - - /* 设置PWM周期和脉冲宽度默认值 */ - rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse); - /* 使能设备 */ - rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL); -} - -// static int pwm_led_sample(int argc, char *argv[]) -// { - -// while (1) -// { -// rt_thread_mdelay(50); -// if (dir) -// { -// pulse += 5000; /* 从0值开始每次增加5000ns */ -// } -// else -// { -// pulse -= 5000; /* 从最大值开始每次减少5000ns */ -// } -// if (pulse >= period) -// { -// dir = 0; -// } -// if (0 == pulse) -// { -// dir = 1; -// } - - -// } -// } -// /* 导出到 msh 命令列表中 */ -// MSH_CMD_EXPORT(pwm_led_sample, pwm sample); - - -/* -功能:最后一行装在24个0,输出24个周期占空比为0的PWM波,作为最后reset延时,这里总时长为24*1.25=37.5us > 24us(要求大于24us) -//如果出现无法复位的情况,只需要在增加数组Pixel_Buf[Pixel_NUM+1][24]的行数,并改写Reset_Load即可,这里不做演示了, -*/ -static void Reset_Load(void) -{ - uint8_t i; - for(i=0;i<24;i++) - { - Pixel_Buf[Pixel_NUM][i] = 0; - } -} - -/* -功能:发送数组Pixel_Buf[Pixel_NUM+1][24]内的数据,发送的数据被存储到定时器1通道1的CCR寄存器,用于控制PWM占空比 -参数:(&htim1)定时器1,(TIM_CHANNEL_1)通道1,((uint32_t *)Pixel_Buf)待发送数组, - (Pixel_NUM+1)*24)发送个数,数组行列相乘 -*/ -#define LED_MAXNUM 16 -static void RGB_SendArray(void) -{ - - for(int i=1;i<=LED_MAXNUM;i++) - { - for(int j=0;j<24;j++) - { - /* 设置PWM周期和脉冲宽度 */ - rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, Pixel_Buf[i][j]); - rt_thread_delay(1); - } - } - // HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_2, (uint32_t *)Pixel_Buf,(Pixel_NUM+1)*24); -} - -/* -功能:设定单个RGB LED的颜色,把结构体中RGB的24BIT转换为0码和1码 -参数:LedId为LED序号,Color:定义的颜色结构体 -*/ - //刷新WS2812B灯板显示函数 -static void RGB_Flush(void) -{ - Reset_Load(); //复位 - RGB_SendArray(); //发送数据 - - -} - -void RGB_SetOne_Color(uint8_t LedId,RGB_Color_TypeDef Color) -{ - uint8_t i; - if(LedId > Pixel_NUM)return; //avoid overflow 防止写入ID大于LED总数 - //这里是对 Pixel_Buf[LedId][i]写入一个周期内高电平的持续时间(或者说时PWM的占空比寄存器CCR1), - for(i=0;i<8;i++) Pixel_Buf[LedId][i] = ( ((Color.G/5) & (1 << (7 -i)))? (CODE_1):CODE_0 );//数组某一行0~7转化存放G - for(i=8;i<16;i++) Pixel_Buf[LedId][i] = ( ((Color.R/5) & (1 << (15-i)))? (CODE_1):CODE_0 );//数组某一行8~15转化存放R - for(i=16;i<24;i++) Pixel_Buf[LedId][i] = ( ((Color.B/5) & (1 << (23-i)))? (CODE_1):CODE_0 );//数组某一行16~23转化存放B -} - -//调用RGB_SetOne_Color函数,完成对多个LED的颜色设置。 -void RGB_SetMore_Color(uint8_t head, uint8_t heal,RGB_Color_TypeDef color) -{ - uint8_t i=0; - for(i=head;i<=heal;i++) - { - RGB_SetOne_Color(i,color) ; - } -} - - - -//用来显示单个颜色的函数,只能从第一个开始显示,不好用 -//void RGB_RED(uint16_t Pixel_Len) -//{ -// uint16_t i; -// for(i=0;i #include "my_func.h" -#include "RGB.h" +#include "drv_matrix_led.h" #define THREAD_PRIORITY 25 #define THREAD_STACK_SIZE 4096 #define THREAD_TIMESLICE 5 +#define BSP_USING_ONBOARD_LED_MATRIX + +/* define LED */ +enum{ + EXTERN_LED_0, + EXTERN_LED_1, + EXTERN_LED_2, + EXTERN_LED_3, + EXTERN_LED_4, + EXTERN_LED_5, + EXTERN_LED_6, + EXTERN_LED_7, + EXTERN_LED_8, + EXTERN_LED_9, + EXTERN_LED_10, + EXTERN_LED_11, + EXTERN_LED_12, + EXTERN_LED_13, + EXTERN_LED_14, + EXTERN_LED_15, + EXTERN_LED_16, + EXTERN_LED_17, + EXTERN_LED_18, +}; + +rt_thread_t led_matrix_thread = RT_NULL; rt_thread_t MQTT_Thread = RT_NULL; rt_thread_t Snake_Thread = RT_NULL; rt_thread_t Infrared_Thread = RT_NULL; @@ -406,12 +432,37 @@ void cdc_entry(void *parameter) rt_thread_mdelay(500); } } -void led_entry(void *parameter) +void led_matrix_example_entry(void *parameter) { + // while (1) + // { + // RGB_Show_64(); // 调用RGB灯板显示函数 + // rt_thread_mdelay(500); + // } + int count = 0; while (1) { - RGB_Show_64(); // 调用RGB灯板显示函数 - rt_thread_mdelay(500); + for (int i = EXTERN_LED_0; i <= EXTERN_LED_18; i++) + { + switch (count) + { + case 0: + Set_LEDColor(i, LED_RED); + break; + case 1: + Set_LEDColor(i, LED_GREEN); + break; + case 2: + Set_LEDColor(i, LED_BLUE); + break; + default: + return; + break; + } + RGB_Reflash(); + rt_thread_delay(20); + } + count = (count + 1) % 3; } } // /* 用于接收消息的信号量 */ @@ -581,20 +632,17 @@ void inf_init(void) } } MSH_CMD_EXPORT_ALIAS(inf_init, inf, "Infrared"); -void led_init(void) +int led_init(void) { - RGB_Init(); - - LED_Thread = rt_thread_create("LED_Thread", led_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE); - - if (LED_Thread != RT_NULL) + led_matrix_thread = rt_thread_create("led matrix demo", led_matrix_example_entry, RT_NULL, 1024, 20, 20); + if(led_matrix_thread == RT_NULL) { - rt_thread_startup(LED_Thread); - } - else - { - rt_kprintf("LED_Thread Create Failed!\n"); + rt_kprintf("led matrix demo thread creat failed!\n"); + return 0; } + rt_thread_mdelay(200); // avoid multi-thread on LED matrix transmit. + rt_thread_startup(led_matrix_thread); + } void tst_init(void) { diff --git a/rtconfig.h b/rtconfig.h index da0e167..cdf16f8 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -622,6 +622,8 @@ #define BSP_USING_SRAM #define BSP_USING_ONBOARD_LCD #define BSP_USING_ONBOARD_LCD_PWM_BL +#define BSP_USING_ONBOARD_LED_MATRIX +#define BSP_USING_LED_MATRIX_RS485_DEMO #define BSP_USING_SPI_FLASH #define BSP_USING_FS #define BSP_USING_FLASH_FATFS