基于Linux的智能家居(工厂模式)

news/2024/10/2 22:29:49 标签: 智能家居, linux, 香橙派

目录

1.项目概述

2.程序框架

3.函数准备

3.1需要函数知识点

3.2编码提醒

4.代码

5.注意事项


1.项目概述

控制端有两个,语音串口UART和Tcp通讯。

执行端有IO输出和IO输入。

2.程序框架

程序分为3部分-------------1.输入控制  2.输出设备  3.主函数-多线程

输入控制的模块----实现指令的收集,并将数据放在一个定义的<cmd.h>头文件种。

输出设备模块----实现设备初始化、设备启动、设备关闭的函数封装放在<device.h>种。

主函数-----实现UART和Tcp两个接收线程,不断接收指令并解析指令调用不同的设备函数封装

亮点,在<.h>封装了一个类,调用<.h>可以编码一个对象,然后用链表将一类对象串起来。

将对象的功能实现都封装成了函数,放在对象的结构体中,使用时直接调用。

3.函数准备

3.1需要函数知识点

main中

  • 线程创建、线程退出、线程等待、wiringPi库、链表增加、链表查找

<cmd.h><device.h>中

  • 链表创建、函数指针

设备控制、输入控制中

  • wiringPi库、结构体赋值、链表增加

3.2编码提醒

出现不能用NULL指针,包含头文件<stdlib.h>

香橙派编译wiringpi库时用gcc  buzzer.c -o test -lwiringPi -lwiringPiDev -lpthread -Im -Icrypt -Irt

C语言函数没有return语句时,会返回最后一次赋值的变量的值

函数指针是定义一个指针指向函数,调用时直接用P()或者(*p)()都行,函数名和函数地址一样

指针有点神奇,指针是一个地址,但是同时也可以直接用指针使用地址里的值

没办法:这里的代码在结构体实现里用了函数的递归。错错错。第575课,添加声音识别模块,形参和实参不一样的。形参只是形式,需要函数调用时赋予实参。

炫技:这里的代码用了链表串联设备

堆栈的存储:在函数体前面的变量,后面的函数可以直接用

Orangepi的引脚图

4.代码

main中

#include <stdio.h>
#include <stdlib.h>
#include "contrldevice.h"  
#include "inputcmd.h"
#include <string.h>
#include <wiringPi.h>
#include <pthread.h>
#include<unistd.h>

struct OutputDevice * pdevicehead=NULL; /*device head */
struct InputDevice  *  pInputhead=NULL;  /*Input head */
struct InputDevice  *  tmp=NULL;

struct OutputDevice *find_cmd_device(char *name,struct OutputDevice * pdevicehead)
{
	struct OutputDevice *tmp=pdevicehead;
	
	if(tmp==NULL ){   /* compare is correct*/
		return NULL;
	}else{
		while(tmp!=NULL){
			if(0==strcmp(tmp->device_name,name)){
				return tmp;
			}
			tmp=tmp->next;
		}
		return NULL;
	}
}

struct InputDevice *find_input_device(char *name,struct InputDevice * pdevicehead)
{
	struct InputDevice *tmp=pdevicehead;
	
	if(tmp==NULL ){   /* compare is correct*/
		return NULL;
	}else{
		while(tmp!=NULL){
			if(0==strcmp(tmp->cmddevice_name,name)){
				return tmp;
			}
			tmp=tmp->next;
		}
		return NULL;
	}
}

void * voice_thread(void *data)  /* voice_thread */
{   
	int nread;
	struct InputDevice * tmp=find_input_device("voice",pInputhead);
	if(tmp==NULL){
		printf("no find voice phead!\r\n");
		pthread_exit(NULL);
		
	}else{
		if(tmp->init(tmp)==-1){
			printf("voice init faild!\r\n");
			pthread_exit(NULL);
		}
	}
	
	while(1){
		memset(tmp->cmd,0,sizeof(tmp->cmd));  
		nread=tmp->getcmd(tmp);            /* when uart no data come cmd while get uart overtime */
		if(nread==0){
			printf("voice cmd no data arival!\r\n");
		}else{
			printf("voice recive %d data:%s\r\n",nread,tmp->cmd);
		}
	}	

}

void * sockread_thread(void *data) 
{
	int nread;
	while(1){
		memset(tmp->cmd,'\0',sizeof(tmp->cmd));
		nread=read(tmp->ss_fd,&tmp->cmd,sizeof(tmp->cmd));
		if(nread==0){                 /* cannot recv cmd */ 
			printf("connect is cutdon! \r\n");
			pthread_exit(NULL);
		}else{                         /*can recive cmd */ 
			printf("server  receved %d cmd:%s \r\n",nread,tmp->cmd); 	
		}
	}
			
}


