From 183cc814465bd71591bb557c9a279c3883ddedd3 Mon Sep 17 00:00:00 2001 From: TuxMonkey <8196772+tuxmonkey@user.noreply.gitee.com> Date: Mon, 3 Nov 2025 22:14:44 +0800 Subject: [PATCH] add ws2812task --- .../application/indicator_app/ws2812status.c | 181 ++++++++++++++++++ .../application/indicator_app/ws2812status.h | 28 +++ User_Code/module/periph/ws2812/ws2812.c | 42 ++++ User_Code/module/periph/ws2812/ws2812.h | 12 ++ 4 files changed, 263 insertions(+) create mode 100644 User_Code/application/indicator_app/ws2812status.c create mode 100644 User_Code/application/indicator_app/ws2812status.h create mode 100644 User_Code/module/periph/ws2812/ws2812.c create mode 100644 User_Code/module/periph/ws2812/ws2812.h diff --git a/User_Code/application/indicator_app/ws2812status.c b/User_Code/application/indicator_app/ws2812status.c new file mode 100644 index 0000000..e56eebb --- /dev/null +++ b/User_Code/application/indicator_app/ws2812status.c @@ -0,0 +1,181 @@ +/** + * @file ws2812Task.c + * @brief WS2812 LED control task + * @author TuxMonkey (nqx2004@gmail.com) + * @version 1.0 + * @date 2025-07-08 + * + * @copyright Copyright (c) 2025 DLMU-C.ONE + * + * @par 修改日志: + * + *
Date Version Author Description + *
2025-07-08 1.0 TuxMonkey 内容 + *
+ */ + +//真的有点抽象了不太清楚什么问题,24V的时候用16的分频器可以 5V时候就只能用32的分频器了 spi6的 这个之后注意一下吧 如果要开发还是用32的分频器。 + +/*---------------------INCLUDES----------------------*/ +#include "ws2812Task.h" +#include "ws2812.h" +#include "cmsis_os.h" +#include "tim.h" +#include "delayticks.h" + +#include "robot_def.h" + +/*---------------------VARIABLES---------------------*/ +uint8_t r = 0; +uint8_t g = 0; +uint8_t b = 0; + +/*---------------------FUNCTIONS---------------------*/ +/** + * @brief WS2812 LED control task + * @param argument + */ +void ws2812Task(void const *argument) { + while (1) { + ////Test for mode changes + // if(HAL_GPIO_ReadPin(USER_KEY_GPIO_Port, USER_KEY_Pin) == 0) // 读取用户按键状态,可以用于调试或其他功能 + // { + // delay_ticks(50); + // if(HAL_GPIO_ReadPin(USER_KEY_GPIO_Port, USER_KEY_Pin) == 0) + // { + // RobotMode ++; + // if (RobotMode > 5) + // { + // RobotMode = 0; // 如果超过最大模式,则重置为0 + // } + // } + // } + // else{} + + switch (RobotMode) { + case NORMAL_MODE: + BlinkGreen(); + break; + case SYS_ERROR_OCCURRED: + BlinkRed(); + break; + case REMOTE_NOT_CONNECTED: + BlinkYellow(); + break; + case REMOTE_CONNECTED: + BlinkBlue(); + break; + case AUTO_SHOOTING_MODE: + BlinkCyan(); + break; + case IMU_CALIBERATION_MODE: + BlinkPurple(); + break; + default: + BlinkYellow(); + } + + osDelay(10); // 每个任务最后都要写在while里,要不然会导致任务切换失败 + } +} + +void BlinkYellow(void) { + r = 255; // 红色分量 + g = 255; // 绿色分量 + b = 0; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为黄色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void BlinkRed(void) { + r = 255; // 红色分量 + g = 0; // 绿色分量 + b = 0; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为红色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void BlinkGreen(void) { + r = 0; // 红色分量 + g = 255; // 绿色分量 + b = 0; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为绿色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void BlinkBlue(void) { + r = 0; // 红色分量 + g = 0; // 绿色分量 + b = 255; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为蓝色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void BlinkWhite(void) { + r = 255; // 红色分量 + g = 255; // 绿色分量 + b = 255; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为白色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void BlinkCyan(void) { + r = 0; // 红色分量 + g = 255; // 绿色分量 + b = 255; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为青色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void BlinkPurple(void) { + r = 255; // 红色分量 + g = 0; // 绿色分量 + b = 255; // 蓝色分量 + WS2812_Ctrl(r, g, b); // 设置为紫色 + delay_ticks(500); // 延时500毫秒 + WS2812_Ctrl(0, 0, 0); // 关闭LED灯 + delay_ticks(500); // 延时500毫秒 +} + +void PWMControwLed(void) { + // + + // WS2812_Ctrl(r, g, b); + // r++; + // g += 5; + // b += 10; + + // r=0;g=255;b=0;//绿色 + // WS2812_Ctrl(r, g, b); + // delay_ticks(500); // 使用延时函数代替osDelay,避免任务切换失败 + + // r=0;g=0;b=255;//蓝色 + // WS2812_Ctrl(r, g, b); + // delay_ticks(500); + + // r=0;g=255;b=255;//青色 + // WS2812_Ctrl(r, g, b); + // delay_ticks(500); + + // r=255;g=0;b=255;//紫色 + // WS2812_Ctrl(r, g, b); + // delay_ticks(500); + + // r=255;g=255;b=255; + // WS2812_Ctrl(r, g, b); + // delay_ticks(1000); + // osDelay(1); + // osDelay(100); +} diff --git a/User_Code/application/indicator_app/ws2812status.h b/User_Code/application/indicator_app/ws2812status.h new file mode 100644 index 0000000..214f13a --- /dev/null +++ b/User_Code/application/indicator_app/ws2812status.h @@ -0,0 +1,28 @@ +#ifndef ws2812Task_H +#define ws2812Task_H + +/*---------------------INCLUDES----------------------*/ +#include "main.h" +#include "cmsis_os.h" +#include "tim.h" +#include "ws2812.h" + +/*---------------------FUNCTIONS---------------------*/ +void ws2812Task(void const *argument); + +void BlinkYellow(void); + +void BlinkRed(void); + +void BlinkBlue(void); + +void BlinkGreen(void); + +void BlinkWhite(void); + +void BlinkCyan(void); + +void BlinkPurple(void); + + +#endif diff --git a/User_Code/module/periph/ws2812/ws2812.c b/User_Code/module/periph/ws2812/ws2812.c new file mode 100644 index 0000000..4bb6299 --- /dev/null +++ b/User_Code/module/periph/ws2812/ws2812.c @@ -0,0 +1,42 @@ +/** +* @file ws2812.c + * @brief WS2812 LED driver + * @author TuxMonkey (nqx2004@gmail.com) + * @version 1.0 + * @date 2025-07-08 + * + * @copyright Copyright (c) 2025 DLMU-C.ONE + * + * @par 修改日志: + * + *
Date Version Author Description + *
2025-07-08 1.0 TuxMonkey 内容 + *
+ */ + +#include "ws2812.h" + +#define WS2812_LowLevel 0xC0 // 0码 +#define WS2812_HighLevel 0xF0 // 1码 + +/** + * @brief + * @param [in] r My Param doc + * @param [in] g My Param doc + * @param [in] b My Param doc + */ +void WS2812_Ctrl(uint8_t r, uint8_t g, uint8_t b) { + uint8_t txbuf[24]; + uint8_t res = 0; + for (int i = 0; i < 8; i++) { + txbuf[7 - i] = (((g >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1; + txbuf[15 - i] = (((r >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1; + txbuf[23 - i] = (((b >> i) & 0x01) ? WS2812_HighLevel : WS2812_LowLevel) >> 1; + } + HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 0, 0xFFFF); + while (WS2812_SPI_UNIT.State != HAL_SPI_STATE_READY); + HAL_SPI_Transmit(&WS2812_SPI_UNIT, txbuf, 24, 0xFFFF); + for (int i = 0; i < 100; i++) { + HAL_SPI_Transmit(&WS2812_SPI_UNIT, &res, 1, 0xFFFF); + } +} diff --git a/User_Code/module/periph/ws2812/ws2812.h b/User_Code/module/periph/ws2812/ws2812.h new file mode 100644 index 0000000..c8db4b1 --- /dev/null +++ b/User_Code/module/periph/ws2812/ws2812.h @@ -0,0 +1,12 @@ +#ifndef WS2812_H +#define WS2812_H + + +#include "main.h" + + +#define WS2812_SPI_UNIT hspi6 +extern SPI_HandleTypeDef WS2812_SPI_UNIT; + +void WS2812_Ctrl(uint8_t r, uint8_t g, uint8_t b); +#endif