天天看點

智能家居項目(六)

繼電器組四盞燈全部實作及功能驗證

1.一樣的操作方式,複制bathroomLight.c,修改livingroomLight.c和restaurantLight.c兩個檔案

livingroomLight.c:

#include "controlDevices.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 livingroomLightStatus(int status)
{
	
}

struct Devices livingroomLight = {

	.deviceName = "livingroomLight",
	.pinNum = 23,
	.open = livingroomLightOpen,
	.close = livingroomLightClose,
	.deviceInit = livingroomLightInit,
	.changeStatus = livingroomLightStatus
};

struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &livingroomLight;
	}else{
		livingroomLight.next = phead;
		phead = &livingroomLight;
		return phead;
	}
}
           

restaurantLight.c

#include "controlDevices.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 restaurantLightStatus(int status)
{
	
}

struct Devices restaurantLight = {

	.deviceName = "restaurantLight",
	.pinNum = 24,
	.open = restaurantLightOpen,
	.close = restaurantLightClose,
	.deviceInit = restaurantLightInit,
	.changeStatus = restaurantLightStatus
};

struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead)
{
	if(phead == NULL){
		return &restaurantLight;
	}else{
		restaurantLight.next = phead;
		phead = &restaurantLight;
		return phead;
	}
}
           

2.在controlDevices.h最後一行加入下面内容:

struct Devices* addLivingroomLightToDeviceLink(struct Devices *phead);
struct Devices* addRestaurantLightToDeviceLink(struct Devices *phead);
           

3.最後在mainPro.c中編寫測試程式

int main()
{
	char name[128];
	struct Devices* tmp = NULL;

	if(wiringPiSetup() == -1){
    	return -1;
	}
	
	struct Devices* pdeviceHead = NULL;
	pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addUpstairLightToDeviceLink(pdeviceHead);
	pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
	pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
	
	while(1){
		printf("Input:\n");
		scanf("%s",name);
		tmp = findDeviceByName(name, pdeviceHead);
		if(tmp != NULL){
			tmp->deviceInit(tmp->pinNum);
			tmp->open(tmp->pinNum);
		}
	}
	return 0;
}
           

4.最後放到樹莓派上進行編譯運作

gcc mainPro.c bathroomLight.c -lwiringPi
./a.out
           

依次打開四盞燈:

智能家居項目(六)

此時可以看到繼電器上的訓示燈全部亮了起來

上一篇:智能家居項目(五)

下一篇:智能家居項目(七)

繼續閱讀