智能家居(工厂模式)

摘自:智能家居
作者:LEO-max
发布时间: 2021-03-05 00:38:07
网址:https://blog.csdn.net/zouchengzhi1021/article/details/114375529?spm=1001.2014.3001.5502

目录

    • 主流程设计框架及某一功能框架编写
        • InputCommand.h框架
        • contrlDevices.h框架
        • bathroomLight.c框架
    • 四盏灯、火焰传感器及主程序代码(继电器输入控制)
        • bathroomLight.c
        • upstairLight.c
        • livingroomLight.c
        • restaurantLight.c
        • fire.c
        • mainPro.c
        • contrlDevices.h
    • 添加声音识别模块的串口读取功能
        • voiceContrl.c
        • InputCommand.h
        • mainPro.c
    • 添加socket服务器功能
        • socketContrl.c
        • InputCommand.h
        • mainPro.c
    • 主程序代码编写,实现语音和网络线程
        • socketContrl.c
        • voiceContrl.c
        • mainPro.c

主流程设计框架及某一功能框架编写

头文件:contrlDevices.hInputCommand.h
源文件:mainPro.cbathroomLight.cCamera.cfire.clivingroomLight.clock.crestaurantLight.csocketContrl.cupstairLight.cusartContrl.cvoiceContrl.c
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

InputCommand.h框架

