2023-12-21 20:52:17 +00:00
|
|
|
#define _GNU_SOURCE
|
2023-12-21 18:17:20 +00:00
|
|
|
#include <stdio.h>
|
2023-12-21 20:52:17 +00:00
|
|
|
#include <unistd.h>
|
2023-12-21 23:29:22 +00:00
|
|
|
#include <math.h>
|
2023-12-21 18:17:20 +00:00
|
|
|
#include "lis3dh.h"
|
2023-12-21 20:52:17 +00:00
|
|
|
#include "i2c.h"
|
2023-12-21 18:17:20 +00:00
|
|
|
|
2023-12-21 23:29:22 +00:00
|
|
|
double accel_mag(lis3dh_t *lis) {
|
|
|
|
double d = 0.0;
|
|
|
|
|
|
|
|
d = sqrt( powf(lis->acc.x, 2) + powf(lis->acc.y, 2) + powf(lis->acc.z, 2));
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2023-12-21 18:17:20 +00:00
|
|
|
int main() {
|
|
|
|
|
|
|
|
lis3dh_t lis;
|
|
|
|
|
2023-12-21 20:52:17 +00:00
|
|
|
lis.dev.init = i2c_init;
|
|
|
|
lis.dev.read = i2c_read;
|
|
|
|
lis.dev.write = i2c_write;
|
|
|
|
lis.dev.sleep = usleep;
|
|
|
|
lis.dev.deinit = i2c_deinit;
|
|
|
|
|
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
if (lis3dh_init(&lis)) {
|
|
|
|
puts("init ERR");
|
2023-12-21 23:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lis.cfg.mode = LIS3DH_MODE_NORMAL;
|
|
|
|
lis.cfg.range = LIS3DH_FS_2G;
|
|
|
|
lis.cfg.rate = LIS3DH_ODR_100_HZ;
|
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
if (lis3dh_configure(&lis)) {
|
|
|
|
puts("configure ERR");
|
2023-12-21 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
for(int i=0; i<100; i++) {
|
|
|
|
if (lis3dh_poll(&lis)) {
|
|
|
|
puts("poll ERR");
|
|
|
|
}
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
if (lis3dh_read(&lis)) {
|
|
|
|
puts("read ERR");
|
|
|
|
}
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
printf("x: % 04.04f g, y: % 04.04f g, z: % 04.04f g, mag:% 04.04f\n",
|
|
|
|
lis.acc.x, lis.acc.y, lis.acc.z, accel_mag(&lis));
|
|
|
|
}
|
2023-12-21 23:29:22 +00:00
|
|
|
|
2023-12-22 08:26:10 +00:00
|
|
|
if (lis3dh_deinit(&lis)) {
|
|
|
|
puts("deinit ERR");
|
2023-12-21 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|