采用工厂模式以面向对象的方式来封装各种设备模块,方便整合项目以及后期的维护和扩展
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(火灾检测)文章来源:https://www.toymoban.com/news/detail-649019.html
#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(和火灾检测配合实现火灾报警的蜂鸣器)文章来源地址https://www.toymoban.com/news/detail-649019.html
#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)---工厂模式实现灯光控制(继电器组)以及火灾报警模组的封装的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!