Skip to content

Commit

Permalink
Merge pull request #30 from caternuson/iss10_shake
Browse files Browse the repository at this point in the history
Add shake
  • Loading branch information
ladyada authored Mar 19, 2022
2 parents b9a9c47 + c4bb3d9 commit 5891751
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
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!");
}
}

0 comments on commit 5891751

Please sign in to comment.