401 lines
9.6 KiB
C
401 lines
9.6 KiB
C
|
#include <common/sys_config.h>
|
||
|
#include <components/log.h>
|
||
|
#include <modules/wifi.h>
|
||
|
#include <components/netif.h>
|
||
|
#include <components/event.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include "bk_private/bk_init.h"
|
||
|
#include <components/system.h>
|
||
|
#include <os/os.h>
|
||
|
#include <components/shell_task.h>
|
||
|
#include "cli.h"
|
||
|
#include "media_service.h"
|
||
|
#include <driver/pwr_clk.h>
|
||
|
#include <driver/pwr_clk.h>
|
||
|
#include <modules/pm.h>
|
||
|
#if CONFIG_BUTTON
|
||
|
#include <key_main.h>
|
||
|
#include <key_adapter.h>
|
||
|
#endif
|
||
|
#include "sys_driver.h"
|
||
|
#include "sys_hal.h"
|
||
|
#include <driver/gpio.h>
|
||
|
#include "gpio_driver.h"
|
||
|
#include "bk_genie_comm.h"
|
||
|
#include "wifi_boarding_utils.h"
|
||
|
#if (CONFIG_SYS_CPU0)
|
||
|
#include "beken_config.h"
|
||
|
#include "aud_intf.h"
|
||
|
#include "bk_factory_config.h"
|
||
|
#if CONFIG_NETWORK_AUTO_RECONNECT
|
||
|
#include "bk_genie_smart_config.h"
|
||
|
#endif
|
||
|
#include "motor.h"
|
||
|
#endif
|
||
|
|
||
|
#include "app_event.h"
|
||
|
#include "countdown.h"
|
||
|
#include <led_blink.h>
|
||
|
#include <common/bk_include.h>
|
||
|
#include "components/bluetooth/bk_dm_bluetooth.h"
|
||
|
|
||
|
|
||
|
#include <os/os.h>
|
||
|
#include <driver/spi.h>
|
||
|
#include <driver/trng.h>
|
||
|
#include <driver/uart.h>
|
||
|
#include <driver/dma.h>
|
||
|
|
||
|
const int RED = 0x00FF00;
|
||
|
const int BLUE = 0x0000FF;
|
||
|
const int GREEN = 0xFF0000;
|
||
|
const int WHITE = 0xFFFFF;
|
||
|
typedef struct {
|
||
|
beken_thread_t handle;
|
||
|
beken_semaphore_t uart_rx_semaphore;
|
||
|
beken_semaphore_t spi_tx_finish_semaphore;
|
||
|
spi_role_t role;
|
||
|
uint32_t baud_rate;
|
||
|
spi_id_t spi_id;
|
||
|
uart_id_t uart_id;
|
||
|
uint32_t data_len;
|
||
|
uint8_t *data;
|
||
|
uint8_t *uart_get_data;
|
||
|
uint8_t *spi_get_buffer;
|
||
|
dma_id_t spi_tx_dma_chan; /**< SPI tx dma channel */
|
||
|
dma_id_t spi_rx_dma_chan; /**< SPI rx dma channel */
|
||
|
bool exchange_flag;
|
||
|
|
||
|
} spi_data_test_config_t;
|
||
|
|
||
|
|
||
|
spi_data_test_config_t s_spi_test;
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
int adjustColorBrightness(int color, float brightness)
|
||
|
{
|
||
|
// 提取红、绿、蓝通道的值
|
||
|
int red = (color >> 16) & 0xFF;
|
||
|
int green = (color >> 8) & 0xFF;
|
||
|
int blue = color & 0xFF;
|
||
|
|
||
|
// 根据亮度值对每个通道的值进行缩放
|
||
|
red = (int)(red * brightness);
|
||
|
green = (int)(green * brightness);
|
||
|
blue = (int)(blue * 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;
|
||
|
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);
|
||
|
|
||
|
// config.dma_mode = 1;
|
||
|
// // 分配DMA通道
|
||
|
// config.spi_tx_dma_chan = bk_dma_alloc(DMA_DEV_GSPI0);
|
||
|
// config.spi_tx_dma_width = DMA_DATA_WIDTH_8BITS;
|
||
|
|
||
|
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;
|
||
|
// 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;
|
||
|
}
|