智能家居(2)---串口通信(语音识别)控制线程封装

封装语音线程(语音通过串口和主控设备进行交流)实现对智能家居中各种灯光的控制

mainPro.c(主函数)

c 复制代码
#include <stdio.h>
#include "controlDevice.h"
#include "inputCommand.h"
#include <pthread.h>


struct Devices        *pdeviceHead  = NULL;         //设备工厂链表头
struct InputCommander *pcommandHead = NULL;         //指令工厂链表头

struct Devices* findDeviceByName(struct Devices *phead,char *name)          //在设备链表中查找设备(语音和socket均可使用)
{
    struct Devices *tmp = phead;
    if(tmp == NULL){
        printf("The devicesLink is NULL");
        return NULL;
    
    }else{
        while(tmp != NULL){
            if(strcmp(tmp->deviceName,name) == 0){
                return tmp;
            }
            tmp = tmp->next;
        }
   
     return NULL;   

    }

}

struct InputCommander* findCommanderByName(struct InputCommander *phead,char *name)     //在控制链表中查找相关控制
{
    struct InputCommander *tmp = phead;
    if(tmp == NULL){
        printf("The commanderLink is NULL");
        return NULL;
    
    }else{
        while(tmp != NULL){
            if(strcmp(tmp->commandName,name) == 0){
                return tmp;
            }
            tmp = tmp->next;
        }
   
     return NULL;   

    }

}

void doCommand(struct InputCommander *cmd)                              //根据传入的命令控制相关设备
{
    struct Devices *tmp = NULL;

    if(strstr(cmd->command,"START")){                                   //初始化所有设备
        tmp = findDeviceByName(pdeviceHead,"bathroomLight");
        if(tmp != NULL) tmp->deviceInit(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"bedroomLight");
        if(tmp != NULL) tmp->deviceInit(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"livingroomLight");
        if(tmp != NULL) tmp->deviceInit(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"restaurantLight");
        if(tmp != NULL) tmp->deviceInit(tmp->pinNum);
        printf("The devices all init success\n");
        
    }else if(strstr(cmd->command,"OL1")){                               //打开卫生间灯
        tmp = findDeviceByName(pdeviceHead,"bathroomLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        printf("The bathroomLight already open\n");

    }else if(strstr(cmd->command,"CL1")){                               //关闭卫生间灯
        tmp = findDeviceByName(pdeviceHead,"bathroomLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        printf("The bathroomLight already close\n");
    
    }else if(strstr(cmd->command,"OL2")){                               //打开卧室灯
        tmp = findDeviceByName(pdeviceHead,"bedroomLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        printf("The bedroomLight already open\n");
    
    }else if(strstr(cmd->command,"CL2")){                               //关闭卧室灯
        tmp = findDeviceByName(pdeviceHead,"bedroomLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        printf("The bedroomLight already close\n");
    
    }else if(strstr(cmd->command,"OL3")){                               //打开客厅灯
        tmp = findDeviceByName(pdeviceHead,"livingroomLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        printf("The livingroomLight already open\n");
    
    }else if(strstr(cmd->command,"CL3")){                               //关闭客厅灯
        tmp = findDeviceByName(pdeviceHead,"livingroomLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        printf("The livingroomLight already close\n");
    
    }else if(strstr(cmd->command,"OL4")){                               //打开餐厅灯
        tmp = findDeviceByName(pdeviceHead,"restaurantLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        printf("The restaurantLight already open\n");
    
    }else if(strstr(cmd->command,"CL4")){                               //关闭餐厅灯
        tmp = findDeviceByName(pdeviceHead,"restaurantLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        printf("The restaurantLight already close\n");
    
    }else if(strstr(cmd->command,"ALL1")){                              //打开所有的灯
        tmp = findDeviceByName(pdeviceHead,"bathroomLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"bedroomLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"livingroomLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"restaurantLight");
        if(tmp != NULL) tmp->open(tmp->pinNum);
        printf("The Light all open success\n");
    
    }else if(strstr(cmd->command,"ALL0")){                              //关闭所有的灯
        tmp = findDeviceByName(pdeviceHead,"bathroomLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"bedroomLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"livingroomLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        tmp = findDeviceByName(pdeviceHead,"restaurantLight");
        if(tmp != NULL) tmp->close(tmp->pinNum);
        printf("The Light all close success\n");

    }

}

void* voice_pthread(void *data)				//语音识别(串口通信)处理线程函数
{
    struct InputCommander *voiceHandler;
    int n_read;

    voiceHandler = findCommanderByName(pcommandHead,"voice");
    if(voiceHandler == NULL){
        printf("find voice error\n");
        pthread_exit(NULL);
    }else{
        if(voiceHandler->Init(voiceHandler) < 0 ){
            printf("%s init error\n",voiceHandler->commandName);
            pthread_exit(NULL);
        }else
            printf("%s init success\n",voiceHandler->commandName);

        while(1){
            n_read = voiceHandler->getCommand(voiceHandler);
            if(n_read == 0){
                printf("no command from voice\n");
            }else{//根据指令进行控制
                printf("read %d message-->:%s",n_read,voiceHandler->command);
                doCommand(voiceHandler);                //执行命令
                
            }
            
        }
    }


}


int main()
{
    pthread_t voice_pth;
    
    if(wiringPiSetup()<0){//初始化wiringPi外设库

            printf("wiringPi Init failed\n");
            return -1;
        }


    //1.指令工厂初始化
    pcommandHead = addVoiceToCommandLink(pcommandHead);             //将语音控制加入命令控制链表
   

    //2.设备控制工厂初始化
    pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);        //将卫生灯加入设备链表
    pdeviceHead = addbedroomLightToDeviceLink(pdeviceHead);         //将卧室灯加入设备链表
    pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);      //将餐厅灯加入设备链表
    pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);      //将客厅灯加入设备链表
    pdeviceHead = addFireToDeviceLink(pdeviceHead);                 //将火灾检测加入设备链表
    pdeviceHead = addBeepToDeviceLink(pdeviceHead);                 //将蜂鸣器加入设备链表

   
    //3.线程池建立
    //3.1语音线程
    int ret = pthread_create(&voice_pth,NULL,voice_pthread,NULL);
    if (ret != 0) {
        printf("Failed to create voicethread.\n");
        return -1;
    }

	//等待线程退出
    pthread_join(voice_pth,NULL);

	return 0;
}

inputCommand.h(控制类)

c 复制代码
#include <wiringPi.h>
#include <stddef.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>          
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>


struct InputCommander
{
    char commandName[128];      //命令名字
    char command[32];           //具体命令
    char deviceName[128];       //打开的设备名,比如串口
    char port[12];              //端口号
    char ipAddress[32];         //ip地址

    int (*Init)(struct InputCommander *commder);
    int (*getCommand)(struct InputCommander *commder);
    int fd;     //文件描述符
    int baud;   //波特率
    int s_fd;   //socket服务端文件描述符
    int c_fd;   //客户端文件描述符
    
    char log[1024]; //日志
    struct InputCommander *next;
};

struct InputCommander *addVoiceToCommandLink(struct InputCommander *phead);				//语音控制加入命令控制链表

voiceControl.c(语音)

c 复制代码
#include "inputCommand.h"


int voiceInit(struct InputCommander *voiceMes)
{
    int fd;
    fd =serialOpen(voiceMes->deviceName,voiceMes->baud);//打开串口
    if(fd<0){
        printf("open serial fail!\r\n");//判断串口打开是否成功
        return -1;
    }else
        voiceMes->fd = fd;//传回串口文件描述符

    return fd;

}

 int voiceGetCommand(struct InputCommander *voiceMes)
 {
    int nread = 0;
    memset(voiceMes->command,0,sizeof(voiceMes->command));
    nread = read(voiceMes->fd,voiceMes->command,sizeof(voiceMes->command));
    return nread;
 }




struct InputCommander voiceControl = {
    .commandName = "voice",
    .command = {'\0'},
    .deviceName = "/dev/ttyAMA0",
    .baud = 9600,
    .Init = voiceInit,
    .getCommand = voiceGetCommand,
    .log = {'\0'},
    .next = NULL,

};

struct InputCommander *addVoiceToCommandLink(struct InputCommander *phead)
{
    if(phead == NULL){
        return &voiceControl;
    
    }else{
        voiceControl.next=phead;
        phead = &voiceControl;
        return phead;
    }
}
相关推荐
武汉唯众智创2 天前
浅谈智能家居在智慧养老实训室中的作用
智能家居·养老实训室·智慧养老实训室
stm32发烧友2 天前
基于STM32的智能家居安防系统设计
stm32·嵌入式硬件·智能家居
凉开水白菜5 天前
米家通过HomeAssistant控制笔记本电脑开关机
python·电脑·智能家居·米家
脑极体5 天前
大模型,智能家居的春秋战国之交
智能家居
尘浮生5 天前
Java项目实战II基于Java+Spring Boot+MySQL的体育馆使用预约平台的设计与实现(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·maven·智能家居
武汉唯众智创5 天前
软件工程技术专业在物联网应用开发中的关键技术与挑战
智能家居·软件工程技术·物联网应用开发
faec1136 天前
2024年双十一有什么好物推荐?双十一必买好物清单大揭秘
智能家居
远翔调光芯片^138287988726 天前
远翔升压恒流芯片FP7209X与FP7209M什么区别?做以下应用市场摄影补光灯、便携灯、智能家居(调光)市场、太阳能、车灯、洗墙灯、舞台灯必看!
科技·单片机·智能家居·能源
stm32发烧友7 天前
基于STM32的智能家居环境监测系统设计
stm32·嵌入式硬件·智能家居
lucy153027510797 天前
【青牛科技】GC2803:白色家电与安防领域中 ULN2803 的卓越替代者
科技·单片机·智能家居·能源·安防·开关电源·白色家电