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

Agent 时代的开发者技能树重构指南

1. 标题选项 核心关键词:AI Agent、开发者转型、技能树重构、职业跃迁 《Agent 时代降临:普通开发者的技能树重构全指南,从CRUD Boy到AI应用工程师的跃迁之路》 《别再只会写CRUD了!AI Agent浪潮下,你必须掌握的新技能树体系》 《从软件开发到AI Agent开发:2024年开发者技…...

CNN 卷积神经网络

1. 图像基本概念 2. CNN概述 3. 卷积层 3.1 卷积计算 卷积计算 本质上是 卷积核 和 输入数据的局部区域 间做点积&#xff1b; 计算规则&#xff1a;从左到右&#xff0c;从上到下&#xff1b; 3.1.1 Padding 填充 - 填充的像素个数 通过上面的卷积计算过程&#xff0c;最终的…...

我见过最聪明的技术人,都在偷偷培养这3种“非技术能力”

在软件测试行业摸爬滚打这些年&#xff0c;我见过太多天赋异禀的技术从业者&#xff1a;有人能一夜吃透新的自动化测试框架&#xff0c;有人能对着流量日志半小时定位出隐藏半年的内存泄漏问题&#xff0c;有人能把性能测试指标优化到远超行业标准。可几年过去&#xff0c;真正…...

【204期】异地组网一键联机工具

想和朋友异地联机打单机游戏&#xff0c;结果发现没有公网IP连不上&#xff1f;或者居家办公想访问公司局域网里的文件&#xff0c;搞了半天搞不定&#xff1f;今天聊的这类异地组网、内网穿透工具&#xff0c;就是专门解决这些问题的。它能把一个个单独的局域网连接起来&#…...

PddConsumptionModel.java

package pdd;import java.util.ArrayList; import java.util.List; import java.util.Random;/*** 某多多的商业模式&#xff0c;砍价格算法模拟下哈* * * author ZengWenFeng* email 117791303QQ.com* mobile 13805029595* date 2023.11.17*/ public class PddConsumptionMode…...

告别键盘连击烦恼:Keyboard Chatter Blocker终极使用指南

告别键盘连击烦恼&#xff1a;Keyboard Chatter Blocker终极使用指南 【免费下载链接】KeyboardChatterBlocker A handy quick tool for blocking mechanical keyboard chatter. 项目地址: https://gitcode.com/gh_mirrors/ke/KeyboardChatterBlocker 你是否经常在打字时…...

Go语言模板引擎与前端渲染实战

Go语言模板引擎与前端渲染实战 引言 模板引擎是Web开发中连接后端数据与前端展示的关键组件。Go语言标准库提供了强大的模板引擎&#xff0c;本文将深入探讨其使用方法和最佳实践。 一、Go模板引擎基础 1.1 text/template与html/template // text/template - 纯文本模板 import…...

如何快速掌握Ender-3 3D打印机:新手必看的完整配置指南

如何快速掌握Ender-3 3D打印机&#xff1a;新手必看的完整配置指南 【免费下载链接】Ender-3 The Creality3D Ender-3, a fully Open Source 3D printer perfect for new users on a budget. 项目地址: https://gitcode.com/gh_mirrors/en/Ender-3 Ender-3 3D打印机是一…...

Claude Code 终端命令完整指南

引言最初是为了方便我个人学习使用Claude Code才去网络上收集各种终端命令&#xff0c;但想到可能有人同样需要知道这些命令&#xff0c;便打算将其整理发到CSDN上&#xff0c;希望能帮到大家。 有点标题党的是本文并不是真的完整指南&#xff0c;毕竟完整的命令太多了&#xf…...

李力/张明亮/周雍进等合作Nat Com | 山梨酸的高效异源生物合成

近日&#xff0c;福建师范大学李力教授团队与中国科学院大连化学物理研究所周雍进合作在天然产物生物合成与合成生物学领域取得重要突破&#xff0c;相关研究成果以“Toward sustainable food preservatives: high-level production of sorbic acid in engineered Saccharomyce…...