智能家居(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标签中直接添加…...
XML Group端口详解
在XML数据映射过程中,经常需要对数据进行分组聚合操作。例如,当处理包含多个物料明细的XML文件时,可能需要将相同物料号的明细归为一组,或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码,增加了开…...
大话软工笔记—需求分析概述
需求分析,就是要对需求调研收集到的资料信息逐个地进行拆分、研究,从大量的不确定“需求”中确定出哪些需求最终要转换为确定的“功能需求”。 需求分析的作用非常重要,后续设计的依据主要来自于需求分析的成果,包括: 项目的目的…...
SciencePlots——绘制论文中的图片
文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了:一行…...
练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...
oracle与MySQL数据库之间数据同步的技术要点
Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异,它们的数据同步要求既要保持数据的准确性和一致性,又要处理好性能问题。以下是一些主要的技术要点: 数据结构差异 数据类型差异ÿ…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...
Java 加密常用的各种算法及其选择
在数字化时代,数据安全至关重要,Java 作为广泛应用的编程语言,提供了丰富的加密算法来保障数据的保密性、完整性和真实性。了解这些常用加密算法及其适用场景,有助于开发者在不同的业务需求中做出正确的选择。 一、对称加密算法…...
多种风格导航菜单 HTML 实现(附源码)
下面我将为您展示 6 种不同风格的导航菜单实现,每种都包含完整 HTML、CSS 和 JavaScript 代码。 1. 简约水平导航栏 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport&qu…...
Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)
目录 一、👋🏻前言 二、😈sinx波动的基本原理 三、😈波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、🌊波动优化…...
Linux --进程控制
本文从以下五个方面来初步认识进程控制: 目录 进程创建 进程终止 进程等待 进程替换 模拟实现一个微型shell 进程创建 在Linux系统中我们可以在一个进程使用系统调用fork()来创建子进程,创建出来的进程就是子进程,原来的进程为父进程。…...
