一、用户手册对应的I/O
二、工厂模式实现继电器灯控制
三、代码段
- controlDevice.h(设备类)
- main.c(主函数)
- bathroomLight.c(浴室灯)
- bedroomLight.c(卧室灯)
- bedroomLight.c(卧室灯)
- livingroomLight.c(客厅灯)
- 编译
- 运行结果
一、用户手册对应的I/O
二、工厂模式实现继电器灯控制
三、代码段
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); //“浴室灯”加入设备链表函数声明 2
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead); //“卧室灯”加入设备链表函数声明 8
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead); //“餐厅灯”加入设备链表函数声明 13
struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead); //“客厅灯”加入设备链表函数声明 16
main.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;
// 初始化wiringPi库
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;
}
bathroomLight.c(浴室灯)
#include "controlDevice.h" //自定义设备类的文件
int bathroomLightInit(int pinNum) //C语言必须要传参,JAVA不用,可直接访问变量的值
{
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 = 2, //香橙派 2号(wPi)引脚
.Init = bathroomLightInit, //指定初始化函数
.open = bathroomLightOpen, //指定“打开灯”函数
.close = bathroomLightClose, //指定“关闭灯”函数
.changeStatus = bathroomLightStatus
};
struct Devices* addBathroomLightToDeviceLink(struct Devices *phead) //浴室灯(对象)加入设备链表函数
{
if (phead == NULL) {
return &bathroomLight;
}
else {
bathroomLight.next = phead; //以前的头变成.next
phead = &bathroomLight; //更新头
return phead;
}
}
bedroomLight.c(卧室灯)
#include "controlDevice.h"
int bedroomLightInit(int pinNum) //C语言必须要传参,JAVA不用,可直接访问变量的值
{
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 = 8, //香橙派 8号(wPi)引脚
.Init = bedroomLightInit, //指定初始化函数
.open = bedroomLightOpen, //指定“打开灯”函数
.close = bedroomLightClose, //指定“关闭灯”函数
.changeStatus = bedroomLightStatus
};
struct Devices* addBedroomLightToDeviceLink(struct Devices *phead) //卧室灯(对象)加入设备链表函数
{
if (phead == NULL) {
return &bedroomLight;
}
else {
bedroomLight.next = phead; //以前的头变成.next
phead = &bedroomLight; //更新头
return phead;
}
}
restaurantLight.c(餐厅灯)
#include "controlDevice.h" //自定义设备类的文件
int restaurantLightInit(int pinNum) //C语言必须要传参,JAVA不用,可直接访问变量的值
{
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 = 13, //香橙派 13号(wPi)引脚
.Init = restaurantLightInit, //指定初始化函数
.open = restaurantLightOpen, //指定“打开灯”函数
.close = restaurantLightClose, //指定“关闭灯”函数
.changeStatus = restaurantLightStatus
};
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead) //餐厅灯(对象)加入设备链表函数
{
if (phead == NULL) {
return &restaurantLight;
}
else {
restaurantLight.next = phead; //以前的头变成.next
phead = &restaurantLight; //更新头
return phead;
}
}
livingroomLight.c(客厅灯)
#include "controlDevice.h" //自定义设备类的文件
int livingroomLightInit(int pinNum) // C语言必须要传参,JAVA不用,可直接访问变量的值
{
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 = 16, // 香橙派 16号(wPi)引脚
.Init = livingroomLightInit, // 指定初始化函数
.open = livingroomLightOpen, // 指定“打开灯”函数
.close = livingroomLightClose, // 指定“关闭灯”函数
.changeStatus = livingroomLightStatus};
struct Devices *addLivingroomLightToDeviceLink(struct Devices *phead) // 客厅灯(对象)加入设备链表函数
{
if (phead == NULL) {
return &livingroomLight;
}
else {
livingroomLight.next = phead; // 以前的头变成.next
phead = &livingroomLight; // 更新头
return phead;
}
}
编译
gcc *.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt
运行结果