本帖最后由 lovzx 于 2024-5-4 20:02 编辑
本帖最后由 lovzx 于 2024-5-4 20:02 编辑
本帖最后由 lovzx 于 2024-5-4 20:01 编辑
M61-32SU-Kit RGB DEMO
准备工作
- 开发环境 这一步可以参考论坛是各位大佬的教程,写的很详细
- 开发板
因为之前搭建过小安派的开发环境,M61-32开发板和小安派一样都是用的BL616/BL618芯片,这俩芯片开发环境是可以通用的,所以我这边就没有配置什么就可以直接上手写代码了
项目思路
考虑到开发板上有RGB的灯和板载的用户按键,首先就想到了用按键控制RGB的开启和关闭,其次考虑到RGB是内置了三颗红绿蓝的led灯珠,于是就有了通过shell命令来单独控制每一颗灯珠的亮度,控制LED灯亮度首选PWM方式,以上就是全部的功能了
- 板载按键控制RGB开启和关闭
- 通过shell命令单独控制RGB三颗小灯珠的亮度
最终实现的shell命令有下面3个:
- rgb_on 开启RGB灯
- rgb_off 关闭RGB灯
- rgb 控制rgb灯珠亮度,参数可以是0xFF00FF十六进制,也可以是255.200.22十进制格式,数据从高位到低位分别对应的是R,G,B三颗灯珠的参数
运行效果
<iframe src="https://player.bilibili.com/player.html?aid=1454225146&bvid=BV1Bi421k7X6&cid=1530903051&p=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>
代码
按键key代码
//板载key init
gpio = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio, PIN_KEY, GPIO_INPUT | GPIO_PULLDOWN);
bflb_gpio_set(gpio, PIN_KEY);
然后就是在while中循环获取到GPIO的状态通过状态机更新用户事件,然后在回调里面触发RGB动作
//扫描GPIO状态
while (1) {
button_scan();
bflb_mtimer_delay_ms(10);
}
-
读取按键GPIO数据
uint8_t user_button_read(void) {
return bflb_gpio_read(gpio, PIN_KEY);
}
-
更新RGB状态
-
void key_response_callback(void* argv)
{
if (button_event_read(btn) == BTN_EVENT_PRESS_CLICK) {
printf("event: down\n");
rgb_toggle();
}
}
rgb设备初始化
void rgb_init()
{
gpio = bflb_device_get_by_name("gpio");
pwm = bflb_device_get_by_name("pwm_v2_0");
//初始化GPIO为PWM功能
bflb_gpio_init(gpio, RGB_R, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, RGB_G, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
bflb_gpio_init(gpio, RGB_B, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
//pwm配置
struct bflb_pwm_v2_config_s cfg = {
.clk_source = BFLB_SYSTEM_PBCLK,
.clk_div = 80,
.period = 2550,
};
bflb_pwm_v2_init(pwm, &cfg);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_R, 0, 2550);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_G, 0, 2550);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_B, 0, 2500);
bflb_pwm_v2_start(pwm);
}
void rgb_light_on()
{
if (light_status != 1)
{
light_status = 1;
bflb_pwm_v2_channel_negative_start(pwm, PWM_R);
bflb_pwm_v2_channel_positive_start(pwm, PWM_R);
bflb_pwm_v2_channel_positive_start(pwm, PWM_G);
bflb_pwm_v2_channel_negative_start(pwm, PWM_G);
bflb_pwm_v2_channel_positive_start(pwm, PWM_B);
bflb_pwm_v2_channel_negative_start(pwm, PWM_B);
bflb_pwm_v2_start(pwm);
}
}
void rgb_light_off()
{
if (light_status != 0) {
light_status = 0;
//R
bflb_pwm_v2_channel_negative_stop(pwm, PWM_R);
bflb_pwm_v2_channel_positive_stop(pwm, PWM_R);
//G
bflb_pwm_v2_channel_positive_stop(pwm, PWM_G);
bflb_pwm_v2_channel_negative_stop(pwm, PWM_G);
//B
bflb_pwm_v2_channel_positive_stop(pwm, PWM_B);
bflb_pwm_v2_channel_negative_stop(pwm, PWM_B);
bflb_pwm_v2_stop(pwm);
}
}
int rgb_update(int r, int g, int b) {
printf("r: %d, g: %d, b: %d\n", r, g, b);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_R, 0, r * 10);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_G, 0, g * 10);
bflb_pwm_v2_channel_set_threshold(pwm, PWM_B, 0, b * 10);
if (light_status == 0) {
rgb_light_on();
}
}
/**
* @brief 开灯
* @param argc
* @param argv
* @return int
*/
int rgb_shell_on(int argc, const char** argv)
{
rgb_light_on();
return 0;
}
/**
* @brief 关灯
* @param argc
* @param argv
* @return int
*/
int rgb_shell_off(int argc, const char** argv)
{
rgb_light_off();
return 0;
}
/**
* rgb 0xff0000 red
* rgb 0x00ff00 green
* rgb 0x0000ff blue
*
*
* 十进制模式.0结尾可以省略
* rgb 255 red
* rgb 0.255 green
* rgb 0.0.255 blue
*
* @brief
* @param argc
* @param argv
* @return int
*/
int rgb_shell(int argc, const char** argv)
{
if (argc != 2 || argv[1] == '\0' || strlen(argv[1]) <= 0) {
printf("Usage:[%s] 0xFF00|255.0.0\n", argv[0]);
return 0;
}
char* color_str = argv[1];
//转换大小写
color_str = strupr(color_str);
if (strncmp(color_str, "0X", 2) == 0) {
int int_color = strtol(color_str, NULL, 16);
//16-24bit
int r = int_color >> 16 & 0xff;
//9-16bit
int g = int_color >> 8 & 0xff;
//1-8bit
int b = int_color & 0xff;
rgb_update(r, g, b);
return 0;
}
int array[3] = { -1,0,0 };
char* token = strtok(color_str, ".");
int index = 1;
if (strisdigit(color_str)) {
array[0] = atoi(color_str);
}
while (token != NULL) {
token = strtok(NULL, ".");
if (strisdigit(token)) {
array[index] = atoi(token);
}
else {
break;
}
index++;
}
for (int i = 0; i < 3;i++) {
if (array[i] < 0 || array[i] > 255) {
printf("Usage:[%s] 0xFF00|255.0.0\n", argv[0]);
return 0;
}
}
rgb_update(array[0], array[1], array[2]);
return 0;
}
//注册rgb_on shell命令
SHELL_CMD_EXPORT_ALIAS(rgb_shell_on, rgb_on, rgb on);
//注册rgb_off shell 命令
SHELL_CMD_EXPORT_ALIAS(rgb_shell_off, rgb_off, rgb off);
//注册rgb 调节 shell 命令
SHELL_CMD_EXPORT_ALIAS(rgb_shell, rgb, rgb_shell);
点击下载代码