230 lines
6.4 KiB
C
230 lines
6.4 KiB
C
#include "indicator_led.h"
|
|
#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位 */
|
|
|
|
#define LED_NUM 24 // LED灯珠个数
|
|
|
|
rt_thread_t led_blink_thread = RT_NULL;
|
|
rt_thread_t led_breath_thread = RT_NULL;
|
|
|
|
// 灯是否处于特定颜色还是闪烁状态
|
|
uint8_t LED_Blink_State[LED_NUM] = {LED_NOT_BLINKING};
|
|
// 呼吸灯是否开启
|
|
uint8_t LED_Breath_State = LED_BREATH_OFF;
|
|
// 灯闪烁颜色缓存
|
|
RGBColor_TypeDef LED_Blink_Color[LED_NUM] = {0};
|
|
const RGBColor_TypeDef LED_OFF = {0, 0, 0};
|
|
const RGBColor_TypeDef LED_ON = {255, 255, 255};
|
|
/**
|
|
* @brief 设置呼吸LED的开关
|
|
* @param LedBreath_state 开/关 LED_BREATH_ON/LED_BREATH_OFF
|
|
*/
|
|
void LED_BreathTurn(uint8_t LedBreath_state)
|
|
{
|
|
LED_Breath_State = LedBreath_state;
|
|
// 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);
|
|
// }
|
|
}
|
|
/**
|
|
* @brief 设置特定LED的颜色或开关
|
|
* @param LedId LED的序号(0~LED_NUM-1)
|
|
* @param Color 颜色/开关 LED_RED,LED_BLUE,LED_OFF,LED_ON(白色)……
|
|
*/
|
|
void LED_Set(uint16_t LedId, RGBColor_TypeDef Color)
|
|
{
|
|
LedId=LED_CHARGE_ID(LedId);
|
|
LED_Blink_State[LedId] = 0;
|
|
Set_LEDColor(LedId, Color);
|
|
RGB_Reflash();
|
|
}
|
|
/**
|
|
* @brief 设置连续多个特定LED的颜色或开关
|
|
* @param LedId_begin LED的序号(0~LED_NUM-1)开始
|
|
* @param LedId_end LED的序号(0~LED_NUM-1)结束
|
|
* @param Color 颜色/开关 LED_RED,LED_BLUE,LED_OFF,LED_ON(白色)……
|
|
*/
|
|
void LED_SetMore(uint16_t LedId_begin, uint16_t LedId_end, RGBColor_TypeDef Color)
|
|
{
|
|
LedId_begin=LED_CHARGE_ID(LedId_begin);
|
|
LedId_end=LED_CHARGE_ID(LedId_end);
|
|
for (int LedId = LedId_begin; LedId <= LedId_end; LedId++)
|
|
{
|
|
LED_Blink_State[LedId] = 0;
|
|
Set_LEDColor(LedId, Color);
|
|
}
|
|
RGB_Reflash();
|
|
}
|
|
|
|
/**
|
|
* @brief 设置特定LED的闪烁
|
|
* @param LedId LED的序号(0~LED_NUM-1)
|
|
* @param Color 颜色 LED_RED,LED_BLUE……
|
|
*/
|
|
void LED_Blink(uint16_t LedId, RGBColor_TypeDef Color)
|
|
{
|
|
LedId=LED_CHARGE_ID(LedId);
|
|
LED_Blink_State[LedId] = 1;
|
|
LED_Blink_Color[LedId] = Color;
|
|
}
|
|
/**
|
|
* @brief 设置连续多个特定LED的闪烁
|
|
* @param LedId_begin LED的序号(0~LED_NUM-1)开始
|
|
* @param LedId_end LED的序号(0~LED_NUM-1)结束
|
|
* @param Color 颜色 LED_RED,LED_BLUE……
|
|
*/
|
|
void LED_BlinkMore(uint16_t LedId_begin, uint16_t LedId_end, RGBColor_TypeDef Color)
|
|
{
|
|
LedId_begin=LED_CHARGE_ID(LedId_begin);
|
|
LedId_end=LED_CHARGE_ID(LedId_end);
|
|
for (int LedId = LedId_begin; LedId <= LedId_end; LedId++)
|
|
{
|
|
LED_Blink_State[LedId] = 1;
|
|
LED_Blink_Color[LedId] = Color;
|
|
}
|
|
}
|
|
/**
|
|
* @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++)
|
|
{
|
|
if (LED_Blink_State[LedId] == LED_IS_BLINKING)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 流水灯
|
|
*/
|
|
void led_breath_entry(void *parameter)
|
|
{
|
|
int count = 0;
|
|
while (1)
|
|
{
|
|
for (int i = LED_BREATH_ID(1); i <= LED_BREATH_ID(12); i++)
|
|
{
|
|
if (LED_Breath_State==LED_BREATH_OFF)
|
|
{
|
|
rt_thread_mdelay(100);
|
|
i = LED_BREATH_ID(1);
|
|
continue;
|
|
}
|
|
|
|
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(40);
|
|
}
|
|
count = (count + 1) % 3;
|
|
}
|
|
}
|
|
|
|
#define BORAD_NOT_CORRECT 25
|
|
#define BORAD_CORRECT_VOL 250
|
|
|
|
/**
|
|
* @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;
|
|
if(vol>=BORAD_CORRECT_VOL)
|
|
{
|
|
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);
|
|
|
|
/**
|
|
* @brief LED的初始化
|
|
*/
|
|
int led_init(void)
|
|
{
|
|
if (borad_check()==BORAD_NOT_CORRECT)
|
|
{
|
|
return 0;
|
|
}
|
|
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;
|
|
}
|
|
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.
|
|
rt_thread_startup(led_blink_thread);
|
|
rt_thread_startup(led_breath_thread);
|
|
} |