智能家居 (3) ——工厂模式火灾报警

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;
	   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追着太阳跑1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值