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

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

用户手册对应的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;  //以前的头变成.nextphead = &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;  //以前的头变成.nextphead = &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;  //以前的头变成.nextphead = &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; // 以前的头变成.nextphead = &livingroomLight;	  // 更新头return phead;}
}

编译

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

在这里插入图片描述

运行结果

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

http://www.lryc.cn/news/252982.html

相关文章:

  • 第三节:提供者、消费者、Eureka
  • Leetcode刷题详解——等差数列划分
  • 导出主机上所有docker 镜像并导入到其它主机
  • HTML5+CSS3+JS小实例:焦点图波浪切换动画特效
  • Mac电脑如何安装git
  • macOS本地调试k8s源码
  • JS 实现一键复制文本内容
  • 【Linux】echo命令使用
  • Day03 嵌入式---中断
  • wpf devexpress 使用IDataErrorInfo实现input验证
  • shell_81.Linux在命令行中创建使用函数
  • 鱼香ROS一键安装命令(支持微信、docker、ros等)
  • 深入理解 Go 函数:从基础到高级
  • 开启三层交换机DHCP服务
  • jspdf+html2canvas浏览器缩放问题
  • 西南科技大学模拟电子技术实验六(BJT电压串联负反馈放大电路)预习报告
  • JS的监听事件
  • JS Object.values()
  • 基于Java SSM人力资源管理系统
  • 人工智能和程序员
  • Unity优化篇:对于unity DrawCall/Mesh/纹理压缩/内存等方面的常规调试和优化手段
  • 学生信息管理系统
  • 纯代码压缩WordPress前端Html
  • Elasticsearch分词器--空格分词器(whitespace analyzer)
  • 【LeetCode】692. 前K个高频单词
  • 在Windows操作系统上使用rtsp simple server和ffmpeg推送录屏视频流
  • 互联网摸鱼日报(2023-12-05)
  • Android 项目的依赖方式
  • ArcGIS提取DEM中的山脉范围
  • 漏洞复现--万户ezoffice wpsservlet任意文件上传