智能家居-基于香橙派——语音、网络的串口设置(三)

主函数Mainpro.c

#include <stdio.h>
#include <string.h>
#include "controlDevice.h"
#include "InputCommand.h"
#include <unistd.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;
	}
}

struct inputcommander *FindcommandByName(char *name,struct inputcommander*phead)
{
	struct inputcommander *tmp = phead;
	if(phead == NULL){
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->commandName,name) == 0){
				return tmp;
			}
			tmp = tmp->next;
		}
	}
};

int main()
{
	if(wiringPiSetup() == -1){
			printf("set up error!\n");
			return -1;
	}
	struct Device *tmp = NULL;
	struct Device *tmp1 = NULL;
	char *name = "restaurant";
	int frieStatus;
	
	struct Device *pDeviceHead = NULL;
	pDeviceHead = addrBathroomLight(pDeviceHead);
	pDeviceHead = addrlivingRoomLight(pDeviceHead);
	pDeviceHead = addrrestaurantLight(pDeviceHead);
	pDeviceHead = addrsecondfloorLight(pDeviceHead);
	pDeviceHead = addrFireBell(pDeviceHead);
	pDeviceHead = addrBee(pDeviceHead);

	struct inputcommander *pCommandHead = NULL;
	pCommandHead = addrVoiceControl(pCommandHead);
	pCommandHead = addrSocketControl(pCommandHead);
	
	while(1){
		tmp = findDeviceByName("Frie",pDeviceHead);
		if(tmp != NULL){
			tmp->DeviceInit(tmp->pinNum);
			frieStatus = tmp->readStatus(tmp->pinNum);
			tmp1 = findDeviceByName("Bee",pDeviceHead);
			if(tmp1 != NULL){
				tmp1->DeviceInit(tmp1->pinNum);
				if(frieStatus == 0){
					tmp1->DeviceInit(tmp1->pinNum);
					tmp1->open(tmp1->pinNum);
					sleep(3);
					printf("fire\n");
				}else{
					tmp1->DeviceInit(tmp1->pinNum);
					tmp1->close(tmp1->pinNum);
				}
			}
		}
	}
	
	return 0;
}

控制管理InputCommand.h

#include <wiringPi.h>
#include <stdlib.h>



struct inputcommander
{
	char commandName[128];
	char deviceName[128];
	char command[32];
	int (*Init)(struct inputcommander *commander);
	int (*getcommand)(struct inputcommander *commander);
	int fd;
	char port[18];
	char ipaddr[32];
	char log[1024];
	int sfd;

	struct inputcommander *next;
};

struct inputcommander *addrVoiceControl(struct inputcommander *phead);
struct inputcommander *addrSocketControl(struct inputcommander *phead);

语音Control-voice.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


int VoiceInit(struct inputcommander *commander)
{
	int fd;

	if((fd = serialOpen(commander->deviceName,115200))== -1){
		exit(-1);
	}else{
		printf("serial open success\n");
	}
	commander->fd = fd;
	return fd;
}

int VoiceGetCommand(struct inputcommander *commander)
{
	int nread = 0;
	
	nread = read(commander->fd,commander->command,sizeof(commander->command));
	return nread;
}


struct inputcommander VoiceControl={
	.commandName = "voice",
	.deviceName = "/dev/ttyS5",
	.command = {'\0'},
	.Init = VoiceInit,
	.getcommand = VoiceGetCommand,
	.log = {'\0'},

	.next = NULL
};

struct inputcommander *addrVoiceControl(struct inputcommander *phead)
{
	if(phead == NULL){
		return &VoiceControl;
	}else{
		VoiceControl.next = phead;
		phead = &VoiceControl;
		return phead;
	}
};

网络Control-socket.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>



int SocketInit(struct inputcommander *commander)
{
	int s_fd;
	struct sockaddr_in s_addr;
	memset(&s_addr,0,sizeof(struct sockaddr_in));
	
	//1.socket
	
	s_fd = socket(AF_INET,SOCK_STREAM,0);
	if(s_fd == -1){
		perror("socket");
		exit(-1);
	}

	s_addr.sin_family = AF_INET;
	s_addr.sin_port = htons(atoi(commander->port));
	inet_aton(commander->ipaddr,&s_addr.sin_addr);
	//2.bind
	bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));

	//3.listen        
	listen(s_fd,10);
	printf("Socket server listening ........\n");

	commander->sfd = s_fd;

	return s_fd;
}

int SocketGetCommand(struct inputcommander *commander)
{
	int c_fd;
	int nread = 0;
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int len = sizeof(struct sockaddr_in);
	
	c_fd = accept(commander->sfd,(struct sockaddr *)&c_addr,&len);
	
	memset(commander->command,0,sizeof(commander->command));
	nread = read(c_fd,commander->command,sizeof(commander->command));
	if(nread == -1){
		perror("read"); 
	}else if(nread > 0){
		printf("get message:%d\n",nread);
	}else{
		printf("client quit\n");
	}
	
	return nread;
	
}


struct inputcommander SocketControl={
	.commandName = "socket", 
	.command = {'\0'},
	.Init = SocketInit,
	.getcommand = SocketGetCommand,
	.port = "8088",
	.ipaddr = "192.168.0.107",
	.log = {'\0'},

	.next = NULL
};

struct inputcommander *addrSocketControl(struct inputcommander *phead)
{
	if(phead == NULL){
		return &SocketControl;
	}else{
		SocketControl.next = phead;
		phead = &SocketControl;
		return phead;
	}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值