lis3dh/main.c

66 lines
1.5 KiB
C
Raw Normal View History

2023-12-21 20:52:17 +00:00
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
2023-12-21 18:17:20 +00:00
#include "lis3dh.h"
2023-12-21 20:52:17 +00:00
#include "i2c.h"
#include "spi.h"
2024-01-04 14:37:44 +00:00
2023-12-21 18:17:20 +00:00
int main() {
lis3dh_t lis;
struct lis3dh_fifo_data data;
int i;
2023-12-21 18:17:20 +00:00
lis.dev.init = spi_init;
lis.dev.read = spi_read;
lis.dev.write = spi_write;
2023-12-21 20:52:17 +00:00
lis.dev.sleep = usleep;
lis.dev.deinit = spi_deinit;
2023-12-21 20:52:17 +00:00
/* initialise LIS3DH struct */
2023-12-22 08:26:10 +00:00
if (lis3dh_init(&lis)) {
puts("init()");
2023-12-21 23:29:22 +00:00
}
2024-01-04 14:37:44 +00:00
/* reset device just in case */
if (lis3dh_reset(&lis)) {
puts("reset()");
2023-12-28 18:10:34 +00:00
}
2024-01-01 11:05:45 +00:00
lis.cfg.mode = LIS3DH_MODE_HR;
2023-12-23 10:28:43 +00:00
lis.cfg.range = LIS3DH_FS_2G;
2024-01-02 11:00:49 +00:00
lis.cfg.rate = LIS3DH_ODR_400_HZ;
2024-01-04 14:37:44 +00:00
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM_TO_FIFO;
lis.cfg.fifo.size = 20;
2024-01-02 11:00:49 +00:00
2023-12-23 13:31:59 +00:00
/* write device config */
2023-12-22 08:26:10 +00:00
if (lis3dh_configure(&lis)) {
puts("configure()");
2024-01-04 14:37:44 +00:00
}
A:
/* poll fifo register until it reports that the watermark level has
been reached, or that it has overwritten old data, whichever
happens first. */
if (lis3dh_poll_fifo(&lis)) {
puts("poll_fifo()");
2024-01-04 14:37:44 +00:00
}
/* read as many [x y z] sets as specified by watermark level (fth) */
/* copy them to the fifo data struct given below as `fifo' */
if (lis3dh_read_fifo(&lis, &data)) {
puts("read_fifo()");
2023-12-21 18:17:20 +00:00
}
2023-12-29 23:24:15 +00:00
/* read out fifo buffer data */
for(i=0; i<data.size; i++) {
printf("x: %d mg, y: %d mg, z: %d mg\n", data.x[i], data.y[i], data.z[i]);
2024-01-02 11:00:49 +00:00
}
goto A;
/* deinitialise struct */
2023-12-22 08:26:10 +00:00
if (lis3dh_deinit(&lis)) {
puts("deinit()");
2023-12-21 18:17:20 +00:00
}
return 0;
}