按驱动的函数名字,matrix可以运行了

This commit is contained in:
dgjames 2025-01-17 16:46:10 +08:00
parent 6cc3aa9d72
commit 5de7792f95
9 changed files with 107 additions and 316 deletions

View File

@ -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

View File

@ -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

View File

@ -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();
}

View File

@ -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();

View File

@ -174,5 +174,5 @@ void lv_port_indev_init(void)
```
### LED WS2812B
1. menuconfig 配置 pwm
1. menuconfig 配置 pwm/maxrix 驱动 使能
2. msh 报错 msh export 了两次复制的时候

View File

@ -1,216 +0,0 @@
#include "RGB.h"
#include "stdlib.h"
// #include "tim.h"
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdef.h>
/*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);
/*
240240PWM波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]11CCR寄存器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<Pixel_Len;i++)//给对应个数LED写入红色
// {
// RGB_SetOne_Color(i,RED);
// }
//}
//
//灯管实现函数(完成本期效果的实现)
void RGB_Show_64(void)
{
RGB_SetMore_Color(0,63,BLACK); //清空所有的LED数据
RGB_SetMore_Color(0,rand()%8,table[rand()%16]); //第一行随机个灯亮随机颜色
RGB_SetMore_Color(8,rand()%8+8,table[rand()%16]); //第二行。。。。以此类推
// RGB_SetMore_Color(16,rand()%8+16,table[rand()%16]);
// RGB_SetMore_Color(24,rand()%8+24,table[rand()%16]);
// RGB_SetMore_Color(32,rand()%8+32,table[rand()%16]);
// RGB_SetMore_Color(40,rand()%8+40,table[rand()%16]);
// RGB_SetMore_Color(48,rand()%8+48,table[rand()%16]);
// RGB_SetMore_Color(56,rand()%8+56,table[rand()%16]);
RGB_Flush(); //刷新WS2812B的显示
}

View File

@ -1,47 +0,0 @@
#ifndef __RGB_H__
#define __RGB_H__
#include "main.h"
//0码和1码的定义设置的时CCR寄存器的值
//由于使用的思PWM输出模式1计数值<CCR时输出有效电平-高电平CubeMX配置默认有效电平为高电平
#define CODE_1 (840) //1码定时器计数次数控制占空比为84/125 = 66%
#define CODE_0 (700) //0码定时器计数次数控制占空比为42/125 = 33%
extern uint32_t period, pulse, dir;
//单个LED的颜色控制结构体
typedef struct
{
uint8_t R;
uint8_t G;
uint8_t B;
}RGB_Color_TypeDef;
#define Pixel_NUM 64 //LED数量宏定义我们灯板上有64个
static void Reset_Load(void); //该函数用于将数组最后24个数据变为0代表RESET_code
//发送最终数组
static void RGB_SendArray(void);
static void RGB_Flush(void); //刷新RGB显示
void RGB_SetOne_Color(uint8_t LedId,RGB_Color_TypeDef Color);//给一个LED装载24个颜色数据码0码和1码
//void RGB_RED(uint16_t Pixel_Len); //显示红灯
//控制多个LED显示相同的颜色
void RGB_SetMore_Color(uint8_t head, uint8_t heal,RGB_Color_TypeDef color);
void RGB_Show_64(void); //RGB写入函数
int RGB_Init(void);
#endif

View File

@ -15,12 +15,38 @@
#include "infrared.h"
#include <ulog.h>
#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)
{

View File

@ -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