Gsensor

Gsensor节点

Gsensor的设备节点在/dev目录下

1
2
$ ll /dev/accel 
crw-rw-rw- 1 root root 10, 50 Mar 18 12:17 /dev/accel

Gsensor示例

Gsensor示例是通过ioctl函数对节点进行读写

源码

源码只实现了使用默认设置读数据,其他功能用户可以自行实现

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
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#define GBUFF_SIZE 12
#define GSENSOR_IOCTL_MAGIC 'a'

#define GSENSOR_IOCTL_INIT _IO(GSENSOR_IOCTL_MAGIC, 0x01)
#define GSENSOR_IOCTL_RESET _IO(GSENSOR_IOCTL_MAGIC, 0x04)
#define GSENSOR_IOCTL_CLOSE _IO(GSENSOR_IOCTL_MAGIC, 0x02)
#define GSENSOR_IOCTL_START _IO(GSENSOR_IOCTL_MAGIC, 0x03)
#define GSENSOR_IOCTL_GETDATA _IOR(GSENSOR_IOCTL_MAGIC, 0x08, char[GBUFF_SIZE+1])
#define GSENSOR_IOCTL_APP_SET_RATE _IOW(GSENSOR_IOCTL_MAGIC, 0x10, short)
#define GSENSOR_IOCTL_GET_CALIBRATION _IOR(GSENSOR_IOCTL_MAGIC, 0x11, int[3])


struct sensor_axis {
int x;
int y;
int z;
};

char *gsensor_device = "/dev/accel";
int gsensor_fd = -1;

int main(int argc, char **argv){

struct sensor_axis gsensor_data;

gsensor_fd = open(gsensor_device, O_RDWR);

if (0 > gsensor_fd){
printf("gsensor node open failed ...\n");
exit(-1);
}else{
printf("gsensor node open success!!!\n");
}

if(ioctl(gsensor_fd, GSENSOR_IOCTL_START, NULL) == -1) {
printf("gsensor start failed ... \n");
close(gsensor_fd);
exit(-1);
}else{
printf("gsensor start sueecss !!!\n");
}

printf("start to get gsensor data ...\n");
while(1){

if(ioctl(gsensor_fd, GSENSOR_IOCTL_GETDATA, &gsensor_data) == -1) {
printf("gsensor get data faile ... \n");
close(gsensor_fd);
exit(-1);
}

printf("gsensor_data -- x:%d,y:%d,z:%d \n", gsensor_data.x, gsensor_data.y, gsensor_data.z);
sleep(1);
}
close(gsensor_fd);

return 0;
}

编译

在板子上直接使用gcc编译即可

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
$ gcc -o gsensor_sample_demo gsensor_sample_demo.c
gsensor_sample_demo.c: In function ‘main’:
gsensor_sample_demo.c:34:3: warning: implicit declaration of functionexit’ [-Wimplicit-function-declaration]
34 | exit(-1);
| ^~~~
gsensor_sample_demo.c:34:3: warning: incompatible implicit declaration of built-in functionexit
gsensor_sample_demo.c:4:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit
3 | #include <fcntl.h>
+++ |+#include <stdlib.h>
4 |
gsensor_sample_demo.c:41:3: warning: implicit declaration of function ‘close’; did you mean ‘pclose’? [-Wimplicit-function-declaration]
41 | close(gsensor_fd);
| ^~~~~
| pclose
gsensor_sample_demo.c:42:3: warning: incompatible implicit declaration of built-in functionexit
42 | exit(-1);
| ^~~~
gsensor_sample_demo.c:42:3: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit
gsensor_sample_demo.c:53:4: warning: incompatible implicit declaration of built-in functionexit
53 | exit(-1);
| ^~~~
gsensor_sample_demo.c:53:4: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit
gsensor_sample_demo.c:57:3: warning: implicit declaration of functionsleep’ [-Wimplicit-function-declaration]
57 | sleep(1);
| ^~~~~

运行

运行时旋转板子,可以看到gsensor数据的变化

1
2
3
4
5
6
7
8
9
10
11
12
$ ./gsensor_sample_demo
gsensor node open success!!!
gsensor start sueecss !!!
start to get gsensor data ...
gsensor_data -- x:-2112,y:16,z:16448
gsensor_data -- x:-2128,y:176,z:16128
gsensor_data -- x:-1632,y:2784,z:15968
gsensor_data -- x:7440,y:-3760,z:14608
gsensor_data -- x:-512,y:-7280,z:12128
gsensor_data -- x:-14384,y:-2336,z:11280
gsensor_data -- x:9952,y:-5264,z:15216
gsensor_data -- x:-6432,y:3760,z:24896