通俗理解的步骤就是链表通用模板定义(在头文件里定义)、链表的创建(头插尾插,在.C 文件里)、链表的初始化(init配置管脚初始电平等)、链表内容的读取(指令工厂TCP服务端读取客户端发来的指令、串口读取语音模块发来的指令)、链表的遍历查找(找到控制工厂里面需要被控制的链表节点)、链表内容的写入(写入来自指令工厂的控制指令)。

接收的指令,可以放在临时创建的变量里面,或者创建一个本地文件fd保存,或者一开始创建链表通用模板里面定义一个存放接收指令的数组,这样更方便访问。

继电器控制灯代码

contrlEquipments.h 文件(设备类)

#include <wiringPi.h>                  //wiringPi库
#include <stdio.h>
#include <stdlib.h>struct Equipment                       //设备类
{char equipName[128];               //设备名int pinNum;                            //引脚号int (*Init)(int pinNum);           //“初始化设备”函数指针int (*open)(int pinNum);           //“打开设备”函数指针int (*close)(int pinNum);           //“关闭设备”函数指针int (*readStatus)(int pinNum);      //“读取设备状态”函数指针  为火灾报警器准备int (*changeStatus)(int status);    //“改变设备状态”函数指针struct Equipment *next;
};struct Equipment *addBathroomLightToEquipmentLink(struct Equipment *phead);           //“浴室灯”加入设备链表函数声明
struct Equipment *addSecondfloorLightToEquipmentLink(struct Equipment *phead);      //“二楼灯”加入设备链表函数声明
struct Equipment *addLivingroomLightToEquipmentLink(struct Equipment *phead);       //“客厅灯”加入设备链表函数声明
struct Equipment *addRestaurantLightToEquipmentLink(struct Equipment *phead);       //“餐厅灯”加入设备链表函数声明

mainPro.c 文件(主函数)

#include <stdio.h>
#include <string.h>
#include "contrlEquipments.h"int main()
{if(wiringPiSetup() == -1){                               //使用wiringPi库,需初始化硬件               printf("wiringPiSetup failed!\n");return -1; }struct Equipment *pequipHead = NULL;                           //定义初始链表头pequipHead = addBathroomLightToEquipmentLink(pequipHead );        //“浴室灯”加入设备链表pequipHead = addSecondfloorLightToEquipmentLink(pequipHead );     //“二楼灯”加入设备链表pequipHead = addLivingroomLightToEquipmentLink(pequipHead );      //“客厅灯”加入设备链表pequipHead = addRestaurantLightToEquipmentLink(pequipHead );      //“餐厅灯”加入设备链表struct Equipment *tmp = pequipHead ;while(tmp != NULL){                    //继电器功能测试,打开链表上所有灯tmp->Init(tmp->pinNum);         //先初始化tmp->open(tmp->pinNum);tmp = tmp->next;}return 0;
}

bathroomLight.c 文件(浴室灯)

#include "contrlEquipments.h"          //自定义设备类的文件int bathroomLightInit();     //初始化继电器函数声明
int bathroomLightOpen();        //“打开灯”函数声明
int bathroomLightClose();       //“关闭灯”函数声明
//struct Equipment *addBathroomLightToEquipmentLink(struct Equipment *phead);
//浴室灯(对象)加入设备链表函数声明struct Equipment bathroomLight = {            //定义浴室灯(对象).equipName = "bathroomLight",           //名字.pinNum = 2,                           //树莓派 2号(wPi)引脚.Init = bathroomLightInit,              //指定初始化函数.open = bathroomLightOpen,                //指定“打开灯”函数.close = bathroomLightClose             //指定“关闭灯”函数
};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);             //引脚置高电平,断开继电器
}struct Equipment *addBathroomLightToEquipmentLink(struct Equipment *phead)     //浴室灯(对象)加入设备链表函数
{if(phead == NULL){return &bathroomLight;}else{bathroomLight.next = phead;  //以前的头变成.nextphead = &bathroomLight;      //更新头return phead;}
}

secondfloorLight.c 文件(二楼灯)

#include "contrlEquipments.h"              //自定义设备类的文件int secondfloorLightInit();          //初始化继电器函数声明
int secondfloorLightOpen();         //“打开灯”函数声明
int secondfloorLightClose();        //“关闭灯”函数声明
//struct Equipment *addSecondfloorLightToLink(struct Equipment *phead);
//二楼灯(对象)加入设备链表函数声明struct Equipment secondfloorLight = {         //定义二楼灯(对象).equipName = "secondfloorLight",            //名字.pinNum = 4,                               //树莓派 4号(wPi)引脚.Init = secondfloorLightInit,               //指定初始化函数.open = secondfloorLightOpen,             //指定“打开灯”函数.close = secondfloorLightClose,             //指定“关闭灯”函数
};int secondfloorLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);                        //配置引脚为输出模式digitalWrite(pinNum,HIGH);                   //引脚置高电平,断开继电器
}int secondfloorLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);                  //引脚置低电平,闭合继电器
}int secondfloorLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);                 //引脚置高电平,断开继电器
}struct Equipment *addSecondfloorLightToEquipmentLink(struct Equipment *phead)      //二楼灯(对象)加入设备链表函数
{if(phead == NULL){return &secondfloorLight;}else{secondfloorLight.next = phead;phead = &secondfloorLight;return phead;}
}

