智能家居---工厂模式实现灯光控制(继电器组)以及火灾报警模组的封装

采用工厂模式以面向对象的方式来封装各种设备模块,方便整合项目以及后期的维护和扩展

mainPro.c(主函数)

c 复制代码
#include <stdio.h>
#include "controlDevice.h"

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

struct Devices* findDeviceByName(struct Devices *phead,char *name)          //在设备链表中查找设备
{
    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;   

    }

}

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

            printf("wiringPi Init failed\n");
            return -1;
        }
      //设备控制工厂初始化
    pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);        //将卫生灯加入设备链表
    pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);         //将二楼灯加入设备链表
    pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);      //将餐厅灯加入设备链表
    pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);      //将客厅灯加入设备链表
    pdeviceHead = addFireToDeviceLink(pdeviceHead);                 //将火灾检测加入设备链表
    pdeviceHead = addBeepToDeviceLink(pdeviceHead);                 //将蜂鸣器加入设备链表

controlDevice.h( 设备类)

c 复制代码
#include <wiringPi.h>
#include <stddef.h>

struct Devices
{
    char deviceName[128];                       //设备名字
    int status;                                 //状态
    int pinNum;                                 //引脚

    int (*deviceInit)(int pinNum);              //设备初始化函数指针,后面类似
    int (*open)(int pinNum);                    //打开设备
    int (*close)(int pinNum);                   //关闭设备
    

    int (*readStatus)(int pinNum);              //读取引脚状态
    int (*changeStatus)(int status);

    struct Devices *next;                       //方便链表使用的结构体指针
};

struct Devices *addBathroomLightToDeviceLink(struct Devices *phead);        //卫生间灯加入设备链表声明,以下类似
struct Devices *addUpstairLightToDeviceLink(struct Devices *phead);         //二楼灯
struct Devices *addRestaurantLightToDeviceLink(struct Devices *phead);      //餐厅灯
struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead);      //客厅灯
struct Devices *addFireToDeviceLink(struct Devices *phead);                 //火灾检测器
struct Devices *addBeepToDeviceLink(struct Devices *phead);                 //蜂鸣器

bathroomLight.c(浴室灯)

c 复制代码
#include "controlDevice.h"

int bathroomLightInit(int pinNum)
{
     pinMode(pinNum,OUTPUT);
     digitalWrite(pinNum,HIGH);

}

int bathroomLightOpen(int pinNum)
{
    digitalWrite(pinNum,LOW);
}

int bathroomLightClose(int pinNum)
{
    digitalWrite(pinNum,HIGH);

}

int bathroomLightChangeStatus()
{
     //暂时不用,后面用着在完善

}

struct Devices bathroomLight = {

    .deviceName = "bathroomLight",
    .pinNum = 26,
    .deviceInit = bathroomLightInit,
    .open  = bathroomLightOpen,
    .close = bathroomLightClose,
    .changeStatus = bathroomLightChangeStatus,


};

struct Devices *addBathroomLightToDeviceLink(struct Devices *phead)		//将浴室灯加入设备链表的函数
{
    if(phead == NULL){
        return &bathroomLight;
    
    }else{
        bathroomLight.next=phead;
        phead = &bathroomLight;
        return phead;
    }




}

livingroomLight.c(客厅灯)

c 复制代码
#include "controlDevice.h"

int livingroomLightInit(int pinNum)
{
     pinMode(pinNum,OUTPUT);//设置引脚为输出模式
     digitalWrite(pinNum,HIGH);

}


int livingroomLightOpen(int pinNum)
{
    digitalWrite(pinNum,LOW);
}

int livingroomLightClose(int pinNum)
{
    digitalWrite(pinNum,HIGH);

}

int livingroomLightChangeStatus()
{
    //暂时不用,后面用着在完善

}

struct Devices livingroomLight = {

    .deviceName = "livingroomLight",
    .pinNum = 29,
    .deviceInit = livingroomLightInit,
    .open  = livingroomLightOpen,
    .close = livingroomLightClose,
    .changeStatus = livingroomLightChangeStatus,


};

struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead)
{
    if(phead == NULL){
        return &livingroomLight;
    
    }else{
        livingroomLight.next=phead;
        phead = &livingroomLight;
        return phead;
    }


}

restaurantLight.c(餐厅灯)

c 复制代码
#include "controlDevice.h"

int restaurantLightInit(int pinNum)
{
     pinMode(pinNum,OUTPUT);//设置引脚为输出模式
     digitalWrite(pinNum,HIGH);

}


int restaurantLightOpen(int pinNum)
{
    digitalWrite(pinNum,LOW);
}

int restaurantLightClose(int pinNum)
{
    digitalWrite(pinNum,HIGH);

}

int restaurantLightChangeStatus()
{
    //暂时不用,后面用着在完善

}

