基于orangepiZero2智能家具(四)串口通信(语音识别)线程控制

 一、串口通信线程控制代码

mianpro.c

#include "ctrDevice.h"
#include <string.h>
#include <pthread.h>
#include <errno.h>

#include "inputCmd.h"
//定义指令工厂的指令
struct inputCmd* pcmdhead= NULL;    //定义为全局变量,使线程里面也可以使用。

/*struct ctrDevice* findDeviceFunc(struct ctrDevice* phead,char *cmd)
{
	struct ctrDevice* tmp;
	tmp = phead;
	if(tmp == NULL){
		printf("no found!\n");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->DeviceName,cmd)==0)
			{
					return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}*/

struct inputCmd* findInputFunc(struct inputCmd* phead,char *cmd)      
 //查找struct inputCmd里面对应的结构体
{
	struct inputCmd* tmp;
	tmp = phead;
	if(tmp == NULL){
		printf("no found!\n");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->cmdName,cmd)==0)
			{
					return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}
	
void *voiceControlHandler(void *data)
//线程的控制代码
{
	int nread=0;
    struct inputCmd *voiceHandler = NULL;
    voiceHandler =  findInputFunc(pcmdhead,"voice");
 
    if(voiceHandler == NULL){    //判断有无找到对应的结构体。
        printf("find voiceHandler error\n");
        pthread_exit(NULL);
    }else{
        if(voiceHandler->inputCmdInit(voiceHandler) < 0){       //“语音控制”功能初始化
            printf("voiceControl init error\n");
            pthread_exit(NULL);
        }else{
            printf("voiceControl init success\n");
        }
		while(1){
            //不断的读取语音模块发送过来的信息。
			memset(voiceHandler->getCmd,'\0',sizeof(voiceHandler->getCmd));
			nread = voiceHandler->inputGetCmd(voiceHandler);
			if(nread==0){
				printf("No voiceCommand received\n");
			}else{
			printf("nread =%d,cmd=%s\n",nread,voiceHandler->getCmd);
				}
			}
    	}

}


int main(){
		//struct ctrDevice* phead=NULL;
		//struct ctrDevice* tmp=NULL;		
		pthread_t voiceControl_thread;
		if (wiringPiSetup () == -1) { 
	        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; 
	        return 1 ; 
	   		}
		pcmdhead = voiceLink(pcmdhead);
		pthread_create(&voiceControl_thread,NULL,voiceControlHandler,NULL);
 
   		 pthread_join(voiceControl_thread, NULL);		//主函数等待线程退出
	return 0;
}

voiceCnt.c

#include"inputCmd.h"

/*struct inputCmd {
	char deviceName[32];
	char cmdName[32];
	char getCmd[16];
	int fd;
	int sfd;
	char port[12];
	char address[32];
	int (*inputCmdInit)(struct inputCmd* voice);
	int (*inputCmdGetCmd)(struct inputCmd* voice);
	struct inputCmd *next;

}*/

int InitVoice(struct inputCmd* PVoice)    //初始化语音模块
{
	 int fd;
    if ((fd = serialOpen (PVoice->deviceName, 115200)) < 0)//初始化串口
     { 
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; 
        return -1 ; 
    }
    PVoice->fd = fd;
    return fd;

}
int voiceGetCmd(struct inputCmd* PVoice)    //得到语音模块发送的信息
{
	int n_read;
	n_read = read(PVoice->fd,PVoice->getCmd,sizeof(PVoice->getCmd));
	return n_read;
}


struct inputCmd voice = {    //定义语音模块的结构体
	.cmdName="voice",
	.deviceName="/dev/ttyS5",
	.inputCmdInit=InitVoice,
	.next=NULL,
	.fd=0,
	.getCmd='\0',
	.inputGetCmd=voiceGetCmd

};

struct inputCmd* voiceLink(struct inputCmd* PVoice)    //把语音模块的结构体加入链表。
{
	if(PVoice==NULL){
		return &voice;
	}else{
		voice.next = PVoice;
		PVoice = &voice;
		return PVoice;
	}

}

