From 8d3e44554195b783b123efb3415a98b9ed7c8d18 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 16:55:10 +0800 Subject: [PATCH 01/22] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=86=97=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E4=BE=9D=E8=B5=96=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/api/visualize_volume.h | 1 - 1 file changed, 1 deletion(-) diff --git a/lib_rgb/api/visualize_volume.h b/lib_rgb/api/visualize_volume.h index 46d59b5..38a88dc 100644 --- a/lib_rgb/api/visualize_volume.h +++ b/lib_rgb/api/visualize_volume.h @@ -1,7 +1,6 @@ #ifndef CACHE_OPERATIONS_H #define CACHE_OPERATIONS_H #include -#include "samples_to_levels.h" #include "rgb_effect.h" /** * \brief 初始化缓存缓冲区。 From ab84321fd61944cf907d1821e0e89b6ca9a60a29 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 16:55:26 +0800 Subject: [PATCH 02/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8f70d75..cfa0fce 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,9 @@ `lib_rgb` 是一个用于控制RGB灯条的C语言库,提供了一系列功能,比如颜色渐变、呼吸灯效果和色调循环等。它支持通过RGB和HSV颜色空间来控制灯条的颜色输出。 ## 功能 + +- **响度可视化**:通过检测音频样本中的响度,实现RGB跟随音量大小跳动 + - **颜色渐变**:通过逐步调整RGB值来模拟呼吸灯效果。 - **色调循环**:在HSV颜色空间中循环改变色调以实现颜色渐变。 @@ -14,17 +17,12 @@ - **颜色填充**:将单一颜色填充到整个RGB条中,也可以将RGB条分组,同时输出不同的颜色或响应 -- **音量响应**:根据响度值(-xdB~0dB),获取音量等级,以实现响度可视化 - -- **RGB灯条驱动**:启动一个永久循环,持续更新RGB灯条的颜色。 - -- **HSV颜色循环驱动**:驱动HSV颜色空间中的颜色循环,以实现连续的颜色渐变效果。 - - +- **音量响应**:根据响度值(-xdB~0dB),获取音量等级 ## 使用方法 ### 包含头文件 + 在你的项目中包含`rgb_effect.h`文件。 ```c @@ -33,26 +31,30 @@ ### 快速开始 -如果你想要一个开箱即用的持续的颜色渐变效果,可以使用`cycleRGB_driver`或`cycleHSV_driver`函数。 +如果你想要尝试开箱即用的持续的颜色渐变效果,可以使用`examples/app_hsv_cycle_example/src/hsv_cycle_example.c`中的`hsv_cycle_example`函数,具体的用法可以参考该示例。 注意:当前用于驱动LED组的默认的IO,定义在`Tile[1]`的`4A3`端口上,因此你需要添加对tile的声明。 ```c -par -{ - // 启动RGB颜色渐变驱动 - on tile[1]: cycleRGB_driver(); -} -``` +#include // 包含对封装的定义,引用以使用 on tile[] 语法 -或者: - -```c -par +extern "C" { - // 启动HSV颜色循环驱动 - on tile[1]: cycleHSV_driver(); + void hsv_cycle_example(); } + +int main() // 定义主函数 +{ + par + { + on tile[1]: + { + hsv_cycle_example(); + } + } + return 0; // 返回0,表示程序正常结束 +} + ``` ### 初始化颜色和方向 @@ -93,8 +95,7 @@ output_rgb_array(rgb_array, 3); ## 路线图 -- [ ] 当RGB_MAX等值超限时,raise error -- [ ] 为绘制RGB的函数添加可合并选项,以减少资源占用 +- [x] 为绘制RGB的函数添加可合并选项,以减少资源占用 - [ ] 添加更多应用光效 - [x] RGB渐变 - [x] HSV渐变 @@ -104,6 +105,6 @@ output_rgb_array(rgb_array, 3); - [ ] 使用fp/s32以增加`HSV_to_RGB`的计算效率 ## 贡献 -如果你有任何改进意见或者发现了bug,请通过issues或pull requests来提交。 +如果你有任何改进意见或者发现了bug,请通过issues或pull requests来提交或贡献。 感谢使用`lib_rgb`! \ No newline at end of file From cc3cbc04d99c1ae2452fc45e47e915e2c7a9fb3f Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 17:03:23 +0800 Subject: [PATCH 03/22] =?UTF-8?q?=E8=A7=84=E8=8C=83=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/api/misc_utils.h | 6 +++--- lib_rgb/api/rgb_driver.h | 6 +++--- lib_rgb/api/visualize_volume.h | 3 +-- lib_rgb/src/rgb_effect.c | 1 - 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib_rgb/api/misc_utils.h b/lib_rgb/api/misc_utils.h index 8af0251..0141dbb 100644 --- a/lib_rgb/api/misc_utils.h +++ b/lib_rgb/api/misc_utils.h @@ -1,5 +1,5 @@ -#ifndef _MISC_UTILS_H -#define _MISC_UTILS_H +#ifndef MISC_UTILS_H +#define MISC_UTILS_H #include #include @@ -15,4 +15,4 @@ * 直到到达中心点,完成反转过程。 */ void reverse_buf(uint32_t *start, size_t count); -#endif // _MISC_UTILS_H \ No newline at end of file +#endif // MISC_UTILS_H \ No newline at end of file diff --git a/lib_rgb/api/rgb_driver.h b/lib_rgb/api/rgb_driver.h index 21fccac..eea05a7 100644 --- a/lib_rgb/api/rgb_driver.h +++ b/lib_rgb/api/rgb_driver.h @@ -1,5 +1,5 @@ -#ifndef _RGB_DRIVER_H -#define _RGB_DRIVER_H +#ifndef RGB_DRIVER_H +#define RGB_DRIVER_H #include @@ -29,4 +29,4 @@ void output_24bit_value_serially(uint32_t value); */ void output_rgb_array(uint32_t buf[], uint32_t num_rgbs); -#endif // _RGB_DRIVER_H +#endif // RGB_DRIVER_H diff --git a/lib_rgb/api/visualize_volume.h b/lib_rgb/api/visualize_volume.h index 38a88dc..959221e 100644 --- a/lib_rgb/api/visualize_volume.h +++ b/lib_rgb/api/visualize_volume.h @@ -39,5 +39,4 @@ void write_to_inactive_cache(int address, int16_t data); * current_hue作为当前色调值,用于生成下一个颜色。 */ void visualize_loudness_driver(); -#endif // CACHE_OPERATIONS_H -// TODO: Refact this shit \ No newline at end of file +#endif // CACHE_OPERATIONS_H \ No newline at end of file diff --git a/lib_rgb/src/rgb_effect.c b/lib_rgb/src/rgb_effect.c index 7b2ecd4..5d429d5 100644 --- a/lib_rgb/src/rgb_effect.c +++ b/lib_rgb/src/rgb_effect.c @@ -89,7 +89,6 @@ uint32_t HSV_to_RGB(uint32_t *hue, uint32_t *sat, uint32_t *value) return (g << 16) | (r << 8) | b; } - // 逐步遍历RGB颜色空间,实现呼吸灯效果 uint32_t cycleRGB(uint32_t color, GradientDirection *direction) { From 64d87e8e9ef986a97ba341a85732dd39dd4df7dc Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 17:14:30 +0800 Subject: [PATCH 04/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=87=B30.0.5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/module_build_info | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_rgb/module_build_info b/lib_rgb/module_build_info index e41f7c8..a272bd7 100644 --- a/lib_rgb/module_build_info +++ b/lib_rgb/module_build_info @@ -1,4 +1,4 @@ -VERSION = 0.0.4 +VERSION = 0.0.5 DEPENDENT_MODULES = lib_xcore_math(>=2.1.0) \ From 69c89346fd2c447d6fae7898813d8849dd027125 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 20:19:49 +0800 Subject: [PATCH 05/22] =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AE=8F=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B0=86port=E4=B8=8E=E9=A9=B1=E5=8A=A8=E8=A7=A3?= =?UTF-8?q?=E8=80=A6=EF=BC=88=E8=A7=A3=E5=86=B3=20#2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了相应的测试用例,编译通过 --- examples/app_test_config_rgb_port/Makefile | 24 ++++ .../src/core/PXUA-316-MC-MAX.xn | 106 ++++++++++++++++++ .../src/core/xua_conf.h | 15 +++ examples/app_test_config_rgb_port/src/main.xc | 26 +++++ .../test_cycleHSV_with_vol_level_example.c | 16 +++ lib_rgb/api/rgb_driver.h | 10 ++ lib_rgb/src/rgb_driver.xc | 14 ++- 7 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 examples/app_test_config_rgb_port/Makefile create mode 100644 examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn create mode 100644 examples/app_test_config_rgb_port/src/core/xua_conf.h create mode 100644 examples/app_test_config_rgb_port/src/main.xc create mode 100644 examples/app_test_config_rgb_port/src/test_cycleHSV_with_vol_level_example.c diff --git a/examples/app_test_config_rgb_port/Makefile b/examples/app_test_config_rgb_port/Makefile new file mode 100644 index 0000000..5fe0145 --- /dev/null +++ b/examples/app_test_config_rgb_port/Makefile @@ -0,0 +1,24 @@ +# `TARGET` 变量决定了应用程序编译的目标系统。它可以引用源目录中的一个 XN 文件,或者是在编译时作为 `--target` 选项的一个有效参数。 + +TARGET = PXUA-316-MC-MAX.xn + +# 编译选项 +# 构建应用程序时传递给 xcc 的参数 +# O2: xcc编译器优化等级2 +# report: 打开编译报告 +# g: 生成调试信息 +# fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) +BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope + +USED_MODULES = lib_rgb + +XCC_FLAGS = $(BUILD_FLAGS) + +XMOS_MODULE_PATH = ../../.. +XCOMMON_DISABLE_AUTO_MODULE_SEARCH = 1 + +#============================================================================= +# 下面部分的 Makefile 包含了用于编译 XMOS 应用程序的公共构建基础设施。你无需编辑此处以下的内容。 + +XMOS_MAKE_PATH ?= ../.. +include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common \ No newline at end of file diff --git a/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn b/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn new file mode 100644 index 0000000..036f02c --- /dev/null +++ b/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn @@ -0,0 +1,106 @@ + + + Board + PXUA-316-MC-MAX + + tileref tile[2] + tileref usb_tile + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/app_test_config_rgb_port/src/core/xua_conf.h b/examples/app_test_config_rgb_port/src/core/xua_conf.h new file mode 100644 index 0000000..d02bfae --- /dev/null +++ b/examples/app_test_config_rgb_port/src/core/xua_conf.h @@ -0,0 +1,15 @@ +/** + * @file xua_conf.h + * @brief Defines relating to device configuration and customisation. + * For PXUA-XU316-MC-MAX + */ +#ifndef _XUA_CONF_H_ +#define _XUA_CONF_H_ + +/* Defines relating to feature placement regarding tiles */ +#ifndef RGB_TILE +#define RGB_TILE (1) +#endif + +#include "rgb_driver.h" +#endif diff --git a/examples/app_test_config_rgb_port/src/main.xc b/examples/app_test_config_rgb_port/src/main.xc new file mode 100644 index 0000000..b4cc7e5 --- /dev/null +++ b/examples/app_test_config_rgb_port/src/main.xc @@ -0,0 +1,26 @@ +/** @brief 测试output_rgb_array + * + * @author Vergil Wong + * @date 2023-11-25 + * @param + * @return + */ + +#include // 包含对封装的定义,引用以使用 on tile[] 语法 + +extern "C" +{ + void test_output_rgb_array_example(); +} + +int main() // 定义主函数 +{ + par + { + on tile[1]: + { + test_output_rgb_array_example(); + } + } + return 0; // 返回0,表示程序正常结束 +} diff --git a/examples/app_test_config_rgb_port/src/test_cycleHSV_with_vol_level_example.c b/examples/app_test_config_rgb_port/src/test_cycleHSV_with_vol_level_example.c new file mode 100644 index 0000000..e9f8c64 --- /dev/null +++ b/examples/app_test_config_rgb_port/src/test_cycleHSV_with_vol_level_example.c @@ -0,0 +1,16 @@ +#include +#include +#include "rgb_effect.h" +void test_output_rgb_array_example() +{ + uint32_t buf[NUM_RGBS]; // 定义一个用于存储RGB值的缓冲区,大小由NUM_RGBS宏确定 + uint32_t num_rgbs = 12; + + // Initialize the buffer with example RGB values + for (uint32_t i = 0; i < num_rgbs; ++i) { + buf[i] = (i << 16) | (i << 8) | i; // Just an example pattern for RGB values + } + + // Call the function with the test buffer and number of RGBs + output_rgb_array(buf, num_rgbs); +} \ No newline at end of file diff --git a/lib_rgb/api/rgb_driver.h b/lib_rgb/api/rgb_driver.h index eea05a7..e76da58 100644 --- a/lib_rgb/api/rgb_driver.h +++ b/lib_rgb/api/rgb_driver.h @@ -2,6 +2,16 @@ #define RGB_DRIVER_H #include +#include +//TODO: 在XUA工程环境中测试 + +#ifndef RGB_TILE +#define RGB_TILE (1) +#endif + +#ifndef PORT_RGB_DAISY_CHAIN +#define PORT_RGB_DAISY_CHAIN XS1_PORT_4A +#endif /** 串行输出24位值。 * diff --git a/lib_rgb/src/rgb_driver.xc b/lib_rgb/src/rgb_driver.xc index d424811..e2e12ef 100644 --- a/lib_rgb/src/rgb_driver.xc +++ b/lib_rgb/src/rgb_driver.xc @@ -4,6 +4,7 @@ #include #include "xclib.h" #include +#include "rgb_driver.h" // 如果灯的显示没有出现问题,不要修改这些宏 #define MICROSECONDS 100 @@ -13,8 +14,9 @@ #define T0L_DELAY (93) // 0.9 μs #define T1L_DELAY (63) // 0.6 μs +// RGB_TILE 通常为0/1,PORT_RGB_DAISY_CHAIN为4bit port +on tile[RGB_TILE]: out port p_rgb_4bit = PORT_RGB_DAISY_CHAIN; -on tile[1] : out port p_rgb_rgb = XS1_PORT_4A; // 注意:当前代码仅适用于4bit端口 timer tmr; // 向4A3写时序,点亮RT23 @@ -22,19 +24,19 @@ uint32_t output_bit_on_4th_pin(uint32_t bit, uint32_t fall_time) { if (bit == 1) { - p_rgb_rgb<:0xff; // 输出到4位端口 + p_rgb_4bit<:0xff; // 输出到4位端口 tmr when timerafter(fall_time + T1H_DELAY):> fall_time; // 等待T1H时长 // printf("[x]fall_time: %u\n", fall_time); - p_rgb_rgb<:0x00; // 将第4位设置为低电平 + p_rgb_4bit<:0x00; // 将第4位设置为低电平 tmr when timerafter(fall_time + T1L_DELAY):> fall_time; // 等待T1L时长 // printf("[x]fall_time: %u\n", fall_time); } else if (bit == 0) { - p_rgb_rgb<:0xff; // 输出到4位端口 + p_rgb_4bit<:0xff; // 输出到4位端口 tmr when timerafter(fall_time + T0H_DELAY):> fall_time; // 等待T0H时长 // printf("[x]fall_time: %u\n", fall_time); - p_rgb_rgb<:0x00; // 将第4位设置为低电平 + p_rgb_4bit<:0x00; // 将第4位设置为低电平 tmr when timerafter(fall_time + T0L_DELAY):> fall_time; // 等待T0L时长 // printf("[x]fall_time: %u\n", fall_time); } @@ -52,7 +54,7 @@ void output_24bit_value_serially(uint32_t value) uint32_t fall_time = 0; tmr:> fall_time; // // 重置 RT23,由于引脚默认为低,此处不做重置 - // p_rgb_rgb <: 0x000000; + // p_rgb_4bit <: 0x000000; // tmr when timerafter(fall_time + RT23_RST_DELAY):> fall_time; value = value << 8; From 5f6961bc294e46dcad563de7a54056345087e19b Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 22:28:09 +0800 Subject: [PATCH 06/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86RGB=E5=B9=B3?= =?UTF-8?q?=E6=BB=91=E4=B8=8B=E9=99=8D=E7=9A=84=E4=BB=A3=E7=A0=81=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Makefile | 0 .../src/cycleHSV_vol_level_example.c} | 2 +- .../src/main.xc | 4 +- .../Makefile | 24 ++++++++ .../src/cycleHSV_vol_level_smooth_example.c | 57 +++++++++++++++++++ .../src/main.xc | 24 ++++++++ 6 files changed, 108 insertions(+), 3 deletions(-) rename examples/{app_test_cycleHSV_with_vol_level_example => app_cycleHSV_vol_level_example}/Makefile (100%) rename examples/{app_test_cycleHSV_with_vol_level_example/src/test_cycleHSV_with_vol_level_example.c => app_cycleHSV_vol_level_example/src/cycleHSV_vol_level_example.c} (96%) rename examples/{app_test_cycleHSV_with_vol_level_example => app_cycleHSV_vol_level_example}/src/main.xc (88%) create mode 100644 examples/app_cycleHSV_vol_level_smooth_example/Makefile create mode 100644 examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c create mode 100644 examples/app_cycleHSV_vol_level_smooth_example/src/main.xc diff --git a/examples/app_test_cycleHSV_with_vol_level_example/Makefile b/examples/app_cycleHSV_vol_level_example/Makefile similarity index 100% rename from examples/app_test_cycleHSV_with_vol_level_example/Makefile rename to examples/app_cycleHSV_vol_level_example/Makefile diff --git a/examples/app_test_cycleHSV_with_vol_level_example/src/test_cycleHSV_with_vol_level_example.c b/examples/app_cycleHSV_vol_level_example/src/cycleHSV_vol_level_example.c similarity index 96% rename from examples/app_test_cycleHSV_with_vol_level_example/src/test_cycleHSV_with_vol_level_example.c rename to examples/app_cycleHSV_vol_level_example/src/cycleHSV_vol_level_example.c index 269c211..dea75ac 100644 --- a/examples/app_test_cycleHSV_with_vol_level_example/src/test_cycleHSV_with_vol_level_example.c +++ b/examples/app_cycleHSV_vol_level_example/src/cycleHSV_vol_level_example.c @@ -4,7 +4,7 @@ #include "samples_to_levels.h" #include "rgb_effect.h" #include "timer.h" -void test_cycleHSV_with_vol_level_example() +void cycleHSV_vol_level_example() { uint32_t buf[NUM_RGBS]; // 定义一个用于存储RGB值的缓冲区,大小由NUM_RGBS宏确定 uint32_t current_hue = 0; // 从红色开始的当前色相值 diff --git a/examples/app_test_cycleHSV_with_vol_level_example/src/main.xc b/examples/app_cycleHSV_vol_level_example/src/main.xc similarity index 88% rename from examples/app_test_cycleHSV_with_vol_level_example/src/main.xc rename to examples/app_cycleHSV_vol_level_example/src/main.xc index d29ec3a..9e43de8 100644 --- a/examples/app_test_cycleHSV_with_vol_level_example/src/main.xc +++ b/examples/app_cycleHSV_vol_level_example/src/main.xc @@ -13,7 +13,7 @@ #include // 包含对封装的定义,引用以使用 on tile[] 语法 extern "C"{ - void test_cycleHSV_with_vol_level_example(); + void cycleHSV_vol_level_example(); } int main() // 定义主函数 @@ -22,7 +22,7 @@ int main() // 定义主函数 { on tile[1]: { - test_cycleHSV_with_vol_level_example(); + cycleHSV_vol_level_example(); } } return 0; // 返回0,表示程序正常结束 diff --git a/examples/app_cycleHSV_vol_level_smooth_example/Makefile b/examples/app_cycleHSV_vol_level_smooth_example/Makefile new file mode 100644 index 0000000..2b38775 --- /dev/null +++ b/examples/app_cycleHSV_vol_level_smooth_example/Makefile @@ -0,0 +1,24 @@ +# `TARGET` 变量决定了应用程序编译的目标系统。它可以引用源目录中的一个 XN 文件,或者是在编译时作为 `--target` 选项的一个有效参数。 + +TARGET = XCORE-AI-EXPLORER + +# 编译选项 +# 构建应用程序时传递给 xcc 的参数 +# O2: xcc编译器优化等级2 +# report: 打开编译报告 +# g: 生成调试信息 +# fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) +BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope + +USED_MODULES = lib_rgb + +XCC_FLAGS = $(BUILD_FLAGS) + +XMOS_MODULE_PATH = ../../.. +XCOMMON_DISABLE_AUTO_MODULE_SEARCH = 1 + +#============================================================================= +# 下面部分的 Makefile 包含了用于编译 XMOS 应用程序的公共构建基础设施。你无需编辑此处以下的内容。 + +XMOS_MAKE_PATH ?= ../.. +include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common \ No newline at end of file diff --git a/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c b/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c new file mode 100644 index 0000000..b67aa1e --- /dev/null +++ b/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c @@ -0,0 +1,57 @@ +#include +#include +#include // 包含基本的输入输出函数 +#include "samples_to_levels.h" +#include "rgb_effect.h" +#include "timer.h" +#define DELAY_DECREASE 20 // 降低亮度时的额外延迟 + +// 该函数用于平滑过渡HSV颜色和音量级别 +void cycleHSV_vol_level_smooth_example() +{ + uint32_t buf[NUM_RGBS]; // 存储RGB值的缓冲区 + uint32_t current_hue = 0; // 当前色调 + uint32_t current_color[NUM_RGB_GROUPS] = {0x000000, 0x000000}; // 当前颜色数组 + size_t random_levels[NUM_RGB_GROUPS]; // 随机音量级别数组 + size_t previous_levels[NUM_RGB_GROUPS] = {0}; // 存储先前的音量级别 + + srand(1); // 设置随机数种子 + + while (1) // 无限循环 + { + for (size_t i = 0; i < NUM_RGB_GROUPS; i++) // 遍历RGB组 + { + // 获取新的随机音量级别 + random_levels[i] = get_volume_level(rand() % 101 - 100); + // 如果新的级别低于之前的级别,逐渐降低 + while (previous_levels[i] > random_levels[i]) + { + previous_levels[i]--; // 降低当前级别 + // 用当前颜色和级别填充渐变 + fill_gradient_with_groups(buf, current_color, previous_levels); + delay_milliseconds(DELAY_TIME_RGB); // 延迟 + // 更新每一步的RGB颜色 + for (size_t j = 0; j < NUM_RGB_GROUPS; j++) + { + current_color[j] = cycleHSV(¤t_hue); + } + } + } + + // 用随机颜色和级别填充渐变 + fill_gradient_with_groups(buf, current_color, random_levels); + delay_milliseconds(DELAY_TIME_RGB); // 延迟 + + // 更新当前颜色 + for (size_t i = 0; i < NUM_RGB_GROUPS; i++) + { + current_color[i] = cycleHSV(¤t_hue); + } + + // 更新先前的音量级别 + for (size_t i = 0; i < NUM_RGB_GROUPS; i++) + { + previous_levels[i] = random_levels[i]; + } + } +} diff --git a/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc b/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc new file mode 100644 index 0000000..60f9b26 --- /dev/null +++ b/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc @@ -0,0 +1,24 @@ +/** @brief 平滑过渡HSV颜色和音量级别 + * @author Vergil Wong + * @date 2023-11-25 + * @param + * @return + */ + +#include // 包含对封装的定义,引用以使用 on tile[] 语法 + +extern "C"{ + void cycleHSV_vol_level_smooth_example(); +} + +int main() // 定义主函数 +{ + par + { + on tile[1]: + { + cycleHSV_vol_level_smooth_example(); + } + } + return 0; // 返回0,表示程序正常结束 +} From 8736beb673823dec82431a781134f6a573d60cd1 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 22:50:06 +0800 Subject: [PATCH 07/22] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=B8=AD=E7=9A=84=E5=AE=9A=E4=B9=89=EF=BC=8C=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E5=8F=AF=E4=BB=A5=E6=AD=A3=E7=A1=AE=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E7=81=AF=E7=9A=84=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/test_fill_gradient_with_groups.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c b/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c index 5e13859..7483f8c 100644 --- a/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c +++ b/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c @@ -2,15 +2,14 @@ #include // 包含基本的输入输出函数 #include // 包含对封装的定义,引用以使用 on tile[] 语法 #include "samples_to_levels.h" +#define NUM_RGB_GROUPS 3 #include "rgb_effect.h" #include "timer.h" - void test_fill_gradient_with_groups() { uint32_t buffer[NUM_RGBS]; - uint32_t colors[] = {0xFF0000, 0x00FF00}; // 红,绿 - size_t num_filled_rgb[] = {2, 3}; // 每组填充数量 - size_t num_groups = sizeof(colors) / sizeof(colors[0]); + uint32_t colors[] = {0xFF0000, 0x00FF00, 0x0000FF}; // 红,绿,蓝 + size_t num_filled_rgb[] = {4, 4, 4}; // 每组填充数量 int success = 1; // 使用int代替bool,1代表true,0代表false fill_gradient_with_groups(buffer, colors, num_filled_rgb); @@ -19,8 +18,8 @@ void test_fill_gradient_with_groups() // 验证 for (size_t i = 0; i < NUM_RGBS; i++) { - size_t group = i / (NUM_RGBS / num_groups); - size_t group_led_start = group * (NUM_RGBS / num_groups); + size_t group = i / (NUM_RGBS / NUM_RGB_GROUPS); + size_t group_led_start = group * (NUM_RGBS / NUM_RGB_GROUPS); uint32_t expected_color = (i < group_led_start + num_filled_rgb[group]) ? colors[group] : 0x000000; if (buffer[i] != expected_color) { From c1c3864c0fbf74f9d0deebe447dfe6a5524d5830 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 23:01:17 +0800 Subject: [PATCH 08/22] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BA=86=E5=86=97?= =?UTF-8?q?=E4=BD=99=E7=9A=84test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/api/samples_to_levels.h | 8 -------- lib_rgb/src/samples_to_levels.c | 13 ------------- 2 files changed, 21 deletions(-) diff --git a/lib_rgb/api/samples_to_levels.h b/lib_rgb/api/samples_to_levels.h index f5cefa8..c0a2c7e 100644 --- a/lib_rgb/api/samples_to_levels.h +++ b/lib_rgb/api/samples_to_levels.h @@ -31,14 +31,6 @@ */ size_t get_volume_level(int loudness_value); -/** - * 测试不同响度值对应的音量等级。 - * - * 此函数定义了一系列响度值,并使用 get_volume_level 函数 - * 来获取每个响度值对应的音量等级,然后打印出来。 - */ -void volume_level_test(); - /** * 将音频样本转换为音量级别。 * diff --git a/lib_rgb/src/samples_to_levels.c b/lib_rgb/src/samples_to_levels.c index aeec7e2..c5237bf 100644 --- a/lib_rgb/src/samples_to_levels.c +++ b/lib_rgb/src/samples_to_levels.c @@ -101,17 +101,4 @@ void samples_to_levels(int16_t sample_in[], size_t levels[]) levels[i] = get_volume_level(rms); // printf("rms[%d]: %d \n", i, rms); } -} - -// 测试不同响度值对应的音量等级是否符合预期 -void volume_level_test() { - // 测试不同的响度值 - uint32_t test_loudness_values[] = {1, 0, -2, -5, -8, -20, -30, -40, -60, -62, -70, }; - int num_tests = sizeof(test_loudness_values) / sizeof(test_loudness_values[0]); - - for (int i = 0; i < num_tests; ++i) { - printf("Loudness: %lu dB, Volume Level: %u\n", - test_loudness_values[i], - get_volume_level((size_t)test_loudness_values[i])); - } } \ No newline at end of file From efeed1c18cf4e0607f9077ce56205b5eca7a867d Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sat, 25 Nov 2023 23:02:11 +0800 Subject: [PATCH 09/22] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=BC=98=E5=8C=96=E4=BA=86?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E7=9A=84Makefile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/Makefile | 5 +--- examples/app_test_vol_to_level/Makefile | 24 +++++++++++++++++++ examples/app_test_vol_to_level/src/main.xc | 22 +++++++++++++++++ .../src/test_vol_to_level.c | 15 ++++++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 examples/app_test_vol_to_level/Makefile create mode 100644 examples/app_test_vol_to_level/src/main.xc create mode 100644 examples/app_test_vol_to_level/src/test_vol_to_level.c diff --git a/examples/Makefile b/examples/Makefile index b37c7ed..585c6d5 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,10 +1,7 @@ # This variable should contain a space separated list of all # the directories containing buildable applications (usually # prefixed with the app_ prefix) -BUILD_SUBDIRS = ./app_rgb_cycle_breathing_example \ - ./app_hsv_cycle_example \ - ./app_test_cycleHSV_with_vol_level_example \ - ./app_test_fill_gradient_with_groups \ +BUILD_SUBDIRS := $(wildcard ./app_*) # This variable should contain a space separated list of all # the directories containing buildable plugins (usually diff --git a/examples/app_test_vol_to_level/Makefile b/examples/app_test_vol_to_level/Makefile new file mode 100644 index 0000000..2b38775 --- /dev/null +++ b/examples/app_test_vol_to_level/Makefile @@ -0,0 +1,24 @@ +# `TARGET` 变量决定了应用程序编译的目标系统。它可以引用源目录中的一个 XN 文件,或者是在编译时作为 `--target` 选项的一个有效参数。 + +TARGET = XCORE-AI-EXPLORER + +# 编译选项 +# 构建应用程序时传递给 xcc 的参数 +# O2: xcc编译器优化等级2 +# report: 打开编译报告 +# g: 生成调试信息 +# fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) +BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope + +USED_MODULES = lib_rgb + +XCC_FLAGS = $(BUILD_FLAGS) + +XMOS_MODULE_PATH = ../../.. +XCOMMON_DISABLE_AUTO_MODULE_SEARCH = 1 + +#============================================================================= +# 下面部分的 Makefile 包含了用于编译 XMOS 应用程序的公共构建基础设施。你无需编辑此处以下的内容。 + +XMOS_MAKE_PATH ?= ../.. +include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common \ No newline at end of file diff --git a/examples/app_test_vol_to_level/src/main.xc b/examples/app_test_vol_to_level/src/main.xc new file mode 100644 index 0000000..424a75b --- /dev/null +++ b/examples/app_test_vol_to_level/src/main.xc @@ -0,0 +1,22 @@ +/** @brief 测试不同的响度值转化为音量等级 + * @author Vergil Wong + * @date 2023-11-25 + * @param + * @return + */ + +#include // 包含对封装的定义,引用以使用 on tile[] 语法 + +extern "C" +{ + void test_vol_to_level(); +} + +int main() // 定义主函数 +{ + par + { + on tile[1]: test_vol_to_level(); + } + return 0; // 返回0,表示程序正常结束 +} diff --git a/examples/app_test_vol_to_level/src/test_vol_to_level.c b/examples/app_test_vol_to_level/src/test_vol_to_level.c new file mode 100644 index 0000000..8adc97c --- /dev/null +++ b/examples/app_test_vol_to_level/src/test_vol_to_level.c @@ -0,0 +1,15 @@ +#include +#include +#include "rgb_effect.h" +void test_vol_to_level() +{ + // 测试不同的响度值 + uint32_t test_loudness_values[] = {1, 0, -2, -5, -8, -20, -30, -40, -60, -62, -70, }; + int num_tests = sizeof(test_loudness_values) / sizeof(test_loudness_values[0]); + + for (int i = 0; i < num_tests; ++i) { + printf("Loudness: %lu dB, Volume Level: %u\n", + test_loudness_values[i], + get_volume_level((int)test_loudness_values[i])); + } +} \ No newline at end of file From 44bc75ee1a35e22cacab9dbcef7576762dd61598 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 00:22:09 +0800 Subject: [PATCH 10/22] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E7=BC=96=E8=AF=91=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/app_cycleHSV_vol_level_example/Makefile | 2 +- examples/app_cycleHSV_vol_level_smooth_example/Makefile | 2 +- examples/app_hsv_cycle_example/Makefile | 2 +- examples/app_rgb_cycle_breathing_example/Makefile | 2 +- examples/app_test_HSV_to_RGB/Makefile | 2 +- examples/app_test_fill_gradient_with_groups/Makefile | 2 +- examples/app_test_vol_to_level/Makefile | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/app_cycleHSV_vol_level_example/Makefile b/examples/app_cycleHSV_vol_level_example/Makefile index 2b38775..9c186d4 100644 --- a/examples/app_cycleHSV_vol_level_example/Makefile +++ b/examples/app_cycleHSV_vol_level_example/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb diff --git a/examples/app_cycleHSV_vol_level_smooth_example/Makefile b/examples/app_cycleHSV_vol_level_smooth_example/Makefile index 2b38775..9c186d4 100644 --- a/examples/app_cycleHSV_vol_level_smooth_example/Makefile +++ b/examples/app_cycleHSV_vol_level_smooth_example/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb diff --git a/examples/app_hsv_cycle_example/Makefile b/examples/app_hsv_cycle_example/Makefile index cd4d4c4..7d58c58 100644 --- a/examples/app_hsv_cycle_example/Makefile +++ b/examples/app_hsv_cycle_example/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb lib_xcore_math diff --git a/examples/app_rgb_cycle_breathing_example/Makefile b/examples/app_rgb_cycle_breathing_example/Makefile index 1760f2e..514af0b 100644 --- a/examples/app_rgb_cycle_breathing_example/Makefile +++ b/examples/app_rgb_cycle_breathing_example/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb diff --git a/examples/app_test_HSV_to_RGB/Makefile b/examples/app_test_HSV_to_RGB/Makefile index 2b38775..9c186d4 100644 --- a/examples/app_test_HSV_to_RGB/Makefile +++ b/examples/app_test_HSV_to_RGB/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb diff --git a/examples/app_test_fill_gradient_with_groups/Makefile b/examples/app_test_fill_gradient_with_groups/Makefile index 2b38775..9c186d4 100644 --- a/examples/app_test_fill_gradient_with_groups/Makefile +++ b/examples/app_test_fill_gradient_with_groups/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb diff --git a/examples/app_test_vol_to_level/Makefile b/examples/app_test_vol_to_level/Makefile index 2b38775..9c186d4 100644 --- a/examples/app_test_vol_to_level/Makefile +++ b/examples/app_test_vol_to_level/Makefile @@ -8,7 +8,7 @@ TARGET = XCORE-AI-EXPLORER # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +BUILD_FLAGS = -O2 -g -report -fxscope USED_MODULES = lib_rgb From d2dcdc1c40ac44ceaf70481f02e36f8a5a188fba Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 00:24:47 +0800 Subject: [PATCH 11/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=BD=BF?= =?UTF-8?q?=E7=94=A8xua=E9=85=8D=E7=BD=AE=E7=9A=84=E9=80=89=E9=A1=B9?= =?UTF-8?q?=EF=BC=8C=E7=8E=B0=E5=9C=A8=E9=80=9A=E8=BF=87Makefile=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=EF=BC=88issue=20#2=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 现在通过 _XUA_CONF_H_EXISTS_ 宏控制 - 编译时在BUILD_FLAGS += -D_XUA_CONF_H_EXISTS_ 以使用xua_conf.h --- examples/app_test_config_rgb_port/Makefile | 3 ++- .../app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn | 2 +- examples/app_test_config_rgb_port/src/core/xua_conf.h | 1 - ...eHSV_with_vol_level_example.c => test_config_rgb_port.c} | 0 lib_rgb/api/rgb_driver.h | 6 +++++- lib_rgb/api/rgb_effect.h | 6 +++++- 6 files changed, 13 insertions(+), 5 deletions(-) rename examples/app_test_config_rgb_port/src/{test_cycleHSV_with_vol_level_example.c => test_config_rgb_port.c} (100%) diff --git a/examples/app_test_config_rgb_port/Makefile b/examples/app_test_config_rgb_port/Makefile index 5fe0145..9ee29aa 100644 --- a/examples/app_test_config_rgb_port/Makefile +++ b/examples/app_test_config_rgb_port/Makefile @@ -8,7 +8,8 @@ TARGET = PXUA-316-MC-MAX.xn # report: 打开编译报告 # g: 生成调试信息 # fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) -BUILD_FLAGS = -O2 -g -DDEBUG_PRINT_ENABLE=1 -report -fxscope +# _XUA_CONF_H_EXISTS_: 提示编译器,编译部分文件时包含xua_conf.h +BUILD_FLAGS = -O2 -g -report -fxscope -D_XUA_CONF_H_EXISTS_ USED_MODULES = lib_rgb diff --git a/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn b/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn index 036f02c..c7a918a 100644 --- a/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn +++ b/examples/app_test_config_rgb_port/src/core/PXUA-316-MC-MAX.xn @@ -60,7 +60,7 @@ - + diff --git a/examples/app_test_config_rgb_port/src/core/xua_conf.h b/examples/app_test_config_rgb_port/src/core/xua_conf.h index d02bfae..dd936e1 100644 --- a/examples/app_test_config_rgb_port/src/core/xua_conf.h +++ b/examples/app_test_config_rgb_port/src/core/xua_conf.h @@ -11,5 +11,4 @@ #define RGB_TILE (1) #endif -#include "rgb_driver.h" #endif diff --git a/examples/app_test_config_rgb_port/src/test_cycleHSV_with_vol_level_example.c b/examples/app_test_config_rgb_port/src/test_config_rgb_port.c similarity index 100% rename from examples/app_test_config_rgb_port/src/test_cycleHSV_with_vol_level_example.c rename to examples/app_test_config_rgb_port/src/test_config_rgb_port.c diff --git a/lib_rgb/api/rgb_driver.h b/lib_rgb/api/rgb_driver.h index e76da58..68451ed 100644 --- a/lib_rgb/api/rgb_driver.h +++ b/lib_rgb/api/rgb_driver.h @@ -1,10 +1,14 @@ #ifndef RGB_DRIVER_H #define RGB_DRIVER_H - #include #include //TODO: 在XUA工程环境中测试 +// 在Makefile中定义编译选项,以使用xua配置项覆盖定义 +#ifdef _XUA_CONF_H_EXISTS_ + #include "xua_conf.h" +#endif + #ifndef RGB_TILE #define RGB_TILE (1) #endif diff --git a/lib_rgb/api/rgb_effect.h b/lib_rgb/api/rgb_effect.h index 712d5e0..5365503 100644 --- a/lib_rgb/api/rgb_effect.h +++ b/lib_rgb/api/rgb_effect.h @@ -1,12 +1,16 @@ #ifndef RGB_EFFECT_H #define RGB_EFFECT_H - #include #include #include "rgb_driver.h" #include "samples_to_levels.h" #include "misc_utils.h" +// 在Makefile中定义编译选项,以使用xua配置项覆盖定义 +#ifdef _XUA_CONF_H_EXISTS_ + #include "xua_conf.h" +#endif + // RGB灯的数量 #ifndef NUM_RGBS #define NUM_RGBS (12) From 7b33d5a45e0f5f75412b13799593ecb39c917dd2 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 00:25:08 +0800 Subject: [PATCH 12/22] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app_test_HSV_to_RGB/src/test_hsv_to_rgb.c | 3 ++- .../src/main.xc | 4 +-- .../src/test_fill_gradient_with_groups.c | 26 ++----------------- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/examples/app_test_HSV_to_RGB/src/test_hsv_to_rgb.c b/examples/app_test_HSV_to_RGB/src/test_hsv_to_rgb.c index d997cac..e0d9349 100644 --- a/examples/app_test_HSV_to_RGB/src/test_hsv_to_rgb.c +++ b/examples/app_test_HSV_to_RGB/src/test_hsv_to_rgb.c @@ -1,4 +1,5 @@ #include +#include #include "rgb_effect.h" void test_hsv_to_rgb() { @@ -12,5 +13,5 @@ void test_hsv_to_rgb() color = HSV_to_RGB(&hue, &sat, &value); - printf("GRB color is: 0x%06X\n", color); + printf("GRB color is: 0x%06lX\n", color); } \ No newline at end of file diff --git a/examples/app_test_fill_gradient_with_groups/src/main.xc b/examples/app_test_fill_gradient_with_groups/src/main.xc index d9d76bf..7312d9a 100644 --- a/examples/app_test_fill_gradient_with_groups/src/main.xc +++ b/examples/app_test_fill_gradient_with_groups/src/main.xc @@ -1,8 +1,6 @@ /** @brief 测试 fill_gradient_with_groups 函数 * 此测试函数创建一个缓冲区,并定义两种颜色和每组的填充数量,然后调用 - * fill_gradient_with_groups 函数来填充缓冲区。之后,它会验证缓冲区中的颜色 - * 是否符合预期:每组的前N个LED应该是指定的颜色,剩余的LED应该是关闭的(黑色)。 - * 如果所有LED的颜色都正确,测试通过;否则,输出错误信息并标记测试失败。 + * fill_gradient_with_groups 函数来填充缓冲区,并渲染RGB。 * @author Vergil Wong * @date 2023-11-25 * @param diff --git a/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c b/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c index 7483f8c..506ec16 100644 --- a/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c +++ b/examples/app_test_fill_gradient_with_groups/src/test_fill_gradient_with_groups.c @@ -1,36 +1,14 @@ #include #include // 包含基本的输入输出函数 #include // 包含对封装的定义,引用以使用 on tile[] 语法 -#include "samples_to_levels.h" -#define NUM_RGB_GROUPS 3 + #include "rgb_effect.h" #include "timer.h" void test_fill_gradient_with_groups() { uint32_t buffer[NUM_RGBS]; - uint32_t colors[] = {0xFF0000, 0x00FF00, 0x0000FF}; // 红,绿,蓝 + uint32_t colors[] = {0x110000, 0x001100, 0x000011}; // 红,绿,蓝 size_t num_filled_rgb[] = {4, 4, 4}; // 每组填充数量 - int success = 1; // 使用int代替bool,1代表true,0代表false 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_RGB_GROUPS); - size_t group_led_start = group * (NUM_RGBS / NUM_RGB_GROUPS); - uint32_t expected_color = (i < group_led_start + num_filled_rgb[group]) ? colors[group] : 0x000000; - if (buffer[i] != expected_color) - { - success = 0; // 设置为false - printf("Test failed at LED %zu: expected 0x%06lX, got 0x%06lX\n", i, expected_color, buffer[i]); - break; - } - } - - if (success) - { - printf("Test passed: All LEDs are correctly set.\n"); - } } \ No newline at end of file From dcc547b630b189863af6f0cd62e2c91368e155dd Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 00:37:38 +0800 Subject: [PATCH 13/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++-- lib_rgb/src/visualize_volume.c | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cfa0fce..d6efc3d 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,20 @@ ### 包含头文件 -在你的项目中包含`rgb_effect.h`文件。 +在你的项目中包含`rgb_effect.h`文件,它是较高等级的灯效头文件,用于实现各种灯效,以及向RGB输出灯效 ```c #include "rgb_effect.h" ``` +或者直接包含`visualize_volume.h`,它是最高等级的应用头文件,用于实现可视化的响度。你需要为它创建一个线程,并向它的缓存中写入音频样本,以驱动RGB。 + +```c +#include "visualize_volume.h" +``` + +//TODO: 后续我们会创建一个例程,帮助你快速构建一个线程安全的应用。 + ### 快速开始 如果你想要尝试开箱即用的持续的颜色渐变效果,可以使用`examples/app_hsv_cycle_example/src/hsv_cycle_example.c`中的`hsv_cycle_example`函数,具体的用法可以参考该示例。 @@ -100,9 +108,9 @@ output_rgb_array(rgb_array, 3); - [x] RGB渐变 - [x] HSV渐变 - [ ] 每个灯珠独立的HSV渐变 - - [x] 音频响度响应 - [ ] 使用fp/s32以增加`HSV_to_RGB`的计算效率 +- [ ] 提供音频响度响应的例程与说明 ## 贡献 如果你有任何改进意见或者发现了bug,请通过issues或pull requests来提交或贡献。 diff --git a/lib_rgb/src/visualize_volume.c b/lib_rgb/src/visualize_volume.c index 6bc8865..25bdfd3 100644 --- a/lib_rgb/src/visualize_volume.c +++ b/lib_rgb/src/visualize_volume.c @@ -45,7 +45,7 @@ uint32_t current_hue = 0; // 从红色开始的当前色相值 uint32_t current_color[NUM_RGB_GROUPS] = {0x000000, 0x000000}; -// 根据音频的响度,点亮不同数量的rgb灯 +// 根据音频的响度,点亮不同数量的rgb灯,实现响度可视化 void visualize_loudness_driver() { // 响度对应的RGB灯等级 From 03e0b5793156bcafc46e630854c003faa9b5b6b5 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 10:19:39 +0800 Subject: [PATCH 14/22] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=B4=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/api/rgb_driver.h | 1 + lib_rgb/src/rgb_driver.xc | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib_rgb/api/rgb_driver.h b/lib_rgb/api/rgb_driver.h index 68451ed..bbf43e3 100644 --- a/lib_rgb/api/rgb_driver.h +++ b/lib_rgb/api/rgb_driver.h @@ -2,6 +2,7 @@ #define RGB_DRIVER_H #include #include +#include "misc_utils.h" //TODO: 在XUA工程环境中测试 // 在Makefile中定义编译选项,以使用xua配置项覆盖定义 diff --git a/lib_rgb/src/rgb_driver.xc b/lib_rgb/src/rgb_driver.xc index e2e12ef..7645e67 100644 --- a/lib_rgb/src/rgb_driver.xc +++ b/lib_rgb/src/rgb_driver.xc @@ -7,9 +7,8 @@ #include "rgb_driver.h" // 如果灯的显示没有出现问题,不要修改这些宏 -#define MICROSECONDS 100 -#define RT23_RST_DELAY (MICROSECONDS * 200000) // 200 ms 当前没有使用,因为引脚通常自动拉低 -#define T0H_DELAY (33) // 0.3 μs, assuming MICROSECONDS is 100 times the actual microsecond count +#define RT23_RST_DELAY (MICRO_SECOND * 200000) // 200 ms 当前没有使用,因为引脚通常自动拉低 +#define T0H_DELAY (33) // 0.3 μs, assuming MICRO_SECOND is 100 times the actual microsecond count #define T1H_DELAY (63) // 0.6 μs #define T0L_DELAY (93) // 0.9 μs #define T1L_DELAY (63) // 0.6 μs From 68665a63c8125abbc3a4a0955157603e54f256b0 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 10:19:55 +0800 Subject: [PATCH 15/22] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8D=95=E8=89=B2=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/cycleHSV_vol_level_smooth_example.c | 3 +- .../app_vol_level_smooth_example/Makefile | 24 +++++++++++ .../app_vol_level_smooth_example/src/main.xc | 29 +++++++++++++ .../src/vol_level_smooth_example.c | 42 +++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 examples/app_vol_level_smooth_example/Makefile create mode 100644 examples/app_vol_level_smooth_example/src/main.xc create mode 100644 examples/app_vol_level_smooth_example/src/vol_level_smooth_example.c diff --git a/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c b/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c index b67aa1e..fb69137 100644 --- a/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c +++ b/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c @@ -1,7 +1,6 @@ #include #include #include // 包含基本的输入输出函数 -#include "samples_to_levels.h" #include "rgb_effect.h" #include "timer.h" #define DELAY_DECREASE 20 // 降低亮度时的额外延迟 @@ -29,7 +28,7 @@ void cycleHSV_vol_level_smooth_example() previous_levels[i]--; // 降低当前级别 // 用当前颜色和级别填充渐变 fill_gradient_with_groups(buf, current_color, previous_levels); - delay_milliseconds(DELAY_TIME_RGB); // 延迟 + delay_milliseconds(DELAY_DECREASE); // 延迟 // 更新每一步的RGB颜色 for (size_t j = 0; j < NUM_RGB_GROUPS; j++) { diff --git a/examples/app_vol_level_smooth_example/Makefile b/examples/app_vol_level_smooth_example/Makefile new file mode 100644 index 0000000..9c186d4 --- /dev/null +++ b/examples/app_vol_level_smooth_example/Makefile @@ -0,0 +1,24 @@ +# `TARGET` 变量决定了应用程序编译的目标系统。它可以引用源目录中的一个 XN 文件,或者是在编译时作为 `--target` 选项的一个有效参数。 + +TARGET = XCORE-AI-EXPLORER + +# 编译选项 +# 构建应用程序时传递给 xcc 的参数 +# O2: xcc编译器优化等级2 +# report: 打开编译报告 +# g: 生成调试信息 +# fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) +BUILD_FLAGS = -O2 -g -report -fxscope + +USED_MODULES = lib_rgb + +XCC_FLAGS = $(BUILD_FLAGS) + +XMOS_MODULE_PATH = ../../.. +XCOMMON_DISABLE_AUTO_MODULE_SEARCH = 1 + +#============================================================================= +# 下面部分的 Makefile 包含了用于编译 XMOS 应用程序的公共构建基础设施。你无需编辑此处以下的内容。 + +XMOS_MAKE_PATH ?= ../.. +include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common \ No newline at end of file diff --git a/examples/app_vol_level_smooth_example/src/main.xc b/examples/app_vol_level_smooth_example/src/main.xc new file mode 100644 index 0000000..5eebaf1 --- /dev/null +++ b/examples/app_vol_level_smooth_example/src/main.xc @@ -0,0 +1,29 @@ +/** @brief 测试音量响应&HSV色彩循环 + * + * 此函数不断地循环通过HSV色彩空间,并根据音量水平来更新RGB条的颜色。 + * 每个颜色组都会根据音量水平的随机值来更新其亮度。当前色相值从红色开始, + * 并在每次循环中更新,以通过HSV色彩空间进行循环。每次循环后,将当前颜色 + * 应用到RGB条的相应组中。此函数旨在并发执行,以模拟实时音乐响应的灯光效果。 + * @author Vergil Wong + * @date 2023-11-25 + * @param + * @return + */ + +#include // 包含对封装的定义,引用以使用 on tile[] 语法 + +extern "C"{ + void vol_level_smooth_example(); +} + +int main() // 定义主函数 +{ + par + { + on tile[1]: + { + vol_level_smooth_example(); + } + } + return 0; // 返回0,表示程序正常结束 +} diff --git a/examples/app_vol_level_smooth_example/src/vol_level_smooth_example.c b/examples/app_vol_level_smooth_example/src/vol_level_smooth_example.c new file mode 100644 index 0000000..5448a51 --- /dev/null +++ b/examples/app_vol_level_smooth_example/src/vol_level_smooth_example.c @@ -0,0 +1,42 @@ +#include +#include +#include // 包含基本的输入输出函数 +#include "rgb_effect.h" +#include "timer.h" + +#define DELAY_DECREASE 40 // 降低亮度时的额外延迟 +void vol_level_smooth_example() +{ + uint32_t buf[NUM_RGBS]; // 定义一个用于存储RGB值的缓冲区,大小由NUM_RGBS宏确定 + uint32_t current_color[NUM_RGB_GROUPS] = {0xFF0000, 0xFF0000}; + + srand(1); // 初始化随机数种子 + size_t random_levels[NUM_RGB_GROUPS]; + size_t previous_levels[NUM_RGB_GROUPS] = {0}; // 存储先前的音量级别 + while (1) + { + for (size_t i = 0; i < NUM_RGB_GROUPS; i++) + { + *(random_levels + i) = get_volume_level(rand() % 101 - 100); + // 如果新的级别低于之前的级别,逐渐降低 + while (previous_levels[i] > random_levels[i]) + { + previous_levels[i]--; // 降低当前级别 + // 用当前颜色和级别填充渐变 + fill_gradient_with_groups(buf, current_color, previous_levels); + delay_milliseconds(DELAY_DECREASE); // 延迟 + } + } + + // 用当前渐变颜色填充RGB数组,然后发送给rgb阵列 + fill_gradient_with_groups(buf, current_color, random_levels); + // 延迟以控制显示持续时间 + delay_milliseconds(DELAY_TIME_RGB); + + // 更新先前的音量级别 + for (size_t i = 0; i < NUM_RGB_GROUPS; i++) + { + previous_levels[i] = random_levels[i]; + } + } +} \ No newline at end of file From 35c884be29614245f98c2c5141d222de81a6a542 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 13:06:38 +0800 Subject: [PATCH 16/22] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=A1=BA=E5=BA=8F=E4=BB=A5=E9=81=BF=E5=85=8D=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=AF=AF=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/hsv_cycle_example.c | 2 +- .../src/rgb_cycle_breathing_example.c | 2 +- lib_rgb/api/rgb_effect.h | 2 +- lib_rgb/src/rgb_effect.c | 32 +++++++++++-------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/examples/app_hsv_cycle_example/src/hsv_cycle_example.c b/examples/app_hsv_cycle_example/src/hsv_cycle_example.c index 6479eb6..a9cb75b 100644 --- a/examples/app_hsv_cycle_example/src/hsv_cycle_example.c +++ b/examples/app_hsv_cycle_example/src/hsv_cycle_example.c @@ -12,7 +12,7 @@ void hsv_cycle_example() while (1) { // 用当前渐变颜色填充RGB数组,然后发送给rgb阵列 - fill_gradient(buf, NUM_RGBS, current_color); + fill_gradient(buf, current_color, NUM_RGBS); // 延迟以控制显示持续时间 delay_milliseconds(DELAY_TIME_RGB); diff --git a/examples/app_rgb_cycle_breathing_example/src/rgb_cycle_breathing_example.c b/examples/app_rgb_cycle_breathing_example/src/rgb_cycle_breathing_example.c index 8d445b1..be1de2c 100644 --- a/examples/app_rgb_cycle_breathing_example/src/rgb_cycle_breathing_example.c +++ b/examples/app_rgb_cycle_breathing_example/src/rgb_cycle_breathing_example.c @@ -11,7 +11,7 @@ void rgb_cycle_breathing_example() while (1) { // 用当前渐变颜色填充RGB数组 - fill_gradient(buf, NUM_RGBS, current_color); + fill_gradient(buf, current_color, NUM_RGBS); // 延迟以控制显示持续时间 delay_milliseconds(DELAY_TIME_RGB); // 更改下一个渐变的基色 diff --git a/lib_rgb/api/rgb_effect.h b/lib_rgb/api/rgb_effect.h index 5365503..71dbba3 100644 --- a/lib_rgb/api/rgb_effect.h +++ b/lib_rgb/api/rgb_effect.h @@ -145,7 +145,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, size_t num_filled_rgb, uint32_t color); +void fill_gradient(uint32_t *buf, uint32_t color, size_t num_filled_rgb); /** * 将多种颜色分组填充到RGB条中,并控制显示颜色的时间长度。 diff --git a/lib_rgb/src/rgb_effect.c b/lib_rgb/src/rgb_effect.c index 5d429d5..e2103d7 100644 --- a/lib_rgb/src/rgb_effect.c +++ b/lib_rgb/src/rgb_effect.c @@ -5,7 +5,7 @@ #include "rgb_driver.h" #include "samples_to_levels.h" #include "misc_utils.h" -#include "rgb_effect.h" +#include "rgb_effect.h" // 检查HSV值是否在有效范围内,如果溢出则将值设为最大值,并打印报错 void is_HSV_valid(uint32_t *hue, uint32_t *sat, uint32_t *value) @@ -33,9 +33,9 @@ uint32_t HSV_to_RGB(uint32_t *hue, uint32_t *sat, uint32_t *value) { #ifdef HSV_VALID_CHECK - #if HSV_VALID_CHECK == 0 - (is_HSV_valid(hue, sat, value)); - #endif +#if HSV_VALID_CHECK == 0 + (is_HSV_valid(hue, sat, value)); +#endif #endif uint32_t g; // 现在G是最高8位 @@ -141,11 +141,11 @@ uint32_t cycleHSV(uint32_t *hue) (*hue) = 0; // printf("--------hue is: 0x%06X----------\n", *hue); } - return(HSV_to_RGB(hue, &sat, &value)); + return (HSV_to_RGB(hue, &sat, &value)); } // 将单一颜色填充到整个RGB条中,并控制显示颜色的时间长度 -void fill_gradient(uint32_t *buf, size_t num_filled_rgb, uint32_t color) +void fill_gradient(uint32_t *buf, uint32_t color, size_t num_filled_rgb) { // 遍历缓冲区的每个元素,将其设置为提供的颜色值 for (size_t i = 0; i < num_filled_rgb; i++) @@ -164,30 +164,34 @@ void fill_gradient(uint32_t *buf, size_t num_filled_rgb, uint32_t color) delay_milliseconds(DELAY_TIME_RGB); } -// 将多种颜色分组填充到RGB条中,并控制显示颜色的时间长度。 +// 将多种颜色按分组填充到RGB条中 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_RGB_GROUPS; - for (size_t group = 0; group < NUM_RGB_GROUPS; group++) { - // 填充每个组的LED + for (size_t group = 0; group < NUM_RGB_GROUPS; group++) + { + // 填充每个组的RGB uint32_t *group_buf = buf + group * max_leds_per_group; - for (size_t i = 0; i < num_filled_rgb[group]; i++) { + for (size_t i = 0; i < num_filled_rgb[group]; i++) + { *(group_buf + i) = colors[group]; } - // 熄灭该组剩余的LED - for (size_t i = num_filled_rgb[group]; i < max_leds_per_group; i++) { + // 熄灭该组剩余的RGB + for (size_t i = num_filled_rgb[group]; i < max_leds_per_group; i++) + { *(group_buf + i) = 0x000000; } // 如果组号为奇数,翻转该组对应的buffer元素 - if (group % 2 != 0) { + if (group % 2 != 0) + { reverse_buf(group_buf, max_leds_per_group); } } // 将缓冲区输出到RGB条 output_rgb_array(buf, NUM_RGBS); +} -} \ No newline at end of file From 47a202691b6f2dcbd8faa297238d21a8150f005b Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 15:09:17 +0800 Subject: [PATCH 17/22] =?UTF-8?q?HUE=E7=9A=84=E6=AD=A5=E8=BF=9B=E5=80=BC?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E6=98=AF=E4=B8=80=E4=B8=AA=E5=8F=AF=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/api/rgb_effect.h | 5 +++++ lib_rgb/src/rgb_effect.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib_rgb/api/rgb_effect.h b/lib_rgb/api/rgb_effect.h index 71dbba3..ad3c0ee 100644 --- a/lib_rgb/api/rgb_effect.h +++ b/lib_rgb/api/rgb_effect.h @@ -53,6 +53,10 @@ #define DELAY_TIME_RGB (1000 / RGB_MAX) #endif +// 控制cycleHSV中,HUE递增的步进值,该步进值应能整除360,且不超过它 +#ifndef HUE_STEP +#define HUE_STEP (1) +#endif /** * 定义渐变方向的枚举类型。 * @@ -161,4 +165,5 @@ void fill_gradient(uint32_t *buf, uint32_t color, size_t num_filled_rgb); */ void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb); +void fill_gradient_with_groups_colorful(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb); #endif //RGB_EFFECT_H \ No newline at end of file diff --git a/lib_rgb/src/rgb_effect.c b/lib_rgb/src/rgb_effect.c index e2103d7..39085a0 100644 --- a/lib_rgb/src/rgb_effect.c +++ b/lib_rgb/src/rgb_effect.c @@ -133,7 +133,7 @@ uint32_t cycleHSV(uint32_t *hue) uint32_t value = 100; if (*hue < 359) { - (*hue) += 1; + (*hue) += HUE_STEP; // printf("hue is: 0x%06X\n", *hue); } else @@ -195,3 +195,32 @@ void fill_gradient_with_groups(uint32_t *buf, uint32_t *colors, size_t *num_fill output_rgb_array(buf, NUM_RGBS); } +// 将多种颜色按RGB的数量,分组填充到RGB条中 +void fill_gradient_with_groups_colorful(uint32_t *buf, uint32_t *colors, size_t *num_filled_rgb) +{ + size_t max_leds_per_group = NUM_RGBS / NUM_RGB_GROUPS; + + for (size_t group = 0; group < NUM_RGB_GROUPS; group++) + { + // 填充每个组的RGB,其中每个RGB的颜色是独立赋值的 + uint32_t *group_buf = buf + group * max_leds_per_group; + for (size_t i = 0; i < num_filled_rgb[group]; i++) + { + *(group_buf + i) = colors[i + group * max_leds_per_group]; + } + + // 熄灭该组剩余的RGB + for (size_t i = num_filled_rgb[group]; i < max_leds_per_group; i++) + { + *(group_buf + i) = 0x000000; + } + + // 如果组号为奇数,翻转该组对应的buffer元素 + if (group % 2 != 0) + { + reverse_buf(group_buf, max_leds_per_group); + } + } + // 将缓冲区输出到RGB条 + output_rgb_array(buf, NUM_RGBS); +} \ No newline at end of file From 26391ab9536c32336556c03421967173c66df2ce Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 15:09:33 +0800 Subject: [PATCH 18/22] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86RGB=E7=81=AF?= =?UTF-8?q?=E5=B8=A6=E7=9A=84=E5=B9=BB=E5=BD=A9=E6=B8=90=E5=8F=98=E6=95=88?= =?UTF-8?q?=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app_hsv_cycle_per_rgb_example/Makefile | 24 ++++++++++ .../src/core/xua_conf.h | 26 +++++++++++ .../src/hsv_cycle_per_rgb_example.c | 45 +++++++++++++++++++ .../app_hsv_cycle_per_rgb_example/src/main.xc | 32 +++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 examples/app_hsv_cycle_per_rgb_example/Makefile create mode 100644 examples/app_hsv_cycle_per_rgb_example/src/core/xua_conf.h create mode 100644 examples/app_hsv_cycle_per_rgb_example/src/hsv_cycle_per_rgb_example.c create mode 100644 examples/app_hsv_cycle_per_rgb_example/src/main.xc diff --git a/examples/app_hsv_cycle_per_rgb_example/Makefile b/examples/app_hsv_cycle_per_rgb_example/Makefile new file mode 100644 index 0000000..87cb5a7 --- /dev/null +++ b/examples/app_hsv_cycle_per_rgb_example/Makefile @@ -0,0 +1,24 @@ +# `TARGET` 变量决定了应用程序编译的目标系统。它可以引用源目录中的一个 XN 文件,或者是在编译时作为 `--target` 选项的一个有效参数。 + +TARGET = XCORE-AI-EXPLORER + +# 编译选项 +# 构建应用程序时传递给 xcc 的参数 +# O2: xcc编译器优化等级2 +# report: 打开编译报告 +# g: 生成调试信息 +# fxscope: 使用 xSCOPE,对代码进行跟踪(默认使用xlink) +BUILD_FLAGS = -O2 -g -report -fxscope -D_XUA_CONF_H_EXISTS_ + +USED_MODULES = lib_rgb lib_xcore_math + +XCC_FLAGS = $(BUILD_FLAGS) + +XMOS_MODULE_PATH = ../../.. +XCOMMON_DISABLE_AUTO_MODULE_SEARCH = 1 + +#============================================================================= +# 下面部分的 Makefile 包含了用于编译 XMOS 应用程序的公共构建基础设施。你无需编辑此处以下的内容。 + +XMOS_MAKE_PATH ?= ../.. +include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common \ No newline at end of file diff --git a/examples/app_hsv_cycle_per_rgb_example/src/core/xua_conf.h b/examples/app_hsv_cycle_per_rgb_example/src/core/xua_conf.h new file mode 100644 index 0000000..9cc6aae --- /dev/null +++ b/examples/app_hsv_cycle_per_rgb_example/src/core/xua_conf.h @@ -0,0 +1,26 @@ +/** + * @file xua_conf.h + * @brief Defines relating to device configuration and customisation. + * For PXUA-XU316-MC-MAX + */ +#ifndef _XUA_CONF_H_ +#define _XUA_CONF_H_ + +/* Defines relating to feature placement regarding tiles */ +#ifndef RGB_TILE +#define RGB_TILE (1) +#endif + +#ifndef DELAY_TIME_RGB +#define DELAY_TIME_RGB (60) +#endif + +#ifndef RGB_MAX +#define RGB_MAX (20) +#endif + +#ifndef HUE_STEP +#define HUE_STEP (10) +#endif + +#endif diff --git a/examples/app_hsv_cycle_per_rgb_example/src/hsv_cycle_per_rgb_example.c b/examples/app_hsv_cycle_per_rgb_example/src/hsv_cycle_per_rgb_example.c new file mode 100644 index 0000000..33d9bc3 --- /dev/null +++ b/examples/app_hsv_cycle_per_rgb_example/src/hsv_cycle_per_rgb_example.c @@ -0,0 +1,45 @@ +#include +#include +#include // 包含基本的输入输出函数 +#include "rgb_effect.h" +#include "timer.h" +#include "xmath/xmath.h" +#include + +void init_colors(uint32_t *current_colors, uint32_t *previous_colors, uint32_t current_hue){ + for (size_t i = 0; i < NUM_RGBS; i++) + { + *(current_colors + i) = cycleHSV(¤t_hue); + } + // TODO: 当使用xs3_memcpy时,复制的后几个数据看起来是随机的 + memcpy(previous_colors, current_colors, NUM_RGBS * sizeof(uint32_t)); +} + +void hsv_cycle_per_rgb_example() +{ + uint32_t buf[NUM_RGBS]; // 定义一个用于存储RGB值的缓冲区,大小由NUM_RGBS宏确定 + uint32_t current_hue = 0; // 从红色开始的当前色相值 + uint32_t WORD_ALIGNED current_colors[NUM_RGBS]; //用于存储当前颜色值的数组。 + uint32_t WORD_ALIGNED previous_colors[NUM_RGBS];//用于存储上一组颜色值的数组 + size_t levels[NUM_RGB_GROUPS]; //指定每个RGB组亮灯的个数 + init_colors(current_colors, previous_colors, current_hue); //初始化颜色 + while (1) + { + + for (size_t i = 0; i < NUM_RGB_GROUPS; i++) + { + *(levels + i) = (NUM_RGBS / NUM_RGB_GROUPS)-1; + } + // 用当前渐变颜色填充RGB数组,然后发送给rgb阵列 + fill_gradient_with_groups_colorful(buf, current_colors, levels); + + // 延迟以控制显示持续时间 + delay_milliseconds(DELAY_TIME_RGB); + // 更改下一组颜色,其中每个灯继承上一个灯的颜色,第一个灯通过cycleHSV计算出新的颜色 + memcpy(current_colors+1, previous_colors, (NUM_RGBS-1) * sizeof(uint32_t)); + current_colors[0] = cycleHSV(¤t_hue); + + // 保留本轮颜色的记录 + memcpy(previous_colors, current_colors, NUM_RGBS * sizeof(uint32_t)); + } +} \ No newline at end of file diff --git a/examples/app_hsv_cycle_per_rgb_example/src/main.xc b/examples/app_hsv_cycle_per_rgb_example/src/main.xc new file mode 100644 index 0000000..ce6eabb --- /dev/null +++ b/examples/app_hsv_cycle_per_rgb_example/src/main.xc @@ -0,0 +1,32 @@ +/** @brief RGB灯条HSV循环渐变 + * + * 此函数使用初始色相值初始化两个颜色数组current_colors和preview_colors。 + * 它通过调用'cycleHSV'函数为每个颜色位置生成一个基于当前色相的颜色值。 + * 然后,它在一个无限循环中,使用'fill_gradient_with_groups_colorful'函数 + * 将颜色渐变填充到一个RGB缓冲区,并通过'output_rgb_array'函数输出。 + * 在每次循环的末尾,颜色数组会更新,为下一次迭代准备新的颜色。 + * + * @author Vergil Wong + * @date 2023-11-26 + * @param + * @return + */ + +#include // 包含对封装的定义,引用以使用 on tile[] 语法 + +extern "C" +{ + void hsv_cycle_per_rgb_example(); +} + +int main() // 定义主函数 +{ + par + { + on tile[1]: + { + hsv_cycle_per_rgb_example(); + } + } + return 0; // 返回0,表示程序正常结束 +} From adc05560bf31b0ebf58880054827fea10ac361c0 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 15:27:26 +0800 Subject: [PATCH 19/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0README.md=EF=BC=8C?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E4=BA=86=E7=A4=BA=E4=BE=8B=E7=9A=84=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d6efc3d..37aeefc 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,30 @@ # RGB灯条驱动库 -![_453de8b7-7edc-4efc-94c3-809a46000e66](https://img.pawpaw.cn:4430/assets/1699707514504RmN8t1mh.jpeg) +_453de8b7-7edc-4efc-94c3-809a46000e66 ## 简介 `lib_rgb` 是一个用于控制RGB灯条的C语言库,提供了一系列功能,比如颜色渐变、呼吸灯效果和色调循环等。它支持通过RGB和HSV颜色空间来控制灯条的颜色输出。 ## 功能 -- **响度可视化**:通过检测音频样本中的响度,实现RGB跟随音量大小跳动 +### 组件 -- **颜色渐变**:通过逐步调整RGB值来模拟呼吸灯效果。 +| 功能 | 示例 | 备注 | +| ------------ | ------------------------------------ | ------------------------------------------------------------ | +| **颜色渐变** | `app_rgb_cycle_breathing_example` | 通过逐步调整RGB值来模拟呼吸灯效果。 | +| **色调循环** | `app_hsv_cycle_example` | 在HSV颜色空间中循环改变色调以实现颜色渐变。 | +| **颜色转换** | `app_test_HSV_to_RGB` | 将HSV颜色值转换为RGB颜色值。 | +| **颜色填充** | `app_test_fill_gradient_with_groups` | 将单一颜色填充到整个RGB条中,也可以将RGB条分组,同时输出不同的颜色或响应。 | +| **幻彩渐变** | `app_hsv_cycle_per_rgb_example` | 每个灯珠独立的HSV渐变,以获得颜色流动的效果。 | -- **色调循环**:在HSV颜色空间中循环改变色调以实现颜色渐变。 +### 高级应用 -- **颜色转换**:将HSV颜色值转换为RGB颜色值。 - -- **颜色填充**:将单一颜色填充到整个RGB条中,也可以将RGB条分组,同时输出不同的颜色或响应 - -- **音量响应**:根据响度值(-xdB~0dB),获取音量等级 +| 功能 | 示例 | 备注 | +| -------------------------- | --------------------------------------- | ----------------------------------------------------- | +| **响度可视化** | 待补充 | 通过检测音频样本中的响度,实现RGB跟随音量大小跳动 | +| **色调循环&音量响应** | `app_cycleHSV_vol_level_example` | 根据响度值(-xdB~0dB),获取音量等级。 | +| **平滑&音量响应** | `app_vol_level_smooth_example` | 一种过渡效果,当响度下降时,逐级熄灭RGB,而不是突变。 | +| **平滑&色调循环&音量响应** | `app_cycleHSV_vol_level_smooth_example` | 结合了HSV颜色渐变,平滑过渡,以及音量响应 | ## 使用方法 @@ -104,10 +111,10 @@ output_rgb_array(rgb_array, 3); ## 路线图 - [x] 为绘制RGB的函数添加可合并选项,以减少资源占用 -- [ ] 添加更多应用光效 +- [x] 添加更多应用光效 - [x] RGB渐变 - [x] HSV渐变 - - [ ] 每个灯珠独立的HSV渐变 + - [x] 每个灯珠独立的HSV渐变 - [x] 音频响度响应 - [ ] 使用fp/s32以增加`HSV_to_RGB`的计算效率 - [ ] 提供音频响度响应的例程与说明 From d8247c8b33d7b12c363dfe0e2c26e9de35ce9eb4 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 15:40:18 +0800 Subject: [PATCH 20/22] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app_cycleHSV_vol_level_smooth_example/src/main.xc | 2 +- examples/app_vol_level_smooth_example/src/main.xc | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc b/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc index 60f9b26..155ed3a 100644 --- a/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc +++ b/examples/app_cycleHSV_vol_level_smooth_example/src/main.xc @@ -1,4 +1,4 @@ -/** @brief 平滑过渡HSV颜色和音量级别 +/** @brief 平滑过渡&HSV循环&模拟音量响应 * @author Vergil Wong * @date 2023-11-25 * @param diff --git a/examples/app_vol_level_smooth_example/src/main.xc b/examples/app_vol_level_smooth_example/src/main.xc index 5eebaf1..07e63b7 100644 --- a/examples/app_vol_level_smooth_example/src/main.xc +++ b/examples/app_vol_level_smooth_example/src/main.xc @@ -1,9 +1,8 @@ -/** @brief 测试音量响应&HSV色彩循环 +/** @brief 音量响应&平滑过渡 * - * 此函数不断地循环通过HSV色彩空间,并根据音量水平来更新RGB条的颜色。 - * 每个颜色组都会根据音量水平的随机值来更新其亮度。当前色相值从红色开始, - * 并在每次循环中更新,以通过HSV色彩空间进行循环。每次循环后,将当前颜色 - * 应用到RGB条的相应组中。此函数旨在并发执行,以模拟实时音乐响应的灯光效果。 + * 每个颜色组都会根据音量水平的随机值来更新其亮度。每次循环后,将当前颜色 + * 应用到RGB条的相应组中。此函数旨在模拟实时音乐响应的灯光效果,并实现平滑过渡下降 + * 与app_cy * @author Vergil Wong * @date 2023-11-25 * @param From 5b3ebb2a58137780a9dae5a746a1115d2f738bb7 Mon Sep 17 00:00:00 2001 From: Vergil_Wong Date: Sun, 26 Nov 2023 15:48:26 +0800 Subject: [PATCH 21/22] =?UTF-8?q?module=E4=BD=BF=E7=94=A8=E4=B8=A5?= =?UTF-8?q?=E6=A0=BC=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -调整了DELAY_DECREASE为40,使得演示效果更佳 --- .../src/cycleHSV_vol_level_smooth_example.c | 2 +- lib_rgb/module_build_info | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c b/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c index fb69137..a0fdbe7 100644 --- a/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c +++ b/examples/app_cycleHSV_vol_level_smooth_example/src/cycleHSV_vol_level_smooth_example.c @@ -3,7 +3,7 @@ #include // 包含基本的输入输出函数 #include "rgb_effect.h" #include "timer.h" -#define DELAY_DECREASE 20 // 降低亮度时的额外延迟 +#define DELAY_DECREASE 40 // 降低亮度时的额外延迟 // 该函数用于平滑过渡HSV颜色和音量级别 void cycleHSV_vol_level_smooth_example() diff --git a/lib_rgb/module_build_info b/lib_rgb/module_build_info index a272bd7..1d97770 100644 --- a/lib_rgb/module_build_info +++ b/lib_rgb/module_build_info @@ -2,7 +2,7 @@ VERSION = 0.0.5 DEPENDENT_MODULES = lib_xcore_math(>=2.1.0) \ -MODULE_XCC_FLAGS = $(XCC_FLAGS) -g -O3 -Wall +MODULE_XCC_FLAGS = $(XCC_FLAGS) -g -O3 -Wall -Wextra -Werror EXPORT_INCLUDE_DIRS = api From 36b82cb4c79d0430539cde76414c22679e721902 Mon Sep 17 00:00:00 2001 From: Vergil Wong Date: Mon, 27 Nov 2023 09:39:44 +0800 Subject: [PATCH 22/22] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=87=B30.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib_rgb/module_build_info | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_rgb/module_build_info b/lib_rgb/module_build_info index 1d97770..517b192 100644 --- a/lib_rgb/module_build_info +++ b/lib_rgb/module_build_info @@ -1,4 +1,4 @@ -VERSION = 0.0.5 +VERSION = 0.1.0 DEPENDENT_MODULES = lib_xcore_math(>=2.1.0) \