封裝語音線程(語音通過串口和主控設備進行交流)實現對智能家居中各種燈光的控制
mainPro.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);}elseprintf("%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(控制類)
#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(語音)
#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;}elsevoiceMes->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;}
}