double click

This commit is contained in:
William Clark 2024-01-01 13:08:06 +00:00
parent 78cde50f13
commit 50a491a1be
6 changed files with 197 additions and 32 deletions

View File

@ -10,7 +10,7 @@ A C89 driver for the 3-axis accelerometer LIS3DH. Supports both i2c and SPI.
> - Interrupt generation > - Interrupt generation
> - Free-fall detection (soon) > - Free-fall detection (soon)
> - Single-click detection > - Single-click detection
> - Double-click detection (soon) > - Double-click detection
> - 4D/6D orientation detection (soon) > - 4D/6D orientation detection (soon)
## Examples ## Examples

View File

@ -13,4 +13,8 @@ The LIS3DH supports two different interrupt "output pins," `INT1` and `INT2`. Th
### single-click.c ### single-click.c
Set up single-click detection using HP filter Set up single-click detection
### double-click.c
Set up double-click detection

127
example/double-click.c Normal file
View File

@ -0,0 +1,127 @@
/*
* SCLICK SCLICK
* _________ __________
* | | | | |
* | | | | |
* ----- ------------------ | ------
* |
* TIME_LIMIT TIME_LIMIT|
* >---------< >----------<|
* LATENCY WINDOW |
* >----------<>----------< | => DCLICK INT
* */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "lis3dh.h"
#include "interrupt.h"
#include "i2c.h"
#define GPIO_INTERRUPT_PIN_INT1 12
int main() {
lis3dh_t lis;
/* set fn ptrs to rw on bus (i2c or SPI) */
lis.dev.init = i2c_init;
lis.dev.read = i2c_read;
lis.dev.write = i2c_write;
lis.dev.sleep = usleep;
lis.dev.deinit = i2c_deinit;
/* initialise LIS3DH struct */
if (lis3dh_init(&lis)) {
/* error handling */
}
/* reset device because it sometimes corrupts itself */
if (lis3dh_reset(&lis)) {
/* error handling */
}
/* register interrupt */
if (int_register(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* set up config */
lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_400_HZ; /* minimum recommended ODR */
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.filter.click = 1; /* enable filtering for CLICK function */
lis.cfg.click.xd = 1; /* enable X axis double click */
lis.cfg.click.yd = 1; /* enable Y axis double click */
lis.cfg.click.zd = 1; /* enable Z axis double click */
lis.cfg.pin1.click = 1; /* enable click int src through pin1 */
lis.cfg.pin1.latch = 1;
/* 1 LSb = 16 mg @ FS_2G
* so a 0.072g 'shock' is 72/16 = 4.5
* However, the device can have up to +- 40mg read error
* 0.112g => 112/16 = 15
*/
lis.cfg.click_ths = 15; /* pretty sensitive */
/* Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer
* --- ODR = the data rate, eg 100, 400...
* [ODR] [1 LSb in milliseconds]
* 400 2.5
*
* For ODR=400:
* time_limit of 75 ms = 75/2.5 = 30
* time_latency of 40 ms = 40/2.5 = 16
* time_window of 500 ms = 500/2.5 = 200
*
*/
lis.cfg.time_limit = 30; /* range: 0-127 */
lis.cfg.time_latency = 16; /* range: 0-255 */
lis.cfg.time_window = 200; /* range: 0-255 */
/* write device config */
if (lis3dh_configure(&lis)) {
/* error handling */
}
for(;;) {
/* poll interrupt on INT1 pin */
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* read CLICK_SRC when interrupt has fired */
if (lis3dh_read_click(&lis)) {
/* error handling */
}
/* only print data if DCLICK=1 in CLICK_SRC */
if (LIS3DH_CLICK_DCLICK(lis.src.click)) {
/* print data gathered from CLICK_SRC */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n",
LIS3DH_CLICK_SRC_X(lis.src.click),
LIS3DH_CLICK_SRC_Y(lis.src.click),
LIS3DH_CLICK_SRC_Z(lis.src.click),
LIS3DH_CLICK_SIGN(lis.src.click),
LIS3DH_CLICK_SCLICK(lis.src.click),
LIS3DH_CLICK_DCLICK(lis.src.click));
}
}
/* unregister interrupt */
if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) {
/* error handling */
}
/* deinitalise struct */
if (lis3dh_deinit(&lis)) {
/* error handling */
}
return 0;
}

View File

