一、在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 status);
struct Devices *next;
};
struct Devices *addBathroomLightToDeviceLink(struct Devives *phead)
二、在upstairLight.c中
#include"contrlDevices.h"
#include<stdlib.h>
int upstairLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int upstairLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int upstairLightCloseInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int upstairLightCloseStatus(int status)
{
}
struct Devices upstairLight={
.deviceName = "upstairLight",
.pinNum = 21,
.open = upstairLightOpen,
.close = upstairLightClose,
.deviceInit = upstairLightCloseInit,
.changeStatus = upstairLightCloseStatus
};
struct Devices *addUpstairLightToDeviceLink(struct Devices *phead)
{
if(phead == NULL)
{
return &upstairLight;
}
else{
upstairLight.next = phead;
phead = &upstairLight;
}
};
三、写一个测试主函数,在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);
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);
}
}
}