【智能家居】五、智能家居项目整合(语音控制线程,网络控制线程、烟雾报警线程)

智能家居项目整合(语音控制线程,网络控制线程、烟雾报警线程)
一、主函数

  • mian.c

二、指令工厂

  • inputCommand.h(输入控制指令)
  • voiceControl.c(语音控制模块指令)
  • socketControl.c(socket网络控制指令)

三、设备工厂

  • bathroomLight.c(浴室灯)
  • bedroomLight.c(卧室灯)
  • restaurantLight.c(餐厅灯)
  • livingroomLight.c(客厅灯)
  • smokeAlarm.c(烟雾报警器)
  • buzzer.c(蜂鸣器)
  • controlDevice.h(控制设备)

四、编译运行
五、视频展示

五、视频展示

智能家居项目整合(语音控制线程,网络控制线程、烟雾报警线程)

一、主函数

mian.c

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include "controlDevice.h"
#include "inputCommand.h"

struct InputCommand *pcommandHead = NULL;	//定义指令工厂初始链表头,用于存储各种输入指令的信息。
struct Devices      *pdeviceHead  = NULL;	//定义设备工厂初始链表头,用于存储各种设备的信息。
struct InputCommand *socketHandler = NULL;	//定义处理 socket 控制的指令对象,用于管理网络控制。

// 按名称查找设备
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;
    }
}

// 查找指定名字的输入指令对象
struct InputCommand* findCommandByName(char *name, struct InputCommand *phead) 
{
    struct InputCommand *tmp = phead;

    if (phead == NULL) {
        return NULL;
    } 
	else {
        while (tmp != NULL) {
            if (strcmp(tmp->commandName, name) == 0) {
                return tmp;
            }
            tmp = tmp->next;
        }
        return NULL;
    }
}

