优化灯光控制

This commit is contained in:
helloyifa 2025-05-22 17:27:21 +08:00
parent ce728f5362
commit c43d0bdae6
8 changed files with 207 additions and 466 deletions

View File

@ -78,6 +78,7 @@ void app_event_asr_evt_callback(media_app_evt_type_t event, uint32_t param)
case MEDIA_APP_EVT_ASR_WAKEUP_IND:
bk_printf("MEDIA_APP_EVT_ASR_WAKEUP_IND_sent_abort_msg\n");
ws2812_led_asr_wakeup();
sent_abort_msg();//zhanyu
app_event_send_msg(APP_EVT_ASR_WAKEUP, 0);

View File

@ -352,7 +352,7 @@ static void handle_system_event(key_event_t event)
if (ispoweron == 1)
{
bk_printf(TAG, "POWER OFFaaa~~~ \r\n");
clearallled();
ws2812_led_clear_all();
ispoweron = 0;
bk_gpio_enable_output(51);
bk_gpio_set_output_low(51);
@ -649,7 +649,8 @@ static void handle_system_event(key_event_t event)
bk_aud_intf_set_spk_gain(0x29); // 0x30
bk_config_write("volume", (void *)&volume, 4);
//poka
ws2812_init();
thing_init();
// spi_led_init();
#endif

View File

@ -81,12 +81,14 @@ static int wanson_asr_result_notify_handle(wanson_asr_handle_t wanson_asr, char
if (asr_result > 0)
{
LOGE("%s \n", "!!!!!!!!!!!!!");
ws2812_led_asr_wakeup();
#if (CONFIG_SYS_CPU1)
aud_tras_drv_set_dialog_run_state_by_asr_result(asr_result);
#endif
#if (CONFIG_SYS_CPU2)
send_spi_led_msg();
asr_to_media_major_msg.event = EVENT_ASR_RESULT_NOTIFY;
asr_to_media_major_msg.result = asr_result;

View File

@ -13,56 +13,32 @@
#include "bk_genie_comm.h"
#include "iot_lamp.h"
#include "spi_led.h"
#define TAG "iot_lamp"
#define LOGI(...) BK_LOGI(TAG, ##__VA_ARGS__)
#define LOGW(...) BK_LOGW(TAG, ##__VA_ARGS__)
#define LOGE(...) BK_LOGE(TAG, ##__VA_ARGS__)
#define LOGD(...) BK_LOGD(TAG, ##__VA_ARGS__)
/*
[{
* "name": "Screen",
* "description": "这是小牛牛的灯光的亮度",
* "properties": {
* "theme": {
* "description": "主题",
* "type": "string"
* },
* "brightness": {
* "description": "当前亮度百分比",
* "type": "number"
* }},
* "methods": {
* "SetTheme": {
* "description": "设置屏幕主题",
* "parameters": {
* "theme_name": {
* "description": "主题模式, light 或 dark",
* "type": "string"
* }
* }
* },
* "SetBrightness": {
* "description": "设置亮度",
* "parameters": {
* "brightness": {
* "description": "0到100之间的整数",
* "type": "number"
* }
* }
* }
* }
* }]
*/
#define IOT_LAMP_DEVICE_NAME "lamp"
#define IOT_LAMP_DEVICE_FUNS_SET_BRIGHTNESS "SetBrightness"
void lamp_init(){
//ws2812_init();
}
void lamp_deinit(){
//ws2812_deinit();
}
cJSON* iot_lamp_get_device_desc(){
cJSON *lamp_desc = cJSON_CreateObject();
cJSON_AddStringToObject(lamp_desc, "name", IOT_LAMP_DEVICE_NAME);
cJSON_AddStringToObject(lamp_desc, "description", "这是小牛牛的灯光");
cJSON_AddStringToObject(lamp_desc, "description", "这是乐小牛的灯光");
cJSON *properties = cJSON_CreateObject();
//定义brightness 属性
@ -97,7 +73,7 @@ cJSON* iot_lamp_get_device_desc(){
cJSON *parameters = cJSON_CreateObject();
cJSON *p_brightness = cJSON_CreateObject();
cJSON_AddStringToObject(p_brightness, "type", "number");
cJSON_AddStringToObject(p_brightness, "description", "0到100之间的整数");
cJSON_AddStringToObject(p_brightness, "description", "0到100之间的整数 0关灯");
cJSON_AddItemToObjectCS(parameters,"brightness",p_brightness);
@ -129,6 +105,11 @@ void iot_lamp_parser_invoke(char* cmd,char * paramters_json){
// {"brightness": 60}
int brightness = cJSON_GetObjectItem(params, "brightness")->valueint;
LOGE("invoke brightness-->%d\n",brightness);
if(brightness == 0){
ws2812_led_clear_all();
}else{
ws2812_set_all_led(0xFFFFFF,1);
}
cJSON_Delete(params);
}

View File

@ -17,6 +17,10 @@ extern "C" {
//#include "bk_websocket_client.h"
#include "cJSON.h"
void lamp_init();
void lamp_deinit();
cJSON * iot_lamp_get_device_desc();
void iot_lamp_parser_invoke(char* cmd,char * paramters_json);
#ifdef __cplusplus

View File

@ -28,6 +28,10 @@
*/
void thing_init()
{
#ifdef IOT_LAMP
lamp_init();
#endif
#ifdef IOT_SPEAKER
speaker_volume_init();
#endif

View File

@ -51,6 +51,20 @@ const int RED = 0x00FF00;
const int BLUE = 0x0000FF;
const int GREEN = 0xFF0000;
const int WHITE = 0xFFFFF;
const int TEST_1 = 0xFF0000;//GRB
const int TEST_2 = 0x00FF00;
const int TEST_3 = 0x0000FF;
const int TEST_4 = 0xFFFF00;
const int TEST_5 = 0xFF00FF;
const int TEST_6 = 0x00FFFF;
const int TEST_7 = 0xFFFFFF;
#define WS2812_LED_NUM 6
static uint8_t led_data[WS2812_LED_NUM *24*sizeof(uint8_t)];
static int ws2812_start = 0;
typedef struct
{
beken_thread_t handle;
@ -70,8 +84,15 @@ typedef struct
} spi_data_test_config_t;
spi_data_test_config_t s_spi_test;
static spi_data_test_config_t s_spi_test;
static int colorIndex = 0;
static int colors[] = {RED, GREEN, WHITE};
static beken_queue_t spi_led_msg_que = NULL;
static beken_thread_t spi_led_thread_hdl = NULL;
//static beken_thread_t spi_led_thread_hdl_gsensor = NULL;
int adjustColorBrightness(int color, float brightness)
{
// 提取红、绿、蓝通道的值
@ -88,13 +109,90 @@ int adjustColorBrightness(int color, float brightness)
return (red << 16) | (green << 8) | blue;
}
int open_dmaspi(int color, float brightness)
{
bk_printf("SPI xinxin\n");
int controlled_lednum = 6;
// int total_lednum = 6;
bk_err_t err;
static void spi_led_task_gsensor(void *arg)
{
turnonall_Led(1);
}
static void spi_led_task(void *arg)
{
bk_err_t ret = BK_OK;
while(ws2812_start){
// bk_printf("rtos_pop_from_queue22222222\n");
ws2812_msg_t msg;
ret = rtos_pop_from_queue(&spi_led_msg_que, &msg, BEKEN_WAIT_FOREVER);
bk_printf("rtos_pop_from_queue\n");
if (kNoErr == ret)
{
switch(msg.type){
//唤醒词 灯效果
case TYPE_WAKEUP:
ws2812_set_all_led(0xFFFFFF,0);
ws2812_set_all_led(0xFFFFFF,1);
rtos_delay_milliseconds(50);
ws2812_set_all_led(0xFFFFFF,0);
rtos_delay_milliseconds(50);
ws2812_set_all_led(0xFFFFFF,1);
rtos_delay_milliseconds(50);
ws2812_set_all_led(0xFFFFFF,0);
break;
case TYPE_OFF:
ws2812_set_all_led(0xFFFFFF,0);
break;
case TYPE_CHANGE_COLOR:
break;
}
}
}
/* delete msg queue */
ret = rtos_deinit_queue(&spi_led_msg_que);
if (ret != kNoErr)
{
bk_printf("delete spi_led_msg_que fail\n");
}
spi_led_msg_que = NULL;
/* delete task */
if(spi_led_thread_hdl!= NULL) {
rtos_delete_thread(&spi_led_thread_hdl);
spi_led_thread_hdl = NULL;
}
bk_printf("delete spi_led_thread task\n");
}
void ws2812_set_all_led(int color,int brightness){
bk_printf("ws2812_set_all_led\n");
memset(led_data,0x0C,sizeof(led_data));
// 对颜色进行亮度调整
int adjustedColor = adjustColorBrightness(color, brightness);
int index = 0;
// 为灯填充数据
for (int j = 0; j < WS2812_LED_NUM; j++)
{
for (int i = 23; i >= 0; i--)
{
int bit = (adjustedColor >> i) & 0x01;
if (bit)
{
led_data[index++] = 0xFC;
}
else
{
led_data[index++] = 0xC0;
}
}
}
bk_spi_write_bytes(0, led_data, WS2812_LED_NUM*24);
}
void ws2812_init(){
bk_printf("ws2812_init\n");
spi_config_t config = {0};
config.role = SPI_ROLE_MASTER;
config.bit_width = SPI_BIT_WIDTH_8BITS;
@ -104,9 +202,8 @@ int open_dmaspi(int color, float brightness)
config.baud_rate = 6500000;
config.bit_order = SPI_MSB_FIRST;
//gpio_dev_unmap(GPIO_46);
//config.dma_mode = 1;
// // 分配DMA通道
//分配DMA通道
//config.spi_tx_dma_chan = bk_dma_alloc(DMA_DEV_GSPI0);
//config.spi_tx_dma_width = DMA_DATA_WIDTH_8BITS;
@ -120,438 +217,77 @@ int open_dmaspi(int color, float brightness)
// bk_spi_driver_init();
// bk_spi_init(0, &config);
err = bk_spi_driver_init();
if (err != BK_OK)
{
bk_printf("SPI driver init failed, err: %d\n", err);
return -1;
}
bk_spi_init(0, &config);
// bk_spi_register_tx_finish_isr(0, (spi_isr_t)spi_data_test_tx_finish_isr, NULL);
// 初始化 SPI 设备
// err = bk_spi_init(0, &config);
// if (err != BK_OK) {
// bk_printf("SPI init failed, err: %d\n", err);
// bk_spi_driver_deinit();
// return -1;
// }
// 注册发送完成回调函数
int tx_len = 6 * 24; // 6 个灯,每个灯 24 位
UINT8 *buf = os_malloc(tx_len * sizeof(UINT8));
if (buf == NULL)
{
bk_printf("Memory allocation failed!\n");
return -1;
}
int index = 0;
// 对颜色进行亮度调整
int adjustedColor = adjustColorBrightness(color, brightness);
// 为灯填充数据
for (int j = 0; j < controlled_lednum; j++)
{
for (int i = 23; i >= 0; i--)
{
int bit = (adjustedColor >> i) & 0x01;
if (bit)
{
buf[index++] = 0xFC;
}
else
{
buf[index++] = 0xE0;
}
}
}
// for (int i = 0; i < tx_len; i++)
// {
// buf[i] = 0xFC;
// }
// 发送数据
err = bk_spi_dma_write_bytes(0, buf, tx_len);
// err = bk_spi_write_bytes(0, buf, tx_len);
if (err != BK_OK)
{
bk_printf("SPI write bytes failed, err: %d\n", err);
}
else
{
bk_printf("SPI write bytes success\n");
}
rtos_delay_milliseconds(100);
// 释放DMA通道
// bk_dma_free(DMA_DEV_GSPI0, config.spi_tx_dma_chan);
// 反初始化SPI
// bk_spi_deinit(id);
os_free(buf);
bk_spi_deinit(0);
bk_dma_free(DMA_DEV_DTCM, s_spi_test.spi_tx_dma_chan);
bk_dma_free(DMA_DEV_DTCM, s_spi_test.spi_rx_dma_chan);
return 0;
}
void clearallled()
{
// int controlled_lednum = 6;
int total_lednum = 6;
int err;
spi_config_t config = {0};
config.role = SPI_ROLE_MASTER;
config.bit_width = SPI_BIT_WIDTH_8BITS;
config.polarity = 0;
config.phase = 0;
config.wire_mode = SPI_4WIRE_MODE;
config.baud_rate = 8000000;
config.bit_order = SPI_MSB_FIRST;
// gpio_dev_unmap(GPIO_46);
config.dma_mode = 1;
s_spi_test.spi_tx_dma_chan = bk_dma_alloc(DMA_DEV_DTCM);
config.spi_tx_dma_chan = s_spi_test.spi_tx_dma_chan;
s_spi_test.spi_rx_dma_chan = bk_dma_alloc(DMA_DEV_DTCM);
config.spi_rx_dma_chan = s_spi_test.spi_rx_dma_chan;
config.spi_tx_dma_width = DMA_DATA_WIDTH_8BITS;
config.spi_rx_dma_width = DMA_DATA_WIDTH_8BITS;
err = bk_spi_driver_init();
if (err != BK_OK)
{
bk_printf("SPI driver init failed, err: %d\n", err);
return;
}
bk_spi_init(0, &config);
int tx_len = total_lednum * 24; // 6 个灯,每个灯 24 位
UINT8 *buf = os_malloc(tx_len * sizeof(UINT8));
if (buf == NULL)
{
bk_printf("Memory allocation failed!\n");
return;
}
// 先将整个 buf 数组初始化为 0
for (int i = 0; i < tx_len; i++)
{
buf[i] = 0xC0;
}
err = bk_spi_dma_write_bytes(0, buf, tx_len);
// err = bk_spi_write_bytes(0, buf, tx_len);
if (err != BK_OK)
{
bk_printf("SPI write bytes failed, err: %d\n", err);
}
else
{
bk_printf("SPI write bytes success\n");
}
rtos_delay_milliseconds(100);
// 释放DMA通道
// bk_dma_free(DMA_DEV_GSPI0, config.spi_tx_dma_chan);
// 反初始化SPI
// bk_spi_deinit(id);
os_free(buf);
bk_spi_deinit(0);
bk_dma_free(DMA_DEV_DTCM, s_spi_test.spi_tx_dma_chan);
bk_dma_free(DMA_DEV_DTCM, s_spi_test.spi_rx_dma_chan);
return;
}
// 控制多个灯同时显示指定颜色,并可调节亮度
void setAllLedsColor(int color, float brightness)
{
bk_err_t err;
int controlled_lednum = 6;
int total_lednum = 6;
spi_config_t config = {0};
config.role = SPI_ROLE_MASTER;
config.bit_width = SPI_BIT_WIDTH_8BITS;
config.polarity = 0;
config.phase = 0;
config.wire_mode = SPI_4WIRE_MODE;
config.baud_rate = 6500000;
config.bit_order = SPI_MSB_FIRST;
// gpio_dev_unmap(GPIO_46);
bk_spi_driver_init();
bk_spi_init(0, &config);
bk_printf("SPI blue\n");
err = bk_spi_driver_init();
if (err != BK_OK)
{
bk_printf("SPI driver init failed, err: %d\n", err);
// bk_spi_driver_deinit();
return;
}
// 初始化 SPI 设备
err = bk_spi_init(0, &config);
if (err != BK_OK)
{
bk_printf("SPI init failed, err: %d\n", err);
// bk_spi_driver_deinit();
return;
}
int tx_len = total_lednum * 24; // 6 个灯,每个灯 24 位
UINT8 *buf = os_malloc(tx_len * sizeof(UINT8));
if (buf == NULL)
{
bk_printf("Memory allocation failed!\n");
// bk_spi_driver_deinit();
return;
}
// 先将整个 buf 数组初始化为 0
// 先将整个 buf 数组初始化为 0 对应的编码
if (controlled_lednum != total_lednum)
{
for (int i = 0; i < tx_len; i++)
{
buf[i] = 0xC0;
}
}
// os_memset(buf, 0, tx_len);
bk_printf("setAllLedsColor\n");
int index = 0, ret;
// 对颜色进行亮度调整
int adjustedColor = adjustColorBrightness(color, brightness);
// 为灯填充数据
for (int j = 0; j < controlled_lednum; j++)
{
for (int i = 23; i >= 0; i--)
{
int bit = (adjustedColor >> i) & 0x01;
if (bit)
{
buf[index++] = 0xFC;
}
else
{
buf[index++] = 0xC0;
}
}
}
ret = bk_spi_write_bytes(0, buf, tx_len);
if (ret)
bk_printf("spi dma send error%d\r\n", ret);
else
{
for (int i = 0; i < tx_len; i++)
{
bk_printf("%02x,", buf[i]);
if ((i + 1) % 32 == 0)
bk_printf("\r\n");
}
bk_printf("\r\n");
}
os_free(buf);
// bk_spi_driver_deinit();
return;
}
// void turnonall_Led(int Colour,float brightness){
// // float brightness = 1; // 亮度值,范围从 0.0 到 1.0
// setAllLedsColor(Colour, brightness);
// }
static int colorIndex = 0;
void turnonall_Led(float brightness)
{
int colors[] = {RED, GREEN, WHITE};
int currentColor = colors[colorIndex];
// 调用设置颜色的函数
open_dmaspi(currentColor, brightness);
// 更新颜色索引,实现循环
colorIndex = (colorIndex + 1) % 3;
}
//static beken_queue_t spi_led_msg_que = NULL;
static beken_thread_t spi_led_thread_hdl = NULL;
static beken_thread_t spi_led_thread_hdl_gsensor = NULL;
static void spi_led_task_gsensor(void *arg)
{
// bk_err_t ret = BK_OK;
// bk_printf("rtos_pop_from_queue22222222\n");
// spi_led_msg_t msg;
// ret = rtos_pop_from_queue(&spi_led_msg_que, &msg, BEKEN_WAIT_FOREVER);
// bk_printf("rtos_pop_from_queue\n");
// if (kNoErr == ret)
// {
turnonall_Led(1);
bk_printf("turnonall_Led\n");
// }
/* delete msg queue */
// ret = rtos_deinit_queue(&spi_led_msg_que);
// if (ret != kNoErr)
// {
// bk_printf("delete spi_led_msg_que fail\n");
// }
// spi_led_msg_que = NULL;
/* delete task */
if(spi_led_thread_hdl_gsensor!= NULL) {
rtos_delete_thread(&spi_led_thread_hdl_gsensor);
spi_led_thread_hdl_gsensor = NULL;
}
//spi_led_thread_hdl_gsensor = NULL;
bk_printf("delete spi_led_thread_hdl_gsensor task\n");
// rtos_delete_thread(NULL);
}
static void spi_led_task(void *arg)
{
// bk_err_t ret = BK_OK;
// bk_printf("rtos_pop_from_queue22222222\n");
// spi_led_msg_t msg;
// ret = rtos_pop_from_queue(&spi_led_msg_que, &msg, BEKEN_WAIT_FOREVER);
// bk_printf("rtos_pop_from_queue\n");
// if (kNoErr == ret)
// {
open_dmaspi(WHITE, 1);
rtos_delay_milliseconds(50);
clearallled();
rtos_delay_milliseconds(50);
open_dmaspi(WHITE, 1);
rtos_delay_milliseconds(50);
clearallled();
rtos_delay_milliseconds(50);
open_dmaspi(WHITE, 1);
bk_printf("delete spi_led_ms87878787877\n");
// }
/* delete msg queue */
// ret = rtos_deinit_queue(&spi_led_msg_que);
// if (ret != kNoErr)
// {
// bk_printf("delete spi_led_msg_que fail\n");
// }
// spi_led_msg_que = NULL;
/* delete task */
if(spi_led_thread_hdl!= NULL) {
rtos_delete_thread(&spi_led_thread_hdl);
spi_led_thread_hdl = NULL;
}
// spi_led_thread_hdl = NULL;
bk_printf("delete spi_led_thread task\n");
}
void spi_led_init_gsensor()
{
bk_printf("spi_led_init_gsensor\n");
ws2812_start = 1;
bk_err_t ret = BK_OK;
// ret = rtos_init_queue(&spi_led_msg_que, "spi_led_msg_que", sizeof(spi_led_msg_t), 100);
// if (ret != kNoErr)
// {
// bk_printf("create spi_led_msg queue fail\n");
// }
ret = rtos_create_thread(&spi_led_thread_hdl_gsensor,
4,
"spi_led_gsensor",
(beken_thread_function_t)spi_led_task_gsensor,
32 * 1024,
NULL);
ret = rtos_init_queue(&spi_led_msg_que, "spi_led_msg_que", sizeof(ws2812_msg_t), 2);
if (ret != kNoErr)
{
spi_led_thread_hdl_gsensor = NULL;
bk_printf("create spi_led_msg queue fail\n");
}
}
void spi_led_init()
{
bk_printf("spi_led_init_hi_a_mi_nuo\n");
bk_err_t ret = BK_OK;
// ret = rtos_init_queue(&spi_led_msg_que, "spi_led_msg_que", sizeof(spi_led_msg_t), 100);
// if (ret != kNoErr)
// {
// bk_printf("create spi_led_msg queue fail\n");
// }
ret = rtos_create_thread(&spi_led_thread_hdl,
4,
"spi_led",
(beken_thread_function_t)spi_led_task,
32 * 1024,
5 * 1024,
NULL);
if (ret != kNoErr)
{
spi_led_thread_hdl = NULL;
}
}
void send_spi_led_msg()
{
// bk_printf("send_spi_led_msg\n");
// spi_led_msg_t msg;
// if (spi_led_msg_que)
// {
// bk_printf("send_spi_led_msg_rtos_push_to_queue\n");
// rtos_push_to_queue(&spi_led_msg_que, &msg, BEKEN_NO_WAIT);
// }else{
// // rtos_push_to_queue(&spi_led_msg_que, &msg, BEKEN_NO_WAIT);
// bk_printf("send_spi_led_msg_rtos_push_to_queue4444444444\n");
// }
// sentAbortmsg();
spi_led_init();
void ws2812_deinit(){
bk_printf("ws2812_deinit\n");
ws2812_start = 0;
// 释放DMA通道
// bk_dma_free(DMA_DEV_GSPI0, config.spi_tx_dma_chan);
// 反初始化SPI
// bk_spi_deinit(id);
bk_spi_deinit(0);
bk_dma_free(DMA_DEV_DTCM, s_spi_test.spi_tx_dma_chan);
bk_dma_free(DMA_DEV_DTCM, s_spi_test.spi_rx_dma_chan);
}
void ws2812_led_wifi_disconnect(){
bk_printf("hi ws2812_led_wifi_disconnect777777\n");
open_dmaspi(RED, 1);
//bk_printf("hi ws2812_led_wifi_disconnect777777\n");
//open_dmaspi(RED, 1);
ws2812_set_all_led(RED,1);
}
void ws2812_led_wifi_connected(){
bk_printf("hi ws2812_led_wifi_connected22\n");
open_dmaspi(WHITE, 1);
//bk_printf("hi ws2812_led_wifi_connected22\n");
//open_dmaspi(WHITE, 1);
ws2812_set_all_led(WHITE,1);
}
void ws2812_led_asr_wakeup(){
if (spi_led_msg_que != NULL)
{
ws2812_msg_t msg;
msg.type = TYPE_WAKEUP;
rtos_push_to_queue(&spi_led_msg_que, &msg, BEKEN_NO_WAIT);
}
void ws2812_led_clear_all(){
bk_printf("hi ws2812_led_clear_all8888\n");
clearallled();
}
void spi_led_init_gsensor()
{
int currentColor = colors[colorIndex];
// 调用设置颜色的函数
ws2812_set_all_led(currentColor, 1);
// 更新颜色索引,实现循环
colorIndex = (colorIndex + 1) % 3;
}
void ws2812_led_change_color(){
ws2812_msg_t msg;
msg.type = TYPE_CHANGE_COLOR;
rtos_push_to_queue(&spi_led_msg_que, &msg, BEKEN_NO_WAIT);
}
void ws2812_led_clear_all(){
//bk_printf("hi ws2812_led_clear_all8888\n");
//clearallled();
ws2812_set_all_led(0x000000,1);
}

View File

@ -4,10 +4,18 @@
extern "C" {
#endif
typedef enum sleep_mode
{
TYPE_OFF= 0,
TYPE_WAKEUP,
TYPE_CHANGE_COLOR,
} led_type;
typedef struct
{
} spi_led_msg_t;
led_type type;
} ws2812_msg_t;
char* getGlobalSessionId(void);
int open_dmaspi(int color, float brightness);
@ -22,7 +30,11 @@ void ws2812_led_clear_all();
void spi_led_init_gsensor();
uint8_t get_binary_data_status();
void ws2812_init();
void ws2812_deinit();
void ws2812_set_all_led(int color,int brightness);
void ws2812_led_asr_wakeup();
void ws2812_led_change_color();
#ifdef __cplusplus
}
#endif