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

智能家居(1)---工厂模式实现灯光控制(继电器组)以及火灾报警模组的封装

采用工厂模式以面向对象的方式来封装各种设备模块,方便整合项目以及后期的维护和扩展

mainPro.c(主函数)

#include <stdio.h>
#include "controlDevice.h"struct Devices        *pdeviceHead  = NULL;         //设备工厂链表头struct Devices* findDeviceByName(struct Devices *phead,char *name)          //在设备链表中查找设备
{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;   }}int main()
{if(wiringPiSetup()<0){//初始化wiringPi外设库printf("wiringPi Init failed\n");return -1;}//设备控制工厂初始化pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);        //将卫生灯加入设备链表pdeviceHead = addbedroomLightToDeviceLink(pdeviceHead);         //将卧室灯加入设备链表pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);      //将餐厅灯加入设备链表pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);      //将客厅灯加入设备链表pdeviceHead = addFireToDeviceLink(pdeviceHead);                 //将火灾检测加入设备链表pdeviceHead = addBeepToDeviceLink(pdeviceHead);                 //将蜂鸣器加入设备链表

controlDevice.h( 设备类)

#include <wiringPi.h>
#include <stddef.h>struct Devices
{char deviceName[128];                       //设备名字int status;                                 //状态int pinNum;                                 //引脚int (*deviceInit)(int pinNum);              //设备初始化函数指针,后面类似int (*open)(int pinNum);                    //打开设备int (*close)(int pinNum);                   //关闭设备int (*readStatus)(int pinNum);              //读取引脚状态int (*changeStatus)(int status);struct Devices *next;                       //方便链表使用的结构体指针
};struct Devices *addBathroomLightToDeviceLink(struct Devices *phead);        //卫生间灯加入设备链表声明,以下类似
struct Devices *addbedroomLightToDeviceLink(struct Devices *phead);         //卧室灯
struct Devices *addRestaurantLightToDeviceLink(struct Devices *phead);      //餐厅灯
struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead);      //客厅灯
struct Devices *addFireToDeviceLink(struct Devices *phead);                 //火灾检测器
struct Devices *addBeepToDeviceLink(struct Devices *phead);                 //蜂鸣器

bathroomLight.c(浴室灯)

#include "controlDevice.h"int bathroomLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);}int bathroomLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int bathroomLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);}int bathroomLightChangeStatus()
{//暂时不用,后面用着在完善}struct Devices bathroomLight = {.deviceName = "bathroomLight",.pinNum = 26,.deviceInit = bathroomLightInit,.open  = bathroomLightOpen,.close = bathroomLightClose,.changeStatus = bathroomLightChangeStatus,};struct Devices *addBathroomLightToDeviceLink(struct Devices *phead)		//将浴室灯加入设备链表的函数
{if(phead == NULL){return &bathroomLight;}else{bathroomLight.next=phead;phead = &bathroomLight;return phead;}}

livingroomLight.c(客厅灯)

#include "controlDevice.h"int livingroomLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);//设置引脚为输出模式digitalWrite(pinNum,HIGH);}int livingroomLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int livingroomLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);}int livingroomLightChangeStatus()
{//暂时不用,后面用着在完善}struct Devices livingroomLight = {.deviceName = "livingroomLight",.pinNum = 29,.deviceInit = livingroomLightInit,.open  = livingroomLightOpen,.close = livingroomLightClose,.changeStatus = livingroomLightChangeStatus,};struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &livingroomLight;}else{livingroomLight.next=phead;phead = &livingroomLight;return phead;}}

restaurantLight.c(餐厅灯)

#include "controlDevice.h"int restaurantLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);//设置引脚为输出模式digitalWrite(pinNum,HIGH);}int restaurantLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int restaurantLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);}int restaurantLightChangeStatus()
{//暂时不用,后面用着在完善}struct Devices restaurantLight = {.deviceName = "restaurantLight",.pinNum = 28,.deviceInit = restaurantLightInit,.open  = restaurantLightOpen,.close = restaurantLightClose,.changeStatus = restaurantLightChangeStatus,};struct Devices *addRestaurantLightToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &restaurantLight;}else{restaurantLight.next=phead;phead = &restaurantLight;return phead;}}

beedroomLight(卧室灯)

#include "controlDevice.h"int bedroomLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);//设置引脚为输出模式digitalWrite(pinNum,HIGH);}int bedroomLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int bedroomLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);}int bedroomLightChangeStatus()
{//暂时不用,后面用着在完善}struct Devices bedroomLight = {.deviceName = "bedroomLight",.pinNum = 27,.deviceInit = bedroomLightInit,.open  = bedroomLightOpen,.close = bedroomLightClose,.changeStatus = bedroomLightChangeStatus,};struct Devices *addbedroomLightToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &bedroomLight;}else{bedroomLight.next=phead;phead = &bedroomLight;return phead;}}

fire.c(火灾检测)

#include "controlDevice.h"int fireInit(int pinNum)
{pinMode(pinNum,INPUT);//设置引脚为输出模式}int fireStatusRead(int pinNum)
{return digitalRead(pinNum);}int fireChangeStatus()
{//暂时不用,后面用着在完善}struct Devices fire = {.deviceName = "fire",.pinNum = 25,.deviceInit = fireInit,.readStatus = fireStatusRead,.changeStatus = fireChangeStatus,};struct Devices *addFireToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &fire;}else{fire.next=phead;phead = &fire;return phead;}}

beep.c(和火灾检测配合实现火灾报警的蜂鸣器)

#include "controlDevice.h"int beepInit(int pinNum)
{pinMode(pinNum,OUTPUT);//设置引脚为输出模式digitalWrite(pinNum,HIGH);}int beepOpen(int pinNum)
{digitalWrite(pinNum,LOW);}int beepClose(int pinNum)
{digitalWrite(pinNum,HIGH);}int beepChangeStatus()
{//暂时不用,后面用着在完善}struct Devices beep = {.deviceName = "beep",.pinNum = 24,.deviceInit = beepInit,.open = beepOpen,.close = beepClose,.changeStatus = beepChangeStatus,};struct Devices *addBeepToDeviceLink(struct Devices *phead)
{if(phead == NULL){return &beep;}else{beep.next=phead;phead = &beep;return phead;}}

相关文章:

智能家居(1)---工厂模式实现灯光控制(继电器组)以及火灾报警模组的封装

采用工厂模式以面向对象的方式来封装各种设备模块&#xff0c;方便整合项目以及后期的维护和扩展 mainPro.c&#xff08;主函数&#xff09; #include <stdio.h> #include "controlDevice.h"struct Devices *pdeviceHead NULL; //设备工厂链…...

kubernetes的存储卷使用

目录 一、为什么使用存储卷 二、emptyDir存储卷 1.概念 2.创建Pod emptyDir 3. 验证emptyDir存储卷 三、hostPath存储卷 1.概念 2.创建Pod hostPath 3.验证hostPath存储卷 三、nfs共享存储卷 1.概念 2.安装nfs&#xff0c;配置nfs服务 3.创建Pod 4.验证nfs存储卷 一、…...

centos 之安装 openssl 1.1.1报错

源码make时报错&#xff0c;可能是系统的perl的版本太低问题。 [rootlocalhost ~]# cpan -a | grep Test::More Test::More 0.92 1.302171 EXODIST/Test-Simple-1.302171.tar.gz [rootlocalhost ~]# cpan -a | grep Text::Template [rootlocalhost ~]# …...

matlab使用教程(16)—图论中图的定义与修改

1.修改现有图的节点和边 此示例演示如何使用 addedge 、 rmedge 、 addnode 、 rmnode 、 findedge 、 findnode 及 subgraph 函数访问和修改 graph 或 digraph 对象中的节点和/或边。 1.1 添加节点 创建一个包含四个节点和四条边的图。s 和 t 中的对应元素用于指定每条…...

【C++面向对象】--- 继承 的奥秘(下篇)

个人主页&#xff1a;平行线也会相交&#x1f4aa; 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 平行线也会相交 原创 收录于专栏【C之路】&#x1f48c; 本专栏旨在记录C的学习路线&#xff0c;望对大家有所帮助&#x1f647;‍ 希望我们一起努力、成长&…...

Android 面试笔记整理-Binder机制

作者&#xff1a;浪人笔记 面试可能会问到的问题 从IPC的方式问到Binder的优势为什么zygote跟其他服务进程的通讯不使用BinderBinder线程池和Binder机制 等等这些问题都是基于你对Binder的理解还有对其他IPC通讯的理解 IPC方式有多少种 传统的IPC方式有Socket、共享内存、管道…...

编程小白的自学笔记十三(python办公自动化读写文件)

系列文章目录 编程小白的自学笔记十二&#xff08;python爬虫入门四Selenium的使用实例二&#xff09; 编程小白的自学笔记十一&#xff08;python爬虫入门三Selenium的使用实例详解&#xff09; 编程小白的自学笔记十&#xff08;python爬虫入门二实例代码详解&#xff09;…...

【Mariadb高可用MHA】

目录 一、概述 1.概念 2.组成 3.特点 4.工作原理 二、案例介绍 1.192.168.42.3 2.192.168.42.4 3.192.168.42.5 4.192.168.42.6 三、实际构建MHA 1.ssh免密登录 1.1 所有节点配置hosts 1.2 192.168.42.3 1.3 192.168.42.4 1.4 192.168.42.5 1.5 192.168.42.6 …...

网络五层协议

应用层&#xff08;http,https&#xff09;&#xff0c;传输层(udp,tcp)&#xff0c;网络层(ip)&#xff0c;数据链路层&#xff0c;物理层 什么是http?http 与https 的区别_日晞的博客-CSDN博客 TCP 与UDP 区别_互联网业务udp小包传输_日晞的博客-CSDN博客...

零售行业供应链管理核心KPI指标(一) – 能力、速度、效率和成本

有关零售行业供应链管理KPI指标的综合性分享&#xff0c;涉及到供应链能力、速度、效率和成本总共九大指标&#xff0c;是一个大框架&#xff0c;比较核心也比较综合。 衡量消费品零售企业供应链管理效率和水平的核心KPI通常有哪些&#xff1f; 图片来源-派可数据&#xff08;…...

MySQL面试题二

1、关系型和非关系型数据库的区别&#xff1f; 关系型数据库的优点 容易理解&#xff0c;因为它采用了关系模型来组织数据。 可以保持数据的一致性。 数据更新的开销比较小。 支持复杂查询&#xff08;带 where 子句的查询&#xff09; 非关系型数据库&#xff08;NOSQL&#x…...

码银送书第五期《互联网广告系统:架构、算法与智能化》

广告平台的建设和完善是一项长期工程。例如&#xff0c;谷歌早于2003年通过收购Applied Semantics开展Google AdSense 项目&#xff0c;而直到20年后的今天&#xff0c;谷歌展示广告平台仍在持续创新和提升。广告平台是负有营收责任的复杂在线平台&#xff0c;对其进行任何改动…...

分布式理论

CAP和BASE CAP C一致性&#xff08;Consistency&#xff09; 在分布式环境下&#xff0c;一致性是指数据在多个副本之间能否保持一致性的特征。在一致性的需求下&#xff0c;当一个系统在数据一致的状态下执行更新操作后&#xff0c;应该保证系统的数据仍然处于一致性的状态…...

Excel设置某列或者某行不某行不可以编辑,只读属性

设置单元格只读的三种方式: 1、通过单元格只读按钮&#xff0c;设置为只为 设置行或者列的只读属性&#xff0c;可以设置整行或者整列只读 2、设置单元格编辑控件为标签控件(标签控件不可编辑) 3、通过锁定行&#xff0c;锁定行的修改。锁定的行与只读行的区别在于锁定的行不…...

vue elementui v-for 循环el-table-column 第一列数据变到最后一个

这个动态渲染table表格时发现el-table-column 第一列数据变到最后一个 序号被排到后面 代码 修改后 <el-table:data"tableData"tooltip-effect"dark"style"width: 100%"height"500"><template v-for"(item, index) i…...

宝塔部署阿里云盘webdav

安装Docker 我的系统是CentOS8&#xff0c;如果直接安装会出错&#xff0c;可以看这篇文章&#xff1a;Failed to download metadata for repo ‘appstream‘ docker 国内镜像&#xff1a; http://hub-mirror.c.163.com/下载镜像 宝塔安装docker管理器&#xff0c;然后搜索…...

Ceph分布式存储系统优化分析

Ceph支持多种存储访问接口&#xff0c;现有的多种性能测试工具都可用于Ceph的性能测试&#xff0c;如测试块接口性能的fio&#xff0c;iometer等&#xff1b;测试CephFS接口的filebench&#xff0c;fio等;测试对象接口的cosbench等。Ceph有专用的基准测试集CBT&#xff0c;其包…...

supOS APP开发者课程练习册创建服务(命名:getPropertiesHistory)

创建服务&#xff08;命名&#xff1a;getPropertiesHistory&#xff09;,调用getPropertiesHistory()服务&#xff0c;获取“催化裂化一车间”对象的“重质馏分油_进”最近5分钟内的历史值&#xff0c;每一分钟取一个值&#xff0c;开始时间和结束时间需要调用时间格式化功能集…...

认识excel篇3之数据的有效性(数据验证)

数据有效性不仅能够对单元格的输入数据进行条件限制&#xff0c;还可以在单元格中创建下拉列表菜单方便用户选择输入。如果没有做数据验证&#xff0c;单元格内默认可以输入任意类型的数据。数据验证就是限制单元格输入数据&#xff08;必须输入符合要求的才能输入&#xff09;…...

adb 命令行执行单元测试

文章目录 1、配置 adb 环境变量2、adb 执行测试3、官方文档解读 adb 使用&#xff08;1&#xff09;第一条执行测试的adb命令&#xff08;2&#xff09;am instrument 参数&#xff08;3&#xff09;-e 参数 的 key-value键值对&#xff08;4&#xff09;用法用例 4、存在问题 …...

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

docker详细操作--未完待续

docker介绍 docker官网: Docker&#xff1a;加速容器应用程序开发 harbor官网&#xff1a;Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台&#xff0c;用于将应用程序及其依赖项&#xff08;如库、运行时环…...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》

引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...

关于nvm与node.js

1 安装nvm 安装过程中手动修改 nvm的安装路径&#xff0c; 以及修改 通过nvm安装node后正在使用的node的存放目录【这句话可能难以理解&#xff0c;但接着往下看你就了然了】 2 修改nvm中settings.txt文件配置 nvm安装成功后&#xff0c;通常在该文件中会出现以下配置&…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

汽车生产虚拟实训中的技能提升与生产优化​

在制造业蓬勃发展的大背景下&#xff0c;虚拟教学实训宛如一颗璀璨的新星&#xff0c;正发挥着不可或缺且日益凸显的关键作用&#xff0c;源源不断地为企业的稳健前行与创新发展注入磅礴强大的动力。就以汽车制造企业这一极具代表性的行业主体为例&#xff0c;汽车生产线上各类…...

2025盘古石杯决赛【手机取证】

前言 第三届盘古石杯国际电子数据取证大赛决赛 最后一题没有解出来&#xff0c;实在找不到&#xff0c;希望有大佬教一下我。 还有就会议时间&#xff0c;我感觉不是图片时间&#xff0c;因为在电脑看到是其他时间用老会议系统开的会。 手机取证 1、分析鸿蒙手机检材&#x…...

CRMEB 中 PHP 短信扩展开发:涵盖一号通、阿里云、腾讯云、创蓝

目前已有一号通短信、阿里云短信、腾讯云短信扩展 扩展入口文件 文件目录 crmeb\services\sms\Sms.php 默认驱动类型为&#xff1a;一号通 namespace crmeb\services\sms;use crmeb\basic\BaseManager; use crmeb\services\AccessTokenServeService; use crmeb\services\sms\…...

C# 表达式和运算符(求值顺序)

求值顺序 表达式可以由许多嵌套的子表达式构成。子表达式的求值顺序可以使表达式的最终值发生 变化。 例如&#xff0c;已知表达式3*52&#xff0c;依照子表达式的求值顺序&#xff0c;有两种可能的结果&#xff0c;如图9-3所示。 如果乘法先执行&#xff0c;结果是17。如果5…...

【C++】纯虚函数类外可以写实现吗?

1. 答案 先说答案&#xff0c;可以。 2.代码测试 .h头文件 #include <iostream> #include <string>// 抽象基类 class AbstractBase { public:AbstractBase() default;virtual ~AbstractBase() default; // 默认析构函数public:virtual int PureVirtualFunct…...