赞
踩
1.代码设计思路草图:
2.代码思路讲解:
a)一个指令工厂,一个控制工厂,实际上就是通过链表链起来的数据。具体怎么链接起来,就是基于简单工厂模式的类与对象的概念,上一篇文章有学习记录。
b)主函数语音指令程序和tcp指令程序各起一个线程,然后通过指令找到对应的控制程序,实现对应的模块的功能。
有了思路之后,我们就可以来建立项目文件代码了。
1.我们先再桌面建立一个项目文件:
2.然后打开文件,逐个写好先:
3.然后打开Souce Insight,把文件都导入进去
因为用这个来写,更加的直观和方便。
1.先看主函数mainPro.c
#include<stdio.h> #include"controlDevices.h" #include<string.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 *name = "bathroomLight"; if(-1 == wiringPiSetup()){ return -1; } struct Devices *pdeviceHead = NULL; pdeviceHead = addBathroomLightInit(pdeviceHead); struct Devices *tmp = findDeviceByName(name,pdeviceHead); if(tmp != NULL){ tmp->deviceInit(tmp->pinNum); tmp->open(tmp->pinNum); } return 0; }
2.外设设备的头文件controlDevices.h
#include<wiringPi.h> #include<stdlib.h> struct Devices { char deviceName[128]; int pinNum; int (*deviceInit)(int pinNum;); int (*open)(int pinNum;); int (*close)(int pinNum;); struct Devices *next; }; struct Devices *addBathroomLightInit(struct Devices *phead);
3.浴室灯bathroomLight.c
#include"controlDevices.h" int bathroomLightInit(int pinNum) { pinMode(pinNum,OUTPUT); digitalWrite(pinNum,HIGH); } int bathroomLightOpen(int pinNum) { digitalWrite(pinNum,LOW); } int bathroomLightClose(int pinNum) { digitalWrite(pinNum,HIGH); } struct Devices bathroomLight = { .deviceName = "bathroomLight", .pinNum = 28, .deviceInit = bathroomLightInit, .open = bathroomLightOpen, .close = bathroomLightClose }; struct Devices *addBathroomLightInit(struct Devices *phead) { if(phead == NULL){ return &bathroomLight; }else{ bathroomLight.next = phead; phead = &bathroomLight; } }
4.写完之后,利用FileZilla文件传输软件,将文件smartHouse文件传到树莓派终端:
5.进行代码编译,然后执行:
6.测试结果:
7.继电器其余四盏灯全部实现:
其余三盏灯的代码格式和上面bathroomLight.c的一样,这里就不一一展示,已经实现了。
等所有的模块都测试完,会有整个项目的实现效果和总结。
它们也是io口驱动,跟实现灯代码一样,就是要注意模块的使用说明。
1.火焰传感器fire.c
#include"controlDevices.h" int fireInit(int pinNum) { pinMode(pinNum,INPUT); digitalWrite(pinNum,HIGH); } int fireStatusRead(int pinNum) { return digitalRead(pinNum); } struct Devices fire = { .deviceName = "fire", .pinNum = 0, .deviceInit = fireInit, .readStatus = fireStatusRead }; struct Devices *addFireToDevicesLink(struct Devices *phead) { if(phead == NULL){ return &fire; }else{ fire.next = phead; phead =
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。