control.h 文件(设备类)
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
struct Devices
{
char name[128];
int status;
int pinName;
int (*open)(int pinName);
int (*close)(int pinName);
int (*deviceInit)(int pinName);
int (*readStaus)(); //读取状态
int (*changeStatus)(int status);
struct Devices* next;
};
struct Devices* addbathroomLink(struct Devices* head);
struct Devices* addupstairLink(struct Devices* head);
struct Devices* addrestaurantLink(struct Devices* head);
struct Devices* addlivingroomLink(struct Devices* head);
struct Devices* addfiretoLink(struct Devices* head);
mainPro.c 文件(主函数)
#include <stdio.h>
#include "controDevice.h"
#include <string.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->name,name)==0){
return tmp;
}
tmp=tmp->next;
}
return NULL;
}
}
int main(){
if(wiringPiSetup() == -1){
printf("wiringPiSetup failed!\n");
return -1;
}
char*name="bathroomLight";
struct Devices* tem=NULL;
tem=addbathroomLink(tem);
tem=addupstairLink(tem);
tem=addrestaurantLink(tem);
tem=addlivingroomLink(tem);
tem= addfiretoLink(tem); //新加的火灾模块
struct Devices* lamb=tem;
while(lamb!=NULL){
lamb->deviceInit(lamb->pinName);
lamb->open(lamb->pinName);
lamb=lamb->next;
}
struct Devices* lamp=findDeviceByName(name,tem);
if(lamp!=NULL){
lamp->deviceInit(lamp->pinName);
lamp->open(lamp->pinName);
}
return 0;
}
fire.c(火焰传感器)
#include "controDevice.h"
int firetoInit(int pinName){
pinMode(pinName,INPUT); //配置引脚为输入模式
digitalWrite(pinName,HIGH); //引脚置高电平,状态为1;默认刚开始无火灾,有火灾再拉低
}
int firetostatusread(int pinName){
return digitalRead(pinName); /读取引脚电平,并返回,高电平返回 1,低电平返回 0(有火)
}
struct Devices fireto = {
.name="fireto",
.pinName=25,
.deviceInit=firetoInit,
.readStaus=firetostatusread
};
struct Devices* addfiretoLink(struct Devices* head){
if(head==NULL){
return &fireto;
}
else
{
fireto.next=head;
head=&fireto;
return head;
}
}