当前位置:   article > 正文

智能家居实战-------树莓派之继电器控制火灾报警器_基于树莓派的火灾自动报警系统

基于树莓派的火灾自动报警系统

一、在controlDevices.h中

struct Devices
{
   char deviceName[128];
   int status;
   int pinNum;
   int (*open)(int pinNum);
   int (*close)(int pinNum);
   int (*deviceInit)(int pinNum);
   int (*readStatus)(int pinNum);
   int (*changeStatus)(int pinNum);
   struct Devices *next;
};
struct Devices* addBathroomLightToDeviceLink(struct Device *phead);
struct Devices* addUpstairLightToDeviceLink(struct Device *phead);
struct Devices* addfireIfOrNotToDeviceLink(struct Device *phead);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.在fire.c中

#include "controlDevices.h"
#include <stdlib.h>
int fireIfOrNotInit(int pinNum)
{
     pinMode(pinNum,INPUT);
     digitalWrite(pinNum,HIGH);
}

int fireStatusRead(int pinNum)
{
     return digitalRead(pinNum);
}

struct Devices fireIfOrNot{
            .deviceName = "fireIfOrNot",
            .pinNum = 25,
            .deviceInit = fireIfOrNotInit,
            .changeStatus = fireStatusRead
};

struct Devices *addFireIfOrNotToDeviceLink(struct                    Devices *phead)
{
      if(phead == NULL)
      {
           return &fireIfOrNot;
}
     else{
           fireIfOrNot->next = phead;
           phead = &fireIfOrNot;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

3.在mainpro.c中

#include<stdio.h>
#include"controlDevices.h"
#include<string.h>
struct Device *findDeviceByName(char *name,struct Devices *phead)
{
   structmpt Devices *tmp = phead;
   if(phead == NULL)
   {
   return NULL;
   }
else{
   while(tmp!=NULL)
   {
     if(strcmp(tmp->devicename , name){
          return tmp;
   }
   tmp = tmp->next;   
  }
return NULL;
  }
}


int main()
{
   char name[128];
   struct Devices *pdeviceHead = NULL;
   pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
   pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
   pdeviceHead = addfireIfOrNotToDeviceLink(pdeviceHead);
   struct Devices *tmp = findDeviceByName(name,pdeviceHead);
   while(1)
  {
   printf("input\n");
   scanf("%s",name);
   if(tmp !=NULL)
   {
      tmp->deviceInit(tmp->pinNum);
      tmp->open(tmp->pinNum);
   }
  }
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/673940
推荐阅读
相关标签
  

闽ICP备14008679号