forked from PAWPAW/lib_rgb
重构了部分API,移除了例程中的bin
- 将配置部分移入头文件 - 修正部分类型问题 - 修正部分API
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
**/.build*
|
||||
**/bin**
|
||||
.vscode
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +1,5 @@
|
||||
#ifndef _RGB_EFFECT_H
|
||||
#define _RGB_EFFECT_H
|
||||
#ifndef RGB_EFFECT_H
|
||||
#define RGB_EFFECT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
@@ -7,6 +7,48 @@
|
||||
#include "volume_level.h"
|
||||
#include "misc_utils.h"
|
||||
|
||||
// RGB灯的数量
|
||||
#ifndef NUM_RGBS
|
||||
#define NUM_RGBS (12)
|
||||
#endif
|
||||
|
||||
// RGB灯组的数量,不应超过NUM_RGBS
|
||||
#ifndef NUM_RGB_GROUPS
|
||||
#define NUM_RGB_GROUPS (2)
|
||||
#endif
|
||||
|
||||
// 控制RGB亮度的最大值,不超过255
|
||||
#ifndef RGB_MAX
|
||||
#define RGB_MAX (20)
|
||||
#endif
|
||||
|
||||
// TODO:优化为编译时检查?
|
||||
// 开关饱和检查,该检查会引入性能损失,在需要高刷新率时关闭它
|
||||
#ifndef HSV_VALID_CHECK
|
||||
#define HSV_VALID_CHECK (1)
|
||||
#endif
|
||||
|
||||
// 控制HUE的最大值,不超过360°
|
||||
#ifndef HSV_HUE_MAX
|
||||
#define HSV_HUE_MAX (360)
|
||||
#endif
|
||||
|
||||
// 控制饱和度的最大值,不超过100
|
||||
#ifndef HSV_SATURATION_MAX
|
||||
#define HSV_SATURATION_MAX (100)
|
||||
#endif
|
||||
|
||||
// 控制VALUE,不超过100
|
||||
#ifndef HSV_VALUE_MAX
|
||||
#define HSV_VALUE_MAX (100)
|
||||
#endif
|
||||
|
||||
// 微秒,定义每次颜色更新的延迟,在cycleRGB下,
|
||||
// (1000/RGB_MAX)代表每个B/R/G值的爬升/缓降时间约为1000ms
|
||||
#ifndef DELAY_TIME_RGB
|
||||
#define DELAY_TIME_RGB (1000 / RGB_MAX)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 定义渐变方向的枚举类型。
|
||||
*
|
||||
@@ -99,7 +141,7 @@ uint32_t HSV_to_RGB(uint32_t *hue, uint32_t *sat, uint32_t *value);
|
||||
* \param buf 指向存储RGB值的缓冲区的指针,每个元素代表RGB条中一个颜色单元的颜色值。
|
||||
* \param color 要填充的统一颜色值。
|
||||
*/
|
||||
void fill_gradient(uint32_t *buf, uint32_t color);
|
||||
void fill_gradient(uint32_t *buf, size_t num_filled_rgb, uint32_t color);
|
||||
|
||||
|
||||
/**
|
||||
@@ -134,9 +176,8 @@ void cycleHSV_driver();
|
||||
* \param buf 指向存储RGB值的缓冲区的指针,每个元素代表RGB条中一个颜色单元的颜色值。
|
||||
* \param colors 指向存储每组颜色值的数组的指针,每个元素代表一个组的颜色值。
|
||||
* \param num_filled_rgb 指向存储每组已填充RGB单元数量的数组的指针,每个元素代表一个组已填充的RGB单元数量。
|
||||
* \param num_groups 组的总数。
|
||||
*/
|
||||
void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb, size_t num_groups);
|
||||
void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb);
|
||||
|
||||
/**
|
||||
* 测试音量响应&HSV色彩循环
|
||||
@@ -165,4 +206,4 @@ void test_HSV_to_RGB();
|
||||
* 如果所有LED的颜色都正确,测试通过;否则,输出错误信息并标记测试失败。
|
||||
*/
|
||||
void test_fill_gradient_with_groups();
|
||||
#endif // _RGB_EFFECT_H
|
||||
#endif // RGB_EFFECT_H
|
||||
@@ -1,6 +1,5 @@
|
||||
#ifndef _VOLUME_LEVEL_H
|
||||
#define _VOLUME_LEVEL_H
|
||||
|
||||
#ifndef VOLUME_LEVEL_H
|
||||
#define VOLUME_LEVEL_H
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
@@ -22,4 +21,4 @@ unsigned get_volume_level(int loudness_value);
|
||||
*/
|
||||
void volume_level_test();
|
||||
|
||||
#endif // _VOLUME_LEVEL_H
|
||||
#endif //VOLUME_LEVEL_H
|
||||
@@ -7,56 +7,10 @@
|
||||
#include "rgb_driver.h"
|
||||
#include "volume_level.h"
|
||||
#include "misc_utils.h"
|
||||
|
||||
// RGB灯的数量
|
||||
#ifndef NUM_RGBS
|
||||
#define NUM_RGBS (12)
|
||||
#endif
|
||||
|
||||
// RGB灯组的数量,不应超过NUM_RGBS
|
||||
#ifndef NUM_RGB_GROUPS
|
||||
#define NUM_RGB_GROUPS (2)
|
||||
#endif
|
||||
|
||||
// 控制RGB亮度的最大值,不超过255
|
||||
#ifndef RGB_MAX
|
||||
#define RGB_MAX (20)
|
||||
#endif
|
||||
|
||||
// TODO:优化为编译时检查?
|
||||
// 开关饱和检查,该检查会引入性能损失,在需要高刷新率时关闭它
|
||||
#ifndef HSV_VALID_CHECK
|
||||
#define HSV_VALID_CHECK (1)
|
||||
#endif
|
||||
|
||||
// 控制HUE的最大值,不超过360°
|
||||
#ifndef HSV_HUE_MAX
|
||||
#define HSV_HUE_MAX (360)
|
||||
#endif
|
||||
|
||||
// 控制饱和度的最大值,不超过100
|
||||
#ifndef HSV_SATURATION_MAX
|
||||
#define HSV_SATURATION_MAX (100)
|
||||
#endif
|
||||
|
||||
// 控制VALUE,不超过100
|
||||
#ifndef HSV_VALUE_MAX
|
||||
#define HSV_VALUE_MAX (100)
|
||||
#endif
|
||||
|
||||
// 微秒,定义每次颜色更新的延迟,在cycleRGB下,
|
||||
// (1000/RGB_MAX)代表每个B/R/G值的爬升/缓降时间约为1000ms
|
||||
#ifndef DELAY_TIME_RGB
|
||||
#define DELAY_TIME_RGB (1000 / RGB_MAX)
|
||||
#endif
|
||||
#include "rgb_effect.h"
|
||||
|
||||
// TODO:当RGB_MAX等值超限时,raise error
|
||||
// 定义一个枚举来表示渐变方向
|
||||
typedef enum
|
||||
{
|
||||
INCREMENTING, // 亮度递增
|
||||
DECREMENTING // 亮度递减
|
||||
} GradientDirection;
|
||||
|
||||
|
||||
// 检查HSV值是否在有效范围内,如果溢出则将值设为最大值,并打印报错
|
||||
@@ -218,11 +172,11 @@ void fill_gradient(uint32_t *buf, size_t num_filled_rgb, uint32_t color)
|
||||
}
|
||||
|
||||
// 将多种颜色分组填充到RGB条中,并控制显示颜色的时间长度。
|
||||
void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb, size_t num_groups)
|
||||
void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb)
|
||||
{
|
||||
size_t max_leds_per_group = NUM_RGBS / num_groups;
|
||||
size_t max_leds_per_group = NUM_RGBS / NUM_RGB_GROUPS;
|
||||
|
||||
for (size_t group = 0; group < num_groups; group++) {
|
||||
for (size_t group = 0; group < NUM_RGB_GROUPS; group++) {
|
||||
// 填充每个组的LED
|
||||
uint32_t *group_buf = buf + group * max_leds_per_group;
|
||||
for (size_t i = 0; i < num_filled_rgb[group]; i++) {
|
||||
@@ -243,8 +197,6 @@ void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_fill
|
||||
// 将缓冲区输出到RGB条
|
||||
output_rgb_array(buf, NUM_RGBS);
|
||||
|
||||
// 延迟以控制显示持续时间
|
||||
delay_milliseconds(DELAY_TIME_RGB);
|
||||
}
|
||||
|
||||
// TODO:添加可合并选项
|
||||
@@ -263,7 +215,9 @@ void cycleHSV_driver()
|
||||
{
|
||||
// 用当前渐变颜色填充RGB数组,然后发送给rgb阵列
|
||||
fill_gradient(buf, NUM_RGBS, current_color);
|
||||
|
||||
|
||||
// 延迟以控制显示持续时间
|
||||
delay_milliseconds(DELAY_TIME_RGB);
|
||||
// 更改下一个颜色
|
||||
current_color = cycleHSV(¤t_hue);
|
||||
|
||||
@@ -287,7 +241,8 @@ void cycleRGB_driver()
|
||||
{
|
||||
// 用当前渐变颜色填充RGB数组
|
||||
fill_gradient(buf, NUM_RGBS, current_color);
|
||||
|
||||
// 延迟以控制显示持续时间
|
||||
delay_milliseconds(DELAY_TIME_RGB);
|
||||
// 更改下一个渐变的基色
|
||||
current_color = cycleRGB(current_color, &direction);
|
||||
}
|
||||
@@ -316,8 +271,9 @@ void test_cycleHSV_with_vol_level()
|
||||
}
|
||||
|
||||
// 用当前渐变颜色填充RGB数组,然后发送给rgb阵列
|
||||
fill_gradient_with_groups(buf, current_color, random_levels, NUM_RGB_GROUPS);
|
||||
|
||||
fill_gradient_with_groups(buf, current_color, random_levels);
|
||||
// 延迟以控制显示持续时间
|
||||
delay_milliseconds(DELAY_TIME_RGB);
|
||||
// 更改下一个颜色
|
||||
for (size_t i = 0; i < NUM_RGB_GROUPS; i++)
|
||||
{
|
||||
@@ -352,8 +308,9 @@ void test_fill_gradient_with_groups() {
|
||||
size_t num_groups = sizeof(colors) / sizeof(colors[0]);
|
||||
int success = 1; // 使用int代替bool,1代表true,0代表false
|
||||
|
||||
fill_gradient_with_groups(buffer, colors, num_filled_rgb, num_groups);
|
||||
|
||||
fill_gradient_with_groups(buffer, colors, num_filled_rgb);
|
||||
// 延迟以控制显示持续时间
|
||||
delay_milliseconds(DELAY_TIME_RGB);
|
||||
// 验证
|
||||
for (size_t i = 0; i < NUM_RGBS; i++) {
|
||||
size_t group = i / (NUM_RGBS / num_groups);
|
||||
|
||||
@@ -25,7 +25,7 @@ unsigned get_volume_level(int loudness_value) {
|
||||
int level = 0;
|
||||
|
||||
// 从最大响度值开始向下找
|
||||
for (int i = 0; i < VOLUME_LEVELS_COUNT; ++i) {
|
||||
for (size_t i = 0; i < VOLUME_LEVELS_COUNT; ++i) {
|
||||
if (loudness_value >= vol_level_table[i].vol) {
|
||||
level = vol_level_table[i].level;
|
||||
break; // 找到后立即退出循环
|
||||
@@ -42,8 +42,8 @@ void volume_level_test() {
|
||||
int num_tests = sizeof(test_loudness_values) / sizeof(test_loudness_values[0]);
|
||||
|
||||
for (int i = 0; i < num_tests; ++i) {
|
||||
printf("Loudness: %d dB, Volume Level: %d\n",
|
||||
printf("Loudness: %u dB, Volume Level: %u\n",
|
||||
test_loudness_values[i],
|
||||
get_volume_level(test_loudness_values[i]));
|
||||
get_volume_level((int)test_loudness_values[i]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user