目录
工厂模式实现烟雾报警
mainPro.c(主函数)
controlDevice.h(设备类)
smokeAlarm.c(烟雾报警器)
buzzer.c(蜂鸣器)
运行结果
线程实现
mainPro.c
智能家居目录
#include
#include
#include
#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 *smokeName = "smokeAlarm";
char *buzzerName = "buzzer";
struct Devices *tmp = NULL;
int smokeStatus; //存放“烟雾传感器”状态
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);
pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
while(1){
tmp = findDeviceByName(smokeName, pdeviceHead);
if(tmp != NULL){
tmp->Init(tmp->pinNum);
smokeStatus = tmp->readStatus(tmp->pinNum);
tmp = findDeviceByName(buzzerName, pdeviceHead);
if(tmp != NULL){
if( smokeStatus == 0 ){
tmp->Init(tmp->pinNum);
tmp->open(tmp->pinNum);
}else{
tmp->Init(tmp->pinNum);
tmp->close(tmp->pinNum);
}
}
}
}
return 0;
}
#include //wiringPi库
#include
#include
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); //“客厅灯”加入设备链表函数声明
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead); //“烟雾报警器”加入设备链表函数声明
struct Devices* addBuzzerToDeviceLink(struct Devices *phead); //“蜂鸣器”加入设备链表函数声明
#include "controlDevice.h" //自定义设备类的文件
int smokeAlarmInit(int pinNum) //C语言必须要传参,JAVA不用,可直接访问变量的值
{
pinMode(pinNum,INPUT); //配置引脚为输入模式
//digitalWrite(pinNum,HIGH); //引脚置高电平,断开继电器
}
int smokeAlarmReadStatus(int pinNum)
{
return digitalRead(pinNum);
}
int smokeAlarmStatus(int status)
{
}
struct Devices smokeAlarm = { //定义烟雾报警器(对象)
.deviceName = "smokeAlarm", //名字
.pinNum = 6, //香橙派 6号(wPi)引脚
.Init = smokeAlarmInit, //指定初始化函数
.readStatus = smokeAlarmReadStatus,
.changeStatus = smokeAlarmStatus
};
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead) //烟雾报警器(对象)加入设备链表函数
{
if(phead == NULL){
return &smokeAlarm;
}else{
smokeAlarm.next = phead; //以前的头变成.next
phead = &smokeAlarm; //更新头
return phead;
}
}
#include "controlDevice.h" //自定义设备类的文件
int buzzerInit(int pinNum)
{
pinMode(pinNum,OUTPUT); //配置引脚为输出模式
digitalWrite(pinNum,HIGH); //引脚置高电平,蜂鸣器关闭
}
int buzzerOpen(int pinNum)
{
digitalWrite(pinNum,LOW); //引脚置低电平,蜂鸣器开启
}
int buzzerClose(int pinNum)
{
digitalWrite(pinNum,HIGH); //引脚置高电平,蜂鸣器关闭
}
struct Devices buzzer = { //定义蜂鸣器(对象)
.deviceName = "buzzer", //名字
.pinNum = 9, //香橙派 9号(wpi)引脚
.Init = buzzerInit, //指定初始化函数
.open = buzzerOpen, //指定“开启蜂鸣器”函数
.close = buzzerClose, //指定“关闭蜂鸣器”函数
};
struct Devices* addBuzzerToDeviceLink(struct Devices *phead) //蜂鸣器(对象)加入设备链表函数
{
if(phead == NULL){
return &buzzer;
}else{
buzzer.next = phead;
phead = &buzzer;
return phead;
}
}
详情参考 智能家居项目整合
#include
#include "controlDevice.h"
#include "inputCommand.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;
}
}
void *smokeAlarmThread(void *data) //“烟雾报警器线程”执行的函数
{
int smokeStatus; //存放“烟雾传感器”状态
struct Devices *tmp = NULL;
while(1){
tmp = findDeviceByName("smokeAlarm", pdeviceHead);
if(tmp != NULL){
tmp->Init(tmp->pinNum);
smokeStatus = tmp->readStatus(tmp->pinNum);
tmp = findDeviceByName("buzzer", pdeviceHead);
if(tmp != NULL){
if( smokeStatus == 0 ){
tmp->Init(tmp->pinNum);
tmp->open(tmp->pinNum);
}else{
tmp->Init(tmp->pinNum);
tmp->close(tmp->pinNum);
}
}
}
}
}
int main()
{
if (wiringPiSetup () == -1) {
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
pthread_t smokeAlarm_thread;
//设备控制工厂初始化
//pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
//pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
//pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
//pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);
pthread_create(&smokeAlarm_thread,NULL,smokeAlarmThread,NULL);
pthread_join(smokeAlarm_thread, NULL); //主函数等待线程退出
return 0;
}
智能家居(1) —— 工厂模式引入&工厂模式实现继电器控制
智能家居(2) —— 工厂模式实现烟雾报警
智能家居(3) —— 串口通信(语音识别)线程控制
智能家居(4) —— 网络服务器线程控制
智能家居(5) —— 智能家居项目整合(语音控制线程,网络控制线程、烟雾报警线程)
网络编程知识预备(1) —— 7层OSI网络模型
网络编程知识预备(2) —— 三次握手与四次挥手、半连接状态、2MSL
网络编程知识预备(3) —— TCP流量控制(滑动窗口)、拥塞控制
网络编程知识预备(4) —— SOCKET、TCP、HTTP之间的区别与联系
网络编程知识预备(5) —— 了解应用层的HTTP协议与HTTPS协议
网络编程知识预备(6) —— libcurl库简介及其编程访问百度首页
智能家居(6) —— 香橙派摄像头安装实现监控功能
智能家居(7) —— 人脸识别 & 翔云平台编程使用(编译openSSL支持libcurl的https访问、安装SSL依赖库openSSL)
智能家居(8) —— 香橙派摄像头加入设备工厂