Khadas Docs

Amazing Khadas, always amazes you!

User Tools

Site Tools


products:sbc:common:applications:gsensor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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 /dev/accel 
 +``` 
 + 
 +===== 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 <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; 
 +
 +``` 
 + 
 +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_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 
 +``` 
Last modified: 2022/06/29 21:51 by 127.0.0.1