86 lines
1.9 KiB
C
86 lines
1.9 KiB
C
#include "includes.h"
|
||
|
||
/* ==================== 氛围灯循环状态 ==================== */
|
||
#define RGB_INTERVAL_MS 300 /* 颜色切换间隔 300ms */
|
||
|
||
static const uint8_t auto_rgb_seq[] = {
|
||
COLOR_CYA, /* 青 */
|
||
COLOR_B, /* 蓝 */
|
||
COLOR_G, /* 绿 */
|
||
COLOR_R, /* 红 */
|
||
};
|
||
#define AUTO_RGB_CNT (sizeof(auto_rgb_seq)/sizeof(auto_rgb_seq[0]))
|
||
|
||
static uint8_t rgb_state = 0;
|
||
static uint32_t rgb_tick_stamp = 0;
|
||
|
||
static uint8_t key_last = KEY_NULL;
|
||
|
||
void app_tm1617_init(void)
|
||
{
|
||
rt_mutex_take(TM1617_Mutex, RT_WAITING_FOREVER);
|
||
TM1617_Init(); /* 清零 + 显示模式 + 初始亮度 */
|
||
TM1617_SetRGB(COLOR_OFF, 0); /* 确保熄灭 */
|
||
rt_mutex_release(TM1617_Mutex);
|
||
|
||
rgb_state = 0;
|
||
rgb_tick_stamp = 0;
|
||
key_last = KEY_NULL;
|
||
}
|
||
|
||
void app_tm1617_set_rgb(uint8_t color, uint8_t brightness)
|
||
{
|
||
if (brightness > 7)
|
||
brightness = 7;
|
||
|
||
rt_mutex_take(TM1617_Mutex, RT_WAITING_FOREVER);
|
||
TM1617_SetRGB(color, brightness);
|
||
rt_mutex_release(TM1617_Mutex);
|
||
}
|
||
|
||
void app_tm1617_off(void)
|
||
{
|
||
rt_mutex_take(TM1617_Mutex, RT_WAITING_FOREVER);
|
||
TM1617_SetRGB(COLOR_OFF, 0);
|
||
rt_mutex_release(TM1617_Mutex);
|
||
}
|
||
|
||
/* 氛围灯自动循环:300ms 切换一次颜色 */
|
||
void app_tm1617_auto_loop(void)
|
||
{
|
||
uint32_t now = rt_tick_get();
|
||
|
||
if ((now - rgb_tick_stamp) >= RGB_INTERVAL_MS)
|
||
{
|
||
rgb_tick_stamp = now;
|
||
|
||
if (rgb_state >= AUTO_RGB_CNT)
|
||
rgb_state = 0;
|
||
|
||
rt_mutex_take(TM1617_Mutex, RT_WAITING_FOREVER);
|
||
TM1617_SetRGB(auto_rgb_seq[rgb_state], 7);
|
||
rt_mutex_release(TM1617_Mutex);
|
||
|
||
rgb_state++;
|
||
if (rgb_state >= AUTO_RGB_CNT)
|
||
rgb_state = 0;
|
||
}
|
||
}
|
||
|
||
uint8_t app_tm1617_scan_key(void)
|
||
{
|
||
uint8_t key;
|
||
|
||
rt_mutex_take(TM1617_Mutex, RT_WAITING_FOREVER);
|
||
key = (uint8_t)TM1617_ScanFkey();
|
||
rt_mutex_release(TM1617_Mutex);
|
||
|
||
if (key != key_last)
|
||
{
|
||
key_last = key;
|
||
TM1617_Handle(key);
|
||
return key;
|
||
}
|
||
return KEY_NULL;
|
||
}
|