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-21 18:17:20 +00:00
|
|
|
if (!lis3dh_init(&lis)) {
|
2023-12-21 23:29:22 +00:00
|
|
|
puts("init OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
lis.cfg.mode = LIS3DH_MODE_NORMAL;
|
|
|
|
lis.cfg.range = LIS3DH_FS_2G;
|
|
|
|
lis.cfg.rate = LIS3DH_ODR_100_HZ;
|
|
|
|
|
|
|
|
if (!lis3dh_configure(&lis)) {
|
|
|
|
puts("configure OK");
|
2023-12-21 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 23:29:22 +00:00
|
|
|
if (!lis3dh_poll(&lis)) {
|
|
|
|
puts("poll OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lis3dh_read(&lis)) {
|
|
|
|
puts("read OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("x: %.3g, y: %.3g, z: %.3g\n", lis.acc.x, lis.acc.y, lis.acc.z);
|
|
|
|
printf("mag: %.3g\n", accel_mag(&lis));
|
|
|
|
|
2023-12-21 18:17:20 +00:00
|
|
|
if (!lis3dh_deinit(&lis)) {
|
2023-12-21 23:29:22 +00:00
|
|
|
puts("deinit OK");
|
2023-12-21 18:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|