智能家居-基于香橙派zreo2——手把手搭建、继电器控制灯(一)
一、什么是设计模式
设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的
设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。
使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码可靠性。
二、类和对象
类是面向对象程序设计实现信息封装的基础。类是一种用户定义的引用数据类型(结构体),也称类类型。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例称为对象。
struct Animal{int age;int sex; //成员属性void *peat();void *pbeat();//成员方法
}
对象:类的实例称为对象
struct Animal dog;
struct Animal cat;
三、工厂模式
工厂模式(Factory Pattern)是 最常用的设计模式之一。
这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
工厂模式的实现
————————————————
版权声明:本文为CSDN博主「大头1213」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Zy_1213/article/details/126679238
四、工厂模式实现继电器控制所有灯(自己的代码)
新建文件夹取名smartHouse,新建文件如下
在Source Insight里建立项目
主函数Mainpro.c
#include <stdio.h>
#include <string.h>
#include "controlDevice.h"struct Device *findDeviceByName(char *name,struct Device *phead)
{struct Device* 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()
{if(wiringPiSetup() == -1){printf("set up error!\n");return -1;}char *name = "restaurant";struct Device *pDeviceHead = NULL;pDeviceHead = addrBathroomLight(pDeviceHead);pDeviceHead = addrlivingRoomLight(pDeviceHead);pDeviceHead = addrrestaurantLight(pDeviceHead);pDeviceHead = addrsecondfloorLight(pDeviceHead);struct Device *tmp = NULL;char nn[128];while(1){printf("Input:\n");scanf("%s",nn);tmp = findDeviceByName(nn,pDeviceHead);if(tmp != NULL){tmp->DeviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}}return 0;
}
设备管理controlDevice.h
#include <wiringPi.h>
#include <stdlib.h>struct Device
{char DeviceName[128];int status;int pinNum;int (*open)(int pinNum);int (*close)(int pinNum);int (*DeviceInit)(int pinNum);int (*readStatus)();int (*changeStatus)();struct Device *next;
};struct Device *addrBathroomLight(struct Device *phead);
struct Device *addrlivingRoomLight(struct Device *phead);
struct Device *addrrestaurantLight(struct Device *phead);
struct Device *addrsecondfloorLight(struct Device *phead);
浴室灯Light-bathroom.c
#include "controlDevice.h"
#include <stdlib.h>int BathroomLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int BathroomLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);
}int BathroomLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}int BathroomLightSataus(int status)
{}struct Device BathroomLight ={.DeviceName = "Bathroom",.pinNum = 5,.open = BathroomLightOpen,.close = BathroomLightClose,.DeviceInit = BathroomLightInit,.changeStatus = BathroomLightSataus};struct Device *addrBathroomLight(struct Device *phead)
{if(phead == NULL){return &BathroomLight;}else{BathroomLight.next = phead;phead = &BathroomLight;return phead;}
};
客厅灯Light-livingroom
#include "controlDevice.h"
#include <stdlib.h>int livingRoomLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int livingRoomLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);
}int livingRoomLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}int livingRoomLightSataus(int status)
{}struct Device livingRoomLight ={.DeviceName = "livingRoom",.pinNum = 2,.open = livingRoomLightOpen,.close = livingRoomLightClose,.DeviceInit = livingRoomLightInit,.changeStatus = livingRoomLightSataus};struct Device *addrlivingRoomLight(struct Device *phead)
{if(phead == NULL){return &livingRoomLight;}else{livingRoomLight.next = phead;phead = &livingRoomLight;return phead;}
};
餐厅灯Light-restaurant.c
#include "controlDevice.h"
#include <stdlib.h>int restaurantLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int restaurantLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);
}int restaurantLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}int restaurantLightSataus(int status)
{}struct Device restaurantLight ={.DeviceName = "restaurant",.pinNum = 7,.open = restaurantLightOpen,.close = restaurantLightClose,.DeviceInit = restaurantLightInit,.changeStatus = restaurantLightSataus};struct Device *addrrestaurantLight(struct Device *phead)
{if(phead == NULL){return &restaurantLight;}else{restaurantLight.next = phead;phead = &restaurantLight;return phead;}
};
二楼灯Light-secondfloor.c
#include "controlDevice.h"
#include <stdlib.h>int secondfloorLightOpen(int pinNum)
{digitalWrite(pinNum,LOW);
}int secondfloorLightClose(int pinNum)
{digitalWrite(pinNum,HIGH);
}int secondfloorLightInit(int pinNum)
{pinMode(pinNum,OUTPUT);digitalWrite(pinNum,HIGH);
}int secondfloorLightSataus(int status)
{}struct Device secondfloorLight ={.DeviceName = "secondfloor",.pinNum = 8,.open = secondfloorLightOpen,.close = secondfloorLightClose,.DeviceInit = secondfloorLightInit,.changeStatus = secondfloorLightSataus};struct Device *addrsecondfloorLight(struct Device *phead)
{if(phead == NULL){return &secondfloorLight;}else{secondfloorLight.next = phead;phead = &secondfloorLight;return phead;}
};
准备就绪
编译运行
运行结果
智能家居-基于香橙派zreo2——手把手搭建、继电器控制灯(一)相关推荐
- 智能家居(6) —— 香橙派摄像头安装实现监控功能
目录 一.USB摄像头测试 二.face_recognition 人脸识别库的安装和测试方法 三.face_recognition 的测试方法 face_recognition的使用 定位人脸,人脸识 ...
- 智能家居(8) —— 香橙派摄像头加入设备工厂
这里的摄像头只是当作一个设备去用,目前实现通过串口指令然后system()进行拍照.然后翔云平台进行人脸对比,未实现自动人脸检测(不会py).所以摄像头没有另创线程.但是做视频监控可以另创线程. 监控 ...
- 智能家居 (3) ——智能家居工厂模式介绍实现继电器控制灯
目录 智能家居工厂模式整体设计框架 继电器控制灯代码 contrlEquipments.h 文件(设备类) mainPro.c 文件(主函数) bathroomLight.c 文件(浴室灯) seco ...
- Mixly(米思齐)的安装以及基于Arduino开发板实现电容触摸控制灯
Mixly(米思齐)的安装以及基于Arduino开发板实现电容触摸控制灯 1.Mixly下载 http://mixly.org/bnu-maker/mixly-arduino-win Mixly软件安 ...
- 从零开始的DIY智能家居 - 基于 ESP32 的土壤湿度传感器
文章目录 前言 硬件选择 代码解析 获取代码 设备控制命令: 设备和协议初始化流程: 配置设备信息 回调函数注册 数据获取与发送流程 总结 前言 自从上次做了那个 甲醛传感器 和 水浊度传感器 之后开 ...
- linux嵌入式智能家居,基于LINUX平台的嵌入式智能家居控制系统研究
摘要: 随着时代的进步,人们对于居住环境的要求越来越高,一种更加舒适,安全,智能的家居体系应运而生,而计算机技术和物联网技术的飞速发展也为智能家居的出现提供了可能.本课题正是在物联网技术飞速提升的背景 ...
- 智能家居 DIY 教程连载4——手把手教你连云
云乃万物互联之本 Hi,各位小伙伴,DIY 活动已经来到了尾声,第四周的任务是整个项目中最有趣也是最重要的部分--物联网.本周的任务完成之后,也就意味着整个项目就完全做完啦,是不是迫不及待先把整个 D ...
- 基于java智能家居,基于JAVA的智能家居控制系统的设计(信息控制端的设计)-论文二稿...
基于JAVA的智能家居控制系统的设计(信息控制端的设计)-论文二稿 PINGDINGSHAN UNIVERSITY毕业论文 (设计 )题 目:基于 JAVA 的智能家居控制系 统的设计(信息控制端的设 ...
- android服务器智能家居,基于Android的智能家居系统
五.参考文献 [1]卜晓晓.基于Android和Wi-Fi的智能家居系统的设计与实现[D].镇江:江苏大学,2016. [2]闫哲,杜涛,左海利.智能家居控制系统的设计与实现[J],自动化技术与应用. ...
最新文章
- 微软CEO纳德拉拥抱Linux意欲何为?
- 10.31模拟:总结
- c语言mysql 学生信息管理系统_学生信息管理系统学生时代小作品源码(C语言版)...
- 你是怎么发现你的同事很有钱的?
- 精通JavaScript攻击框架:AttackAPI(上)
- JAVA实训心得体会(精选4篇)
- CrossMap基因坐标转换:hg38和hg37互换
- nginx集群的搭建
- 计算机模拟题操作题错误,计算机模拟试卷操作题答案.doc
- python 图像处理 书籍_清华大学出版社-图书详情-《深度学习技术图像处理入门》...
- 企业网站建设教程:自己怎么建网站,做网站的步骤有哪些
- 商业研究(20):滴滴出行,进军海外包车?与OTA携程和包车创业公司,共演“三国杀”?看看分析师、投资人和权威人士等10个人的观点碰撞
- 从价值出发,技术管理痛点的正解
- 162手写板合封芯片专用IC输出可达50V外围简单SOP8封装
- 注册Office教育版账号流程实现oneDrive1TB储存
- 论文笔记-精读-8.22-Manifold Regularized Dynamic Network Pruning
- 计算机工程学院运动会加油稿50字,运动会加油稿50字(30篇)
- 李宏毅svm_【李宏毅机器学习笔记】 18、支持向量机(Support Vector Machine,SVM)...
- 用python做通讯录包括姓名地址qq_我是如何用Python获取整个学校女生电话和QQ?技术撩妹...
- UDP实战模拟——聊天模拟器