Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shake #30

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions Adafruit_LSM6DS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ void Adafruit_LSM6DS::enablePedometer(bool enable) {
@param thresh The threshold (sensitivity)
*/
/**************************************************************************/
void Adafruit_LSM6DS::enableWakeup(bool enable, uint8_t duration = 0,
uint8_t thresh = 20) {
void Adafruit_LSM6DS::enableWakeup(bool enable, uint8_t duration,
uint8_t thresh) {
// enable or disable functionality
Adafruit_BusIO_Register tapcfg = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LSM6DS_TAP_CFG);
Expand Down Expand Up @@ -748,6 +748,26 @@ bool Adafruit_LSM6DS::awake(void) {
return wake_evt.read();
}

/**************************************************************************/
/*!
@brief Simple shake detection. Must call enableWakeup() first.
@returns True if shake (wake) detected, otherwise false.
*/
/**************************************************************************/
bool Adafruit_LSM6DS::shake(void) {
Adafruit_BusIO_Register tapcfg = Adafruit_BusIO_Register(
i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LSM6DS_TAP_CFG);
Adafruit_BusIO_RegisterBits slope_en =
Adafruit_BusIO_RegisterBits(&tapcfg, 1, 4);
Adafruit_BusIO_RegisterBits timer_en =
Adafruit_BusIO_RegisterBits(&tapcfg, 1, 7);
// only check if enabled
if (slope_en.read() && timer_en.read()) {
return awake();
}
return false;
}

/**************************************************************************/
/*!
@brief Reset the pedometer count
Expand Down
3 changes: 2 additions & 1 deletion Adafruit_LSM6DS.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ class Adafruit_LSM6DS {
void configInt2(bool drdy_temp, bool drdy_g, bool drdy_xl);
void highPassFilter(bool enabled, lsm6ds_hp_filter_t filter);

void enableWakeup(bool enable, uint8_t duration, uint8_t thresh);
void enableWakeup(bool enable, uint8_t duration = 0, uint8_t thresh = 20);
bool awake(void);
bool shake(void);

void enablePedometer(bool enable);
void resetPedometer(void);
Expand Down
40 changes: 40 additions & 0 deletions examples/adafruit_lsm6ds33_shake/adafruit_lsm6ds33_shake.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Basic demo for accelerometer/gyro readings from Adafruit LSM6DS33

#include <Adafruit_LSM6DS33.h>

// For SPI mode, we need a CS pin
#define LSM_CS 10
// For software-SPI mode we need SCK/MOSI/MISO pins
#define LSM_SCK 13
#define LSM_MISO 12
#define LSM_MOSI 11

Adafruit_LSM6DS33 lsm6ds33;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens

Serial.println("Adafruit LSM6DS33 shake test!");

if (!lsm6ds33.begin_I2C()) {
// if (!lsm6ds33.begin_SPI(LSM_CS)) {
// if (!lsm6ds33.begin_SPI(LSM_CS, LSM_SCK, LSM_MISO, LSM_MOSI)) {
Serial.println("Failed to find LSM6DS33 chip");
while (1) {
delay(10);
}
}

Serial.println("LSM6DS33 Found!");

// enable shake detection
lsm6ds33.enableWakeup(true);
}

void loop() {
// check for shake
if (lsm6ds33.shake()) {
Serial.println("SHAKE!");
}
}