Skip to content

Commit 1d58b0a

Browse files
committed
[wip] LIS3DH Click interrupt detection
1 parent 3db9c9d commit 1d58b0a

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/components/i2c/drivers/drvLis3dh.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ bool drvLis3dh::begin() {
4343
// Note: some Adafruit_LIS3DH variants offer setDataRate; if present this
4444
// keeps defaults. Keep configuration minimal to avoid API mismatches.
4545

46+
// setup interrupt for click detection (boolean event)
47+
_lis->setClick(1, LIS3DH_CLICKTHRESHHOLD); // 0-2 clicks, threshold 50
48+
delay(100); // ignore any spurious interrupts right after setup
49+
_lis->readAndClearInterrupt();
50+
4651
return true;
4752
}
4853

@@ -58,7 +63,7 @@ bool drvLis3dh::begin() {
5863
bool drvLis3dh::getEventRaw(sensors_event_t *rawEvent) {
5964
if (!_lis)
6065
return false;
61-
66+
WS_DEBUG_PRINTLN("[drvLis3dh] Getting raw event...");
6267
// Read an Adafruit_Sensor compatible event from the device
6368
sensors_event_t event;
6469
_lis->getEvent(&event);
@@ -69,6 +74,43 @@ bool drvLis3dh::getEventRaw(sensors_event_t *rawEvent) {
6974
event.acceleration.y * event.acceleration.y +
7075
event.acceleration.z * event.acceleration.z);
7176
rawEvent->data[0] = mag;
77+
WS_DEBUG_PRINT("[drvLis3dh] Raw magnitude: ");
78+
WS_DEBUG_PRINTLN(mag);
79+
return true;
80+
}
81+
82+
/******************************************************************************/
83+
/*!
84+
@brief Gets the LIS3DH's boolean sensor event.
85+
@param booleanEvent
86+
Pointer to the sensor event.
87+
@returns True if the sensor event was obtained successfully, False
88+
otherwise.
89+
*/
90+
/******************************************************************************/
91+
bool drvLis3dh::getEventBoolean(sensors_event_t *booleanEvent) {
92+
if (!_lis) {
93+
return false;
94+
}
95+
WS_DEBUG_PRINTLN("[drvLis3dh] Checking for click event...");
96+
uint8_t result = _lis->getClick();
97+
WS_DEBUG_PRINT(" Click result: ");
98+
WS_DEBUG_PRINTHEX(result);
99+
WS_DEBUG_PRINTLN("");
100+
// does it & 0x30, then 0x20 is double click, 0x10 is single click
101+
WS_DEBUG_PRINTLN("[drvLis3dh] Click result & 0x30: ");
102+
WS_DEBUG_PRINTHEX(result & 0x30);
103+
WS_DEBUG_PRINTLN("");
104+
uint8_t interruptReason = _lis->readAndClearInterrupt();
105+
WS_DEBUG_PRINTLN("[drvLis3dh] Interrupt reason: ");
106+
WS_DEBUG_PRINTHEX(interruptReason);
107+
WS_DEBUG_PRINTLN("");
108+
109+
// todo: switch on interrupt type - needs driver library expanding
110+
if (interruptReason == 1 && result == 1) {
111+
WS_DEBUG_PRINTLN("[drvLis3dh] Click event detected!");
112+
}
113+
booleanEvent->data[0] = result ? 1.0f : 0.0f;
72114
return true;
73115
}
74116

@@ -82,9 +124,10 @@ bool drvLis3dh::getEventRaw(sensors_event_t *rawEvent) {
82124
*/
83125
/******************************************************************************/
84126
bool drvLis3dh::getEventAccelerometer(sensors_event_t *accelEvent) {
85-
if (!_lis)
127+
if (!_lis) {
86128
return false;
87-
129+
}
130+
WS_DEBUG_PRINTLN("[drvLis3dh] Getting accelerometer event...");
88131
// Fill the provided event with sensor data
89132
_lis->getEvent(accelEvent);
90133
return true;

src/components/i2c/drivers/drvLis3dh.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
#ifndef DRV_LIS3DH_H
1616
#define DRV_LIS3DH_H
1717

18+
#include "Wippersnapper_V2.h"
1819
#include "drvBase.h"
1920
#include <Adafruit_LIS3DH.h>
2021

2122
class Adafruit_LIS3DH; // forward
2223

24+
// Adjust this number for the sensitivity of the 'click' force
25+
// this strongly depend on the range! for 16G, try 5-10
26+
// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80
27+
#define LIS3DH_CLICKTHRESHHOLD 40
28+
2329
/**************************************************************************/
2430
/*!
2531
@brief Class that provides a driver interface for a LIS3DH accelerometer.
@@ -59,6 +65,17 @@ class drvLis3dh : public drvBase {
5965
/*******************************************************************************/
6066
bool begin() override;
6167

68+
/******************************************************************************/
69+
/*!
70+
@brief Gets the LIS3DH's boolean sensor event.
71+
@param booleanEvent
72+
Pointer to the sensor event.
73+
@returns True if the sensor event was obtained successfully, False
74+
otherwise.
75+
*/
76+
/******************************************************************************/
77+
bool getEventBoolean(sensors_event_t *booleanEvent) override;
78+
6279
/*******************************************************************************/
6380
/*!
6481
@brief Gets the LIS3DH's raw sensor event (magnitude stored in

0 commit comments

Comments
 (0)