文章目录
智能家居功能分析图
- 两个控制系统:主控制系统和子控制系统,引入子控制系统是为了串口功能引脚,树莓派只有一个串口通信,而射频编码模块315/433Mhz和红外都需串口通信
基于工厂模式的智能家居设计
线程池包含:
- 语音控制
- sock客户端
- 摄像头识别
- 火灾报警
指令工厂和控制工厂的结构体中包含:
- 标记:用于区分不同的文件
- 函数:用于控制或接收
- 初始化:用于驱动不同的文件内的功能.
工厂模式实现继电器控制
- 工厂模式的特点就是便于创建对象,也就需要合理的构建类,继电器控制是通过树莓派的通用IO口输出功能。每个设备对象都有名字和用到的引脚以及引脚状态,每个设备对象都需要初始化引脚功能,都可以控制引脚的开与关,甚至可以读取引脚状态。
- 在设备文件内定义对象的时候是定义一个变量,所以需要分号结尾
- 树莓派不容易识别null,在.h头文件中添加
#include <stdlib.h>
- 在设备文件中,定义的对象的属性不能为全局函数所用,需要通过在函数的形参定义需要用到的属性,这样函数内部才能使用,这点与java不一样,java只要对象有该属性,函数就可以使用
mainPro.c(主函数)
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "controlDevice.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->deviceName,name)==0){
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
int main()
{
char name[128];
struct Devices *tmp = NULL;
if (wiringPiSetup () == -1) {
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
struct Devices *pdeviceHead = NULL;
pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
while(1){
printf("Input:\n");
scanf("%s",name);
tmp = findDeviceByName(name, pdeviceHead);
if(tmp != NULL){
tmp->Init(tmp->pinNum);
tmp->open(tmp->pinNum);
}
}
return 0;
}
controlDevice.h(设备类)
#include <wiringPi.h> //wiringPi库
#include <stdio.h>
#include <stdlib.h>
struct Devices //设备类
{
char deviceName[128]; //设备名
int status; //设备引脚的状态
int pinNum; //设备用到的引脚号
int (*Init)(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* addBedroomLightToDeviceLink(struct Devices *phead);
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead);
bathroomLight.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 bathroomLightStatus(int status)
{
}
struct Devices bathroomLight = {
.deviceName = "bathroomLight",
.pinNum = 21,
.Init = bathroomLightInit,
.open = bathroomLightOpen,
.close = bathroomLightClose,
.changeStatus = bathroomLightStatus
};
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &bathroomLight;
}else{
bathroomLight.next = phead;
phead = &bathroomLight;
return phead;
}
}
bedroomLight.c(卧室灯)
#include "controlDevice.h"
int bedroomLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int bedroomLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int bedroomLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int bedroomLightStatus(int status)
{
}
struct Devices bedroomLight = { )
.deviceName = "bedroomLight",
.pinNum = 22,
.Init = bedroomLightInit,
.open = bedroomLightOpen,
.close = bedroomLightClose,
.changeStatus = bedroomLightStatus
};
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &bedroomLight;
}else{
bedroomLight.next = phead;
phead = &bedroomLight;
return phead;
}
}
restaurantLight.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 restaurantLightStatus(int status)
{
}
struct Devices restaurantLight = {
.deviceName = "restaurantLight",
.pinNum = 23,
.Init = restaurantLightInit,
.open = restaurantLightOpen,
.close = restaurantLightClose,
.changeStatus = restaurantLightStatus
};
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &restaurantLight;
}else{
restaurantLight.next = phead;
phead = &restaurantLight;
return phead;
}
}
livingroomLight.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 livingroomLightStatus(int status)
{
}
struct Devices livingroomLight = {
.deviceName = "livingroomLight",
.pinNum = 24,
.Init = livingroomLightInit,
.open = livingroomLightOpen,
.close = livingroomLightClose,
.changeStatus = livingroomLightStatus
};
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL){
return &livingroomLight;
}else{
livingroomLight.next = phead;
phead = &livingroomLight;
return phead;
}
}
运行:
gcc bathroomLight.c bedroomLight.c livingroomLight.c restaurantLight.c mainPro.c -lwiringPi
运行结果: