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

目录

语音识别线程控制代码

inputCommand.h

#include
#include
#include
#include
#include

struct Command
{
    char commandName[128];
    char deviceFilesName[128];
    char command[32];
    int fd;
    int (*Init)(struct Command *file);
    int s_fd;
    char ipAdress[32];
    char port[12];
    int (*getCommand)(struct Command *cmd);

    struct Command *next;
};

struct Command *addvoiceControlToCommandLink(struct Command *phead);

mainPro.c

#include
#include
#include "inputCommand.h"
#include
#include

struct Command *addVoiceControlToCommandLink(struct Command *phead);
struct Command *findCommandByName(char *name,struct Command *phead);
void *voiceControl_thread(void *data);

struct Command *cmdhead = NULL;

int main()
{
    cmdhead = addVoiceControlToCommandLink(cmdhead);

    pthread_t voiceControl_thread_t;

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

voiceControl.c

#include "inputCommand.h"
#include

int voiceControlInit(struct Command *file);

int voiceControlGetCommand(struct Command *cmd);
struct Command *addVoiceControlToCommandLink(struct Command *phead);

struct Command voiceControl = {
    .commandName = "voiceControl",
    .deviceFilesName = "/dev/ttyAMA0",
    .command = {'\0'},
    .Init = voiceControlInit,
    .getCommand = voiceControlGetCommand,
};

int voiceControlInit(struct Command *file)
{
    int fd;
    if((fd = serialOpen(file->deviceFilesName,9600)) == -1){
        exit(-1);
    }
    file->fd = fd;
}

int voiceControlGetCommand(struct Command *cmd)
{
    int nread = 0;
    nread = read(cmd->fd,cmd->command,sizeof(cmd->command));
    if(nread == 0){
        printf("usart for voice over time\n");
    }else{
    return nread;
    }
}

struct Command *addVoiceControlToCommandLink(struct Command *phead)
{
    if(phead == NULL){
        return &voiceControl;
    }else{
        voiceControl.next = phead;
        phead = &voiceControl;
        return phead;
    }
}

代码测试

注意,树莓派的串口出厂状态是绑定到蓝牙的,然后我们使用引导字符和一些调试接口,这里我们应该记得打开树莓派的正常串口通信功能,使用串口进行正常的数据通信。

[En]

Note that the raspberry pie serial port factory status is bound to Bluetooth, and then we use the boot character and some debugging interface, here we should remember to turn on the raspberry pie normal serial communication function, use the serial port for normal data communication.

连接语音模块和覆盆子馅饼并运行程序。

[En]

Connect the voice module and raspberry pie and run the program.

所有指令均可识别成功!

下一步,可以根据语音识别的不同指令操纵相应的继电器,从而实现对智能家居中不同灯光的语音控制,详细代码发布在博文中。

[En]

In the next step, the corresponding relays can be manipulated according to the different instructions of speech recognition, so as to realize the voice control of different lights in the intelligent home, and the detailed code is posted in the blog post.

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

; 往期文章

智能家居 (1) ——智能家居整体功能框架
智能家居 (2) ——设计模式的引入
智能家居 (3) ——工厂模式继电器控制灯
智能家居 (4) ——工厂模式火焰报警
智能家居 (5) —— LD3320语音模块二次开发
智能家居 (6) ——语音识别线程控制
智能家居 (7) ——网络服务器线程控制
智能家居 (8) ——智能家居项目整合(网络控制线程、语音控制线程,火灾报警线程)
网络编程知识预备(1) ——了解OSI网络模型
网络编程知识预备(2) ——浅显易懂的三次握手与四次挥手
网络编程知识预备(3) ——SOCKET、TCP、HTTP之间的区别与联系
网络编程知识预备(4) ——了解HTTP协议与HTTPS协议
网络编程知识预备(5) ——libcurl库简介及其编程访问百度首页
智能家居 (9) ——人脸识别摄像头安装实现监控功能
智能家居 (10) ——人脸识别祥云平台编程使用
智能家居 (11) ——树莓派摄像头捕捉人脸并识别
智能家居 (12) ——人脸识别整合到智能家居系统
智能家居 (13) ——智能家居加入手机app端控制

Original: https://blog.csdn.net/zhuguanlin121/article/details/117073596
Author: 行稳方能走远
Title: 智能家居 (6) ——语音识别线程控制

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/512996/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球