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 [2026/02/03 22:53] (current) gray update by using AI |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | # Gsensor | + | ====== G-Sensor ====== |
| + | |||
| + | ===== Introduction ===== | ||
| + | |||
| + | This guide explains how to identify the G-Sensor device node and read acceleration data from it using a C program. | ||
| + | |||
| + | ===== System Configuration ===== | ||
| + | |||
| + | Verify the G-Sensor device node exists: | ||
| + | |||
| + | ```shell | ||
| + | $ ll /dev/accel | ||
| + | ``` | ||
| + | Expected output: | ||
| + | ```shell | ||
| + | crw-rw-rw- 1 root root 10, 50 Mar 18 12:17 / | ||
| + | ``` | ||
| + | |||
| + | ===== Demo Source Code ===== | ||
| + | |||
| + | The following sample program demonstrates reading data from the G-Sensor using default settings. You can extend it to implement other functions. | ||
| + | |||
| + | ```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 program: | ||
| + | |||
| + | ```shell | ||
| + | $ gcc -o gsensor_sample_demo gsensor_sample_demo.c | ||
| + | ``` | ||
| + | |||
| + | ===== Demonstrate ===== | ||
| + | |||
| + | Run the compiled program. While it is running, tilt or move the board to observe changes in the acceleration values. | ||
| + | |||
| + | ```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: | ||
| + | ``` | ||