struct InputCommander
{char commandName[128];	//名字char command[32];		//指令int (*Init)(char* name, char* ipAddress, char* port);	//操作函数int (*getCommand)(char* cmd);	//获得数据char log[1024];struct InputCommander* next;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

contrlDevices.h框架

struct Devices
{char devicesName[128];int status;				//状态int (*open)();			//打开int (*close)();			//关闭int (*devicesInit)();	//初始化int (*readStatus)();	//读状态int(*changeStatus)(int status);		//改变状态struct Devices* next;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

bathroomLight.c框架

#include "contrlDevices.h"int bathroomLightOpen()
{}int bathroomLightClose()
{}int bathroomLightCloseInit()
{}int bathroomLightCloseStatus(int status)
{}struct Devices bathroomLight
{.name = "bathLight",.open = bathroomLightOpen,.close = bathroomLightClose,.deviceInit = bathroomLightCloseInit,.changeStatus = bathroomLightCloseStatus
};struct Devices* addBathroomLightToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &bathroomLight;}else {bathroomLight.next = phead;	//	头插法phead = &bathroomLight}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

四盏灯、火焰传感器及主程序代码(继电器输入控制)

bathroomLight.c

#include "contrlDevices.h"int bathroomLightOpen(int pinNum)	//打开
{digitalWrite(pinNum, LOW);	//低电平开启
}int bathroomLightClose(int pinNum)	//关闭
{digitalWrite(pinNum, HIGH);	//高电平关闭
}int bathroomLightCloseInit(int pinNum)	//初始化
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);
}int bathroomLightCloseStatus(int status)	//保存
{}struct Devices bathroomLight =
{.devicesName = "bathLight",.pinNum = 22,.open = bathroomLightOpen,.close = bathroomLightClose,.deviceInit = bathroomLightCloseInit,.changeStatus = bathroomLightCloseStatus
};struct Devices* addBathroomLightToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &bathroomLight;}else {bathroomLight.next = phead;	//	头插法phead = &bathroomLight;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

upstairLight.c

#include "contrlDevices.h"int upstairsLightOpen(int pinNum)	//打开
{digitalWrite(pinNum, LOW);	//低电平开启
}int upstairsLightClose(int pinNum)	//关闭
{digitalWrite(pinNum, HIGH);	//高电平关闭
}int upstairsLightCloseInit(int pinNum)	//初始化
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);
}int upstairsLightCloseStatus(int status)	//保存
{}struct Devices upstairsLight =
{.devicesName = "upstairsLight",.pinNum = 21,.open = upstairsLightOpen,.close = upstairsLightClose,.deviceInit = upstairsLightCloseInit,.changeStatus = upstairsLightCloseStatus
};struct Devices* addUpstairsLightToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &upstairsLight;}else {upstairsLight.next = phead;	//	头插法phead = &upstairsLight;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

livingroomLight.c

#include "contrlDevices.h"int livingroomLightOpen(int pinNum)	//打开
{digitalWrite(pinNum, LOW);	//低电平开启
}int livingroomLightClose(int pinNum)	//关闭
{digitalWrite(pinNum, HIGH);	//高电平关闭
}int livingroomLightCloseInit(int pinNum)	//初始化
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);
}int livingroomLightCloseStatus(int status)	//保存
{}struct Devices livingroomLight =
{.devicesName = "livingroomLight",.pinNum = 23,.open = livingroomLightOpen,.close = livingroomLightClose,.deviceInit = livingroomLightCloseInit,.changeStatus = livingroomLightCloseStatus
};struct Devices* addLivingroomLightToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &livingroomLight;}else {livingroomLight.next = phead;	//	头插法phead = &livingroomLight;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

restaurantLight.c

#include "contrlDevices.h"int livingroomLightOpen(int pinNum)	//打开
{digitalWrite(pinNum, LOW);	//低电平开启
}int livingroomLightClose(int pinNum)	//关闭
{digitalWrite(pinNum, HIGH);	//高电平关闭
}int livingroomLightCloseInit(int pinNum)	//初始化
{pinMode(pinNum, OUTPUT);digitalWrite(pinNum, HIGH);
}int livingroomLightCloseStatus(int status)	//保存
{}struct Devices livingroomLight =
{.devicesName = "livingroomLight",.pinNum = 23,.open = livingroomLightOpen,.close = livingroomLightClose,.deviceInit = livingroomLightCloseInit,.changeStatus = livingroomLightCloseStatus
};struct Devices* addLivingroomLightToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &livingroomLight;}else {livingroomLight.next = phead;	//	头插法phead = &livingroomLight;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

fire.c

#include "contrlDevices.h"int fireStatusRead(int pinNum)
{return digitalWrite(pinNum, HIGH);
}int fireInit(int pinNum)	//初始化
{pinMode(pinNum, INPUT);digitalWrite(pinNum, HIGH);
}struct Devices fire =
{.devicesName = "fire",.pinNum = 25,.deviceInit = fireInit,.readStatus = fireStatusRead
};struct Devices* addFireToDeviceLink(struct Devices* phead)
{if (phead == NULL) {return &fire;}else {fire.next = phead;	//	头插法phead = &fire;}
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.h"struct Devices* findDeviceByName(char* name, struct Devices* phead)
{struct Devices* tmp = phead;if (phead == NULL) {return NULL;}else {while (tmp != NULL) {if (strcmp(tmp->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;if (wiringPiSetup() == -1) {return -1;}struct Devices* pdeviceHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead)//struct Devices* tmp = findDeviceByName(name, pdeviceHead);while(1){printf("Inupt:\n");scanf("%s", name);tmp = findDeviceByName(name, pdeviceHead);if (tmp != NULL) {tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}// 1.指令工厂初始化// 2.设备控制工厂初始化// 3.线程池建立// 3.1 语音线程// 3.2 socket线程// 3.3 摄像头线程// 3.4 火灾线程return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

contrlDevices.h

#include <wiringPi.h>
#include <stdlib.h>struct Devices
{char devicesName[128];int status;				//状态int pinNum;int (*open)(int pinNum);			//打开int (*close)(int pinNum);			//关闭int (*deviceInit)(int pinNum);	//初始化int (*readStatus)();	//读状态int(*changeStatus)(int status);		//改变状态struct Devices* next;
};struct Devices* addRestaurantLightToDeviceLink(struct Devices* phead);
struct Devices* addLivingroomLightToDeviceLink(struct Devices* phead);
struct Devices* addBathroomLightToDeviceLink(struct Devices* phead);
struct Devices* addUpstairsLightToDeviceLink(struct Devices* phead);
struct Devices* addFireToDeviceLink(struct Devices* phead)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

添加声音识别模块的串口读取功能

voiceContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>int voiceInit(struct InputCommander *voicer, char* ipAddress, char* port)
{int fd;if ((fd = serialOpen(voicer->deviceName, 9600)) == -1) {	//初始化串口,波特率9600exit(-1);}voicer->fd = fd;return fd;
}int voiceGetCommand(struct InputCommander* voicer)
{int nread = 0;nread = read(voicer->fd, voicer->command, sizeof(voicer->command));if (nread == 0) {printf("usart for voice read over time\n");}else {return nread;}
}struct InputCommander voiceContrl = 
{.commandName = "voice",.deviceName = "/dev/ttyAMAO",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &voiceContrl;}else {voiceContrl.next = phead;	//	头插法phead = &voiceContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

InputCommand.h

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>int voiceInit(struct InputCommander *voicer, char* ipAddress, char* port)
{int fd;if ((fd = serialOpen(voicer->deviceName, 9600)) == -1) {	//初始化串口,波特率9600exit(-1);}voicer->fd = fd;return fd;
}int voiceGetCommand(struct InputCommander* voicer)
{int nread = 0;nread = read(voicer->fd, voicer->command, sizeof(voicer->command));if (nread == 0) {printf("usart for voice read over time\n");}else {return nread;}
}struct InputCommander voiceContrl = 
{.commandName = "voice",.deviceName = "/dev/ttyAMAO",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &voiceContrl;}else {voiceContrl.next = phead;	//	头插法phead = &voiceContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.h"
#include "InputCommand.h"struct Devices* findDeviceByName(char* name, struct Devices* phead)
{struct Devices* tmp = phead;if (phead == NULL) {return NULL;}else {while (tmp != NULL) {if (strcmp(tmp->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;if (wiringPiSetup() == -1) {return -1;}struct Devices* pdeviceHead = NULL;struct InputCommander* pcommandHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);pcommandHead = addVoiceContrlToCommandLink(pcommandHead);//struct Devices* tmp = findDeviceByName(name, pdeviceHead);while(1){printf("Inupt:\n");scanf("%s", name);tmp = findDeviceByName(name, pdeviceHead);if (tmp != NULL) {tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}// 1.指令工厂初始化// 2.设备控制工厂初始化// 3.线程池建立// 3.1 语音线程// 3.2 socket线程// 3.3 摄像头线程// 3.4 火灾线程return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

添加socket服务器功能

socketContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>int socketInit(struct InputCommander* socketMes, char* ipAddress, char* port)
{int s_fd;int c_fd;struct sockaddr_in s_addr;/*struct sockaddr_in c_addr;*///做初始化memset(&s_addr, 0, sizeof(struct sockaddr_in));/*memset(&c_addr, 0, sizeof(struct sockaddr_in));*/// sockets_fd = socket(AF_INET, SOCK_STREAM, 0);if (s_fd == -1) {perror("socket");exit(-1);}s_addr.sin_family = AF_INET;s_addr.sin_port = htons(atoi(socketMes->port));inet_aton(socketMes->ipAddress, &s_addr.sin_addr);// bindbind(s_fd, (struct sockaddr*) & s_addr, sizeof(struct sockaddr_in));//listenlisten(s_fd, 10);socketMes->sfd = s_fd;return s_fd;
}int socketGetCommand(struct InputCommander* socketMes)
{int c_fd;int n_read;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);c_fd = accept(socketMes->sfd, (struct sockaddr*) & c_addr, &clen);n_read = read(c_fd, socketMes->command, sizeof(socketMes->command));if (n_read == -1) {perror("read");}else if (n_read > 0) {printf("\get:%d\n", n_read);}else {printf("client quit\n");}return n_read;
}struct InputCommander socketContrl =
{.commandName = "socketServer",.command = {'\0'},.port = "8088",.ipAddress = "192.168.4.126",.Init = socketInit,.getCommand = socketGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addSocketContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &socketContrl;}else {socketContrl.next = phead;	//	头插法phead = &socketContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

InputCommand.h

#include <wiringPi.h>
#include <stdlib.h>struct InputCommander
{char commandName[128];	//名字char deviceName[128];char command[32];		//指令int (*Init)(struct InputCommander* voicer, char* ipAddress, char* port);	//操作函数int (*getCommand)(struct InputCommander* voicer);	//获得数据char log[1024];int fd;char  port[12];char ipAddress[32];int sfd;struct InputCommander* next;
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead);
struct InputCommander* addSocketContrlToCommandLink(struct InputCommander* phead);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.h"
#include "InputCommand.h"struct Devices* findDeviceByName(char* name, struct Devices* phead)
{struct Devices* tmp = phead;if (phead == NULL) {return NULL;}else {while (tmp != NULL) {if (strcmp(tmp->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;if (wiringPiSetup() == -1) {return -1;}struct Devices* pdeviceHead = NULL;struct InputCommander* pcommandHead = NULL;pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);pcommandHead = addVoiceContrlToCommandLink(pcommandHead);pcommandHead = addSocketContrlToCommandLink(pcommandHead);//struct Devices* tmp = findDeviceByName(name, pdeviceHead);while(1){printf("Inupt:\n");scanf("%s", name);tmp = findDeviceByName(name, pdeviceHead);if (tmp != NULL) {tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}// 1.指令工厂初始化// 2.设备控制工厂初始化// 3.线程池建立// 3.1 语音线程// 3.2 socket线程// 3.3 摄像头线程// 3.4 火灾线程return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

主程序代码编写,实现语音和网络线程

socketContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>int socketInit(struct InputCommander* socketMes, char* ipAddress, char* port)
{int s_fd;int c_fd;struct sockaddr_in s_addr;/*struct sockaddr_in c_addr;*///做初始化memset(&s_addr, 0, sizeof(struct sockaddr_in));/*memset(&c_addr, 0, sizeof(struct sockaddr_in));*/// sockets_fd = socket(AF_INET, SOCK_STREAM, 0);if (s_fd == -1) {perror("socket");exit(-1);}s_addr.sin_family = AF_INET;s_addr.sin_port = htons(atoi(socketMes->port));inet_aton(socketMes->ipAddress, &s_addr.sin_addr);// bindbind(s_fd, (struct sockaddr*) & s_addr, sizeof(struct sockaddr_in));//listenlisten(s_fd, 10);printf("socket Server listening\n");socketMes->sfd = s_fd;return s_fd;
}int socketGetCommand(struct InputCommander* socketMes)
{int c_fd;int n_read=0;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);c_fd = accept(socketMes->sfd, (struct sockaddr*) & c_addr, &clen);n_read = read(c_fd, socketMes->command, sizeof(socketMes->command));if (n_read == -1) {perror("read");}else if (n_read > 0) {printf("\get:%d\n", n_read);}else {printf("client quit\n");}return n_read;
}struct InputCommander socketContrl =
{.commandName = "socketServer",.command = {'\0'},.port = "8088",.ipAddress = "192.168.4.126",.Init = socketInit,.getCommand = socketGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addSocketContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &socketContrl;}else {socketContrl.next = phead;	//	头插法phead = &socketContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

voiceContrl.c

#include "InputCommand.h"
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>int voiceInit(struct InputCommander* voicer, char* ipAddress, char* port)
{int fd;if ((fd = serialOpen(voicer->deviceName, 9600)) == -1) {	//初始化串口,波特率9600exit(-1);}voicer->fd = fd;return fd;
}int voiceGetCommand(struct InputCommander* voicer)
{int nread = 0;memset(voicer->command, '\0',sizeof(voicer->command));nread = read(voicer->fd, voicer->command, sizeof(voicer->command));if (nread == 0) {printf("voice no datas\n");}else {return nread;}}struct InputCommander voiceContrl = 
{.commandName = "voice",.deviceName = "/dev/ttyAMAO",.command = {'\0'},.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};struct InputCommander* addVoiceContrlToCommandLink(struct InputCommander* phead)
{if (phead == NULL) {return &voiceContrl;}else {voiceContrl.next = phead;	//	头插法phead = &voiceContrl;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

mainPro.c

#include <stdio.h>
#include <string.h>
#include "contrlDevices.h"
#include "InputCommand.h"
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>//全局变量
struct InputCommander* pcommandHead = NULL;
struct Devices* pdeviceHead = NULL;
struct InputCommander* socketHandler = NULL;
int c_fd;struct Devices* findDeviceByName(char* name, struct Devices* phead)
{struct Devices* tmp = phead;if (phead == NULL) {return NULL;}else {while (tmp != NULL) {if (strcmp(tmp->devicesName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}struct InputCommander* findCommandByName(char* name, struct InputCommander* phead)
{struct InputCommander* tmp = phead;if (phead == NULL) {return NULL;}else {while (tmp != NULL) {if (strcmp(tmp->commandName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}void* voice_thread(void* datas)
{struct InputCommander* voiceHandler;int nread;voiceHandler = findCommandByName("voice", pCommandHead);if (voiceHandler == NULL) {printf("find voiceHandler error");pthread_exit(NULL);//return NULL;}else {if (voiceHandler->Init(voiceHandler,NULL,NULL) < 0) {printf("voice init error\n");pthread_exit(NULL);//return NULL;}else {printf("%s init success\n", voiceHandler->commandName);}while (1) {nread = voiceHandler->getCommand(voiceHandler);if (nread == 0) {printf("nodata from voice\n");}else {printf("do divece contrl:%s\n", voiceHandler->command);}}}
}void* read_thread(void* datas)
{int n_read;memset(socketHandler->command,'\0', sizeof(socketHandler->command));n_read = read(c_fd, socketHandler->command, sizeof(socketHandler->command));if (n_read == -1) {perror("read");}else if (n_read > 0) {printf("\nget:%d,%s\n", n_read, socketHandler->command);}else {printf("client quit\n");}return n_read;
}void* socket_thread(void* datas)
{int n_read=0;pthread_t* readThread;struct sockaddr_in c_addr;memset(&c_addr, 0, sizeof(struct sockaddr_in));int clen = sizeof(struct sockaddr_in);//int nread;socketHandler = findCommandByName("socketServer", pdeviceHead);if (socketHandler == NULL) {printf("find socketHandler error");pthread_exit(NULL);//return NULL;}else {printf("%s init success\n", socketHandler->commandName);}socketHandler->Init(socketHandler,NULL,NULL);while (1) {c_fd = accept(socketHandler->sfd, (struct sockaddr*) & c_addr, &clen);pthread_create(&readThread, NULL, read_thread, NULL);}
}int main()
{//char* name = "bathroomLight";char name[128];struct Devices* tmp = NULL;pthread_t* voiceThread;pthread_t* socketThread;if (wiringPiSetup() == -1) {return -1;}// 1.指令工厂初始化pcommandHead = addVoiceContrlToCommandLink(pcommandHead);pcommandHead = addSocketContrlToCommandLink(pcommandHead);// 2.设备控制工厂初始化pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);pdeviceHead = addFireToDeviceLink(pdeviceHead);// 3.线程池建立// 3.1 语音线程pthread_create(&voiceThread,NULL, voice_thread,NULL);// 3.2 socket线程pthread_create(&socketThread, NULL, socket_thread, NULL);// 3.3 摄像头线程// 3.4 火灾线程//struct Devices* tmp = findDeviceByName(name, pdeviceHead);/*while (1) {printf("Inupt:\n");scanf("%s", name);tmp = findDeviceByName(name, pdeviceHead);if (tmp != NULL) {tmp->deviceInit(tmp->pinNum);tmp->open(tmp->pinNum);}}*///不退出,等待线程pthread_join(voiceThread,NULL);pthread_join(socketThread, NULL);return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266745.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机应用基础0006 19秋在线作业2,川大《计算机应用基础0006》13春在线作业2

计算机,建筑工程川大《计算机应用基础0006》13春在线作业214春13秋都有 试卷总分&#xff1a;100 测试时间&#xff1a;--单选题、单选题(共 50 道试题&#xff0c;共 100 分。)1. 在Windows资源管理器中&#xff0c;要把图标设置成缩略图方式&#xff0c;应在下面哪组菜单中设…

【c++基础】vector中按照Point类型某一个变量进行排序

code // sort(a.begin(), a.end(), cmpy);//subfunction bool cmpy(cv::Point const& a, cv::Point const& b) {return a.y < b.y; } 完转载于:https://www.cnblogs.com/happyamyhope/p/9269457.html

二叉树的三叉链表存储和基本操作

三叉链表存储表示 改进于二叉链表&#xff0c;增加指向父节点的指针&#xff0c;能更好地实现结点间的访问。 存储结构 /* 二叉树的三叉链表存储表示 */typedef struct BiTPNode{TElemType data;struct BiTPNode *parent,*lchild,*rchild; /* 双亲、左右孩子指针 */}BiTPNode,*…

麟龙指标通达信指标公式源码_通达信指标公式源码单阳不破主图指标公式

做价值的传播者&#xff0c;一路同行&#xff0c;一起成长问题&#xff1a;怎样才能每天都收到这类文章&#xff01;答案&#xff1a;只需点击上方《通达信公式指标》{通达信单阳不破}MA30: MA(C,30 ),COLORGREEN;MA30_: IF(MA30>REF(MA30,1),MA30,DRAWNULL),COLORMAGENTA;阳…

计算机基础中的分层教学,分层教学法在计算机基础课程中的应用研究

摘要&#xff1a;随着信息科技的发展&#xff0c;计算机技术发展成为国人关注的重点。职业教育作为中国教育的关注重点之一&#xff0c;其在近些年的发展中&#xff0c;由于不断扩大招生地源&#xff0c;学生的学习能力和学习进度都相差很大&#xff0c;再加上传统的教学方法&a…

树莓派外设开发之玩传感器

参考&#xff1a;用树莓派玩传感器 作者&#xff1a;LEO-max 发布时间&#xff1a; 2021-02-24 16:30:46 网址&#xff1a;https://blog.csdn.net/zouchengzhi1021/article/details/114026649?spm1001.2014.3001.5502 目录接线红外避障传感器声音传感器火焰传感器烟雾传感器超…

xxx.jar 中没有主清单属性

xxx.jar 中没有主清单属性 springboot 中是可以通过 jar 将整个项目打包成一个fat jar 的, 这个大家都知道. <!-- 打包jar文件时&#xff0c;配置manifest文件&#xff0c;加入lib包的jar依赖 --><plugin><groupId>org.apache.maven.plugins</groupId>…

面试软件设计人员的方法,附面试题。我的面试注重实效

前段时间招聘。因为我一直在我的部门推行一些有效却被绝大多数中国公司忽视的开发理念&#xff0c;比如平级人事结构、测试驱动开发、制度化绩效、设计先行、迭发等等&#xff0c;所以招软件设计师非常困难。最终问题还算解决了吧。以下是我的面试总结。  一般来说&#xff0…

rtc校准算法_STM32实时时钟RTC日历算法

本帖最后由 shipeng1989 于 2019-5-16 08:32 编辑最近工作上需要利用STM32的自带RTC编写日历功能。其实对于公历万年历并不是很复杂&#xff0c;但是由于自带RTC断电后只能做32位二进制秒计数不能更新日期信息&#xff0c;要从根本上解决这个问题就需要编写一个算法可以根据计数…

全国计算机等级考试题库二级C操作题100套(第90套)

第90套&#xff1a; 函数fun的功能是&#xff1a;统计所有小于等于n(n>2)的素数的个数&#xff0c;素数的个数作为函数值返回。 请在程序的下划线处填入正确的内容并把下划线删除&#xff0c;使程序得出正确的结果。 注意&#xff1a;源程序存放在考生文件夹下的BLANK1.C中…

智能合约从入门到精通:完整范例

简介&#xff1a;前几篇文章我们一直在讨论Solidity语言的相关语法&#xff0c;从本文开始&#xff0c;我们将介绍智能合约开发。今天我们将介绍一个完整范例。 此章节将介绍一个完整案例来帮助开发者快速了解合约的开发规范及流程。 注意&#xff1a; 在进行案例编写前&#x…

高考英语口语测试软件,练习英语口语的app有哪些

随着国际化的发展&#xff0c;英语越来越普遍。英语中口语也是很重要的成分之一。也是跟老外交流或者英语考试的重要考点之一。我们有很多方法锻炼口语。什么方法最快捷最有效方便&#xff1f;下面我们看看几款最受欢迎的英语口语练习app。练习口语的app1、口语侠一款非常实用的…

chkconfig命令会立即生效吗_UG绘制波纹管,整体变形命令你会用吗?

最近又有小伙伴问我波纹管怎么画了&#xff0c;这个以前教过大家&#xff0c;文章名字叫饮料吸管和排水管&#xff0c;看来这个名字不够专业&#xff0c;大家找不到啊&#xff0c;今天就来重新绘制一个&#xff0c;用一个新的方法&#xff0c;大家来额外学习一下&#xff0c;了…

Windows Phone 8 开发资源汇总

1) 提前下载安装包&#xff0c;离线安装https://dev.windowsphone.com/en-us/downloadsdk 2&#xff09;在线安装地址http://www.microsoft.com/en-us/download/details.aspx?id354713) 案例代码&#xff1a;Windows Phone Samples: learn through code http://code.msdn.micr…

vue中mixin 感觉很牛逼(父子组件融合成一个新组件)

vue中提供了一种混合机制--mixins&#xff0c;用来更高效的实现组件内容的复用。最开始我一度认为这个和组件好像没啥区别。。后来发现错了。下面我们来看看mixins和普通情况下引入组件有什么区别&#xff1f; 组件在引用之后相当于在父组件内开辟了一块单独的空间&#xff0c;…

C语言入门日记

参考&#xff1a;C语言入门日记 作者&#xff1a;9art0 发布时间&#xff1a;2020-08-30 16:37:46 网址&#xff1a;https://blog.csdn.net/GatoWong/article/details/108307915?spm1001.2014.3001.5501 C语言入门日记1.1.C语言搭建及system函数1.2.C语言的基础框架解释1.3.C程…

测试电视是不是4k的软件,怎么判断4K电视真假?教你快速检测的方法!

原标题&#xff1a;怎么判断4K电视真假?教你快速检测的方法!4K电视从进入市场之后一直都受到企业的力捧&#xff0c;随着电视企业对4K电视的大力度宣传和消费环境的逐渐成熟&#xff0c;越来越多的消费者开始认可4K电视&#xff0c;并在购机时表明首选4K电视。4K电视顾名思义就…

等压线上怎么画风向_战场上骑兵应该怎么拔刀?从清人佩刀为何总是刀柄向后说起...

在以前的文章《兵器谱|挂错地方死得快&#xff01;图说骑兵马刀的佩带与悬挂》&#xff0c;有读者问&#xff1a;清代的武将大多数都是刀柄朝右边&#xff0c;挂在屁股后面&#xff0c;算不算双附耳式悬挂法的一个变种呢&#xff1f;这样到底是不是真的有助于马上拔刀&#xff…

整合axis2到web项目中

1.将axis2.war发布到%TOMCAT_HOME%\webapps中&#xff0c;发布后的目录为%TOMCAT_HOME%\webapps\axis2 &#xff0c;该目录定义为%AXIS2_HOME%. 2.拷贝%AXIS2_HOME%\WEB-INF目录下所有内容到%项目App%\WEB-INF目录下。 3.修改%项目APP%\WEB-INF目录下的web.xml文件&#xff0c…

win10计算机跑分,鲁大师如何跑分_鲁大师跑分详细教程

很多小伙伴都知道鲁大师可以测试电脑的性能&#xff0c;就是大家称为的跑分&#xff0c;跑分的大小跟电脑的性能成正比&#xff0c;跑的越高性能越好。那么该如何在鲁大师里面进行跑分测试呢?其实只要打开性能测试就可以了&#xff0c;具体的教程下面一起来看看吧。鲁大师跑分…