struct Devices restaurantLight = {

    .deviceName = "restaurantLight",
    .pinNum = 28,
    .deviceInit = restaurantLightInit,
    .open  = restaurantLightOpen,
    .close = restaurantLightClose,
    .changeStatus = restaurantLightChangeStatus,


};

struct Devices *addRestaurantLightToDeviceLink(struct Devices *phead)
{
    if(phead == NULL){
        return &restaurantLight;
    
    }else{
        restaurantLight.next=phead;
        phead = &restaurantLight;
        return phead;
    }


}

upstairLight.c(二楼灯)

c 复制代码
#include "controlDevice.h"

int upstairLightInit(int pinNum)
{
     pinMode(pinNum,OUTPUT);//设置引脚为输出模式
     digitalWrite(pinNum,HIGH);

}


int upstairLightOpen(int pinNum)
{
    digitalWrite(pinNum,LOW);
}

int upstairLightClose(int pinNum)
{
    digitalWrite(pinNum,HIGH);

}

int upstairLightChangeStatus()
{
    //暂时不用,后面用着在完善

}

struct Devices upstairLight = {

    .deviceName = "upstairLight",
    .pinNum = 27,
    .deviceInit = upstairLightInit,
    .open  = upstairLightOpen,
    .close = upstairLightClose,
    .changeStatus = upstairLightChangeStatus,


};

struct Devices *addUpstairLightToDeviceLink(struct Devices *phead)
{
    if(phead == NULL){
        return &upstairLight;
    
    }else{
        upstairLight.next=phead;
        phead = &upstairLight;
        return phead;
    }




}

fire.c(火灾检测)

c 复制代码
#include "controlDevice.h"

int fireInit(int pinNum)
{
     pinMode(pinNum,INPUT);//设置引脚为输出模式

}

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


int fireChangeStatus()
{
    //暂时不用,后面用着在完善

}

struct Devices fire = {

    .deviceName = "fire",
    .pinNum = 25,
    .deviceInit = fireInit,
    .readStatus = fireStatusRead,
    .changeStatus = fireChangeStatus,


};

struct Devices *addFireToDeviceLink(struct Devices *phead)
{
    if(phead == NULL){
        return &fire;
    
}else{
    fire.next=phead;
    phead = &fire;
    return phead;
}

}

beep.c(和火灾检测配合实现火灾报警的蜂鸣器)

c 复制代码
#include "controlDevice.h"


int beepInit(int pinNum)
{
     pinMode(pinNum,OUTPUT);//设置引脚为输出模式
     digitalWrite(pinNum,HIGH);

}

int beepOpen(int pinNum)
{
    digitalWrite(pinNum,LOW);

}

int beepClose(int pinNum)
{
    digitalWrite(pinNum,HIGH);

}


int beepChangeStatus()
{
    //暂时不用,后面用着在完善

}

struct Devices beep = {

    .deviceName = "beep",
    .pinNum = 24,
    .deviceInit = beepInit,
    .open = beepOpen,
    .close = beepClose,
    .changeStatus = beepChangeStatus,


};

struct Devices *addBeepToDeviceLink(struct Devices *phead)
{
    if(phead == NULL){
        return &beep;
    
    }else{
        beep.next=phead;
        phead = &beep;
        return phead;
    }


}
相关推荐
武汉唯众智创8 小时前
浅谈智能家居在智慧养老实训室中的作用
智能家居·养老实训室·智慧养老实训室
stm32发烧友15 小时前
基于STM32的智能家居安防系统设计
stm32·嵌入式硬件·智能家居
凉开水白菜3 天前
米家通过HomeAssistant控制笔记本电脑开关机
python·电脑·智能家居·米家
脑极体3 天前
大模型,智能家居的春秋战国之交
智能家居
尘浮生3 天前
Java项目实战II基于Java+Spring Boot+MySQL的体育馆使用预约平台的设计与实现(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·maven·智能家居
武汉唯众智创4 天前
软件工程技术专业在物联网应用开发中的关键技术与挑战
智能家居·软件工程技术·物联网应用开发
faec1134 天前
2024年双十一有什么好物推荐?双十一必买好物清单大揭秘
智能家居
远翔调光芯片^138287988725 天前
远翔升压恒流芯片FP7209X与FP7209M什么区别?做以下应用市场摄影补光灯、便携灯、智能家居(调光)市场、太阳能、车灯、洗墙灯、舞台灯必看!
科技·单片机·智能家居·能源
stm32发烧友5 天前
基于STM32的智能家居环境监测系统设计
stm32·嵌入式硬件·智能家居
lucy153027510796 天前
【青牛科技】GC2803:白色家电与安防领域中 ULN2803 的卓越替代者
科技·单片机·智能家居·能源·安防·开关电源·白色家电