主流程设计框架及某一功能框架编写
头文件:
contrlDevices.h
InputCommand.h
源文件:
mainPro.c
bathroomLight.c
Camera.c
fire.c
livingroomLight.c
lock.c
restaurantLight.c
socketContrl.c
upstairLight.c
usartContrl.c
voiceContrl.c
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;
};
contrlDevices.h框架
struct Devices
{
char devicesName[128];
int status;
int (*open)();
int (*close)();
int (*devicesInit)();
int (*readStatus)();
int(*changeStatus)(int status);
struct Devices* next;
};
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
}
};
四盏灯、火焰传感器及主程序代码(继电器输入控制)
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;
}
};
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;
}
};
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;
}
};
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;
}
};
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;
}
};
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[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)
while(1){
printf("Inupt:\n");
scanf("%s", name);
tmp = findDeviceByName(name, pdeviceHead);
if (tmp != NULL) {
tmp->deviceInit(tmp->pinNum);
tmp->open(tmp->pinNum);
}
return 0;
}
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)
添加声音识别模块的串口读取功能
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) {
exit(-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;
}
}
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) {
exit(-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;
}
}
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[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);
while(1){
printf("Inupt:\n");
scanf("%s", name);
tmp = findDeviceByName(name, pdeviceHead);
if (tmp != NULL) {
tmp->deviceInit(tmp->pinNum);
tmp->open(tmp->pinNum);
}
return 0;
}
添加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;
memset(&s_addr, 0, sizeof(struct sockaddr_in));
s_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);
bind(s_fd, (struct sockaddr*) & s_addr, sizeof(struct sockaddr_in));
listen(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;
}
}
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);
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[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);
while(1){
printf("Inupt:\n");
scanf("%s", name);
tmp = findDeviceByName(name, pdeviceHead);
if (tmp != NULL) {
tmp->deviceInit(tmp->pinNum);
tmp->open(tmp->pinNum);
}
return 0;
}
主程序代码编写,实现语音和网络线程
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;
memset(&s_addr, 0, sizeof(struct sockaddr_in));
s_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);
bind(s_fd, (struct sockaddr*) & s_addr, sizeof(struct sockaddr_in));
listen(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;
}
}
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) {
exit(-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;
}
}
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);
}
else {
if (voiceHandler->Init(voiceHandler,NULL,NULL) < 0) {
printf("voice init error\n");
pthread_exit(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);
socketHandler = findCommandByName("socketServer", pdeviceHead);
if (socketHandler == NULL) {
printf("find socketHandler error");
pthread_exit(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[128];
struct Devices* tmp = NULL;
pthread_t* voiceThread;
pthread_t* socketThread;
if (wiringPiSetup() == -1) {
return -1;
}
pcommandHead = addVoiceContrlToCommandLink(pcommandHead);
pcommandHead = addSocketContrlToCommandLink(pcommandHead);
pdeviceHead = addBathroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addUpstairsLightToDeviceLink(pdeviceHead);
pdeviceHead = addLivingroomLightToDeviceLink(pdeviceHead);
pdeviceHead = addRestaurantLightToDeviceLink(pdeviceHead);
pdeviceHead = addFireToDeviceLink(pdeviceHead);
pthread_create(&voiceThread,NULL, voice_thread,NULL);
pthread_create(&socketThread, NULL, socket_thread, NULL);
pthread_join(voiceThread,NULL);
pthread_join(socketThread, NULL);
return 0;
}