【智能家居】一、工厂模式实现继电器灯控制

用户手册对应的I/O
工厂模式实现继电器灯控制
代码段

  • controlDevice.h(设备设备)
  • main.c(主函数)
  • bathroomLight.c(浴室灯)
  • bedroomLight.c(卧室灯)
  • restaurantLight.c(餐厅灯)
  • livingroomLight.c(客厅灯)
  • 编译
  • 运行结果

用户手册对应的I/O在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

工厂模式实现继电器灯控制

在这里插入图片描述

在这里插入图片描述

代码段

controlDevice.h(设备类)

#include <wiringPi.h>					//wiringPi库
#include <stdio.h>
#include <stdlib.h>
 
struct Devices                          //设备类
{
    char deviceName[128];               //设备名
    int status;                         //状态
    int pinNum;							//引脚号
 
    int (*Init)(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);		//“浴室灯”加入设备链表函数声明 2
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead);	        //“卧室灯”加入设备链表函数声明 8
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);		//“餐厅灯”加入设备链表函数声明 13
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead);		//“客厅灯”加入设备链表函数声明 16

main.c(主函数)

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "controlDevice.h"

// 按名称查找设备
struct Devices *findDeviceByName(char *name, struct Devices *phead)
{
    struct Devices *tmp =phead;

    if (phead == NULL) {
        return NULL;
    }
	else {
        while (tmp != NULL) {
            if (strcmp(tmp->deviceName,name)==0) {
                return tmp;
            }
            tmp = tmp->next;
        }
        return NULL;
    }
}

int main()
{
	char name[128];
	struct Devices *tmp = NULL;

	// 初始化wiringPi库
	if (wiringPiSetup() == -1) {
		fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
		return 1;
	}

	// 定义初始设备链表头
	struct Devices *pdeviceHead = NULL;
	// “浴室灯”加入设备链表
	pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
	// “卧室灯”加入设备链表
	pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
	// “餐厅灯”加入设备链表
	pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
	// “客厅灯”加入设备链表
	pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);

	// 无限循环,接受用户输入
	while (1)
	{
		printf("Input:\n");
		scanf("%s", name);
		tmp = findDeviceByName(name, pdeviceHead);

		// 如果找到设备
		if (tmp != NULL) {
			tmp->Init(tmp->pinNum); // 先初始化
			tmp->open(tmp->pinNum); // 打开设备
		}
	}
	return 0;
}

bathroomLight.c(浴室灯)

#include "controlDevice.h"			//自定义设备类的文件
 
int bathroomLightInit(int pinNum)           //C语言必须要传参,JAVA不用,可直接访问变量的值
{
	pinMode(pinNum,OUTPUT);					//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int bathroomLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				//引脚置低电平,闭合继电器
}
 
int bathroomLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int bathroomLightStatus(int status)
{
	
}
 
struct Devices bathroomLight = {			//定义浴室灯(对象)
	.deviceName = "bathroomLight",			//名字
	.pinNum = 2,							//香橙派 2号(wPi)引脚
	.Init = bathroomLightInit,				//指定初始化函数
	.open = bathroomLightOpen,				//指定“打开灯”函数
	.close = bathroomLightClose,			//指定“关闭灯”函数
    .changeStatus = bathroomLightStatus
};
 
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead)		//浴室灯(对象)加入设备链表函数
{
	if (phead == NULL) {
		return &bathroomLight;
	}
	else {
		bathroomLight.next = phead;  //以前的头变成.next
		phead = &bathroomLight;      //更新头
		return phead;
	}
}

bedroomLight.c(卧室灯)

#include "controlDevice.h"
 
int bedroomLightInit(int pinNum)            //C语言必须要传参,JAVA不用,可直接访问变量的值
{
	pinMode(pinNum,OUTPUT);					//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int bedroomLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				//引脚置低电平,闭合继电器
}
 
int bedroomLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int bedroomLightStatus(int status)
{
	
}
 
struct Devices bedroomLight = {			//定义卧室灯(对象)
	.deviceName = "bedroomLight",		//名字
	.pinNum = 8,						//香橙派 8号(wPi)引脚
	.Init = bedroomLightInit,			//指定初始化函数
	.open = bedroomLightOpen,			//指定“打开灯”函数
	.close = bedroomLightClose,			//指定“关闭灯”函数
    .changeStatus = bedroomLightStatus
};
 
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead)		//卧室灯(对象)加入设备链表函数
{
	if (phead == NULL) {
		return &bedroomLight;
	}
	else {
		bedroomLight.next = phead;  //以前的头变成.next
		phead = &bedroomLight;      //更新头
		return phead;
	}
}

restaurantLight.c(餐厅灯)

#include "controlDevice.h"			//自定义设备类的文件
 
