当前位置: 首页 > news >正文

智能家居(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)---串口通信(语音识别)控制线程封装

封装语音线程&#xff08;语音通过串口和主控设备进行交流&#xff09;实现对智能家居中各种灯光的控制 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错误&#xff08;Slave_IO_Running: Yes Slave_SQL_Running: No&#xff09; Slave_IO_Running: Yes Slave_SQL_Running: No报错&#xff1a; Last_SQL_Error: Could not execute Delete_rows event on table hr.test; Can’t find record in ‘test’, Erro…...

毕业论文格式设置总结

毕业论文格式 一般每个学校都有一些自己的论文格式&#xff0c;不过也有一些是没有很详细的。 1、总体格式 论文标题&#xff1a;https://www.cnrencai.com/lunwen/lunwengeshi/870479.html?ivk_sa1024320u论文格式&#xff1a;https://wenku.baidu.com/view/c96a82ea432891…...

7-3 整数四则运算

本题要求编写程序&#xff0c;计算2个正整数的和、差、积、商并输出。题目保证输入和输出全部在整型范围内。 输入格式: 输入在一行中给出2个正整数A和B。 输出格式: 在4行中按照格式“A 运算符 B 结果”顺序输出和、差、积、商。 输入样例: 3 2输出样例: 3 2 5 3 - …...

React 全栈体系(一)

第一章 React入门 一、React简介 1. 是什么&#xff1f; 是一个将数据渲染为HTML视图的开源JavaScript库。 2. 谁开发的&#xff1f; 由Facebook开源 3. 为什么要学&#xff1f; 原生JavaScript操作DOM繁琐&#xff0c;效率低&#xff08;DOM-API 操作 UI&#xff09; 使…...

SpringBoot代理访问本地静态资源400 404

SpringBoot代理访问静态资源400 404 背景&#xff1a;pdf文件上传到linux服务器上&#xff0c;使用SpringBoot代理访问问题&#xff1a;访问过程中可能会出现400、404问题 前提&#xff1a;保证有文件&#xff0c;并且文件路径正确 SpringBoot如何配置静态资源代理&#xff0…...

Java导出数据到Excel

系列文章目录 文章目录 系列文章目录前言一、为什么需要导出数据到Excel?二、使用Java导出数据到Excel的步骤1.添加依赖2.编写导出逻辑3.运行测试总结前言 当今数据处理的场景中,Excel仍然是一个不可或缺的工具,用于存储、分析和共享数据。在Java应用程序中,有时候需要将数…...

IDEA常用设置与maven项目部署

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

想学好网络技术,这一张纸就够了

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

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.为什么使用迭代器&#xff1f; 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…...

财务数据分析之现金流量表模板分享

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

日常BUG——通过命令行创建vue项目报错

&#x1f61c;作 者&#xff1a;是江迪呀✒️本文关键词&#xff1a;日常BUG、BUG、问题分析☀️每日 一言 &#xff1a;存在错误说明你在进步&#xff01; 一、问题描述 在使用vue命令行创建一个vue项目时&#xff0c;出现一下的错误&#xff1a; vue create my…...

CSS3 新特性

圆角阴影文字阴影线性渐变变换&#xff08;transform&#xff09;背景rgba伪元素&#xff1a;伪类 伪元素区别动画&#xff08;animate&#xff09;...

微信记录---推荐系统---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)描述了字符串"构成模式"&#xff0c;经常被用于检查字符串是否符合预定的格式要求。 用一个例子快速演示正则表达式基本使用方法&#xff1a;检查某个字符串是否是6位数字 // 要检查的字符串va…...

windows10/11 修改docker镜像存储目录

windows10/11 修改docker镜像存储目录 windows10/11 修改docker镜像存储目录查看docker的状态关闭所有正在运行的实例导出WSL子系统镜像注销现有的wsl重新创建wsl系统 windows10/11 修改docker镜像存储目录 docker默认pull的镜像在c盘&#xff0c;随着镜像的增加&#xff0c;C…...

AI黑马挑战赛,探索研发新趋势丨IDCF

随着AI的出现&#xff0c;获取知识的成本大幅降低&#xff0c;当DevOps与AI相结合时&#xff0c;必将产生全新的化学反应。不断涌现的AI新工具提醒我们&#xff0c;一个全新的研发工作范式正在逐渐形成。而DevOps的核心理念是敏捷协同&#xff0c;作为工程师&#xff0c;如何通…...

关于onload事件

onload事件是在网页中的所有内容&#xff08;包括图片、样式表、脚本等&#xff09;都加载完成后触发的事件。它常用于在页面加载完成后执行一些操作&#xff0c;例如初始化页面元素、绑定事件监听器等。 可以通过以下方式来使用onload事件&#xff1a; 在HTML标签中直接添加…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

脑机新手指南(八):OpenBCI_GUI:从环境搭建到数据可视化(下)

一、数据处理与分析实战 &#xff08;一&#xff09;实时滤波与参数调整 基础滤波操作 60Hz 工频滤波&#xff1a;勾选界面右侧 “60Hz” 复选框&#xff0c;可有效抑制电网干扰&#xff08;适用于北美地区&#xff0c;欧洲用户可调整为 50Hz&#xff09;。 平滑处理&…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

使用分级同态加密防御梯度泄漏

抽象 联邦学习 &#xff08;FL&#xff09; 支持跨分布式客户端进行协作模型训练&#xff0c;而无需共享原始数据&#xff0c;这使其成为在互联和自动驾驶汽车 &#xff08;CAV&#xff09; 等领域保护隐私的机器学习的一种很有前途的方法。然而&#xff0c;最近的研究表明&…...

算法岗面试经验分享-大模型篇

文章目录 A 基础语言模型A.1 TransformerA.2 Bert B 大语言模型结构B.1 GPTB.2 LLamaB.3 ChatGLMB.4 Qwen C 大语言模型微调C.1 Fine-tuningC.2 Adapter-tuningC.3 Prefix-tuningC.4 P-tuningC.5 LoRA A 基础语言模型 A.1 Transformer &#xff08;1&#xff09;资源 论文&a…...

NPOI操作EXCEL文件 ——CAD C# 二次开发

缺点:dll.版本容易加载错误。CAD加载插件时&#xff0c;没有加载所有类库。插件运行过程中用到某个类库&#xff0c;会从CAD的安装目录找&#xff0c;找不到就报错了。 【方案2】让CAD在加载过程中把类库加载到内存 【方案3】是发现缺少了哪个库&#xff0c;就用插件程序加载进…...

安卓基础(Java 和 Gradle 版本)

1. 设置项目的 JDK 版本 方法1&#xff1a;通过 Project Structure File → Project Structure... (或按 CtrlAltShiftS) 左侧选择 SDK Location 在 Gradle Settings 部分&#xff0c;设置 Gradle JDK 方法2&#xff1a;通过 Settings File → Settings... (或 CtrlAltS)…...