本例实验的WS2812B模块:
本例实验的WS2812B模块的电原理图
本案例使用串口
串口是一种采用串行通信方式的扩展接口,可以用于连接计算机和外设,或者实现不同设备之间的数据传输。串口的特点是通信线路简单,成本低,适用于远距离通信,但传输速度较慢。常见的串口有RS-232、RS-422、RS-485等,它们有不同的电平标准、传输距离和负载能力。串口通信需要遵循一定的通信协议,如UART、SPI等,来规定数据的格式、时序和控制信号。
【花雕动手做】实验三十五:通过0#串口接收字符串控制WS2812B的点亮和变色
实验开源代码
- #include "asr.h"
- extern "C"{ void * __dso_handle = 0 ;}
- #include "setup.h"
- #include "HardwareSerial.h"
- #include "myLib/asr_ws2812.h"
-
- uint32_t snid;
- String Rec;
- void UART_RX();
- void ASR_CODE();
- void app();
-
- //{speak:小蝶-清新女声,vol:10,speed:10,platform:haohaodada}
- //{playid:10001,voice:欢迎使用人工智能AI语音助手,用智能管家唤醒我}
- //{playid:10002,voice:我退下了,用智能管家唤醒我}
-
- WS2812 ASR_WS2812_27(27);
- void UART_RX(){
- while (1) {
- if(Serial.available() > 0){
- Rec = Serial.readString();
- if(Rec == "ho"){
- ASR_WS2812_27.setBrightness(30);
- ASR_WS2812_27.pixel_set_all_color(255,0,0);
- ASR_WS2812_27.pixel_show();
- Serial.println("hong");
- }
- else if(Rec == "lan"){
- ASR_WS2812_27.setBrightness(40);
- ASR_WS2812_27.pixel_set_all_color(0,0,255);
- ASR_WS2812_27.pixel_show();
- Serial.println("lan");
- }
- else if(Rec == "lv"){
- ASR_WS2812_27.setBrightness(40);
- ASR_WS2812_27.pixel_set_all_color(0,255,0);
- ASR_WS2812_27.pixel_show();
- Serial.println("lv");
- }
- }
- delay(2);
- }
- vTaskDelete(NULL);
- }
-
- /*描述该功能...
- */
- void ASR_CODE(){
- //语音识别功能框,与语音识别成功时被自动调用一次。
- set_state_enter_wakeup(15000);
- switch (snid) {
- case 0:
- digitalWrite(4,0);
- Serial.println("hello");
- break;
- case 1:
- ASR_WS2812_27.setBrightness(30);
- ASR_WS2812_27.pixel_set_all_color(255,0,0);
- ASR_WS2812_27.pixel_show();
- break;
- case 2:
- ASR_WS2812_27.setBrightness(40);
- ASR_WS2812_27.pixel_set_all_color(0,0,255);
- ASR_WS2812_27.pixel_show();
- break;
- case 3:
- ASR_WS2812_27.setBrightness(40);
- ASR_WS2812_27.pixel_set_all_color(0,255,0);
- ASR_WS2812_27.pixel_show();
- break;
- }
-
- }
-
- void app(){
- //操作系统的一个线程,独立主循环任务,可支持多个类似线程任务。
- //当存在多个线程任务时,注意优先级与占用内存设置。
- while (1) {
- delay(100);
- }
- vTaskDelete(NULL);
- }
-
- void sys_sleep_hook()
- {
- digitalWrite(4,1);
- ASR_WS2812_27.pixel_set_all_color(0,0,0);
- ASR_WS2812_27.pixel_show();
-
- }
-
- void hardware_init(){
- xTaskCreate(UART_RX,"UART_RX",128,NULL,4,NULL);
- xTaskCreate(app,"app",128,NULL,4,NULL);
- //需要操作系统启动后初始化的内容
- vol_set(7);
- setPinFun(13,SECOND_FUNCTION);
- setPinFun(14,SECOND_FUNCTION);
- Serial.begin(9600);
- Rec = "";
- ASR_WS2812_27.pixel_init(3);
- vTaskDelete(NULL);
- }
-
- void setup()
- {
- //需要操作系统启动前初始化的内容
- //{ID:0,keyword:"唤醒词",ASR:"智能管家",ASRTO:"我在"}
- //{ID:1,keyword:"命令词",ASR:"打开红灯",ASRTO:"已经打开红灯"}
- //{ID:2,keyword:"命令词",ASR:"打开蓝灯",ASRTO:"已经打开蓝灯"}
- //{ID:3,keyword:"命令词",ASR:"打开绿灯",ASRTO:"已经打开绿灯"}
- setPinFun(4,FIRST_FUNCTION);
- pinMode(4,output);
- digitalWrite(4,1);
- }
复制代码
训练模型与编译烧录
实验串口返回情况
串口输出截图
实验场景图
|