void * socket_thread(void *data) 
{
	int nread;
	pthread_t socketreadthread;
	tmp=find_input_device("socket",pInputhead);
	if(tmp==NULL){
		printf("no find socket phead!\r\n");
		pthread_exit(NULL);
		
	}else{
		if(tmp->init(tmp)==-1){
			printf("socket init faild!\r\n");
			pthread_exit(NULL);
		}
	}
		
	while(1){
		
		if(tmp->getcmd(tmp)==-1){
			perror("error");  /* get ss_fd error  */
		}
		/*socket  read线程*/
		pthread_create(&socketreadthread,NULL,sockread_thread,NULL);
		pthread_join(socketreadthread,NULL);   /* 等读线程退出在进行  */
	}
}



int main(void)
{
	char device_name[32];

	pthread_t voicethread; 
	pthread_t socketthread; 
	

	
	if(-1==wiringPiSetup()){
		printf("wiringpi init fail!\r\n");
		return 0;
	}
	/*1.指令工厂初始化  加入设备 */
	pInputhead=add_input_voice(pInputhead);
	pInputhead=add_input_socket(pInputhead);
	
	/*2.设备工厂初始化   加入设备 */
	pdevicehead=add_device_dingnerlight(pdevicehead);
	pdevicehead=add_device_restlight(pdevicehead);
	pdevicehead=add_device_firedetect(pdevicehead);
	
	/*2.1找设备 */
	/*struct OutputDevice * tmp=find_cmd_device(device_name,pdevicehead);
	while(1){
		printf("input device name:");
		scanf("%s",device_name);
		tmp=find_cmd_device(device_name,pdevicehead);
		printf("input cmd name 1/2:");
		scanf("%d",&cmd);
		switch(cmd){
			case 1:
				tmp->init();
				tmp->open();
				break;
			case 2:
				tmp->init();
				tmp->close();
				break;
			default:
				printf("no this cmd");
				break;
		}
	}*/
	
	
/*3. 线程池建立*/
	/*3.1 语音线程*/
	pthread_create(&voicethread,NULL,voice_thread,NULL);
	
	/*3.2socket线程*/
	pthread_create(&socketthread,NULL,socket_thread,NULL);
	
	
	
	/*3.3摄像头线程*/
	/*3.4 火灾线程*/
	
	pthread_join(voicethread,NULL);  /*线程等待不让主程序退出*/
	pthread_join(socketthread,NULL);  /*线程等待不让主程序退出*/
	return 0; 
}

contrldevice.h

#include <stdio.h>

struct OutputDevice{
	char device_name[32];
	int status;
	
	int (*init)();
	int (*open)();
	int (*close)();
	
	int (*read)();
	int (*changeStatus)();
	
	struct OutputDevice *next;
	
};
struct OutputDevice * add_device_dingnerlight(struct OutputDevice *phead);
struct OutputDevice * add_device_restlight(struct OutputDevice *phead);
struct OutputDevice * add_device_firedetect(struct OutputDevice *phead);

inputcmd.h

#include <stdio.h> 


struct InputDevice{
	
	char cmddevice_name[32];
	char  cmd[32];
	int  fd;
	int  ss_fd;
	
	int (*init)();
	int (*getcmd)();
	
	struct InputDevice *next;
	
};

struct InputDevice * add_input_voice(struct InputDevice *phead);
struct InputDevice * add_input_socket(struct InputDevice *phead); 

dingnerlight.c

#include <stdio.h> 
#include "contrldevice.h"  //include i can use struct
#include <wiringPi.h>

#define dingnerlightPin 2
/*struct OutputDevive{
	char device_name[32];
	int status;
	
	int (*init)();
	int (*open)();
	int (*close)();
	
	int (*read)();
	int (*changeStatus)();
	
	sturct OutputDevive *next;
	
} */

int dingnerlight_init(void)
{
	pinMode(dingnerlightPin,OUTPUT);
	digitalWrite(dingnerlightPin,HIGH);
	
	return 0;
	
}
int dingnerlight_open(void)
{
	digitalWrite(dingnerlightPin,LOW);
	return 0;
}

int dingnerlight_close(void)
{
	digitalWrite(dingnerlightPin,HIGH);
	return 0;
}

