add ws2812task

This commit is contained in:
TuxMonkey
2025-11-03 22:14:44 +08:00
parent 7a113a32dd
commit 183cc81446
4 changed files with 263 additions and 0 deletions

View File

@@ -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 修改日志:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2025-07-08 <td>1.0 <td>TuxMonkey <td>内容
* </table>
*/
//真的有点抽象了不太清楚什么问题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);
}

View File

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

View File

@@ -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 修改日志:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2025-07-08 <td>1.0 <td>TuxMonkey <td>内容
* </table>
*/
#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);
}
}

View File

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