Files
luban-lite-t3e-pro/application/rt-thread/t3e-pro/components/speech_sr/app_sr_fun.c

271 lines
7.1 KiB
C
Raw Normal View History

2025-09-30 11:56:06 +08:00
/**
* @file app_sr.c
* @brief this file contains basic fuctions and DP data report fucntion which is base on DP type
* @author
* @date 2020.03.13
* @par email:
* @copyright HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
* @par company
* http://www.tuya.com
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "ci130x.h"
#include "app_sr_fun.h"
#include "custom.h"
#include "nvs_eepom.h"
#include "zigbee_fun.h"
#include "zigbee.h"
//#define LOG_TAG "app_sr" // 该模块对应的标签。不定义时默认NO_TAG
#define LOG_LVL LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
#include <ulog.h> // 必须在 LOG_TAG与LOG_LVL下面
rt_thread_t speech_thread=NULL;
void app_sr_task(void *pvParameter);
//struct srserial_configure uart_config=RT_SERIAL_CONFIG_DEFAULT;
static rt_device_t srserial;
int speech_uart_int(void)
{
//串口初始化
char uart_name[]="uart2";
LOG_D("Try to open(%s)",uart_name);
srserial = rt_device_find(uart_name);
if (!srserial)
{
LOG_D("find %s failed!\n", uart_name);
return -RT_ERROR;
}
rt_err_t ret = rt_device_open(srserial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
// ASSERT(ret != RT_EOK, "open %s failed : %d !\n", uart_name, ret);
if (ret != RT_EOK)
{
LOG_D("open %s failed : %d !\n", uart_name, ret);
return -RT_ERROR;
}
//uart_config.
//配置串口
//rt_device_control(srserial,RT_DEVICE_CTRL_CONFIG,(void*)&uart_config);
// //创建动态消息队列 返回消息队列对象的句柄
// sr_rx_evt_queue= rt_mq_create("zigbee_mq", //消息队列的名称
// sizeof(ZIGEvent), //消息队列中一条消息的最大长度,单位字节
// 10, //消息队列的最大个数
// RT_IPC_FLAG_PRIO ); //消息队列采用的阻塞线程的等待方式它可以取如下数值RT_IPC_FLAG_FIFO //按照先进先出排序或RT_IPC_FLAG_PRIO //按照优先级排序
// if(!sr_rx_evt_queue)
// {
// LOG_E("RT-Thread sr_rx_evt_queue create mq fail");
// }
//创建任务
speech_thread = rt_thread_create("app_sr", //名称
app_sr_task, //线程代码
RT_NULL, //参数
4096, //栈大小
15, //优先级
12); //时间片
if (speech_thread != RT_NULL)
{
rt_thread_startup(speech_thread); //线程进入就绪态
}
else
{
LOG_D("dynamic_thread create failure\n");
}
return RT_EOK;
}
//串口发送
void speech_uart_sendByte(unsigned char value)
{
rt_device_write(srserial, 0, &value, 1);
}
void speech_uart_sendBytes(unsigned char *in, unsigned short len)
{
//LOG_D("zigbee_uart_sendBytes\n");
ulog_hex(LOG_TAG,8,(const char *)in,len);//一行 32byte内容
rt_device_write(srserial, 0, in, len);
}
#define SR_RX_BUF_SIZE (128)
#define SR_TX_BUF_SIZE (128)
void app_sr_task(void *pvParameter)
{
// ZIGEvent evt;
// evt.length=0;
// evt.buffStr=NULL;
//char ch;
char ci130x_recbuff[64];
memset(ci130x_recbuff,0,sizeof(ci130x_recbuff));
// ZIGEvent evt;
// evt.event=SR_STATE_IDLE;
// evt.length=0;
// evt.buffStr=NULL;
// rt_thread_mdelay(1000);
// Configure a temporary buffer for the incoming data
uint8_t *rxdata = (uint8_t *) malloc(SR_RX_BUF_SIZE);
LOG_D("app_sr_task running");
//ci130x_protocol_init();
for(;;)
{
// // rt_thread_mdelay(1000);
// // LOG_D("app_sr_task");
// if(evt.buffStr)//接收到的是指针 用完释放内存
// {
// LOG_D("free receData.buffStr:%x",(uint32_t)evt.buffStr);
// free(evt.buffStr);
// evt.buffStr=NULL;
// }
// //rt_thread_mdelay(10);
// if(rt_mq_recv(sr_rx_evt_queue,&evt,sizeof(ZIGEvent),10)==RT_EOK)
// {//上报
// switch(evt.event)
// {
// default:
// {
// LOG_D( "event error!!!");
// }
// break;
// }
// }
// else
{
rt_thread_mdelay(100);
int rxBytes = rt_device_read(srserial, -1, rxdata, SR_RX_BUF_SIZE);
if (rxBytes > 7)
{
// //rxdata[rxBytes] = 0;//在串口接收的数据增加结束符
// //LOG_D( "rx:%s", (char *)rxdata);
// LOG_D("rxBytes:%X\r\n",rxBytes);
// LOG_HEX(LOG_TAG,8,rxdata,rxBytes);
// memset(ci130x_recbuff,0,sizeof(ci130x_recbuff));
// for(int rxBytesindex=0;rxBytesindex<rxBytes;rxBytesindex++)
// {
// rt_kprintf(":%02X\r\n",rxdata[rxBytesindex]);
// ci130x_recbuff[rxBytesindex]=rxdata[rxBytesindex];
// //uart_receive_input(ch);//#error "please call this fuction in the interrupt fuction of serial receive, and delete this line"
// //ci130x_uart_receive_input(rxdata[rxBytesindex]);//#error "please call this fuction in the interrupt fuction of serial receive, and delete this line"
// }
// //esp_log_buffer_hex(TAG,rxdata,rxBytes);
if( (rxdata[0]==0xA5)//帧头
&&(rxdata[1]==0xFA)//帧头
&&(rxdata[7]==0xFB)//帧尾
)
{//一帧数据
//提取命令
rt_kprintf("cmd:%02X\r\n",rxdata[4]);
/*
//打开空调 2
//关闭空调 3
if(rxdata[4]==2)
{
rt_kprintf("open air\r\n");
evt.event=ZIG_STATE_1on;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
else if(rxdata[4]==3)
{
rt_kprintf("close air\r\n");
evt.event=ZIG_STATE_1off;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
//制冷模式 5
//制热模式 6
else if(rxdata[4]==5)
{
rt_kprintf("hot mode\r\n");
evt.event=ZIG_STATE_2on;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
else if(rxdata[4]==6)
{
rt_kprintf("cool mode\r\n");
evt.event=ZIG_STATE_2off;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
//除湿模式 7
//送风模式 8
else if(rxdata[4]==7)
{
rt_kprintf("shi mode\r\n");
evt.event=ZIG_STATE_3on;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
else if(rxdata[4]==8)
{
rt_kprintf("fan mode\r\n");
evt.event=ZIG_STATE_3off;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
//升高温度 9
//降低温度 10
else if(rxdata[4]==9)
{
rt_kprintf("add temp\r\n");
evt.event=ZIG_STATE_4on;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}
else if(rxdata[4]==10)
{
rt_kprintf("sub temp\r\n");
evt.event=ZIG_STATE_4off;
rt_mq_send(custom_rx_evt_queue,&evt, sizeof(ZIGEvent));
}*/
}
}
else
{
if (rxBytes < 0)
pr_debug("read() return [%ld] %s\n",rt_get_errno(), rt_strerror(rt_get_errno()));
}
//ci130x_uart_service();//#error "please call this fucntion in main while(1){}, and delete this line"
}
}
}