livingroomLight.c 文件(客厅灯)

#include "contrlEquipments.h"              //自定义设备类的文件int livingroomLightInit();           //初始化继电器函数声明
int livingroomLightOpen();          //“打开灯”函数声明
int livingroomLightClose();         //“关闭灯”函数声明
//struct Equipment *addLivingroomLightToLink(struct Equipment *phead);
//客厅灯(对象)加入设备链表函数声明struct Equipment livingroomLight = {          //定义客厅灯(对象).equipName = "livingroomLight",             //名字.pinNum = 1,                               //树莓派 1号(wPi)引脚.Init = livingroomLightInit,                //指定初始化函数.open = livingroomLightOpen,              //指定“打开灯”函数.close = livingroomLightClose,              //指定“关闭灯”函数
};int livingroomLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);                        //配置引脚为输出模式digitalWrite(pinNum,HIGH);                   //引脚置高电平,断开继电器
}int livingroomLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);                  //引脚置低电平,闭合继电器
}int livingroomLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);                 //引脚置高电平,断开继电器
}struct Equipment *addLivingroomLightToEquipmentLink(struct Equipment *phead)       //客厅灯(对象)加入设备链表函数
{if(phead == NULL){return &livingroomLight;}else{livingroomLight.next = phead;phead = &livingroomLight;return phead;}
}

restaurantLight.c 文件(餐厅灯)

#include "contrlEquipments.h"              //自定义设备类的文件int restaurantLightInit();           //初始化继电器函数声明
int restaurantLightOpen();          //“打开灯”函数声明
int restaurantLightClose();         //“关闭灯”函数声明
struct Equipment *addRestaurantLightToLink(struct Equipment *phead);
//餐厅灯(对象)加入设备链表函数声明struct Equipment restaurantLight = {          //定义餐厅灯(对象).equipName = "restaurantLight",             //名字.pinNum = 3,                               //树莓派 3号(wPi)引脚.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 *addRestaurantLightToEquipmentLink(struct Equipment *phead)       //餐厅灯(对象)加入设备链表函数
{if(phead == NULL){return &restaurantLight;}else{restaurantLight.next = phead;phead = &restaurantLight;return phead;}
}

编译运行:

继电器四个灯都亮了

由于mainPro.c文件中指定继电器功能测试,打开链表上所有灯,可以发现继电器组全部亮红灯(如下图所示)。也可以将实际的家居灯接入到继电器中实现控制灯光的效果。

通过名字开关灯

其他代码和上面的代码一样,就mainPro.c修改了一下

mainPro.c代码如下:

