2023-12-30 13:10:40 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "lis3dh.h"
|
|
|
|
#include "interrupt.h"
|
|
|
|
#include "i2c.h"
|
|
|
|
|
|
|
|
/* GPIO 12 or Pin 32 */
|
|
|
|
#define GPIO_INTERRUPT_PIN 12
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
lis3dh_t lis;
|
|
|
|
struct lis3dh_fifo_data fifo;
|
2024-01-04 21:13:49 +00:00
|
|
|
int i;
|
2023-12-30 13:10:40 +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;
|
|
|
|
|
2024-01-02 15:35:35 +00:00
|
|
|
/* initalise LIS3DH struct */
|
2023-12-30 13:10:40 +00:00
|
|
|
if (lis3dh_init(&lis)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
2024-01-02 15:35:35 +00:00
|
|
|
/* reset device just in case */
|
2024-01-01 12:05:53 +00:00
|
|
|
if (lis3dh_reset(&lis)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
2023-12-30 13:10:40 +00:00
|
|
|
/* register interrupt */
|
|
|
|
if (int_register(GPIO_INTERRUPT_PIN)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
|
|
|
lis.cfg.mode = LIS3DH_MODE_NORMAL;
|
|
|
|
lis.cfg.range = LIS3DH_FS_2G;
|
|
|
|
lis.cfg.rate = LIS3DH_ODR_100_HZ;
|
|
|
|
lis.cfg.fifo.mode = LIS3DH_FIFO_MODE_STREAM;
|
2024-01-02 15:35:35 +00:00
|
|
|
lis.cfg.fifo.trig = LIS3DH_FIFO_TRIG_INT1; /* trigger interrupt into int pin1 */
|
2024-01-06 01:36:35 +00:00
|
|
|
lis.cfg.pin1.overrun = 1; /* trigger upon FIFO overrun */
|
2024-01-02 15:35:35 +00:00
|
|
|
|
2024-01-01 12:05:53 +00:00
|
|
|
/* set up HP filter to remove DC component */
|
2024-01-02 15:35:35 +00:00
|
|
|
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL_REF;
|
2024-01-01 12:05:53 +00:00
|
|
|
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_4;
|
2024-01-04 21:13:49 +00:00
|
|
|
lis.cfg.filter.fds = 0; /* remove this line, or set to 1 to enable filter */
|
2023-12-30 13:10:40 +00:00
|
|
|
|
2024-01-02 15:35:35 +00:00
|
|
|
/* write device config */
|
2023-12-30 13:10:40 +00:00
|
|
|
if (lis3dh_configure(&lis)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
2024-01-02 15:35:35 +00:00
|
|
|
|
|
|
|
/* read REFERENCE to set filter to current accel field */
|
|
|
|
if (lis3dh_reference(&lis)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
2023-12-30 13:10:40 +00:00
|
|
|
|
|
|
|
/* wait for interrupt from LIS3DH */
|
|
|
|
if (int_poll(GPIO_INTERRUPT_PIN)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
2024-01-06 17:49:36 +00:00
|
|
|
/* read as many [x y z] sets as specified by watermark level (size) */
|
2024-01-02 15:35:35 +00:00
|
|
|
/* copy them to the fifo data struct given below as `fifo' */
|
2023-12-30 13:10:40 +00:00
|
|
|
if (lis3dh_read_fifo(&lis, &fifo)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
2024-01-04 21:13:49 +00:00
|
|
|
for(i=0; i<fifo.size; i++) {
|
|
|
|
printf("x: %d mg, y: %d mg, z: %d mg\n", fifo.x[i], fifo.y[i], fifo.z[i]);
|
2023-12-30 13:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unregister interrupt */
|
|
|
|
if (int_unregister(GPIO_INTERRUPT_PIN)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
2024-01-02 15:35:35 +00:00
|
|
|
/* deinitalise struct */
|
2023-12-30 13:10:40 +00:00
|
|
|
if (lis3dh_deinit(&lis)) {
|
|
|
|
/* error handling */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|