采用工厂模式以面向对象的方式来封装各种设备模块,方便整合项目以及后期的维护和扩展
mainPro.c(主函数)
#include
#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 = addUpstairLightToDeviceLink(pdeviceHead); //将二楼灯加入设备链表
pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead); //将餐厅灯加入设备链表
pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead); //将客厅灯加入设备链表
pdeviceHead = addFireToDeviceLink(pdeviceHead); //将火灾检测加入设备链表
pdeviceHead = addBeepToDeviceLink(pdeviceHead); //将蜂鸣器加入设备链表
controlDevice.h( 设备类)
#include
#include
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 *addUpstairLightToDeviceLink(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;
}
}
upstairLight.c(二楼灯)
#include "controlDevice.h"
int upstairLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);//设置引脚为输出模式
digitalWrite(pinNum,HIGH);
}
int upstairLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int upstairLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int upstairLightChangeStatus()
{
//暂时不用,后面用着在完善
}
struct Devices upstairLight = {
.deviceName = "upstairLight",
.pinNum = 27,
.deviceInit = upstairLightInit,
.open = upstairLightOpen,
.close = upstairLightClose,
.changeStatus = upstairLightChangeStatus,
};
struct Devices *addUpstairLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &upstairLight;
}else{
upstairLight.next=phead;
phead = &upstairLight;
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;
}
}