#include <stdio.h>
#include <string.h>
#include "contrlEquipments.h"struct Equipment* findDeviceByName(char *name, struct Equipment *pdeviceHead)
{while(pdeviceHead != NULL){if(strcmp(name, pdeviceHead->equipName) == 0){printf("找到了\n");return pdeviceHead;}pdeviceHead = pdeviceHead->next;}printf("没找到\n");return NULL;
}int main()
{struct Equipment *rcv = NULL;char name[100];if(wiringPiSetup() == -1){                                                              //使用wiringPi库,需初始化硬件printf("wiringPiSetup failed!\n");return -1;}struct Equipment *pequipHead = NULL;                                          //定义初始链表头pequipHead = addBathroomLightToEquipmentLink(pequipHead );        //“浴室灯”加入设备链表pequipHead = addSecondfloorLightToEquipmentLink(pequipHead );     //“二楼灯”加入设备链表pequipHead = addLivingroomLightToEquipmentLink(pequipHead );      //“客厅灯”加入设备链表pequipHead = addRestaurantLightToEquipmentLink(pequipHead );      //“餐厅灯”加入设备链表struct Equipment *tmp = pequipHead ;while(tmp != NULL){                                     //继电器功能测试,关闭链表上所有灯tmp->Init(tmp->pinNum);         //先初始化tmp->close(tmp->pinNum);tmp = tmp->next;}printf("please input device's name\n");scanf("%s",name);rcv = findDeviceByName(name,pequipHead);rcv->open(rcv->pinNum);return 0;
}

编译运行:

智能家居工厂模式整体设计框架控制设备测试相关推荐

  1. 智能家居 (3) ——智能家居工厂模式介绍实现继电器控制灯

    目录 智能家居工厂模式整体设计框架 继电器控制灯代码 contrlEquipments.h 文件(设备类) mainPro.c 文件(主函数) bathroomLight.c 文件(浴室灯) seco ...

  2. 智能家居计算机控制系统的设计,智能家居控制系统设计知识分享.pdf

    智能家居控制系统设计 智能家居控制系统 专业:电气工程及其自动化 姓名:孟凡磊 指导教师:杨伟新 摘 要 伴随世界信息化水平不断提升, 越来越多的人对生活环境尤其是家 居环境要求随之提升,人们希望自己 ...

  3. 智能家居安防整体解决方案

    对于有些人来说,智能家居只是增添华丽的装饰.高端的电器和昂贵的陈设.但对于另部分人来说,智能家居则意味着利用最新的控制科技提高个人生活品味的同时,提高家居安全防范能力. 我们可以称之为"智能 ...

  4. 智能家居弱电布线设计需要注意什么

    智能家居犹如"一夜成名"般,迎来一个火爆的智能家居潮流.因此,每天需要施工的智能家居综合布线项目只多不会少,那么在我们进行智能家居弱电布线设计环节时,有什么需要注意的的呢?北京综合 ...

  5. 智能家居是什么,设计智能家居系统需要遵守哪些原则

    简单来说,就是用户可以通过手机.智能面板等终端,对音视频设备.照明系统.窗帘控制系统.空调控制系统.安防系统.数字影院系统等进行联网和集中智能控制,从而提升生活舒适度.降低能源消耗的智能化系统. 智能 ...

  6. 智能云工厂模式普惠中小企业,千鸟互联或成纸包装产业链“带头大哥”

    从废纸回收与线上交易平台,到纸包装闭环供应链交易平台,再到聚合中小企业闲置产能,成为具有实质生产能力的纸包装"大工厂".自2017年创立至今,短短5年时间里,千鸟互联完成跨越式的三 ...

  7. 智能家居物联网服务平台设计-论文

    智能家居物联网服务平台设计 摘要 随着物联网.大数据.云计算等技术的发展成熟,推动了物联网应用的蓬勃发展,智能家居作为物联网技术的一个重要应用领域,近几年来得到了广泛的研究,也出现了大量的应用产品.目 ...

  8. android控制中心实现,基于Android平台的智能家居系统控制中心的设计与实现

    摘要: 随着科技的发展,人民生活水平的提高,人们对住宅的要求也越来越高,智能家居就是为解决人们的这一要求诞生的.自1984年第一栋智能建筑诞生以来,世界上出现了很多智能家居解决方案,但都或多或少的存在 ...

  9. ZigBee智能家居安防硬件设计

    0 引 言 在信息产业快速发展的今天,嵌入式设备已经深入到了人类生活的各个方面,智能设备更是成为了人们日常生活中不可或缺的一部分.随着能源的日渐消耗,环保节能刻不容缓. 因此,在现在和未来的家居生活环 ...

最新文章

  1. ktor框架用到了netty吗_教你如何构建异步服务器和客户端的 Kotlin 框架 Ktor
  2. 热门专业没那么难,文科生打开统计学的正确方式!
  3. 2020 ccf推荐中文期刊_CCF推荐国际学术期刊
  4. 基于websocket的聊天实现逻辑(springboot)
  5. 面向对象的三个基本特征(讲解)-转载
  6. iframe之间操作记录
  7. C# Win32API
  8. 群晖安装pip3 模块并启用http server |NS DBI HOMESERVER
  9. 移动办公OA系统选型的任何疑问,这篇文章帮您搞定!
  10. TR069协议与商业应用6——TR069动态库开发
  11. jsp来实现 验证码 登录案例 有图 有码
  12. 从零实现“百度网盘批量重命名”工具
  13. 从Eclipse官网查找自己要的插件地址 来安装
  14. element 表格全局筛选(筛选结果请求后端接口)
  15. Vivo手机安装谷歌Play商店,安装服务框架谷歌Google,支持X90,X80,X70,X60,s系列,IQOO
  16. 关于Maven中pom文件标签的详解,分别对比父工程pom文件与子工程pom文件。
  17. 用Python写个空课表生成器-Excel文件操作实例
  18. visio使用小知识
  19. string转换long
  20. 详解如何使用VS code搭建JavaScript环境(适合小白)

热门文章

  1. 基于微信小程序网上商城、小程序商城毕业设计论文 课题题目参考(1)前台功能和界面
  2. ubuntu16.04安装g2o
  3. 阿里云ACA 使用时间序列分解模型预测商品销量(一)
  4. 使用itextpdf实现截取pdf文档第几页到第几页,进行分片
  5. 草根创业秘诀:如何在3月内单月出货10万元
  6. Windows 权限的继承性、累加性、优先性、交叉性和四项基本原则
  7. 华为文稿演示服务器操作异常修复,服务器日志怎么修复
  8. Java 字符串类型
  9. 合工大计算机专业拟录取名单,2021年合肥工业大学各学院研究生拟录取名单汇总...
  10. 网红、科技大佬、作家,跨界喜剧王罗永浩才是打造个人IP的典范