智能家居(4)---火灾报警线程封装

封装火灾报警线程实现智能家居中的火灾报警功能
mainPro.c(主函数)

#include <stdio.h>
#include "controlDevice.h"
#include "inputCommand.h"

#include <pthread.h>

struct Devices        *pdeviceHead  = NULL;         //设备工厂链表头

struct Devices* findDeviceByName(struct Devices *phead,char *name)          //在设备链表中查找设备(语音和socket均可使用)
{
    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;   

    }

}

void* fire_pthread(void *data)					//火灾报警线程处理函数
{
    struct Devices *fire;
    struct Devices *beep;
    int status;

    fire = findDeviceByName(pdeviceHead,"fire");        //从设备链表找出火灾检测和蜂鸣器
    beep = findDeviceByName(pdeviceHead,"beep");

    fire->deviceInit(fire->pinNum);                     
    beep->deviceInit(beep->pinNum);

     while(1){                                           //检测到火灾通过蜂鸣器报警
        status = fire->readStatus(fire->pinNum);
        if(status == 0){
            beep->open(beep->pinNum);
            printf("fire warning\n");
            delay(20000);
        }else
            beep->close(beep->pinNum);
            
    } 

 
 int main()
{
    pthread_t fire_pth;

    if(wiringPiSetup()<0){//初始化wiringPi外设库

            printf("wiringPi Init failed\n");
            return -1;
        }


    //1.指令工厂初始化


    //2.设备控制工厂初始化
  
    pdeviceHead = addFireToDeviceLink(pdeviceHead);                 //将火灾检测加入设备链表
    pdeviceHead = addBeepToDeviceLink(pdeviceHead);                 //将蜂鸣器加入设备链表

   
    //3.线程池建立
   

    //3.3火灾线程
    ret = pthread_create(&fire_pth,NULL,fire_pthread,NULL);
     if (ret != 0) {
        printf("Failed to create firethread.\n");
        return -1;
    }


    //等待线程退出
    pthread_join(fire_pth,NULL);


    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值