智能家居-基于香橙派——语音识别函数和火警线程实现(五)

目录

Mainpro主函数的语音识别command函数:

语音线程的voiceThread函数

火警线程:

线程函数:

Mainpro函数的火警线程建立:

主函数Mainpro.c


Mainpro主函数的语音识别command函数:

void command(struct inputcommander *commander,char cmd)
{
	struct Device *tmp = NULL;

	switch(cmd)
	{
		case 'B':
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("Bathroom open\n");
			}
			break;
		case 'F':
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("Bathroom close\n");
			}
			break;
		case 'D':
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("DiningRoom open\n");
			}
			break;
		case 'G':
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("DziningRoom close\n");
			}
			break;
		case 'C':
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("LivingRoom open\n");
			}
			break;
		case 'H':
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("LivingRoom close\n");
			}
			break;
		case 'A':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("SecondRoom open\n");
			}
			break;
		case 'J':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("SecondRoom close\n");
			}
			break;
		case 'X':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			break;
		case 'Y':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			break;
	}
}

语音线程的voiceThread函数

void *voiceThread(void *datas)
{
	struct inputcommander *voiceHandler;
	int nread = 0;
	voiceHandler = FindcommandByName("voice",pCommandHead);
	char x;

	if(voiceHandler == NULL){
		printf("find voiceHandler error\n");
		pthread_exit(NULL);
	}else{
		if( voiceHandler->Init(voiceHandler) < 0){
			printf("voice init error\n");
			pthread_exit(NULL);
		}else{
			printf("%s init success\n",voiceHandler->commandName);
		}
		while(1){
			memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
			nread = voiceHandler->getcommand(voiceHandler);
			printf("nread:%d\n",nread);
			if(nread == 0){
				printf("voice nothing\n");
			}else{
				read(voiceHandler->fd,&x,1);
				printf("x=%c\n",x);
				command(voiceHandler,x);
			}
		}
	}	
}

火警线程:

线程函数:

void *fireThread(void *datas)
{
	struct Device *tmp = NULL;
	struct Device *tmp1 = NULL;
	int frieStatus;
	printf("fireBell init success\n");
	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);
				}
			}
		}
	}
}

Mainpro函数的火警线程建立:

pthread_t fire_thread;
pthread_create(&fire_thread,NULL,fireThread,NULL);
pthread_join(fire_thread,NULL);

主函数Mainpro.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "controlDevice.h"
#include "InputCommand.h"
#include <pthread.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>


struct Device *pDeviceHead = NULL;
struct inputcommander *pCommandHead = NULL;
struct inputcommander *socketHandler = NULL;
int c_fd;

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;
		}
	}
};

void command(struct inputcommander *commander,char cmd)
{
	struct Device *tmp = NULL;

	switch(cmd)
	{
		case 'B':
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("Bathroom open\n");
			}
			break;
		case 'F':
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("Bathroom close\n");
			}
			break;
		case 'D':
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("DiningRoom open\n");
			}
			break;
		case 'G':
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("DziningRoom close\n");
			}
			break;
		case 'C':
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("LivingRoom open\n");
			}
			break;
		case 'H':
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("LivingRoom close\n");
			}
			break;
		case 'A':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
				printf("SecondRoom open\n");
			}
			break;
		case 'J':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
				printf("SecondRoom close\n");
			}
			break;
		case 'X':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->open(tmp->pinNum);
			}
			break;
		case 'Y':
			tmp = findDeviceByName("secondfloor",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			tmp = findDeviceByName("Bathroom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			tmp = findDeviceByName("livingRoom",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			tmp = findDeviceByName("restaurant",pDeviceHead);
			if(tmp != NULL){
				tmp->DeviceInit(tmp->pinNum);
				tmp->close(tmp->pinNum);
			}
			break;
	}
}

void *voiceThread(void *datas)
{
	struct inputcommander *voiceHandler;
	int nread = 0;
	voiceHandler = FindcommandByName("voice",pCommandHead);
	char x;

	if(voiceHandler == NULL){
		printf("find voiceHandler error\n");
		pthread_exit(NULL);
	}else{
		if( voiceHandler->Init(voiceHandler) < 0){
			printf("voice init error\n");
			pthread_exit(NULL);
		}else{
			printf("%s init success\n",voiceHandler->commandName);
		}
		while(1){
			memset(voiceHandler->command,'\0',sizeof(voiceHandler->command));
			nread = voiceHandler->getcommand(voiceHandler);
			command(voiceHandler,x);
			printf("nread:%d\n",nread);
			if(nread == 0){
				printf("voice nothing\n");
			}else{
				read(voiceHandler->fd,&x,1);
				printf("x=%c\n",x);
				printf("control:%s\n",voiceHandler->command);
				command(voiceHandler,x);
			}
		}
	}	
}

void* read_t (void *datas)
{
	int nread = 0;
	memset(socketHandler->command,'\0',sizeof(socketHandler->command));
	nread = read(c_fd,socketHandler->command,sizeof(socketHandler->command));
	if(nread == -1){
		perror("read"); 
	}else if(nread > 0){
		printf("get byte:%d,message:%s\n",nread,socketHandler->command);
	}else{
		printf("client quit\n");
	}
	
}


void* socketThread(void *datas)
{
	int nread = 0;
	pthread_t  readt;
	
	struct sockaddr_in c_addr;
	memset(&c_addr,0,sizeof(struct sockaddr_in));
	int len = sizeof(struct sockaddr_in);
	
	socketHandler = FindcommandByName("socket",pCommandHead);
	printf("%s init success\n",socketHandler->commandName);
	if(socketHandler == NULL){
		printf("find socketHandler error\n");
		pthread_exit(NULL);
	}
	socketHandler->Init(socketHandler);

	while(1){
		c_fd = accept(socketHandler->sfd,(struct sockaddr *)&c_addr,&len);
		pthread_create(&readt,NULL,read_t,NULL);
	}
}

void *fireThread(void *datas)
{
	struct Device *tmp = NULL;
	struct Device *tmp1 = NULL;
	int frieStatus;
	printf("fireBell init success\n");
	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);
				}
			}
		}
	}
}

int main()
{
	if(wiringPiSetup() == -1){
			printf("set up error!\n");
			return -1;
	}
	

	pthread_t voice_thread;
	pthread_t socket_thread;
	pthread_t fire_thread;
	//1.设备控制工厂初始化
	
	pDeviceHead = addrBathroomLight(pDeviceHead);
	pDeviceHead = addrlivingRoomLight(pDeviceHead);
	pDeviceHead = addrrestaurantLight(pDeviceHead);
	pDeviceHead = addrsecondfloorLight(pDeviceHead);
	pDeviceHead = addrFireBell(pDeviceHead);
	pDeviceHead = addrBee(pDeviceHead);
	//2.控制工厂初始化
	
	pCommandHead = addrVoiceControl(pCommandHead);
	pCommandHead = addrSocketControl(pCommandHead);

	//3.线程池开辟
	//3.1语音线程
	pthread_create(&voice_thread,NULL,voiceThread,NULL);

	//3.2socket线程
	pthread_create(&socket_thread,NULL,socketThread,NULL);

	//3.3火警线程
	pthread_create(&fire_thread,NULL,fireThread,NULL);
	
	pthread_join(voice_thread,NULL);
	pthread_join(socket_thread,NULL);
	pthread_join(fire_thread,NULL);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值