This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
products:sbc:common:applications:gsensor [2022/06/29 21:51] 127.0.0.1 external edit |
products:sbc:common:applications:gsensor [2025/02/18 01:40] (current) nick |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | # Gsensor | + | ====== G-Sensor ====== |
| + | |||
| + | ===== Introduction ===== | ||
| + | |||
| + | By referring to this page, you will know which node of G-Sensor is, and how to read the value of G-Sensor through C program. | ||
| + | |||
| + | ===== System Configuration ===== | ||
| + | |||
| + | Check G-sensor node. | ||
| + | |||
| + | ```shell | ||
| + | $ ll /dev/accel | ||
| + | crw-rw-rw- 1 root root 10, 50 Mar 18 12:17 / | ||
| + | ``` | ||
| + | |||
| + | ===== Demo Source Code ===== | ||
| + | |||
| + | The source code only realizes the use of default settings to read data, for other functions, you can implement them by yourself. | ||
| + | |||
| + | ```c gsensor_sample_data.c | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define GBUFF_SIZE | ||
| + | #define GSENSOR_IOCTL_MAGIC | ||
| + | |||
| + | #define GSENSOR_IOCTL_INIT | ||
| + | #define GSENSOR_IOCTL_RESET | ||
| + | #define GSENSOR_IOCTL_CLOSE | ||
| + | #define GSENSOR_IOCTL_START | ||
| + | #define GSENSOR_IOCTL_GETDATA | ||
| + | #define GSENSOR_IOCTL_APP_SET_RATE | ||
| + | #define GSENSOR_IOCTL_GET_CALIBRATION | ||
| + | |||
| + | |||
| + | struct sensor_axis { | ||
| + | int x; | ||
| + | int y; | ||
| + | int z; | ||
| + | }; | ||
| + | |||
| + | char *gsensor_device = "/ | ||
| + | int gsensor_fd = -1; | ||
| + | |||
| + | int main(int argc, char **argv){ | ||
| + | |||
| + | struct sensor_axis gsensor_data; | ||
| + | |||
| + | gsensor_fd = open(gsensor_device, | ||
| + | |||
| + | if (0 > gsensor_fd){ | ||
| + | printf(" | ||
| + | exit(-1); | ||
| + | }else{ | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | if(ioctl(gsensor_fd, | ||
| + | printf(" | ||
| + | close(gsensor_fd); | ||
| + | exit(-1); | ||
| + | }else{ | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | while(1){ | ||
| + | |||
| + | if(ioctl(gsensor_fd, | ||
| + | printf(" | ||
| + | close(gsensor_fd); | ||
| + | exit(-1); | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | sleep(1); | ||
| + | } | ||
| + | close(gsensor_fd); | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | Compile the source code obtained. | ||
| + | |||
| + | ```shell | ||
| + | $ gcc -o gsensor_sample_demo gsensor_sample_demo.c | ||
| + | ``` | ||
| + | |||
| + | ===== Demonstrate ===== | ||
| + | |||
| + | Rotate the board while running, you can see the changes in G-Sensor data. | ||
| + | |||
| + | ```shell | ||
| + | $ sudo ./ | ||
| + | gsensor node open success!!! | ||
| + | gsensor start sueecss !!! | ||
| + | start to get gsensor data ... | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | gsensor_data -- x: | ||
| + | ``` | ||