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

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

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;
}
相关推荐
飞睿科技1 天前
【芯片选型指南】乐鑫ESP32-C61核心能力解析:为何它在Wi-Fi 6物联网赛道中优势独具?
科技·嵌入式硬件·物联网·智能家居
星野云联AIoT技术洞察3 天前
Zigbee2MQTT + Home Assistant 集成商业化应用:2025年AIoT平台最佳应用
mqtt·智能家居·边缘计算·home assistant·zigbee2mqtt·开源iot
Smartlabs3 天前
Z-Wave物联网网关开发专用 SDK - Unify
物联网·智能家居·z-wave
RockHopper20253 天前
基于MQTT自定义主题的智能家居统一语义通信中枢
mqtt·智能家居
Smartlabs3 天前
Home Assistant Z-Pi 800全新硬件打造远距离智能家居体验
智能家居·home assistant·z-wave
猪八戒1.04 天前
智能家居项目-搭建工程
stm32·智能家居
-大头.4 天前
智能家居中控:嵌入式开发全流程实战
智能家居
无垠的广袤5 天前
【CPKCOR-RA8D1】Home Assistant 物联网 ADC 电压温度计
嵌入式硬件·物联网·智能家居·瑞萨
权泽谦6 天前
从0到1:基于STM32与FreeRTOS的智能家居中控系统设计实录
stm32·嵌入式硬件·智能家居
Smartlabs7 天前
Shelly智能模块:家居科技革新之选
物联网·wifi·智能家居