Smart home (3)-voice recognition control (thread)


Voice recognition> Raspberry Pi serial port> Control device

1. Create voice control objects in factory mode:

(1) VoiceControl.c file (voice control)

#include "command.h"			//自定义类文件
#include <unistd.h>

void voiceControlInit(struct Command *file);						//“语音控制”功能初始化函数声明
int voiceControlGetCommand(struct Command *cmd);					//“获取指令”函数初始化
struct Command *addVoiceControlToLink(struct Command *phead);		//“语音控制”(对象)加入指令链表函数声明

struct Command voiceControl = {
    
    						//定义“语音控制”对象
	.commandName = "voiceControl",					//名字
	.deviceFilesName = "/dev/ttyAMA0",				//树莓派串口文件所在地址
	.command = {
    
    '\0'},								//初始化储存“指令”的空间
	.Init = voiceControlInit,						//指定“语音控制”功能初始化函数
	.getCommand = voiceControlGetCommand,			//指定“获取指令”函数
};

void voiceControlInit(struct Command *file)
{
    
    
	int fd;
	if((fd = serialOpen(file->deviceFilesName,9600)) == -1){
    
    		//打开树莓派串口,波特率指定为9600
		exit(-1);
	}
	file->fd = fd;				//打开串口文件成功,返回“文件描述符”到“语音控制”链节中
}

int voiceControlGetCommand(struct Command *cmd)
{
    
    
	int nread = 0;
	nread = read(cmd->fd,cmd->command,sizeof(cmd->command));	//读取串口
	return nread;												//返回读取到数据的字节数
}

struct Command *addVoiceControlToLink(struct Command *phead)	//“语音控制”(对象)加入指令方式链表函数
{
    
    
	if(phead == NULL){
    
    
		return &voiceControl;
	}else{
    
    
		voiceControl.next = phead;
		phead = &voiceControl;
		return phead;
	}
}

(2) command.h file (command mode)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>


struct Command
{
    
    
	char commandName[128];						//“控制方式”名
	char deviceFilesName[128];					//存放初始化所需文件的路径
	char command[32];							//存放指令
	int fd;										//存放文件描述符
	void (*Init)(struct Command *file);			//“初始化”函数指针
	int s_fd;
	char ipAdress[32];
	char port[12];
	int (*getCommand)(struct Command *cmd);		//“获取指令”函数指针

	struct Command *next;
};

struct Command *addvoiceControlToLink(struct Command *phead);		//“语音控制”加入指令链表函数声明

2. Multi-threaded control-voice control (debugging):

(1) main.c file (main function)

Debug function only

#include <stdio.h>
#include <string.h>
#include "equipment.h"
#include "command.h"
#include <pthread.h>
#include <unistd.h>

struct Equipment *findEquipByName(char *name,struct Equipment *phead);		//链节查找函数声明
struct Command *findCommandByName(char *name,struct Command *phead);		//链节查找函数声明
void *voiceControl_thread(void *data);										//“语音控制”线程函数声明

struct Command *cmdhead = NULL;					//创建“指令”链表全局变量

int main()
{
    
    
	cmdhead = addVoiceControlToLink(cmdhead);			//“语音控制”对象加入链表

	pthread_t voiceControl_thread_t;					//存放线程 ID

	pthread_create(&voiceControl_thread_t,NULL,voiceControl_thread,NULL);		//创建“语音控制”线程
	pthread_join(voiceControl_thread_t, NULL);			//主函数等待“语音控制”线程退出
	return 0;
}


void *voiceControl_thread(void *data)				//“语音控制”线程
{
    
    
	int nread;
	struct Command *voiceHandler = NULL;

	voiceHandler = findCommandByName("voiceControl",cmdhead);		//寻找“语音控制”链节,返回给voiceHandler
	if(voiceHandler == NULL){
    
    										//找不到
		printf("find voiceHandler error\n");
		pthread_exit(NULL);											//线程退出
	}else{
    
    															//找到
		if(voiceHandler->Init(voiceHandler) < 0){
    
    					//初始化
			printf("voiceHandler init error\n");					//初始化失败
			pthread_exit(NULL);										//线程退出
		}

		while(1){
    
    
			nread = voiceHandler->getCommand(voiceHandler);						//读取指令
			if(nread == 0){
    
    														//没接收到指令
				printf("No command received\n");
			}else{
    
    																//接收到指令
				printf("Get command:%s\n",voiceHandler->command);
			}
		}
	}
}

struct Command *findCommandByName(char *name,struct Command *phead)			//“控制方式”链表查找链节函数
{
    
    
	struct Command *tmp = phead;

	if(phead == NULL){
    
    
		return NULL;
	}

	while(tmp != NULL){
    
    
		if(strcmp(name,tmp->commandName) == 0){
    
    
			return tmp;
		}
		tmp = tmp->next;
	}
	return NULL;
}

(2) YS-LDV7 voice module

Article: Secondary Development of YS-LDV7 Voice Module

3. Past articles:

See previous articles for equipment related content
Smart Home (1)-Factory Mode Relay Control Light
Smart Home (2)-Factory Mode Flame Alarm

Guess you like

Origin blog.csdn.net/lcx1837/article/details/113940341