int restaurantLightInit(int pinNum)         //C语言必须要传参,JAVA不用,可直接访问变量的值
{
	pinMode(pinNum,OUTPUT);					//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int restaurantLightOpen(int pinNum)
{
	digitalWrite(pinNum,LOW);				//引脚置低电平,闭合继电器
}
 
int restaurantLightClose(int pinNum)
{
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}
 
int restaurantLightStatus(int status)
{
	
}
 
struct Devices restaurantLight = {			//定义餐厅灯(对象)
	.deviceName = "restaurantLight",		//名字
	.pinNum = 13,							//香橙派 13号(wPi)引脚
	.Init = restaurantLightInit,			//指定初始化函数
	.open = restaurantLightOpen,			//指定“打开灯”函数
	.close = restaurantLightClose,			//指定“关闭灯”函数
    .changeStatus = restaurantLightStatus
};
 
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead)		//餐厅灯(对象)加入设备链表函数
{
	if (phead == NULL) {
		return &restaurantLight;
	}
	else {
		restaurantLight.next = phead;  //以前的头变成.next
		phead = &restaurantLight;      //更新头
		return phead;
	}
}

livingroomLight.c(客厅灯)

#include "controlDevice.h" //自定义设备类的文件

int livingroomLightInit(int pinNum) // C语言必须要传参,JAVA不用,可直接访问变量的值
{
	pinMode(pinNum, OUTPUT);	// 配置引脚为输出模式
	digitalWrite(pinNum, HIGH); // 引脚置高电平,断开继电器
}

int livingroomLightOpen(int pinNum)
{
	digitalWrite(pinNum, LOW); // 引脚置低电平,闭合继电器
}

int livingroomLightClose(int pinNum)
{
	digitalWrite(pinNum, HIGH); // 引脚置高电平,断开继电器
}

int livingroomLightStatus(int status)
{
}

struct Devices livingroomLight = {	 // 定义客厅灯(对象)
	.deviceName = "livingroomLight", // 名字
	.pinNum = 16,					 // 香橙派 16号(wPi)引脚
	.Init = livingroomLightInit,	 // 指定初始化函数
	.open = livingroomLightOpen,	 // 指定“打开灯”函数
	.close = livingroomLightClose,	 // 指定“关闭灯”函数
	.changeStatus = livingroomLightStatus};

struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead) // 客厅灯(对象)加入设备链表函数
{
	if (phead == NULL) {
		return &livingroomLight;
	}
	else {
		livingroomLight.next = phead; // 以前的头变成.next
		phead = &livingroomLight;	  // 更新头
		return phead;
	}
}

编译

gcc *.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt

在这里插入图片描述

运行结果

在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/211324.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2017年全国硕士研究生入学统一考试管理类专业学位联考英语(二)试题

文章目录 Section I Use of EnglishSection II Reading ComprehensionText 121-细节信息题22-细节信息题23-推断题24-细节信息题25-态度题 Text 226-细节信息题27-细节信息题28-细节信息题29-细节信息题30-细节信息题 Text 331-细节信息题32-细节信息题33-猜词题34-细节信息题3…

基于SSM的生鲜在线销售系统

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 目录…

初始数据结构(加深对旋转的理解)

力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台备战技术面试&#xff1f;力扣提供海量技术面试资源&#xff0c;帮助你高效提升编程技能&#xff0c;轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/rotate-array/submissions/ 与字…

python ping3库检测主机是否能ping通(ping命令)代码示例

文章目录 代码示例 代码示例 #!/usr/bin/env python3 # -*- coding: utf-8 -*-import ping3 import timeencoding utf-8def ping(host, time_out1):"""检查ip是否能被ping通&#xff0c;time_out为超时时间&#xff0c;单位为秒&#xff0c;默认为1秒"&q…

openGauss学习笔记-138 openGauss 数据库运维-例行维护-检查时间一致性

文章目录 openGauss学习笔记-138 openGauss 数据库运维-例行维护-检查时间一致性138.1 操作步骤 openGauss学习笔记-138 openGauss 数据库运维-例行维护-检查时间一致性 数据库事务一致性通过逻辑时钟保证&#xff0c;与操作系统时间无关&#xff0c;但是系统时间不一致会导致…

【C/C++笔试练习】公有派生、构造函数内不执行多态、抽象类和纯虚函数、多态中的缺省值、虚函数的描述、纯虚函数的声明、查找输入整数二进制中1的个数、手套

文章目录 C/C笔试练习选择部分&#xff08;1&#xff09;公有派生&#xff08;2&#xff09;构造函数内不执行多态&#xff08;3&#xff09;抽象类和纯虚函数&#xff08;4&#xff09;多态中的缺省值&#xff08;5&#xff09;程序分析&#xff08;6&#xff09;重载和隐藏&a…

函数指针和指针函数的讲解

文章目录 指针函数函数指针函数指针的定义与指针函数的声明的区别函数指针的定义指针函数的声明 typedef在函数指针方面的使用typedef和using 给函数指针的类型取别名typedef和using 给函数的类型取别名 指针函数 指针函数&#xff1a; 也叫指针型函数&#xff0c;本质上就是一…

VBA数据库解决方案第七讲:如何利用Recordset对象打开数据库的数据记录集

