智能家居(2)---串口通信(语音识别)控制线程封装
封装语音线程(语音通过串口和主控设备进行交流)实现对智能家居中各种灯光的控制
mainPro.c(主函数)
#include <stdio.h>
#include "controlDevice.h"
#include "inputCommand.h"
#include <pthread.h>struct Devices *pdeviceHead = NULL; //设备工厂链表头
struct InputCommander *pcommandHead = NULL; //指令工厂链表头struct Devices* findDeviceByName(struct Devices *phead,char *name) //在设备链表中查找设备(语音和socket均可使用)
{struct Devices *tmp = phead;if(tmp == NULL){printf("The devicesLink is NULL");return NULL;}else{while(tmp != NULL){if(strcmp(tmp->deviceName,name) == 0){return tmp;}tmp = tmp->next;}return NULL; }}struct InputCommander* findCommanderByName(struct InputCommander *phead,char *name) //在控制链表中查找相关控制
{struct InputCommander *tmp = phead;if(tmp == NULL){printf("The commanderLink is NULL");return NULL;}else{while(tmp != NULL){if(strcmp(tmp->commandName,name) == 0){return tmp;}tmp = tmp->next;}return NULL; }}void doCommand(struct InputCommander *cmd) //根据传入的命令控制相关设备
{struct Devices *tmp = NULL;if(strstr(cmd->command,"START")){ //初始化所有设备tmp = findDeviceByName(pdeviceHead,"bathroomLight");if(tmp != NULL) tmp->deviceInit(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"bedroomLight");if(tmp != NULL) tmp->deviceInit(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"livingroomLight");if(tmp != NULL) tmp->deviceInit(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"restaurantLight");if(tmp != NULL) tmp->deviceInit(tmp->pinNum);printf("The devices all init success\n");}else if(strstr(cmd->command,"OL1")){ //打开卫生间灯tmp = findDeviceByName(pdeviceHead,"bathroomLight");if(tmp != NULL) tmp->open(tmp->pinNum);printf("The bathroomLight already open\n");}else if(strstr(cmd->command,"CL1")){ //关闭卫生间灯tmp = findDeviceByName(pdeviceHead,"bathroomLight");if(tmp != NULL) tmp->close(tmp->pinNum);printf("The bathroomLight already close\n");}else if(strstr(cmd->command,"OL2")){ //打开卧室灯tmp = findDeviceByName(pdeviceHead,"bedroomLight");if(tmp != NULL) tmp->open(tmp->pinNum);printf("The bedroomLight already open\n");}else if(strstr(cmd->command,"CL2")){ //关闭卧室灯tmp = findDeviceByName(pdeviceHead,"bedroomLight");if(tmp != NULL) tmp->close(tmp->pinNum);printf("The bedroomLight already close\n");}else if(strstr(cmd->command,"OL3")){ //打开客厅灯tmp = findDeviceByName(pdeviceHead,"livingroomLight");if(tmp != NULL) tmp->open(tmp->pinNum);printf("The livingroomLight already open\n");}else if(strstr(cmd->command,"CL3")){ //关闭客厅灯tmp = findDeviceByName(pdeviceHead,"livingroomLight");if(tmp != NULL) tmp->close(tmp->pinNum);printf("The livingroomLight already close\n");}else if(strstr(cmd->command,"OL4")){ //打开餐厅灯tmp = findDeviceByName(pdeviceHead,"restaurantLight");if(tmp != NULL) tmp->open(tmp->pinNum);printf("The restaurantLight already open\n");}else if(strstr(cmd->command,"CL4")){ //关闭餐厅灯tmp = findDeviceByName(pdeviceHead,"restaurantLight");if(tmp != NULL) tmp->close(tmp->pinNum);printf("The restaurantLight already close\n");}else if(strstr(cmd->command,"ALL1")){ //打开所有的灯tmp = findDeviceByName(pdeviceHead,"bathroomLight");if(tmp != NULL) tmp->open(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"bedroomLight");if(tmp != NULL) tmp->open(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"livingroomLight");if(tmp != NULL) tmp->open(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"restaurantLight");if(tmp != NULL) tmp->open(tmp->pinNum);printf("The Light all open success\n");}else if(strstr(cmd->command,"ALL0")){ //关闭所有的灯tmp = findDeviceByName(pdeviceHead,"bathroomLight");if(tmp != NULL) tmp->close(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"bedroomLight");if(tmp != NULL) tmp->close(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"livingroomLight");if(tmp != NULL) tmp->close(tmp->pinNum);tmp = findDeviceByName(pdeviceHead,"restaurantLight");if(tmp != NULL) tmp->close(tmp->pinNum);printf("The Light all close success\n");}}void* voice_pthread(void *data) //语音识别(串口通信)处理线程函数
{struct InputCommander *voiceHandler;int n_read;voiceHandler = findCommanderByName(pcommandHead,"voice");if(voiceHandler == NULL){printf("find voice error\n");pthread_exit(NULL);}else{if(voiceHandler->Init(voiceHandler) < 0 ){printf("%s init error\n",voiceHandler->commandName);pthread_exit(NULL);}elseprintf("%s init success\n",voiceHandler->commandName);while(1){n_read = voiceHandler->getCommand(voiceHandler);if(n_read == 0){printf("no command from voice\n");}else{//根据指令进行控制printf("read %d message-->:%s",n_read,voiceHandler->command);doCommand(voiceHandler); //执行命令}}}}int main()
{pthread_t voice_pth;if(wiringPiSetup()<0){//初始化wiringPi外设库printf("wiringPi Init failed\n");return -1;}//1.指令工厂初始化pcommandHead = addVoiceToCommandLink(pcommandHead); //将语音控制加入命令控制链表//2.设备控制工厂初始化pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead); //将卫生灯加入设备链表pdeviceHead = addbedroomLightToDeviceLink(pdeviceHead); //将卧室灯加入设备链表pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead); //将餐厅灯加入设备链表pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead); //将客厅灯加入设备链表pdeviceHead = addFireToDeviceLink(pdeviceHead); //将火灾检测加入设备链表pdeviceHead = addBeepToDeviceLink(pdeviceHead); //将蜂鸣器加入设备链表//3.线程池建立//3.1语音线程int ret = pthread_create(&voice_pth,NULL,voice_pthread,NULL);if (ret != 0) {printf("Failed to create voicethread.\n");return -1;}//等待线程退出pthread_join(voice_pth,NULL);return 0;
}
inputCommand.h(控制类)
#include <wiringPi.h>
#include <stddef.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>struct InputCommander
{char commandName[128]; //命令名字char command[32]; //具体命令char deviceName[128]; //打开的设备名,比如串口char port[12]; //端口号char ipAddress[32]; //ip地址int (*Init)(struct InputCommander *commder);int (*getCommand)(struct InputCommander *commder);int fd; //文件描述符int baud; //波特率int s_fd; //socket服务端文件描述符int c_fd; //客户端文件描述符char log[1024]; //日志struct InputCommander *next;
};struct InputCommander *addVoiceToCommandLink(struct InputCommander *phead); //语音控制加入命令控制链表
voiceControl.c(语音)
#include "inputCommand.h"int voiceInit(struct InputCommander *voiceMes)
{int fd;fd =serialOpen(voiceMes->deviceName,voiceMes->baud);//打开串口if(fd<0){printf("open serial fail!\r\n");//判断串口打开是否成功return -1;}elsevoiceMes->fd = fd;//传回串口文件描述符return fd;}int voiceGetCommand(struct InputCommander *voiceMes){int nread = 0;memset(voiceMes->command,0,sizeof(voiceMes->command));nread = read(voiceMes->fd,voiceMes->command,sizeof(voiceMes->command));return nread;}struct InputCommander voiceControl = {.commandName = "voice",.command = {'\0'},.deviceName = "/dev/ttyAMA0",.baud = 9600,.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL,};struct InputCommander *addVoiceToCommandLink(struct InputCommander *phead)
{if(phead == NULL){return &voiceControl;}else{voiceControl.next=phead;phead = &voiceControl;return phead;}
}
相关文章:
智能家居(2)---串口通信(语音识别)控制线程封装
封装语音线程(语音通过串口和主控设备进行交流)实现对智能家居中各种灯光的控制 mainPro.c(主函数) #include <stdio.h> #include "controlDevice.h" #include "inputCommand.h" #include <pthread.h>struct Devices …...
MySql主从复制1032错误(Slave_IO_Running: Yes Slave_SQL_Running: No)
MySql主从复制1032错误(Slave_IO_Running: Yes Slave_SQL_Running: No) Slave_IO_Running: Yes Slave_SQL_Running: No报错: Last_SQL_Error: Could not execute Delete_rows event on table hr.test; Can’t find record in ‘test’, Erro…...
毕业论文格式设置总结
毕业论文格式 一般每个学校都有一些自己的论文格式,不过也有一些是没有很详细的。 1、总体格式 论文标题:https://www.cnrencai.com/lunwen/lunwengeshi/870479.html?ivk_sa1024320u论文格式:https://wenku.baidu.com/view/c96a82ea432891…...
7-3 整数四则运算
本题要求编写程序,计算2个正整数的和、差、积、商并输出。题目保证输入和输出全部在整型范围内。 输入格式: 输入在一行中给出2个正整数A和B。 输出格式: 在4行中按照格式“A 运算符 B 结果”顺序输出和、差、积、商。 输入样例: 3 2输出样例: 3 2 5 3 - …...

React 全栈体系(一)
第一章 React入门 一、React简介 1. 是什么? 是一个将数据渲染为HTML视图的开源JavaScript库。 2. 谁开发的? 由Facebook开源 3. 为什么要学? 原生JavaScript操作DOM繁琐,效率低(DOM-API 操作 UI) 使…...

SpringBoot代理访问本地静态资源400 404
SpringBoot代理访问静态资源400 404 背景:pdf文件上传到linux服务器上,使用SpringBoot代理访问问题:访问过程中可能会出现400、404问题 前提:保证有文件,并且文件路径正确 SpringBoot如何配置静态资源代理࿰…...
Java导出数据到Excel
系列文章目录 文章目录 系列文章目录前言一、为什么需要导出数据到Excel?二、使用Java导出数据到Excel的步骤1.添加依赖2.编写导出逻辑3.运行测试总结前言 当今数据处理的场景中,Excel仍然是一个不可或缺的工具,用于存储、分析和共享数据。在Java应用程序中,有时候需要将数…...

IDEA常用设置与maven项目部署
目录 前言 一、Idea是什么 二、Idea的优点 三、Idea的常用设置 主题设置 设置鼠标悬浮提示 忽略大小写提示 自动导包 取消单行显示Tabs 设置字体 配置类文档注释信息模版 设置文件编码 设置自动编译 水平或者垂直显示代码 快捷方式改成eclipse 设置默认浏览器…...

想学好网络技术,这一张纸就够了
大家好,我是老杨。 马上又到一年一度的大学新生入学季,今年更多家长都给孩子们报了计算机相关专业。 要知道啊,这个计算机专业包含的方向贼多,什么网络工程、软件工程、信息安全、物联网工程、传感网技术、通信工程与电子信息之…...

list的使用和模拟实现
目录 1.list的介绍及使用 1.1 list的介绍 1.2 list的使用 1.2.1 list的构造 1.2.2 list iterator的使用 1.2.3 list capacity 1.2.4 list element access 1.2.5 list modifiers 2.为什么使用迭代器? 3.list的模拟实现 3.1完整代码 3.2代码解析 4.list与…...

Kubernetes 部署DolphinScheduler 创建租户失败
创建租户 报错创建租户失败。后台日志如下 源代码跟踪 org.apache.dolphinscheduler.api.service.impl.TenantServiceImpl / if hdfs startup if (PropertyUtils.getResUploadStartupState()) {createTenantDirIfNotExists(tenantCode); }需要将 resource.storage.type 置为…...

uniapp 获取 view 的宽度、高度以及上下左右左边界位置
<view class"cont-box"></view> /* 获取节点信息的对象 */ getElementRect() {const query uni.createSelectorQuery().in(this);query.select(".cont-box").boundingClientRect(res > {console.log(res);console.log(res.height); // 10…...

财务数据分析之现金流量表模板分享
现金流量表是我们常说的财务数据分析三表之一。它可以呈现一个企业的现金流情况,揭示企业经营管理健康状态,但在实际使用中却有总给人一种用不上、用不好的矛盾感。怎么才能把现金流量表做好?不如借鉴下大神的现金流量表模板。 下面介绍的是…...

日常BUG——通过命令行创建vue项目报错
😜作 者:是江迪呀✒️本文关键词:日常BUG、BUG、问题分析☀️每日 一言 :存在错误说明你在进步! 一、问题描述 在使用vue命令行创建一个vue项目时,出现一下的错误: vue create my…...
CSS3 新特性
圆角阴影文字阴影线性渐变变换(transform)背景rgba伪元素:伪类 伪元素区别动画(animate)...
微信记录---推荐系统---23/8/14 小总结
推荐系统---23/8/14 小总结 1. ACM推荐系统专题研讨会2.图神经网络推荐系统3.表1 模型效果对标:MovieLens 1M4.爬虫技术5.TF-IDF算法6.图 2 海量学术大数据推荐系统技术架构7.图 4 CADAL 平台推荐系统框架设计8.企业推荐系统发展概述MLR(Mixed Logistic Regression)DIEN(Deep…...
学习笔记整理-正则表达式-01-认识正则
一、基本认识 1. 什么是正则表达式 正则表达式(regular expression)描述了字符串"构成模式",经常被用于检查字符串是否符合预定的格式要求。 用一个例子快速演示正则表达式基本使用方法:检查某个字符串是否是6位数字 // 要检查的字符串va…...
windows10/11 修改docker镜像存储目录
windows10/11 修改docker镜像存储目录 windows10/11 修改docker镜像存储目录查看docker的状态关闭所有正在运行的实例导出WSL子系统镜像注销现有的wsl重新创建wsl系统 windows10/11 修改docker镜像存储目录 docker默认pull的镜像在c盘,随着镜像的增加,C…...

AI黑马挑战赛,探索研发新趋势丨IDCF
随着AI的出现,获取知识的成本大幅降低,当DevOps与AI相结合时,必将产生全新的化学反应。不断涌现的AI新工具提醒我们,一个全新的研发工作范式正在逐渐形成。而DevOps的核心理念是敏捷协同,作为工程师,如何通…...
关于onload事件
onload事件是在网页中的所有内容(包括图片、样式表、脚本等)都加载完成后触发的事件。它常用于在页面加载完成后执行一些操作,例如初始化页面元素、绑定事件监听器等。 可以通过以下方式来使用onload事件: 在HTML标签中直接添加…...

AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南
点一下关注吧!!!非常感谢!!持续更新!!! 🚀 AI篇持续更新中!(长期更新) 目前2025年06月05日更新到: AI炼丹日志-28 - Aud…...
vscode里如何用git
打开vs终端执行如下: 1 初始化 Git 仓库(如果尚未初始化) git init 2 添加文件到 Git 仓库 git add . 3 使用 git commit 命令来提交你的更改。确保在提交时加上一个有用的消息。 git commit -m "备注信息" 4 …...

NLP学习路线图(二十三):长短期记忆网络(LSTM)
在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...

自然语言处理——循环神经网络
自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元(GRU)长短期记忆神经网络(LSTM)…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)
上一章用到了V2 的概念,其实 Fiori当中还有 V4,咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务),代理中间件(ui5-middleware-simpleproxy)-CSDN博客…...
代码随想录刷题day30
1、零钱兑换II 给你一个整数数组 coins 表示不同面额的硬币,另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额,返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位带…...

基于Java+MySQL实现(GUI)客户管理系统
客户资料管理系统的设计与实现 第一章 需求分析 1.1 需求总体介绍 本项目为了方便维护客户信息为了方便维护客户信息,对客户进行统一管理,可以把所有客户信息录入系统,进行维护和统计功能。可通过文件的方式保存相关录入数据,对…...

LLMs 系列实操科普(1)
写在前面: 本期内容我们继续 Andrej Karpathy 的《How I use LLMs》讲座内容,原视频时长 ~130 分钟,以实操演示主流的一些 LLMs 的使用,由于涉及到实操,实际上并不适合以文字整理,但还是决定尽量整理一份笔…...

uniapp 小程序 学习(一)
利用Hbuilder 创建项目 运行到内置浏览器看效果 下载微信小程序 安装到Hbuilder 下载地址 :开发者工具默认安装 设置服务端口号 在Hbuilder中设置微信小程序 配置 找到运行设置,将微信开发者工具放入到Hbuilder中, 打开后出现 如下 bug 解…...
go 里面的指针
指针 在 Go 中,指针(pointer)是一个变量的内存地址,就像 C 语言那样: a : 10 p : &a // p 是一个指向 a 的指针 fmt.Println(*p) // 输出 10,通过指针解引用• &a 表示获取变量 a 的地址 p 表示…...