adxl345_driver/test_adxl345.c

49 lines
1,004 B
C

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
struct Sample{
int16_t x;
int16_t y;
int16_t z;
};
int main(){
int fd;
unsigned char buf[64];
size_t count = 6;
ssize_t ret_size;
struct Sample sample;
if((fd = open("/dev/adxl345", O_RDWR)) == -1){
printf("/dev/adxl345 opening failed\n");
return(0);
}
else{
for(int i=0; i<100; i++){
if((ret_size = read(fd, &buf, count)) < 0){
printf("Error while reading the device file.\n");
return(1);
}
else if(ret_size < count){
printf(".");
}
else{
sample.x = (int16_t)(buf[1]<<8 | buf[0]);
sample.y = (int16_t)(buf[3]<<8 | buf[2]);
sample.z = (int16_t)(buf[5]<<8 | buf[4]);
printf("Sample %i: %i %i %i\n",i,sample.x,sample.y,sample.z);
}
}
}
return(0);
}