inputCmd.h

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




struct inputCmd {
	char deviceName[32];	//设备名
	char cmdName[32];		//指令工厂的名字
	char getCmd[16];	//存放的指令
	int fd;			//文件描述符
	int sfd;		//服务器套接字描述符
	char port[12];	//端口
	char address[32];	//ip地址
	int (*inputCmdInit)(struct inputCmd* voice);	//初始化函数
	int (*inputGetCmd)(struct inputCmd* voice);		//获取指令函数
	struct inputCmd *next;		

};

struct inputCmd* voiceLink(struct inputCmd* PVoice);

与各种灯的结合

mainpro.c

#include "ctrDevice.h"
#include <string.h>
#include <pthread.h>
#include <errno.h>

#include "inputCmd.h"

struct ctrDevice* phead;

struct inputCmd* inputHead= NULL;

struct inputCmd* socketHandler= NULL;

struct ctrDevice* findDeviceFunc(char *cmd,struct ctrDevice* phead)
{
	struct ctrDevice* tmp;
	tmp = phead;
	if(tmp == NULL){
		printf("no found!\n");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->DeviceName,cmd)==0)
			{
					return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}

struct inputCmd* findInputFunc(struct inputCmd* phead,char *cmd)
{
	struct inputCmd* tmp;
	tmp = phead;
	if(tmp == NULL){
		printf("no found!\n");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->cmdName,cmd)==0)
			{
					return tmp;
			}
			tmp = tmp->next;
		}
		return NULL;
	}
}


