SumProject/my_pro/indicator_led.c

230 lines
6.4 KiB
C
Raw Normal View History

2025-01-17 21:37:00 +08:00
#include "indicator_led.h"
2025-01-18 12:41:57 +08:00
#include "rtdevice.h"
#define ADC_DEV_NAME "adc1" /* ADC 设备名称 */
#define ADC_DEV_CHANNEL 5 /* ADC 通道 */
#define REFER_VOLTAGE 330 /* 参考电压 3.3V,数据精度乘以100保留2位小数*/
#define CONVERT_BITS (1 << 12) /* 转换位数为12位 */
2025-01-17 21:37:00 +08:00
2025-01-18 00:46:10 +08:00
#define LED_NUM 24 // LED灯珠个数
2025-01-17 21:37:00 +08:00
rt_thread_t led_blink_thread = RT_NULL;
2025-01-18 00:46:10 +08:00
rt_thread_t led_breath_thread = RT_NULL;
2025-01-17 21:37:00 +08:00
2025-01-18 00:46:10 +08:00
// 灯是否处于特定颜色还是闪烁状态
uint8_t LED_Blink_State[LED_NUM] = {LED_NOT_BLINKING};
2025-01-18 11:04:08 +08:00
// 呼吸灯是否开启
uint8_t LED_Breath_State = LED_BREATH_OFF;
2025-01-17 21:37:00 +08:00
// 灯闪烁颜色缓存
RGBColor_TypeDef LED_Blink_Color[LED_NUM] = {0};
const RGBColor_TypeDef LED_OFF = {0, 0, 0};
const RGBColor_TypeDef LED_ON = {255, 255, 255};
2025-01-18 11:04:08 +08:00
/**
* @brief LED的开关
* @param LedBreath_state / LED_BREATH_ON/LED_BREATH_OFF
*/
void LED_BreathTurn(uint8_t LedBreath_state)
{
LED_Breath_State = LedBreath_state;
2025-01-18 12:50:27 +08:00
// if (LedBreath_state == LED_BREATH_OFF)
// {
// // rt_thread_suspend(led_breath_thread);
// LED_SetMore(LED_BREATH_ID(1), LED_BREATH_ID(12), LED_OFF);
// }
// else if (LedBreath_state == LED_BREATH_ON)
// {
// // rt_thread_resume(led_breath_thread);
// }
2025-01-18 11:04:08 +08:00
}
2025-01-17 21:37:00 +08:00
/**
* @brief LED的颜色或开关
2025-01-18 11:23:32 +08:00
* @param LedId LED的序号(0~LED_NUM-1)
2025-01-17 21:37:00 +08:00
* @param Color / LED_RED,LED_BLUE,LED_OFF,LED_ON()
*/
void LED_Set(uint16_t LedId, RGBColor_TypeDef Color)
{
2025-01-18 11:23:32 +08:00
LedId=LED_CHARGE_ID(LedId);
2025-01-18 00:46:10 +08:00
LED_Blink_State[LedId] = 0;
2025-01-17 21:37:00 +08:00
Set_LEDColor(LedId, Color);
RGB_Reflash();
}
/**
* @brief LED的颜色或开关
2025-01-18 11:23:32 +08:00
* @param LedId_begin LED的序号(0~LED_NUM-1)
* @param LedId_end LED的序号(0~LED_NUM-1)
2025-01-17 21:37:00 +08:00
* @param Color / LED_RED,LED_BLUE,LED_OFF,LED_ON()
*/
void LED_SetMore(uint16_t LedId_begin, uint16_t LedId_end, RGBColor_TypeDef Color)
{
2025-01-18 11:23:32 +08:00
LedId_begin=LED_CHARGE_ID(LedId_begin);
LedId_end=LED_CHARGE_ID(LedId_end);
2025-01-18 11:04:08 +08:00
for (int LedId = LedId_begin; LedId <= LedId_end; LedId++)
2025-01-17 21:37:00 +08:00
{
2025-01-18 00:46:10 +08:00
LED_Blink_State[LedId] = 0;
2025-01-17 21:53:03 +08:00
Set_LEDColor(LedId, Color);
2025-01-17 21:37:00 +08:00
}
RGB_Reflash();
}
/**
* @brief LED的闪烁
2025-01-18 11:23:32 +08:00
* @param LedId LED的序号(0~LED_NUM-1)
2025-01-17 21:37:00 +08:00
* @param Color LED_RED,LED_BLUE
*/
void LED_Blink(uint16_t LedId, RGBColor_TypeDef Color)
{
2025-01-18 11:23:32 +08:00
LedId=LED_CHARGE_ID(LedId);
2025-01-18 00:46:10 +08:00
LED_Blink_State[LedId] = 1;
2025-01-17 21:37:00 +08:00
LED_Blink_Color[LedId] = Color;
}
/**
* @brief LED的闪烁
2025-01-18 11:23:32 +08:00
* @param LedId_begin LED的序号(0~LED_NUM-1)
* @param LedId_end LED的序号(0~LED_NUM-1)
2025-01-17 21:37:00 +08:00
* @param Color LED_RED,LED_BLUE
*/
void LED_BlinkMore(uint16_t LedId_begin, uint16_t LedId_end, RGBColor_TypeDef Color)
{
2025-01-18 11:23:32 +08:00
LedId_begin=LED_CHARGE_ID(LedId_begin);
LedId_end=LED_CHARGE_ID(LedId_end);
2025-01-18 11:04:08 +08:00
for (int LedId = LedId_begin; LedId <= LedId_end; LedId++)
2025-01-17 21:37:00 +08:00
{
2025-01-18 00:46:10 +08:00
LED_Blink_State[LedId] = 1;
2025-01-17 21:37:00 +08:00
LED_Blink_Color[LedId] = Color;
2025-01-18 11:04:08 +08:00
}
2025-01-17 21:37:00 +08:00
}
/**
* @brief 0.5s判断各盏灯是否要翻转
*/
void led_blink_entry(void *parameter)
{
uint8_t LED_Blink_ON = 1;
while (1)
{
for (int LedId = 0; LedId < LED_NUM; LedId++)
{
2025-01-18 11:04:08 +08:00
if (LED_Blink_State[LedId] == LED_IS_BLINKING)
2025-01-17 21:37:00 +08:00
{
if (LED_Blink_ON)
{
Set_LEDColor(LedId, LED_Blink_Color[LedId]);
}
else
{
Set_LEDColor(LedId, LED_OFF);
}
}
}
LED_Blink_ON = !LED_Blink_ON;
RGB_Reflash();
rt_thread_mdelay(500);
}
}
2025-01-18 00:46:10 +08:00
/**
2025-01-18 11:04:08 +08:00
* @brief
2025-01-18 00:46:10 +08:00
*/
void led_breath_entry(void *parameter)
{
int count = 0;
while (1)
{
for (int i = LED_BREATH_ID(1); i <= LED_BREATH_ID(12); i++)
{
2025-01-18 11:04:08 +08:00
if (LED_Breath_State==LED_BREATH_OFF)
{
rt_thread_mdelay(100);
i = LED_BREATH_ID(1);
continue;
}
2025-01-18 00:46:10 +08:00
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();
2025-01-18 12:50:27 +08:00
rt_thread_delay(40);
2025-01-18 00:46:10 +08:00
}
count = (count + 1) % 3;
}
}
2025-01-18 12:41:57 +08:00
#define BORAD_NOT_CORRECT 25
2025-01-18 12:58:20 +08:00
#define BORAD_CORRECT_VOL 250
2025-01-18 12:41:57 +08:00
/**
* @brief
*/
static int borad_check(void)
{
rt_adc_device_t adc_dev;
rt_uint32_t value, vol;
rt_err_t ret = RT_EOK;
/* 查找设备 */
adc_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", ADC_DEV_NAME);
return RT_ERROR;
}
/* 使能设备 */
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
/* 读取采样值 */
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
/* 转换为对应电压值 */
vol = value * REFER_VOLTAGE / CONVERT_BITS;
2025-01-18 12:58:20 +08:00
if(vol>=BORAD_CORRECT_VOL)
2025-01-18 12:41:57 +08:00
{
rt_kprintf("NOT correct borad!\n");
rt_kprintf("the value is :%d \n", value);
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
return BORAD_NOT_CORRECT;
}
/* 关闭通道 */
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(borad_check, adc voltage convert sample);
2025-01-18 11:04:08 +08:00
/**
2025-01-18 11:51:19 +08:00
* @brief LED的初始化
2025-01-18 11:04:08 +08:00
*/
2025-01-18 11:51:19 +08:00
int led_init(void)
2025-01-18 00:46:10 +08:00
{
2025-01-18 12:41:57 +08:00
if (borad_check()==BORAD_NOT_CORRECT)
{
return 0;
}
2025-01-18 11:51:19 +08:00
led_blink_thread = rt_thread_create("led blink control thread", led_blink_entry, RT_NULL, 1024, 20, 20);
if (led_blink_thread == RT_NULL)
{
rt_kprintf("led blink control thread creat failed!\n");
return 0;
}
2025-01-18 00:46:10 +08:00
led_breath_thread = rt_thread_create("led breath control thread", led_breath_entry, RT_NULL, 1024, 20, 20);
if (led_breath_thread == RT_NULL)
{
rt_kprintf("led breath control thread creat failed!\n");
return 0;
}
rt_thread_mdelay(200); // avoid multi-thread on LED matrix transmit.
2025-01-18 11:51:19 +08:00
rt_thread_startup(led_blink_thread);
2025-01-18 00:46:10 +08:00
rt_thread_startup(led_breath_thread);
2025-01-17 21:37:00 +08:00
}