struct OutputDevice dingnerlight={      /*"this give valve not init"*/
	.device_name="dingnerlight",
	.init=dingnerlight_init,
	.open=dingnerlight_open,
	.close=dingnerlight_close
};

struct OutputDevice * add_device_dingnerlight(struct OutputDevice *phead)
{
	if(phead==NULL){
		return &dingnerlight;	
	}else{
		dingnerlight.next=phead;
		phead=&dingnerlight;
		return phead;
			
	}
}

firedetect.c

#include <stdio.h> 
#include "contrldevice.h"  //include i can use struct
#include <wiringPi.h>

#define firedetectPin 6
/*struct OutputDevice{
	char device_name[32];
	int status;
	
	int (*init)();
	int (*open)();
	int (*close)();
	
	int (*read)();
	int (*changeStatus)();
	
	sturct OutputDevive *next;
	
} */

int firedetect_init(void)
{
	pinMode(firedetectPin,INPUT);
	
	
	return 0;	
}

int firedetect_read(void)
{
	return digitalRead(firedetectPin);
}


struct OutputDevice firedetect={      /*"this give valve not init"*/
	.device_name="firedetect",
	.init=firedetect_init,
	.read=firedetect_read
};

struct OutputDevice * add_device_firedetect(struct OutputDevice *phead)
{
	if(phead==NULL){
		return &firedetect;	
	}else{
		firedetect.next=phead;
		phead=&firedetect;
		return phead;
			
	}
}

restlight.c

#include <stdio.h> 
#include "contrldevice.h"  //include i can use struct
#include <wiringPi.h>

#define restlightPin 8
/*struct OutputDevice{
	char device_name[32];
	int status;
	
	int (*init)();
	int (*open)();
	int (*close)();
	
	int (*read)();
	int (*changeStatus)();
	
	sturct OutputDevice *next;
	
} */

int restlight_init(void)
{
	pinMode(restlightPin,OUTPUT);
	digitalWrite(restlightPin,HIGH);
	
	return 0;
	
}
int restlight_open(void)
{
	digitalWrite(restlightPin,LOW);
	return 0;
}

int restlight_close(void)
{
	digitalWrite(restlightPin,HIGH);
	return 0;
}

struct OutputDevice restlight={      /*"this give valve not init"*/
	.device_name="restlight",
	.init=restlight_init,
	.open=restlight_open,
	.close=restlight_close
};

struct OutputDevice * add_device_restlight(struct OutputDevice *phead)
{
	if(phead==NULL){
		return &restlight;	
	}else{
		restlight.next=phead;
		return &restlight;		
	}
}

socketcmd.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include "inputcmd.h"/*include i can use struct*/ 

/*struct InputDevice{
	
	char cmddevice_name[32];
	char  cmd[32];
	int  fd;
	int  ss_fd;
	
	int (*init)();
	int (*getcmd)();
	
	struct InputDevice *next;
	
}; */

int socket_getcmd(struct InputDevice *phead)
{
	int len;
	
	struct sockaddr_in c_ddr;  /*save clinet msg*/ 
	
	/*4.accept come and connect for once*/ 
	len=sizeof(c_ddr);

	
	phead->ss_fd=accept(phead->fd,(struct sockaddr *)&c_ddr,&len);
	if(phead->ss_fd == -1){
		perror("accept:");
	}
	/*5.read from connect ss_fd*/ 
	/*5.2 read*/ 
	
	
	printf("conect succese!==========\r\n");
		

				
	return phead->ss_fd;
}

int socket_init(struct InputDevice *phead)
{
	
	int s_fd;
 
	struct sockaddr_in s_ddr;  /*build server msg*/ 
 
	s_fd= socket(AF_INET, SOCK_STREAM, 0);/*1.build a soket specified*/ 
	if(s_fd==-1){
		perror("error is");
	}
	/*2.build all bind*/ 
	s_ddr.sin_family=AF_INET;
	s_ddr.sin_port=htons(8880);
	s_ddr.sin_addr.s_addr=htonl(INADDR_ANY);
	/*give the bind*/ 
	bind(s_fd,(struct sockaddr *)&s_ddr,sizeof(s_ddr));
	/*3.waite for client*/ 
	listen(s_fd,8);
	
	phead->fd=s_fd;

	return s_fd;
}

struct InputDevice struc_socket={
	.cmd={0},
	.cmddevice_name="socket",
	.init=socket_init,
	.getcmd=socket_getcmd
	
};