void ctrCmd(struct inputCmd *cmdhandler)
{	
	 struct ctrDevice *tmp =NULL;
	 static char mark = 0;

	
		if(mark == 0){
			tmp = findDeviceFunc("fire",phead);
			if(tmp != NULL)  tmp->InitDevice(tmp->pinNum);
			tmp = findDeviceFunc("beer",phead);
			if(tmp != NULL)  tmp->InitDevice(tmp->pinNum);
			tmp = findDeviceFunc("livingroomLight",phead);
			if(tmp != NULL)  tmp->InitDevice(tmp->pinNum); 
			tmp = findDeviceFunc("restaurantLight",phead);
			if(tmp != NULL)  tmp->InitDevice(tmp->pinNum);
			tmp = findDeviceFunc("upstairsLight",phead);
			if(tmp != NULL)  tmp->InitDevice(tmp->pinNum);
			tmp = findDeviceFunc("bathroomLight",phead);
			if(tmp != NULL)  tmp->InitDevice(tmp->pinNum);
			printf("设备已全部初始化\n");
			mark = 1;
		}
		if(strcmp("OP1",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("livingroomLight",phead);
			if(tmp != NULL){
				tmp->open(tmp->pinNum);
				printf("已打开客厅灯\n");
			}
		}
		if(strcmp("CL1",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("livingroomLight",phead);
			if(tmp != NULL){
				tmp->close(tmp->pinNum);
				printf("已关闭客厅灯\n");
			}
		}
		if(strcmp("OP2",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("upstairsLight",phead);
			if(tmp != NULL){
				tmp->open(tmp->pinNum);
				printf("已打开卧室灯\n");
			}
		}
		if(strcmp("CL2",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("upstairsLight",phead);
			if(tmp != NULL){
				tmp->close(tmp->pinNum);
				printf("已关闭卧室灯\n");
			}

		}

		if(strcmp("OP3",cmdhandler->getCmd) == 0){
				
			tmp = findDeviceFunc("bathroomLight",phead);
			
			if(tmp != NULL){
				tmp->open(tmp->pinNum);
				printf("已打开浴室灯\n");
			}
		}
		if(strcmp("CL3",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("bathroomLight",phead);
			if(tmp != NULL){
				tmp->close(tmp->pinNum);
				printf("已关闭浴室灯\n");
			}
		}
		
		if(strcmp("OP4",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("restaurantLight",phead);
			if(tmp != NULL){
				tmp->open(tmp->pinNum);
				printf("已打开餐厅灯\n");
			}
		}
		
		if(strcmp("CL4",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("restaurantLight",phead);
			if(tmp != NULL){
				tmp->close(tmp->pinNum);
				printf("已关闭餐厅灯\n");
			}
		}
		
	
		
		if(strcmp("OPA",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("livingroomLight",phead);
			if(tmp != NULL)  tmp->open(tmp->pinNum);
			tmp = findDeviceFunc("restaurantLight",phead);
			if(tmp != NULL)  tmp->open(tmp->pinNum);
			tmp = findDeviceFunc("upstairsLight",phead);
			if(tmp != NULL)  tmp->open(tmp->pinNum);
			tmp = findDeviceFunc("bathroomLight",phead);
			if(tmp != NULL)  tmp->open(tmp->pinNum);
			printf("已打开所有灯\n");
		}
		if(strcmp("CLA",cmdhandler->getCmd) == 0){
			tmp = findDeviceFunc("livingroomLight",phead);
			if(tmp != NULL)  tmp->close(tmp->pinNum);
			tmp = findDeviceFunc("restaurantLight",phead);
			if(tmp != NULL)  tmp->close(tmp->pinNum);
			tmp = findDeviceFunc("upstairsLight",phead);
			if(tmp != NULL)  tmp->close(tmp->pinNum);
			tmp = findDeviceFunc("bathroomLight",phead);
			if(tmp != NULL)  tmp->close(tmp->pinNum);
			printf("已关闭所有灯\n");
		}
	
}
void *voiceControlHandler(void *data)
{
	int nread = 0;
    struct inputCmd *voiceHandler = NULL;
    voiceHandler =  findInputFunc(inputHead,"voice");
 
    if(voiceHandler == NULL){
        printf("find voiceHandler error\n");
        pthread_exit(NULL);
    }else{
        if(voiceHandler->inputCmdInit(voiceHandler) < 0){       //“语音控制”功能初始化
            printf("voiceControl init error\n");
            pthread_exit(NULL);
        }else{
            printf("voiceControl init success\n");
        }
		while(1){
			memset(voiceHandler->getCmd,'\0',sizeof(voiceHandler->getCmd));
			nread = voiceHandler->inputGetCmd(voiceHandler);
			
			if(nread==0){
				printf("No voiceCommand received\n");
			}else{
			printf("nread =%d,cmd=%s\n",nread,voiceHandler->getCmd);
			ctrCmd(voiceHandler);
				}
			}
    	}

}

/*void *socketControlHandler(void *data)
{
	int nread=0;
   pthread_t socketControl_thread2;
    socketHandler =  findInputFunc(inputHead,"voice");
 
    if(socketHandler == NULL){
        printf("find socketHandler error\n");
        pthread_exit(NULL);
    }else{
        if(socketHandler->inputCmdInit(socketHandler) < 0){       //“语音控制”功能初始化
            printf("socketHandler init error\n");
            pthread_exit(NULL);
        }else{
            printf("socketHandler init success\n");
        }
		while(1){
				accept(s_fd,NULL,NULL);

        		printf("connect done!\n");
			
			pthread_create(&socketControl_thread2,NULL,socketControlHandler,NULL);
    	}

}*/


int main(){
		//struct ctrDevice* phead=NULL;
		//struct ctrDevice* tmp=NULL;		
		pthread_t voiceControl_thread;
		if (wiringPiSetup () == -1) { 
	        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ; 
	        return 1 ; 
	   		}
		inputHead = voiceLink(inputHead);
		phead = upstairsLightLink(phead);
		phead = restaurantLightLink(phead);
		phead = bathroomLightLink(phead);
		phead = livingroomLightLink(phead);
		pthread_create(&voiceControl_thread,NULL,voiceControlHandler,NULL);
 
   		 pthread_join(voiceControl_thread, NULL);		//主函数等待线程退出
	return 0;
}

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值