// 根据指令执行相应操作
void Command(struct InputCommand *CmdHandler) 
{
    struct Devices *tmp = NULL;

    // CSHALL 指令:初始化所有设备
    if (strcmp("CSHALL", CmdHandler->command) == 0) {
        tmp = findDeviceByName("smokeAlarm", pdeviceHead);
        if (tmp != NULL)  tmp->Init(tmp->pinNum);
        tmp = findDeviceByName("buzzer", pdeviceHead);
        if (tmp != NULL)  tmp->Init(tmp->pinNum);
        tmp = findDeviceByName("livingroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->Init(tmp->pinNum);
        tmp = findDeviceByName("restaurantLight", pdeviceHead);
        if (tmp != NULL)  tmp->Init(tmp->pinNum);
        tmp = findDeviceByName("bedroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->Init(tmp->pinNum);
        tmp = findDeviceByName("bathroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->Init(tmp->pinNum);
        printf("设备已全部初始化\n");
    }

    // OL1 指令:打开客厅灯
    if (strcmp("OL1", CmdHandler->command) == 0) {
        tmp = findDeviceByName("livingroomLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->open(tmp->pinNum);
            printf("已打开客厅灯\n");
        }
    }

    // CL1 指令:关闭客厅灯
    if (strcmp("CL1", CmdHandler->command) == 0) {
        tmp = findDeviceByName("livingroomLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->close(tmp->pinNum);
            printf("已关闭客厅灯\n");
        }
    }

	// OL2 指令:打开餐厅灯
    if (strcmp("OL2", CmdHandler->command) == 0){
		tmp = findDeviceByName("restaurantLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->open(tmp->pinNum);
            printf("已打开餐厅灯\n");
        }
	}

	// CL2 指令:关闭餐厅灯
    if (strcmp("CL2", CmdHandler->command) == 0) {
		tmp = findDeviceByName("restaurantLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->close(tmp->pinNum);
            printf("已关闭餐厅灯\n");
        }
	}

	// OL3 指令:打开卧室灯
    if (strcmp("OL3", CmdHandler->command) == 0) {
		tmp = findDeviceByName("bedroomLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->open(tmp->pinNum);
            printf("已打开卧室灯\n");
        }
	}

	// CL3 指令:关闭卧室灯
    if (strcmp("CL3", CmdHandler->command) == 0) {
		tmp = findDeviceByName("bedroomLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->close(tmp->pinNum);
            printf("已关闭卧室灯\n");
        }
	}

	// OL4 指令:打开浴室灯
    if (strcmp("OL4", CmdHandler->command) == 0) {
		tmp = findDeviceByName("bathroomLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->open(tmp->pinNum);
            printf("已打开浴室灯\n");
        }
	}

	// CL4 指令:关闭浴室灯
    if (strcmp("CL4", CmdHandler->command) == 0) {
		tmp = findDeviceByName("bathroomLight", pdeviceHead);
        if (tmp != NULL) {
            tmp->close(tmp->pinNum);
            printf("已关闭浴室灯\n");
        }
	}

    // OLALL 指令:打开所有灯
    if (strcmp("OLALL", CmdHandler->command) == 0) {
        tmp = findDeviceByName("livingroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->open(tmp->pinNum);
        tmp = findDeviceByName("restaurantLight", pdeviceHead);
        if (tmp != NULL)  tmp->open(tmp->pinNum);
        tmp = findDeviceByName("bedroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->open(tmp->pinNum);
        tmp = findDeviceByName("bathroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->open(tmp->pinNum);
        printf("已打开所有灯\n");
    }

    // CLALL 指令:关闭所有灯
    if (strcmp("CLALL", CmdHandler->command) == 0) {
        tmp = findDeviceByName("livingroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->close(tmp->pinNum);
        tmp = findDeviceByName("restaurantLight", pdeviceHead);
        if (tmp != NULL)  tmp->close(tmp->pinNum);
        tmp = findDeviceByName("bedroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->close(tmp->pinNum);
        tmp = findDeviceByName("bathroomLight", pdeviceHead);
        if (tmp != NULL)  tmp->close(tmp->pinNum);
        printf("已关闭所有灯\n");
    }
}

// 语音控制线程执行函数
void *voiceControlThread(void *data) 
{
    int nread;
    struct InputCommand *voiceHandler = NULL;
    
    // 找到语音控制的指令处理对象
    voiceHandler = findCommandByName("voice", pcommandHead);

    if (voiceHandler == NULL) {
        printf("find voiceHandler error\n");
        pthread_exit(NULL);
    } 
	else {
        // 初始化语音控制
        if (voiceHandler->Init(voiceHandler) < 0) {
            printf("voiceControl init error\n");
            pthread_exit(NULL);
        } 
		else {
            printf("voiceControl init success\n");
        }

        // 不断读取语音指令并执行相应操作
        while (1) {
            memset(voiceHandler->command, '\0', sizeof(voiceHandler->command));
            nread = voiceHandler->getCommand(voiceHandler);

            if (nread == 0) {
                // 没有获取到语音指令
                printf("No voiceCommand received\n");
            } 
			else {
                // 获取到语音指令,执行相应操作
                printf("Get VoiceCommand -->%s\n", voiceHandler->command);
                Command(voiceHandler);
            }
        }
    }
}


// 读取 TCP 端口指令线程执行函数
void *socketReadThread(void *data) 
{
    int n_read;
    printf("Connect success\n");

    // 不断读取 TCP 端口指令并执行相应操作
    while (1) {
        memset(socketHandler->command, '\0', sizeof(socketHandler->command));
        n_read = read(socketHandler->fd, socketHandler->command, sizeof(socketHandler->command));

        if (n_read == -1) {
            // 读取错误
            perror("read:");
        } 
		else {
            // 读取成功,打印获取到的 TCP 端口指令,并执行相应操作
            printf("Get SocketCommand-->%s\n", socketHandler->command);
            Command(socketHandler);
        }
    }
}


// 网络控制线程执行函数
void *socketControlThread(void *data) 
{
    int c_fd;                           // 客户端文件描述符
    struct sockaddr_in c_addr;
    memset(&c_addr, 0, sizeof(struct sockaddr_in));
    int clen = sizeof(struct sockaddr_in);

    pthread_t socketRead_thread; // 用于读取TCP端口指令的线程

    // 获取“socket”指令处理器
    socketHandler = findCommandByName("socket", pcommandHead);

    if (socketHandler == NULL) {
        // 未找到“socket”指令处理器,打印错误信息并退出线程
        printf("find socketHandler error\n");
        pthread_exit(NULL);
    }

    if (socketHandler->Init(socketHandler) < 0) {   // 初始化“网络控制”功能
        // “网络控制”功能初始化失败,打印错误信息并退出线程
        printf("socketControl init error\n");
        pthread_exit(NULL);
    } 
	else {
        // “网络控制”功能初始化成功,打印成功信息
        printf("socketControl init success\n");
    }

    while (1) {
        // 4. accept,接收连接请求,阻塞至有客户端完成三次握手
        c_fd = accept(socketHandler->s_fd, (struct sockaddr *)&c_addr, &clen);

        // 将客户端套接字描述符返回给“网络控制”链表节点
        socketHandler->fd = c_fd;

        // 创建新线程:用于读取TCP端口指令
        pthread_create(&socketRead_thread, NULL, socketReadThread, NULL);
    }
}

// 烟雾报警器线程执行函数
void *smokeAlarmThread(void *data) 
{
    int smokeStatus; // 存放“烟雾传感器”状态
    struct Devices *tmp = NULL;

    while (1) {
        // 读取烟雾传感器状态
        tmp = findDeviceByName("smokeAlarm", pdeviceHead);
        if (tmp != NULL) {
            smokeStatus = tmp->readStatus(tmp->pinNum);
            delay(100);
            tmp = findDeviceByName("buzzer", pdeviceHead);

            // 根据烟雾传感器状态控制蜂鸣器
            if (tmp != NULL) {
                if (smokeStatus == 0) {
                    tmp->open(tmp->pinNum);
                } 
				else {
                    tmp->close(tmp->pinNum);
                }
            }
        }
    }
}

int main()
{
	// 初始化wiringPi库
	if (wiringPiSetup() == -1) {
		fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
		return 1;
	}

	pthread_t voiceControl_thread;   // 语音控制线程
	pthread_t socketControl_thread;  // 网络控制线程
	pthread_t smokeAlarm_thread;     // 烟雾报警线程

    // 1. 指令工厂初始化
    pcommandHead = addVoiceControlToInputCommandLink(pcommandHead);
    pcommandHead = addSocketControlToInputCommandLink(pcommandHead);

	// 2. 设备控制工厂初始化
	// “浴室灯”加入设备链表
	pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
	// “卧室灯”加入设备链表
	pdeviceHead = addBedroomLightToDeviceLink(pdeviceHead);
	// “餐厅灯”加入设备链表
	pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
	// “客厅灯”加入设备链表
	pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
	// 新增的设备:烟雾报警器
	pdeviceHead = addSmokeAlarmToDeviceLink(pdeviceHead);
	// 新增的设备:蜂鸣器
    pdeviceHead = addBuzzerToDeviceLink(pdeviceHead);

    // 3. 线程池建立
    // 3.1 语音线程
    pthread_create(&voiceControl_thread, NULL, voiceControlThread, NULL);
    // 3.2 socket线程
    pthread_create(&socketControl_thread, NULL, socketControlThread, NULL);
    // 3.3 烟雾报警线程
    pthread_create(&smokeAlarm_thread, NULL, smokeAlarmThread, NULL);
    // 3.4 摄像头线程(预留接口后续加入)

    // 主函数等待线程退出
    pthread_join(voiceControl_thread, NULL);
    pthread_join(socketControl_thread, NULL);
    pthread_join(smokeAlarm_thread, NULL);

	return 0;
}

二、指令工厂

inputCommand.h(输入控制指令)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>      
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringSerial.h>
 
struct InputCommand
{
    char commandName[128];                              //“控制方式”名
    char deviceName[128];               				//“设备工厂”名
    char command[32];                                   //存放指令
    int fd;                                             //存放文件描述符(串口/网络)
    int s_fd;									        //存放服务器套接字描述符
    char port[12];								        //存放端口号
    char ipAdress[32];							        //存放 IP地址
    char log[1024];                                     //日志
    int (*Init)(struct InputCommand *voice);            //“初始化”函数指针
    int (*getCommand)(struct InputCommand *voice);      //“获取指令”函数指针
 
    struct InputCommand *next;
};
 
struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead);	//“语音控制”加入指令链表函数声明

struct InputCommand* addSocketControlToInputCommandLink(struct InputCommand *phead); //“网络控制”加入指令链表函数声明

voiceControl.c(语音控制模块指令)

#include "inputCommand.h"
 
// 语音控制模块初始化函数
int voiceInit(struct InputCommand *voice)
{
    int fd;

	// 打开串口设备 (voice->deviceName),波特率为 115200
    if ((fd = serialOpen (voice->deviceName, 115200)) < 0) { 
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; 
        return 1 ; 
    }
    voice->fd = fd; // 将文件描述符存储在 voice->fd 中

    return fd;
}

// 从语音控制模块获取指令的函数 
int voiceGetCommand(struct InputCommand *voice)
{
    int nread = 0;

	// 从串口 (voice->fd) 读取数据到 voice->command 中
    nread = read(voice->fd, voice->command, sizeof(voice->command));
	//返回读取到数据的字节数,实际读取的指令放到了command里
    return nread;            
}

// 全局变量,表示语音控制的输入命令对象 
struct InputCommand voiceControl = {
    .commandName = "voice",
    .deviceName = "/dev/ttyS5",
    .command = '\0',
    .Init = voiceInit,
    .getCommand = voiceGetCommand,
    .log = {'\0'},
    .next = NULL
};

// 将语音控制对象加入到输入命令链表中的函数 
struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead) //“语音控制”(对象)加入指令方式链表函数
{
	if (phead == NULL) {
		return &voiceControl;
	}else{
		voiceControl.next = phead;
		phead = &voiceControl;

		return phead; // 如果链表不为空,将语音控制对象插入到链表头,并返回链表头的指针
	}
}

socketControl.c(socket网络控制指令)

#include "inputCommand.h"
 
// 套接字初始化函数 
int socketInit(struct InputCommand *socketMsg)
{
    int s_fd;                                       //套接字描述符
	struct sockaddr_in s_addr;
	memset(&s_addr, 0, sizeof(struct sockaddr_in));   //数据清空
 
    // 1. 创建套接字,ipv4,tcp协议
	s_fd=socket(AF_INET, SOCK_STREAM, 0);
	if (s_fd == -1) {
		perror("socket:");
		exit(-1);
	}
 
    s_addr.sin_family = AF_INET;                        //ipv4
	s_addr.sin_port = htons(atoi(socketMsg->port));     //端口号,选择5000以上。honts返回网络字节序,atoi(argv[2])防止端口被占用
	inet_aton(socketMsg->ipAdress, &s_addr.sin_addr);    //转换为网络能识别的格式
 
    // 2. 绑定
	bind(s_fd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in));
 
    // 3. 监听
	listen(s_fd, 10); //监听10个连接
    printf("socket Server listening ...\n");
 
    socketMsg->s_fd = s_fd;	// 保存套接字描述符

    return s_fd;
}
 
// 全局变量,表示 socket 控制的输入命令对象
struct InputCommand socketControl = {
    .commandName = "socket",
    .command = '\0',
    .port = "8080",
    .ipAdress = "192.168.1.254",
    .Init = socketInit,
    .log = {'\0'},
    .next = NULL
};

// 将 socket 控制对象加入到输入命令链表中的函数
struct InputCommand* addSocketControlToInputCommandLink(struct InputCommand *phead)
{
	if (phead == NULL) {
		return &socketControl;
	}
	else {
		socketControl.next = phead;
		phead = &socketControl;
		
		return phead;
	}
}

三、设备工厂

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;
	}
}

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;
	}
}

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

四、编译运行

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咖喱年糕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值