struct InputDevice * add_input_socket(struct InputDevice *phead)
{
	if(phead==NULL){
		return &struc_socket;	
	}else{
		struc_socket.next=phead;
		phead=&struc_socket;
		return phead;		
	}
}

voicecmd.c

  //include i can use struct
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <unistd.h>
#include "inputcmd.h"

/*struct InputDevice{
	
	char cmddevice_name[32];
	char  cmd[32];
	int fd;
	
	int (*init)();
	int (*getcmd)();
	
	sturct InputDevice *next;
	
} */

int voice_getcmd(struct InputDevice *phead)
{
	int nread;
	nread=read(phead->fd,phead->cmd,sizeof(phead->cmd));
	
	return nread;
	

}

int voice_init(struct InputDevice *phead)
{
	int fd1;
	if((fd1=serialOpen("/dev/ttyS5",115200))<0){
		printf("uart init fail!\r\n");
		exit(-1);
	}
	phead->fd=fd1;
	return fd1;
}

struct InputDevice voice={
	.cmd={0},
	.cmddevice_name="voice",
	.init=voice_init,
	.getcmd=voice_getcmd
	
};

struct InputDevice * add_input_voice(struct InputDevice *phead)
{
	if(phead==NULL){
		return &voice;	
	}else{
		voice.next=phead;
		phead=&voice;
		return phead;		
	}
}

5.注意事项


http://www.niftyadmin.cn/n/5021136.html

相关文章

【ES6知识】简介、语法变化、解构赋值

文章目录 一、概述1.1 ECMAScript 简介1.2 ECMAScript 背景1.3 ECMAScript 的历史1.4 ES6 的目标与愿景1.5 学习路线图1.6 环境搭建 二、语法变化2.1 let 与 const2.2 解构赋值2.3 Symbol 一、概述 1.1 ECMAScript 简介 ES6&#xff0c; 全称 ECMAScript 6.0 &#xff0c;是 …

【Java 基础篇】Java HashSet 集合详解:高效存储唯一元素的利器

Java 中的集合框架提供了各种各样的数据结构&#xff0c;用于存储和操作数据。其中&#xff0c;HashSet 是一种常用的集合类&#xff0c;它实现了 Set 接口&#xff0c;用于存储不重复的元素。本篇博客将详细介绍 HashSet 的基本概念、创建和初始化、基本操作、遍历、性能考虑、…

SpringCloud理解篇

一、微服务概述 1、什么是微服务 目前的微服务并没有一个统一的标准&#xff0c;一般是以业务来划分将传统的一站式应用&#xff0c;拆分成一个个的服务&#xff0c;彻底去耦合&#xff0c;一个微服务就是单功能业务&#xff0c;只做一件事。 与微服务相对的叫巨石 。 2、微服…

Git多人开发解决冲突案例

准备工作&#xff1a; 1.创建一个gitee远程仓库https://gitee.com/xxxxxxx.git 2.初始化两个本地git仓库用户&#xff0c;目的是模拟多人协作开发时提交代码发生冲突的场景 3.解决冲突并提交。 进入正题&#xff1a; lisi 通过vim指令修改readme.md文件内容&#xff0c;推送到…

基础术语和模式的学习记录

1 范围 本文件界定了政府和社会资本合作(PPP) 的基础术语&#xff0c;给出了政府和社会资本合作(PPP) 的 模 式 分类和代码。 本文件适用于政府和社会资本合作(PPP) 的所有活动。 2 规范性引用文件 本文件没有规范性引用文件。 3 基础术语 3.1 PPP 主体 3.1.1 政府和社…

Java:jackson实现json缩进美化输出

依赖 <!--json--> <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.10.0</version> </dependency>代码实现 public class JsonUtil {/*** 美化输出*…

CentOS8安装mysql-community-client错误解决

安装MySQL5.7.37的mysql-community-client-5.7.37-1.el7.x86_64.rpm时&#xff0c;提示如下&#xff1a; 提示的意思是缺少依赖软件包。 使用如下命令安装依赖包&#xff1a; yum install libncurse* 实际安装如下两个软件包。 成功后再次安装mysql-community-client-5.7.37…

C++智能指针,强制类型转换

C补充 异常处理 对于如下代码 #include <bits/stdc.h> void foo(int m, int n) {int t m/n; } int main(int argc, char* argv[]) {foo(1, argc-1);return 0; }运行后会提示 “ 出现未处理的异常 ” &#xff0c;这是因为foo函数里未处理 n 为0 的情况&#xff0c;异…