文章目录
- 一、工厂模式简述
- 二、根据工厂模式创建代码
- 三、增加灯的控制数量
- 修改头文件
- 修改main函数
- 添加二楼灯
- 添加餐厅灯
- 添加卧室灯
- 运行
一、工厂模式简述
工厂模式就是我这边有一个厂房(类),就是做鞋子的,你不清楚我可以做那些鞋子,我也不给你说做休闲鞋还是跑步鞋,或者足力健,我根据你的要求来做鞋子。
//定义工厂
struct shoe
{
char Name[128]; //鞋名
int Size; //多少码
struct shoe *Next;
};
//我要跑鞋,名字叫特步,41码子
struct shoe Pao = {
.Name = "TeBu",
.Size = 41,
};
//我要休闲鞋,名字李宁,41码
struct shoe Xiu = {
.Name = "Li_Ning",
.Size = 41,
};
二、根据工厂模式创建代码
1、需要一个工厂来定义不同的灯
2、每个灯都要初始化、打开、关闭。
3、每个灯都要在工厂里
4、需要一个main函数来开始运行
代码
equipment.h
#include <wiringPi.h> //wiringPi库
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Equipment //设备类
{
char equipName[128]; //设备名
int pinNum; //引脚号
int (*Init)(int pinNum); //“初始化设备”函数指针
int (*open)(int pinNum); //“打开设备”函数指针
int (*close)(int pinNum); //“关闭设备”函数指针
struct Equipment* next;
};
struct Equipment* addBathroomLightToLink(struct Equipment* phead); //“浴室灯”加入设备链表函数声明
BathroomLight.c
浴室的灯
#include "equipment.h" //自定义设备类的文件
int bathroomLightInit(); //初始化继电器函数声明
int bathroomLightOpen(); //“打开灯”函数声明
int bathroomLightClose(); //“关闭灯”函数声明
struct Equipment* addBathroomLightToLink(struct Equipment* phead);
//浴室灯(对象)加入设备链表函数声明
struct Equipment bathroomLight = { //定义浴室灯(对象)
.equipName = "bathroomLight", //名字
.pinNum = 21, //树莓派 21号引脚
.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;
}
}
main.c
主函数
#include "equipment.h"
struct Equipment *findEuipment(char *Name, struct Equipment *phead)
{
struct Equipment *ptmp = phead;
if (NULL == phead)
{
return NULL;
}
else {
while (ptmp != NULL)
{
if (strcmp(ptmp->equipName,Name) == 0)
{
return ptmp;
}
ptmp = ptmp->next;
}
return NULL;
}
}
int main()
{
char Name[128];
if (wiringPiSetup() == -1) { //使用wiringPi库,需初始化硬件
printf("初始化串口失败!\n");
return -1;
}
struct Equipment *phead = NULL; //定义初始链表首端
struct Equipment *tmp;
phead = addBathroomLightToLink(phead); //“浴室灯”加入设备链表
while (1)
{
printf("输入灯名\n");
scanf("%s", Name);
tmp = findEuipment(Name,phead);
if(tmp != NULL) {
tmp->Init(tmp->pinNum);
tmp->open(tmp->pinNum);
tmp = tmp->next;
}
}
return 0;
}
然后在树莓派进行编译
输入
bathroomLight
可以看得到对应的继电器亮起来
三、增加灯的控制数量
修改头文件
#include <wiringPi.h> //wiringPi库
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Equipment //设备类
{
char equipName[128]; //设备名
int pinNum; //引脚号
int (*Init)(int pinNum); //“初始化设备”函数指针
int (*open)(int pinNum); //“打开设备”函数指针
int (*close)(int pinNum); //“关闭设备”函数指针
struct Equipment* next;
};
struct Equipment* addBathroomLightToLink(struct Equipment* phead); //“浴室灯”加入设备链表函数声明
struct Equipment* addScendfloorLightToLink(struct Equipment* phead); //“二楼灯”加入设备链表函数声明
struct Equipment* addLivingroomLightToLink(struct Equipment* phead); //“卧室灯”加入设备链表函数声明
struct Equipment* addRestaurantLightToLink(struct Equipment* phead); //“餐厅灯”加入设备链表函数声明
修改main函数
#include "equipment.h"
struct Equipment *findEuipment(char *Name, struct Equipment *phead)
{
struct Equipment *ptmp = phead;
if (NULL == phead)
{
return NULL;
}
else {
while (ptmp != NULL)
{
if (strcmp(ptmp->equipName,Name) == 0)
{
return ptmp;
}
ptmp = ptmp->next;
}
return NULL;
}
}
int main()
{
char Name[128];
if (wiringPiSetup() == -1) { //使用wiringPi库,需初始化硬件
printf("初始化串口失败!\n");
return -1;
}
struct Equipment *phead = NULL; //定义初始链表首端
struct Equipment *tmp;
phead = addBathroomLightToLink(phead); //“浴室灯”加入设备链表
phead = addScendfloorLightToLink(phead); //“二楼灯”
phead = addLivingroomLightToLink(phead); //“卧室灯”
phead = addRestaurantLightToLink(phead); //“餐厅灯”
while (1)
{
printf("输入灯名\n");
scanf("%s", Name);
tmp = findEuipment(Name,phead);
if(tmp != NULL) {
tmp->Init(tmp->pinNum);
tmp->open(tmp->pinNum);
tmp = tmp->next;
}
}
return 0;
}
添加二楼灯
#include "equipment.h" //自定义设备类的文件
int scendfloorLightInit(); //初始化继电器函数声明
int scendfloorLightOpen(); //“打开灯”函数声明
int scendfloorLightClose(); //“关闭灯”函数声明
struct Equipment* addScendfloorLightToLink(struct Equipment* phead);
//二楼灯(对象)加入设备链表函数声明
struct Equipment scendfloorLight = {
.equipName = "scendfloorLight", //名字
.pinNum = 24, //树莓派 24号引脚
.Init = scendfloorLightInit, //指定初始化函数
.open = scendfloorLightOpen, //指定“打开灯”函数
.close = scendfloorLightClose, //指定“关闭灯”函数
};
int scendfloorLightInit(int pinNum)
{
pinMode(pinNum, OUTPUT); //配置引脚为输出模式
digitalWrite(pinNum, HIGH); //引脚置高电平,断开继电器
}
int scendfloorLightOpen(int pinNum)
{
digitalWrite(pinNum, LOW); //引脚置低电平,闭合继电器
}
int scendfloorLightClose(int pinNum)
{
digitalWrite(pinNum, HIGH); //引脚置高电平,断开继电器
}
struct Equipment* addScendfloorLightToLink(struct Equipment* phead)
{
if (phead == NULL) {
return &scendfloorLight;
}
else {
scendfloorLight.next = phead;
phead = &scendfloorLight;
return phead;
}
}
添加餐厅灯
#include "equipment.h" //自定义设备类的文件
int restaurantLightInit(); //初始化继电器函数声明
int restaurantLightOpen(); //“打开灯”函数声明
int restaurantLightClose(); //“关闭灯”函数声明
struct Equipment* addRestaurantLightToLink(struct Equipment* phead);
//餐厅灯(对象)加入设备链表函数声明
struct Equipment restaurantLight = { //定义餐厅灯(对象)
.equipName = "restaurantLight", //名字
.pinNum = 23, //树莓派 23号引脚
.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;
}
}
添加卧室灯
#include "equipment.h" //自定义设备类的文件
int restaurantLightInit(); //初始化继电器函数声明
int restaurantLightOpen(); //“打开灯”函数声明
int restaurantLightClose(); //“关闭灯”函数声明
struct Equipment* addRestaurantLightToLink(struct Equipment* phead);
//餐厅灯(对象)加入设备链表函数声明
struct Equipment restaurantLight = { //定义餐厅灯(对象)
.equipName = "restaurantLight", //名字
.pinNum = 23, //树莓派 23号引脚
.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;
}
}
运行
这样我们继电器上的四个开关就全部打开了
但是现在还有问题,就是目前只能打开不能关闭,所以还需要修改。