当前位置: 首页 > 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、存在问题 …...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

C++实现分布式网络通信框架RPC(3)--rpc调用端

目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中&#xff0c;我们已经大致实现了rpc服务端的各项功能代…...

【人工智能】神经网络的优化器optimizer(二):Adagrad自适应学习率优化器

一.自适应梯度算法Adagrad概述 Adagrad&#xff08;Adaptive Gradient Algorithm&#xff09;是一种自适应学习率的优化算法&#xff0c;由Duchi等人在2011年提出。其核心思想是针对不同参数自动调整学习率&#xff0c;适合处理稀疏数据和不同参数梯度差异较大的场景。Adagrad通…...

剑指offer20_链表中环的入口节点

链表中环的入口节点 给定一个链表&#xff0c;若其中包含环&#xff0c;则输出环的入口节点。 若其中不包含环&#xff0c;则输出null。 数据范围 节点 val 值取值范围 [ 1 , 1000 ] [1,1000] [1,1000]。 节点 val 值各不相同。 链表长度 [ 0 , 500 ] [0,500] [0,500]。 …...

【开发技术】.Net使用FFmpeg视频特定帧上绘制内容

目录 一、目的 二、解决方案 2.1 什么是FFmpeg 2.2 FFmpeg主要功能 2.3 使用Xabe.FFmpeg调用FFmpeg功能 2.4 使用 FFmpeg 的 drawbox 滤镜来绘制 ROI 三、总结 一、目的 当前市场上有很多目标检测智能识别的相关算法&#xff0c;当前调用一个医疗行业的AI识别算法后返回…...

HDFS分布式存储 zookeeper

hadoop介绍 狭义上hadoop是指apache的一款开源软件 用java语言实现开源框架&#xff0c;允许使用简单的变成模型跨计算机对大型集群进行分布式处理&#xff08;1.海量的数据存储 2.海量数据的计算&#xff09;Hadoop核心组件 hdfs&#xff08;分布式文件存储系统&#xff09;&a…...

Caliper 配置文件解析:fisco-bcos.json

config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

MFE(微前端) Module Federation:Webpack.config.js文件中每个属性的含义解释

以Module Federation 插件详为例&#xff0c;Webpack.config.js它可能的配置和含义如下&#xff1a; 前言 Module Federation 的Webpack.config.js核心配置包括&#xff1a; name filename&#xff08;定义应用标识&#xff09; remotes&#xff08;引用远程模块&#xff0…...

华为OD最新机试真题-数组组成的最小数字-OD统一考试(B卷)

题目描述 给定一个整型数组,请从该数组中选择3个元素 组成最小数字并输出 (如果数组长度小于3,则选择数组中所有元素来组成最小数字)。 输入描述 行用半角逗号分割的字符串记录的整型数组,0<数组长度<= 100,0<整数的取值范围<= 10000。 输出描述 由3个元素组成…...

6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础

第三周 Day 3 &#x1f3af; 今日目标 理解类&#xff08;class&#xff09;和对象&#xff08;object&#xff09;的关系学会定义类的属性、方法和构造函数&#xff08;init&#xff09;掌握对象的创建与使用初识封装、继承和多态的基本概念&#xff08;预告&#xff09; &a…...