智能家居 (3) ——语音识别控制端线程


语音识别 > 树莓派串口 > 控制设备

1.工厂模式创建语音控制对象:

(1)voiceControl.c 文件(语音控制)
#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 文件(指令方式类)
#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.多线程控制——语音控制(调试):

(1)main.c 文件(主函数)

仅调试功能

#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语音模块

文章:YS-LDV7语音模块二次开发

3.往期文章:

设备相关内容见往期文章
智能家居 (1) ——工厂模式继电器控制灯
智能家居 (2) ——工厂模式火焰报警器

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~莘莘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值