《VBA数据库解决方案》教程&#xff08;版权10090845&#xff09;是我推出的第二套教程&#xff0c;目前已经是第二版修订了。这套教程定位于中级&#xff0c;是学完字典后的另一个专题讲解。数据库是数据处理的利器&#xff0c;教程中详细介绍了利用ADO连接ACCDB和EXCEL的方法…

zabbix监控nginx

zabbix是什么 web界面提供的一种可视化的监控服务软件 以分布式的方式系统监控以及网络监控&#xff0c;硬件监控等等开源的软件 zabbix的架构 1、c/s模式 客户端和服务端&#xff0c;zabbix server服务端 zabbix agent 客户端 2、通过B/S B是浏览器 S服务端&#xff0c;通…

WEBAPI返回图片显示在VUE前端

WEBAPI部分 通过nuget安装Opencvsharp &#xff0c;这部分用来做图像处理 在controller中写如下方法&#xff0c;我要对原图进行旋转使用了Opencv&#xff0c;如果不需要旋转可以用注释的代码 [HttpGet(Name "ShowImage")]public async Task<IActionResult> …

广州华锐互动:数字孪生系统让生产工艺流程可视化,提高生产效率和质量

随着科技的飞速发展&#xff0c;数字化技术已经深入到各个行业&#xff0c;制造业也不例外。生产制造数字孪生系统作为一种新型的生产管理工具&#xff0c;正逐渐成为制造业的发展新趋势。 生产制造数字孪生系统是一种基于三维数字化技术的生产过程模拟与优化系统。通过对实际生…

【.net core 7】新建net core web api并引入日志、处理请求跨域以及发布

效果图&#xff1a; 1.新建.net core web api项目 选择src文件夹》添加》新建项目 输入框搜索&#xff1a;web api 》选择ASP.NET Core Web API 输入项目名称、选择位置为项目的 src文件夹下 我的项目是net 7.0版本&#xff0c;实际选择请看自己的项目规划 2.处理Progr…

Linux程序设计(下)

系列文章目录 文章目录 系列文章目录十、调试断言 十一、进程和信息号进程表进程调度启动新进程信号**信号处理****发送信号** 十二、POSIX线程线程创建线程同步线程属性取消一个线程pthread_exit, exit, _exit 十三、管道popen, pipe父子进程将管道用作标准输入和标准输出 命名…

mybatis多表查询(xml)

多表查询都用resultMap resultMap 说白了就是他可以手动设置映射参数&#xff0c;例如 可以指定 column代表数据库的参数 property 代表实体类的参数 <id column"roleid" property"id"></id> column代表数据库的参数 property 代表实体类…

C++入门篇第十篇----继承

前言&#xff1a; 本篇我们将开始讲解C的继承&#xff0c;我想要说的是&#xff0c;C的主体基本就是围绕类和对象展开的&#xff0c;继承也是以类和对象为主体&#xff0c;可以说&#xff0c;C相较于C优化的地方就在于它对于结构体的使用方法的高度扩展和适用于更多实际的场景…

外包干了2年,技术退步明显。。。

前言 简单的说下&#xff0c;我大学的一个同学&#xff0c;毕业后我自己去了自研的公司&#xff0c;他去了外包&#xff0c;快两年了我薪资、技术各个方面都有了很大的提升&#xff0c;他在外包干的这两年人都要废了&#xff0c;技术没一点提升&#xff0c;学不到任何东西&…

软件工程 - 第8章 面向对象建模 - 3 - 动态建模

状态图 状态是指在对象生命周期中满足某些条件、执行某些活动或等待某些事件的一个条件和状况 。 案例一&#xff1a;描述烧水器在工作时的详细行为细节 “人就是一个类&#xff0c;而你”、我”、张三”等都是“人这个类的一个实例&#xff0c;站着”、“躺着等都是对象的一…

Edge 旧版本回退

微软官网 下载策略文件 下载后&#xff0c;解压打开 cad 包&#xff0c;把里面的 Windows\ADMX\ 下 3 个 *.admx 文件解压到 C:\Windows\PolicyDefinitions Windows\ADMX\zh-CN 下 3 个 *.adlm 文件解压到 C:\Windows\PolicyDefinitions\zh-CN Windows 搜索 gpedit&#xff…

Swin Transformer实战图像分类(Windows下,无需用到Conda,亲测有效)

目录 前言 一、从官网拿到源码&#xff0c;然后配置自己缺少的环境。 针对可能遇到的错误&#xff1a; 二、数据集获取与处理 2.1 数据集下载 2.2 数据集处理 三、下载预训练权重 四、修改部分参数配置 4.1 修改config.py 4.2 修改build.py 4.3 修改units.py 4.4 修…

LeetCode的几道题

一、捡石头 292 思路就是&#xff1a; 谁面对4块石头的时候&#xff0c;谁就输&#xff08;因为每次就是1-3块石头&#xff0c;如果剩下4块石头&#xff0c;你怎么拿&#xff0c;我都能把剩下的拿走&#xff0c;所以你就要想尽办法让对面面对4块石头的倍数&#xff0c; 比如有…
最新文章