可燃气体传感器 MQ-2 和 蜂鸣器
代码段
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
struct Devices* addSmokeAlarmToDeviceLink(struct Devices *phead); //“烟雾报警器”加入设备链表函数声明 6
struct Devices* addBuzzerToDeviceLink(struct Devices *phead); //“蜂鸣器”加入设备链表函数声明 9
smokeAlarm.c(烟雾报警器)
#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;
}
}
buzzer.c(蜂鸣器)
#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;
}
}
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 *smokeName = "smokeAlarm";
char *buzzerName = "buzzer";
struct Devices *tmp = NULL;
int smokeStatus; //存放“烟雾传感器”状态
if (wiringPiSetup () == -1) {
fprintf (stdout, "Unable to start wiringPi: %sn", 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;
}
模块测试
原文地址:https://blog.csdn.net/m0_62140641/article/details/134705303
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_15925.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。