赞
踩
一、在controlDevices.h中
struct Devices { char deviceName[128]; int status; int pinNum; int (*open)(int pinNum); int (*close)(int pinNum); int (*deviceInit)(int pinNum); int (*readStatus)(int pinNum); int (*changeStatus)(int pinNum); struct Devices *next; }; struct Devices* addBathroomLightToDeviceLink(struct Device *phead); struct Devices* addUpstairLightToDeviceLink(struct Device *phead); struct Devices* addfireIfOrNotToDeviceLink(struct Device *phead);
2.在fire.c中
#include "controlDevices.h" #include <stdlib.h> int fireIfOrNotInit(int pinNum) { pinMode(pinNum,INPUT); digitalWrite(pinNum,HIGH); } int fireStatusRead(int pinNum) { return digitalRead(pinNum); } struct Devices fireIfOrNot{ .deviceName = "fireIfOrNot", .pinNum = 25, .deviceInit = fireIfOrNotInit, .changeStatus = fireStatusRead }; struct Devices *addFireIfOrNotToDeviceLink(struct Devices *phead) { if(phead == NULL) { return &fireIfOrNot; } else{ fireIfOrNot->next = phead; phead = &fireIfOrNot; } }
3.在mainpro.c中
#include<stdio.h> #include"controlDevices.h" #include<string.h> struct Device *findDeviceByName(char *name,struct Devices *phead) { structmpt Devices *tmp = phead; if(phead == NULL) { return NULL; } else{ while(tmp!=NULL) { if(strcmp(tmp->devicename , name){ return tmp; } tmp = tmp->next; } return NULL; } } int main() { char name[128]; struct Devices *pdeviceHead = NULL; pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead); pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead); pdeviceHead = addfireIfOrNotToDeviceLink(pdeviceHead); struct Devices *tmp = findDeviceByName(name,pdeviceHead); while(1) { printf("input\n"); scanf("%s",name); if(tmp !=NULL) { tmp->deviceInit(tmp->pinNum); tmp->open(tmp->pinNum); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。