@ -51,12 +51,13 @@ int main() {
/* 1 LSb = 16 mg @ FS_2G /* 1 LSb = 16 mg @ FS_2G
* so a 0.072g 'shock' is 72/16 = 4.5 * so a 0.072g 'shock' is 72/16 = 4.5
* However, the device can have up to +- 40mg read error * However, the device can have up to +- 40mg read error
* 0.112g => 112/16 = 15 * 0.112g => 112/16 = 7
*/ */
lis.cfg.click_ths = 7; /* pretty sensitive */ lis.cfg.click_ths = 7; /* pretty sensitive */
/* the 'shock' must be gone after 10 ms let's say .. */ /* the 'shock' must be gone after 10 ms let's say ..*/
/* Duration time is measured in N/ODR where: /*
* Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer * --- N = The content of the intX_dur integer
* --- ODR = the data rate, eg 100, 400... * --- ODR = the data rate, eg 100, 400...
* [ODR] [1 LSb in milliseconds] * [ODR] [1 LSb in milliseconds]

View File

@ -200,6 +200,11 @@ struct lis3dh_config {
* 1600 0.6 * 1600 0.6
* 1344 0.744 * 1344 0.744
* 5376 0.186 * 5376 0.186
* (Note: this calc also applies to:
* - time_limit
* - time_window
* - time_latency
* used in CLICK)
*/ */
uint8_t int1_dur; /* 7-bit INT 1 duration value */ uint8_t int1_dur; /* 7-bit INT 1 duration value */
uint8_t int2_dur; /* 7-bit INT 2 duration value */ uint8_t int2_dur; /* 7-bit INT 2 duration value */

82
main.c
View File

@ -1,3 +1,15 @@
/*
* SCLICK SCLICK
* _________ __________
* | | | | |
* | | | | |
* ----- ------------------ | ------
* |
* TIME_LIMIT TIME_LIMIT|
* >---------< >----------<|
* LATENCY WINDOW |
* >----------<>----------< | => DCLICK INT
* */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -46,52 +58,68 @@ int main() {
/* set up config */ /* set up config */
lis.cfg.mode = LIS3DH_MODE_HR; lis.cfg.mode = LIS3DH_MODE_HR;
lis.cfg.range = LIS3DH_FS_2G; lis.cfg.range = LIS3DH_FS_2G;
lis.cfg.rate = LIS3DH_ODR_400_HZ; lis.cfg.rate = LIS3DH_ODR_400_HZ; /* minimum recommended ODR */
lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL; lis.cfg.filter.mode = LIS3DH_FILTER_MODE_NORMAL;
lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8; lis.cfg.filter.cutoff = LIS3DH_FILTER_CUTOFF_8;
lis.cfg.click.latch = 1; lis.cfg.filter.click = 1; /* enable filtering for CLICK function */
lis.cfg.click.xs = 1; lis.cfg.click.xd = 1; /* enable X axis double click */
lis.cfg.click.ys = 1; lis.cfg.click.yd = 1; /* enable Y axis double click */
lis.cfg.click.zs = 1; lis.cfg.click.zd = 1; /* enable Z axis double click */
lis.cfg.pin1.click = 1; lis.cfg.pin1.click = 1; /* enable click int src through pin1 */
lis.cfg.pin1.latch = 1;
/* 1 LSb = 16 mg @ FS_2G /* 1 LSb = 16 mg @ FS_2G
* so a 0.8g 'shock' is 800/16 = 50 * so a 0.072g 'shock' is 72/16 = 4.5
* However, the device can have up to +- 40mg read error
* 0.112g => 112/16 = 15
*/ */
lis.cfg.click_ths = 50; lis.cfg.click_ths = 15; /* pretty sensitive */
/* the 800 mg shock must be gone after 30 ms let's say .. */ /* Duration time is measured in N/ODR where:
/* Duration time is measured in N/ODR where:
* --- N = The content of the intX_dur integer * --- N = The content of the intX_dur integer
* --- ODR = the data rate, eg 100, 400... * --- ODR = the data rate, eg 100, 400...
* [ODR] [1 LSb in milliseconds] * [ODR] [1 LSb in milliseconds]
* 400 2.5 * 400 2.5
* *
* At 400 ODR, 30ms/2.5ms = 16 * For ODR=400:
* time_limit of 75 ms = 75/2.5 = 30
* time_latency of 40 ms = 40/2.5 = 16
* time_window of 500 ms = 500/2.5 = 200
*
*/ */
lis.cfg.time_limit = 16; lis.cfg.time_limit = 30; /* range: 0-127 */
lis.cfg.time_latency = 16; /* range: 0-255 */
lis.cfg.time_window = 200; /* range: 0-255 */
/* write device config */ /* write device config */
if (lis3dh_configure(&lis)) { if (lis3dh_configure(&lis)) {
quit("configure()", &lis); quit("configure()", &lis);
} }
if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
quit("int_poll()", &lis);
}
if (lis3dh_read_click(&lis)) { for(;;) {
quit("read_click()", &lis);
}
/* print data gathered from CLICK_SRC */ /* poll interrupt on INT1 pin */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n", if (int_poll(GPIO_INTERRUPT_PIN_INT1)) {
LIS3DH_CLICK_SRC_X(lis.src.click), quit("int_poll()", &lis);
LIS3DH_CLICK_SRC_Y(lis.src.click), }
LIS3DH_CLICK_SRC_Z(lis.src.click),
LIS3DH_CLICK_SIGN(lis.src.click), /* read CLICK_SRC when interrupt has fired */
LIS3DH_CLICK_SCLICK(lis.src.click), if (lis3dh_read_click(&lis)) {
LIS3DH_CLICK_DCLICK(lis.src.click)); quit("read_click()", &lis);
}
/* only print data if DCLICK=1 in CLICK_SRC */
if (LIS3DH_CLICK_DCLICK(lis.src.click)) {
/* print data gathered from CLICK_SRC */
printf("Click: X=%d, Y=%d, Z=%d, Sign=%d, S_en=%d, D_en=%d\n",
LIS3DH_CLICK_SRC_X(lis.src.click),
LIS3DH_CLICK_SRC_Y(lis.src.click),
LIS3DH_CLICK_SRC_Z(lis.src.click),
LIS3DH_CLICK_SIGN(lis.src.click),
LIS3DH_CLICK_SCLICK(lis.src.click),
LIS3DH_CLICK_DCLICK(lis.src.click));
}
}
/* unregister interrupt */ /* unregister interrupt */
if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) { if (int_unregister(GPIO_INTERRUPT_PIN_INT1)) {