智能家居 (1) ——工厂模式控制灯

目录:

    • 浴室灯 .C 文件:
    • 二楼灯 .C 文件:
    • 客厅灯 .C 文件:
    • 餐厅灯 .C 文件:
    • 设备类 .h 文件:
    • 主函数 .C 文件:

控制树莓派引脚模式 > 控制继电器组 > 控制灯光

浴室灯 .C 文件:

#include "equipment.h"			//自定义设备类的文件

int bathroomLightInit();		//初始化继电器函数声明
int bathroomLightOpen();		//“打开灯”函数声明
int bathroomLightClose();		//“关闭灯”函数声明
struct Equipment *addBathroomLightToLink(struct Equipment *phead);
//浴室灯(对象)加入设备链表函数声明

struct Equipment bathroomLight = {
     			//定义浴室灯(对象)
	.equipName = "bathroomLight",			//名字
	.pinNum = 21,							//树莓派 21号(wPi)引脚
	.Init = bathroomLightInit,				//指定初始化函数
	.open = bathroomLightOpen,				//指定“打开灯”函数
	.close = bathroomLightClose,			//指定“关闭灯”函数
};


int bathroomLightInit(int pinNum)
{
     
	pinMode(pinNum,OUTPUT);					//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}

int bathroomLightOpen(int pinNum)
{
     
	digitalWrite(pinNum,LOW);				//引脚置低电平,闭合继电器
}

int bathroomLightClose(int pinNum)
{
     
	digitalWrite(pinNum,HIGH);				//引脚置高电平,断开继电器
}

struct Equipment *addBathroomLightToLink(struct Equipment *phead)		//浴室灯(对象)加入设备链表函数
{
     
	if(phead == NULL){
     
		return &bathroomLight;
	}else{
     
		bathroomLight.next = phead;
		phead = &bathroomLight;
		return phead;
	}
}

二楼灯 .C 文件:

#include "equipment.h"				//自定义设备类的文件

int secondfloorLightInit();			//初始化继电器函数声明
int secondfloorLightOpen();			//“打开灯”函数声明
int secondfloorLightClose();		//“关闭灯”函数声明
struct Equipment *addSecondfloorLightToLink(struct Equipment *phead);
//二楼灯(对象)加入设备链表函数声明

struct Equipment secondfloorLight = {
     			//定义二楼灯(对象)
	.equipName = "secondfloorLight",			//名字
	.pinNum = 22,								//树莓派 22号(wPi)引脚
	.Init = secondfloorLightInit,				//指定初始化函数
	.open = secondfloorLightOpen,				//指定“打开灯”函数
	.close = secondfloorLightClose,				//指定“关闭灯”函数
};


int secondfloorLightInit(int pinNum)
{
     
	pinMode(pinNum,OUTPUT);						//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);					//引脚置高电平,断开继电器
}

int secondfloorLightOpen(int pinNum)
{
     
	digitalWrite(pinNum,LOW);					//引脚置低电平,闭合继电器
}

int secondfloorLightClose(int pinNum)
{
     
	digitalWrite(pinNum,HIGH);					//引脚置高电平,断开继电器
}

struct Equipment *addSecondfloorLightToLink(struct Equipment *phead)		//二楼灯(对象)加入设备链表函数
{
     
	if(phead == NULL){
     
		return &secondfloorLight;
	}else{
     
		secondfloorLight.next = phead;
		phead = &secondfloorLight;
		return phead;
	}
}

客厅灯 .C 文件:

#include "equipment.h"				//自定义设备类的文件

int livingroomLightInit();			//初始化继电器函数声明
int livingroomLightOpen();			//“打开灯”函数声明
int livingroomLightClose();			//“关闭灯”函数声明
struct Equipment *addLivingroomLightToLink(struct Equipment *phead);
//客厅灯(对象)加入设备链表函数声明

struct Equipment livingroomLight = {
     			//定义客厅灯(对象)
	.equipName = "livingroomLight",				//名字
	.pinNum = 23,								//树莓派 23号(wPi)引脚
	.Init = livingroomLightInit,				//指定初始化函数
	.open = livingroomLightOpen,				//指定“打开灯”函数
	.close = livingroomLightClose,				//指定“关闭灯”函数
};


int livingroomLightInit(int pinNum)
{
     
	pinMode(pinNum,OUTPUT);						//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);					//引脚置高电平,断开继电器
}

int livingroomLightOpen(int pinNum)
{
     
	digitalWrite(pinNum,LOW);					//引脚置低电平,闭合继电器
}

int livingroomLightClose(int pinNum)
{
     
	digitalWrite(pinNum,HIGH);					//引脚置高电平,断开继电器
}

struct Equipment *addLivingroomLightToLink(struct Equipment *phead)		//客厅灯(对象)加入设备链表函数
{
     
	if(phead == NULL){
     
		return &livingroomLight;
	}else{
     
		livingroomLight.next = phead;
		phead = &livingroomLight;
		return phead;
	}
}

餐厅灯 .C 文件:

#include "equipment.h"				//自定义设备类的文件

int restaurantLightInit();			//初始化继电器函数声明
int restaurantLightOpen();			//“打开灯”函数声明
int restaurantLightClose();			//“关闭灯”函数声明
struct Equipment *addRestaurantLightToLink(struct Equipment *phead);
//餐厅灯(对象)加入设备链表函数声明

struct Equipment restaurantLight = {
     			//定义餐厅灯(对象)
	.equipName = "restaurantLight",				//名字
	.pinNum = 24,								//树莓派 24号(wPi)引脚
	.Init = restaurantLightInit,				//指定初始化函数
	.open = restaurantLightOpen,				//指定“打开灯”函数
	.close = restaurantLightClose,				//指定“关闭灯”函数
};


int restaurantLightInit(int pinNum)
{
     
	pinMode(pinNum,OUTPUT);						//配置引脚为输出模式
	digitalWrite(pinNum,HIGH);					//引脚置高电平,断开继电器
}

int restaurantLightOpen(int pinNum)
{
     
	digitalWrite(pinNum,LOW);					//引脚置低电平,闭合继电器
}

int restaurantLightClose(int pinNum)
{
     
	digitalWrite(pinNum,HIGH);					//引脚置高电平,断开继电器
}

struct Equipment *addRestaurantLightToLink(struct Equipment *phead)		//餐厅灯(对象)加入设备链表函数
{
     
	if(phead == NULL){
     
		return &restaurantLight;
	}else{
     
		restaurantLight.next = phead;
		phead = &restaurantLight;
		return phead;
	}
}

设备类 .h 文件:

equipment.h

#include 					//wiringPi库
#include 
#include 

struct Equipment						//设备类
{
     
	char equipName[128];				//设备名
	int pinNum;							//引脚号
	int (*Init)(int pinNum);			//“初始化设备”函数指针
	int (*open)(int pinNum);			//“打开设备”函数指针
	int (*close)(int pinNum);			//“关闭设备”函数指针

	int (*readStatus)(int pinNum);		//“读取设备状态”函数指针
	int (*changeStatus)(int status);	//“改变设备状态”函数指针

	struct Equipment *next;
};

struct Equipment *addBathroomLightToLink(struct Equipment *phead);			//“浴室灯”加入设备链表函数声明
struct Equipment *addSecondfloorLightToLink(struct Equipment *phead);		//“二楼灯”加入设备链表函数声明
struct Equipment *addLivingroomLightToLink(struct Equipment *phead);		//“客厅灯”加入设备链表函数声明
struct Equipment *addRestaurantLightToLink(struct Equipment *phead);		//“餐厅灯”加入设备链表函数声明

主函数 .C 文件:

(继电器功能测试)

#include 
#include 
#include "equipment.h"

int main()
{
     
	if(wiringPiSetup() == -1){
     								//使用wiringPi库,需初始化硬件				  
	printf("wiringPiSetup failed!\n");
	return -1; 
	}

	struct Equipment *equiphead = NULL;						//定义初始链表首端
	equiphead = addBathroomLightToLink(equiphead);			//“浴室灯”加入设备链表
	equiphead = addSecondfloorLightToLink(equiphead);		//“二楼灯”加入设备链表
	equiphead = addLivingroomLightToLink(equiphead);		//“客厅灯”加入设备链表
	equiphead = addRestaurantLightToLink(equiphead);		//“餐厅灯”加入设备链表

	struct Equipment *tmp = equiphead;
	while(tmp != NULL){
     							//继电器功能测试
		tmp->Init(tmp->pinNum);
		tmp->open(tmp->pinNum);
		tmp = tmp->next;
	}

	return 0;
}

你可能感兴趣的:(智能家居,raspberry,pi,linux)