SumProject/applications/assistant.c

164 lines
4.4 KiB
C
Raw Normal View History

2025-03-14 17:01:29 +08:00
/*
* DMA 使
* uart_dma_sample
* uart_dma_sample uart1
* 使使
* "hello RT-Thread!"
*/
2025-03-14 17:01:29 +08:00
#include <rtthread.h>
#include <rtdevice.h>
#include "status.h"
2025-03-14 17:01:29 +08:00
#define SAMPLE_UART_NAME "uart3" /* 串口设备名称 */
2025-03-14 17:01:29 +08:00
#define LOG_TAG "assistant"
#define DBG_LVL DBG_LOG
// #define DBG_LVL DBG_INFO
#define USE_LOG1
#define USE_LOG2
#define USE_LOG3
// #define USE_LOG4
#define USE_LOG5
// #define USE_LOG6
// #define USE_LOG_D
#include "logn.h"
/* 串口接收消息结构 */
struct rx_msg
{
rt_device_t dev;
rt_size_t size;
};
/* 串口设备句柄 */
static rt_device_t serial;
/* 消息队列控制块 */
static struct rt_messagequeue rx_mq;
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
struct rx_msg msg;
rt_err_t result;
msg.dev = dev;
msg.size = size;
result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
if (result == -RT_EFULL)
{
/* 消息队列满 */
rt_kprintf("message queue full\n");
}
return result;
}
void assist_control(char *rx_buffer)
{
LOG3("assist_control CMD:%s",rx_buffer);
if (rt_strstr(rx_buffer, "cc")!=RT_NULL||rt_strstr(rx_buffer, "3")!=RT_NULL)
{
2025-03-21 22:10:18 +08:00
fan_status=1;
fan_on();
rt_kprintf("加大抽风\n");
}
else if (rt_strstr(rx_buffer, "dd")!=RT_NULL||rt_strstr(rx_buffer, "4")!=RT_NULL)
{
2025-03-21 22:10:18 +08:00
fan_status=0;
fan_off();
rt_kprintf("关闭抽风\n");
}
else if (rt_strstr(rx_buffer, "ee")!=RT_NULL||rt_strstr(rx_buffer, "1")!=RT_NULL)
{
2025-03-21 22:10:18 +08:00
light_status=1;
light_on();
rt_kprintf("开灯\n");
}
else if (rt_strstr(rx_buffer, "ff")!=RT_NULL||rt_strstr(rx_buffer, "2")!=RT_NULL)
{
2025-03-21 22:10:18 +08:00
light_status=0;
light_off();
rt_kprintf("关灯\n");
}
2025-03-21 22:10:18 +08:00
else if (rt_strstr(rx_buffer, "bb")!=RT_NULL||rt_strstr(rx_buffer, "2")!=RT_NULL)
{
beep_on();
}
}
2025-03-14 17:01:29 +08:00
static void serial_thread_entry(void *parameter)
{
struct rx_msg msg;
rt_err_t result;
rt_uint32_t rx_length;
static char rx_buffer[BSP_UART3_RX_BUFSIZE + 1];
2025-03-14 17:01:29 +08:00
while (1)
{
rt_memset(&msg, 0, sizeof(msg));
/* 从消息队列中读取消息 */
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
if (result > 0)
{
/* 从串口读取数据 */
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
rx_buffer[rx_length] = '\0';
assist_control(rx_buffer);
2025-03-14 17:01:29 +08:00
/* 打印数据 */
rt_kprintf("%s\n", rx_buffer);
2025-03-14 17:01:29 +08:00
}
}
}
2025-03-14 22:57:49 +08:00
void serial_send(char *str)
{
rt_device_write(serial, 0, str, (sizeof(str) - 1));
}
2025-03-14 17:01:29 +08:00
int aiv_init(void)
{
LOG5("VOICE assistant START");
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
static char msg_pool[256];
char str[] = "hello RT-Thread!\r\n";
rt_strcpy(uart_name, SAMPLE_UART_NAME);
/* 查找串口设备 */
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
/* 初始化消息队列 */
rt_mq_init(&rx_mq, "rx_mq",
msg_pool, /* 存放消息的缓冲区 */
sizeof(struct rx_msg), /* 一条消息的最大长度 */
sizeof(msg_pool), /* 存放消息的缓冲区大小 */
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
2025-03-14 17:01:29 +08:00
/* 以 DMA 接收及轮询发送方式打开串口设备 */
rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
/* 发送字符串 */
// rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(aiv_init, siri start);