工厂模式实现火焰报警
mainPro.c(主函数)
#include "ctrDevice.h"
#include <string.h>
#include <errno.h>
struct ctrDevice* findFunc(struct ctrDevice* phead,char *cmd)
{
struct ctrDevice* tmp;
tmp = phead;
if(tmp == NULL){
printf("no found!\n");
return NULL;
}else{
while(tmp != NULL){
if(strcmp(tmp->DeviceName,cmd)==0)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
int main(){
struct ctrDevice* phead=NULL;
struct ctrDevice* tmp=NULL;
char cmd[32]={'\0'};
if (wiringPiSetup () == -1) {
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
//phead = upstairsLightLink(phead);
//phead = restaurantLightLink(phead);
//phead = bathroomLightLink(phead);
//phead = livingroomLightLink(phead);
//phead = livingroomLightLink(phead);
phead = FireLink(phead);
phead = beerLink(phead);
while(1){
printf("Input:\n");
scanf("%s",cmd);
tmp = findFunc(phead,cmd);
if(tmp!=NULL)
{
if(tmp->readStatus(tmp->pinNum)==1)
{
tmp->next->InitDevice(tmp->pinNum);
tmp->next->close(tmp->pinNum);
}else{
tmp->next->InitDevice(tmp->pinNum);
tmp->next->open(tmp->pinNum);
}
}
memset(cmd,'\0',sizeof(cmd));
}
return 0;
}
controlDevice.h(设备类)
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
struct ctrDevice {
char DeviceName[32];
int pinNum;
int status;
void (*InitDevice)(int pinNum);
void (*open)(int pinNum);
void (*close)(int pinNum);
int (*readStatus)(int pinNum);
int (*changeStatus)(int pinNum);
struct ctrDevice *next;
};
struct ctrDevice* upstairsLightLink(struct ctrDevice* phead);
struct ctrDevice* restaurantLightLink(struct ctrDevice* phead);
struct ctrDevice* bathroomLightLink(struct ctrDevice* phead);
struct ctrDevice* livingroomLightLink(struct ctrDevice* phead);
struct ctrDevice* FireLink(struct ctrDevice* phead);
struct ctrDevice* beerLink(struct ctrDevice* phead);
#include "ctrDevice.h"
/*struct ctrDevice {
char DeviceName[32];
int pinNum;
int status;
void (*InitDevice)(int pinNum);
void (*open)(int pinNum);
void (*close)(int pinNum);
void (*readStatus)(int pinNum);
void (*changeStatus)(int pinNum);
struct ctrDevice *next;
};*/
void InitFire (int pinNum)
{
pinMode(pinNum,INPUT); //配置引脚为输出模式
digitalWrite(pinNum,HIGH); //引脚置高电平,断开继电器
}
int readFireStatus (int pinNum)
{
return digitalRead(pinNum);
}
struct ctrDevice fire={
.DeviceName="fire",
.pinNum = 6,
.InitDevice=InitFire,
.readStatus=readFireStatus
};
struct ctrDevice* FireLink(struct ctrDevice* phead)
{
if(phead==NULL){
return &fire;
}else{
fire.next = phead;
phead = &fire;
return phead;
}
}
buzzer.c(蜂鸣器)
#include "ctrDevice.h"
/*struct ctrDevice {
char DeviceName[32];
int pinNum;
int status;
void (*InitDevice)(int pinNum);
void (*open)(int pinNum);
void (*close)(int pinNum);
void (*readStatus)(int pinNum);
void (*changeStatus)(int pinNum);
struct ctrDevice *next;
};*/
void InitBeer (int pinNum)
{
pinMode(pinNum,OUTPUT); //配置引脚为输出模式
digitalWrite(pinNum,HIGH); //引脚置高电平,断开继电器
}
void beerOpen(int pinNum){
digitalWrite(pinNum,LOW);
}
void beerClose(int pinNum){
digitalWrite(pinNum,HIGH);
}
struct ctrDevice beer={
.DeviceName="beer",
.pinNum = 9,
.InitDevice=InitBeer,
.open=beerOpen,
.close=beerClose
};
struct ctrDevice* beerLink(struct ctrDevice* phead)
{
if(phead==NULL){
return &beer;
}else{
beer.next = phead;
phead = &beer;
return phead;
}
}
线程实现
mainPro.c
#include <pthread.h>
#include "controlDevice.h"
#include "inputCommand.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;
}
}
void *fireAlarmThread(void *data) //“烟雾报警器线程”执行的函数
{
int fireStatus; //存放“烟雾传感器”状态
struct Devices *tmp = NULL;
while(1){
tmp = findDeviceByName("fire", pdeviceHead);
if(tmp != NULL){
tmp->Init(tmp->pinNum);
fireStatus = tmp->readStatus(tmp->pinNum);
tmp = findDeviceByName("buzzer", pdeviceHead);
if(tmp != NULL){
if( fireStatus == 0 ){
tmp->Init(tmp->pinNum);
tmp->open(tmp->pinNum);
}else{
tmp->Init(tmp->pinNum);
tmp->close(tmp->pinNum);
}
}
}
}
}
int main()
{
if (wiringPiSetup () == -1) {
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
pthread_t smokeAlarm_thread;
//设备控制工厂初始化
//pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
//pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
//pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
//pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addFireAlarmToDeviceLink(pdeviceHead);
pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
pthread_create(&fireAlarm_thread,NULL,fireAlarmThread,NULL);
pthread_join(fireAlarm_thread, NULL); //主函数等待线程退出
return 0;
}