在GPIO中断测试章节中,使用到了 poll()
多路转接。作用:检测文件当前状态是否就绪,是不是需要使用到的状态。
文档交叉P475。
#include <poll.h>
int poll(struct pollfd \*fds, nfds\_t nfds, int timeout);
struct pollfd {
int fd; /\* file descriptor \*/
short events; /\* requested events \*/
short revents; /\* returned events \*/
};
截取中断部分重要代码 P532。
这里用到了access函数,解释说明如下:access函数用法,主要功能是判断进程时候可以对文件进行相应的操作。
//open file\_path
sprintf(gpio_path,"/sys/class/gpio%s",argv[1]);
//如果GPIO不存在就将GPIO导出,判断文件是否存在。
if (access(gpio_path,F_OK)){
int len;
int fd;
if (0 > (fd = open("/sys/class/gpio/export",O_WRONLY))){
perror("open error");
exit(-1);
}
len = strlen(argv[1]);
if (len != write(fd,argv[1],len)){
perror("write error");
exit(-1);
}
close(fd);
}
if (gpio\_config("direction","in"))
exit(-1);
if (gpio\_config("edge","both"))
exit(-1);
if (gpio\_config("active\_low","0"))
exit(-1);
sprintf(file_path,"%s/%s",gpio_path,"value");
if (0 > (pfd.fd = open(file_path,O_RDONLY))){
perror("open error");
exit(-1);
}
pfd.events = POLLPRI;
read(pfd.fd,&val,1);
while(1)
{
if (0 > (ret = poll(&pfd,1,-1))){
perror("poll error");
exit(-1);
}
else if (ret == 0){
fprintf(stderr,"poll timeout.\n");
continue;
}
//校验高优先级数据是否可读
if (pfd.revents & POLLPRI){
if (0 >lseek(pfd.fd,0,SEEK\_SET))
{
perror("lseek error");
exit(-1);
}
if (read(pfd.fd,&val,1)<0){
perror("read error");
exit(-1);
}
printf("GPIO 中断触发<value = %c>\n",val);
}
}
2022.5.16 输入设备
linuxc 应用文档p537
输入设备,这章节中判断输入设备的状态和数据解析最重要的是用到了 个 struct input_event
结构体。把握住这个结构体就好啦!!!
struct input\_event {
struct timeval time;
__u16 type;
__u16 code;
__s32 value;
};
type
:type 用于描述发生了哪一种类型的事件。比如:鼠标事件。
code
:code 表示该类事件中的哪一个具体事件。
例如:
value
:内核每次上报事件都会向应用层发送一个数据 value,对 value 值的解释随着 code 的变化而变化。
多点触摸:MT 协议之 Type B 协议 P549
获取触摸点信息。
ioctl()
函数P553的原型列出:
#include <sys/ioctl.h>
int ioctl(int fd, unsigned long request, ...);
对于 input 输入设备,对其执行 ioctl()操作需要使用这些宏,不同的宏表示不同请求指令;譬如使用EVIOCGNAME 宏获取设备名称,使用方式如下:
char name[100];
ioctl(fd, EVIOCGNAME(sizeof(name)), name);
获取触摸屏支持的最大触摸点数:
使用ioctl()
函数将触摸屏信息写入info结构体中。
struct input\_absinfo info;
int max_slots; //最大触摸点数
if (0 > ioctl(fd, EVIOCGABS(ABS_MT_SLOT), &info))
perror("ioctl error");
max_slots = info.maximum + 1 - info.minimum;
例程代码
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
int main(int argc, char \*argv[])
{
struct input\_event in_event = {0};
int x,y;
int down;
int valid;
int fd = -1;
if (argc<2){
fprintf(stderr, "usage: %s <input-dev>\n", argv[0]);
exit(-1);
}
## 最后
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。**
**因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/83a4337d7e63e92f01b0a1b92d2f5319.png)
![img](https://img-blog.csdnimg.cn/img_convert/15621af5dde3c511da527c6b23d8d2b2.jpeg)
![img](https://img-blog.csdnimg.cn/img_convert/3adbc0740ac49cc9165528ed38eeaec4.png)
![img](https://img-blog.csdnimg.cn/img_convert/194b850038d345a4926578d3b47bf8f5.png)
![img](https://img-blog.csdnimg.cn/img_convert/9a0c551f9b58a8ba57b38ac691be760f.png)
![img](https://img-blog.csdnimg.cn/img_convert/e1ca8a56ea3c257eac8a88d12f6723e9.png)
![](https://img-blog.csdnimg.cn/img_convert/0ab714af9edcf3db6cea2da687cb206e.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!
也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!**
[**如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!**](https://bbs.csdn.net/topics/618654289)
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**!!