Skip to content

Commit 3db9c9d

Browse files
committed
[wip] Add LIS3DH
1 parent fd47500 commit 3db9c9d

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit AS5600 Library, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox
10+
depends=OmronD6T - Community Fork, SdFat - Adafruit Fork, Adafruit LIS3DH, Adafruit AS5600 Library, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA237 and INA238 Library, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit MLX90632 Library, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit QMC5883P Library, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SH110X, Adafruit SSD1306, RTClib, StreamUtils, Adafruit SHT4x Library, Adafruit GPS Library, Adafruit uBlox

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ lib_deps =
4646
adafruit/Adafruit HTS221
4747
adafruit/Adafruit HTU21DF Library
4848
adafruit/Adafruit HTU31D Library
49+
adafruit/Adafruit LIS3DH
4950
adafruit/Adafruit LTR390 Library
5051
adafruit/Adafruit LTR329 and LTR303
5152
adafruit/Adafruit PCT2075

src/components/i2c/controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ static const std::map<std::string, FnCreateI2CSensorDriver> I2cFactorySensor = {
168168
const char *driver_name) -> drvBase * {
169169
return new drvLc709203f(i2c, addr, mux_channel, driver_name);
170170
}},
171+
{"lis3dh",
172+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
173+
const char *driver_name) -> drvBase * {
174+
return new drvLis3dh(i2c, addr, mux_channel, driver_name);
175+
}},
171176
{"lps3xhw",
172177
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
173178
const char *driver_name) -> drvBase * {

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "drivers/drvIna238.h"
4141
#include "drivers/drvIna260.h"
4242
#include "drivers/drvLc709203f.h"
43+
#include "drivers/drvLis3dh.h"
4344
#include "drivers/drvLps22hb.h"
4445
#include "drivers/drvLps25hb.h"
4546
#include "drivers/drvLps3xhw.h"
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*!
2+
* @file drvLis3dh.cpp
3+
*
4+
* Driver wrapper for the Adafruit LIS3DH 3-axis accelerometer.
5+
*
6+
*/
7+
8+
#include "drvLis3dh.h"
9+
10+
#include <Adafruit_LIS3DH.h>
11+
#include <math.h>
12+
13+
/******************************************************************************/
14+
/*!
15+
@brief Destructor for a LIS3DH sensor.
16+
*/
17+
/******************************************************************************/
18+
drvLis3dh::~drvLis3dh() {
19+
if (_lis) {
20+
delete _lis;
21+
_lis = nullptr;
22+
}
23+
}
24+
25+
/******************************************************************************/
26+
/*!
27+
@brief Initializes the LIS3DH sensor and begins I2C.
28+
@returns True if initialized successfully, False otherwise.
29+
*/
30+
/******************************************************************************/
31+
bool drvLis3dh::begin() {
32+
// allocate LIS3DH instance with the I2C bus
33+
_lis = new Adafruit_LIS3DH(_i2c);
34+
35+
// Attempt to initialize with provided address
36+
if (!_lis->begin(_address)) {
37+
WS_DEBUG_PRINTLN("LIS3DH failed to initialise!");
38+
return false;
39+
}
40+
41+
// Set a reasonable range and data rate (use library enums)
42+
_lis->setRange(LIS3DH_RANGE_2_G); // 2G range
43+
// Note: some Adafruit_LIS3DH variants offer setDataRate; if present this
44+
// keeps defaults. Keep configuration minimal to avoid API mismatches.
45+
46+
return true;
47+
}
48+
49+
/******************************************************************************/
50+
/*!
51+
@brief Gets the LIS3DH's raw sensor event.
52+
@param rawEvent
53+
Pointer to the sensor event.
54+
@returns True if the sensor event was obtained successfully, False
55+
otherwise.
56+
*/
57+
/******************************************************************************/
58+
bool drvLis3dh::getEventRaw(sensors_event_t *rawEvent) {
59+
if (!_lis)
60+
return false;
61+
62+
// Read an Adafruit_Sensor compatible event from the device
63+
sensors_event_t event;
64+
_lis->getEvent(&event);
65+
66+
// Calculate magnitude of the acceleration vector (m/s^2) and store in
67+
// event->data[0] to be consistent with other drivers that expose "raw"
68+
float mag = sqrtf(event.acceleration.x * event.acceleration.x +
69+
event.acceleration.y * event.acceleration.y +
70+
event.acceleration.z * event.acceleration.z);
71+
rawEvent->data[0] = mag;
72+
return true;
73+
}
74+
75+
/******************************************************************************/
76+
/*!
77+
@brief Gets the LIS3DH's accelerometer sensor event (x,y,z in m/s^2).
78+
@param accelEvent
79+
Pointer to the accelerometer sensor event.
80+
@returns True if the sensor event was obtained successfully, False
81+
otherwise.
82+
*/
83+
/******************************************************************************/
84+
bool drvLis3dh::getEventAccelerometer(sensors_event_t *accelEvent) {
85+
if (!_lis)
86+
return false;
87+
88+
// Fill the provided event with sensor data
89+
_lis->getEvent(accelEvent);
90+
return true;
91+
}
92+
93+
void drvLis3dh::ConfigureDefaultSensorTypes() {
94+
_default_sensor_types_count = 1;
95+
_default_sensor_types[0] =
96+
wippersnapper_sensor_SensorType_SENSOR_TYPE_ACCELEROMETER;
97+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*!
2+
* @file drvLis3dh.h
3+
*
4+
* Driver wrapper for the Adafruit LIS3DH 3-axis accelerometer.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef DRV_LIS3DH_H
16+
#define DRV_LIS3DH_H
17+
18+
#include "drvBase.h"
19+
#include <Adafruit_LIS3DH.h>
20+
21+
class Adafruit_LIS3DH; // forward
22+
23+
/**************************************************************************/
24+
/*!
25+
@brief Class that provides a driver interface for a LIS3DH accelerometer.
26+
*/
27+
/**************************************************************************/
28+
class drvLis3dh : public drvBase {
29+
public:
30+
/*******************************************************************************/
31+
/*!
32+
@brief Constructor for a LIS3DH sensor.
33+
@param i2c
34+
The I2C interface.
35+
@param sensorAddress
36+
7-bit device address.
37+
@param mux_channel
38+
The I2C multiplexer channel.
39+
@param driver_name
40+
The name of the driver.
41+
*/
42+
/*******************************************************************************/
43+
drvLis3dh(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
44+
const char *driver_name)
45+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {}
46+
47+
/*******************************************************************************/
48+
/*!
49+
@brief Destructor for a LIS3DH sensor.
50+
*/
51+
/*******************************************************************************/
52+
~drvLis3dh();
53+
54+
/*******************************************************************************/
55+
/*!
56+
@brief Initializes the LIS3DH sensor and begins I2C.
57+
@returns True if initialized successfully, False otherwise.
58+
*/
59+
/*******************************************************************************/
60+
bool begin() override;
61+
62+
/*******************************************************************************/
63+
/*!
64+
@brief Gets the LIS3DH's raw sensor event (magnitude stored in
65+
event->data[0]).
66+
@param rawEvent
67+
Pointer to the sensor event to fill.
68+
@returns True if the event was obtained successfully, False otherwise.
69+
*/
70+
/*******************************************************************************/
71+
bool getEventRaw(sensors_event_t *rawEvent) override;
72+
73+
/*******************************************************************************/
74+
/*!
75+
@brief Gets the LIS3DH's accelerometer sensor event (x,y,z in m/s^2).
76+
@param accelEvent
77+
Pointer to the accelerometer sensor event.
78+
@returns True if the sensor event was obtained successfully, False
79+
otherwise.
80+
*/
81+
/*******************************************************************************/
82+
bool getEventAccelerometer(sensors_event_t *accelEvent) override;
83+
84+
void ConfigureDefaultSensorTypes() override;
85+
86+
protected:
87+
Adafruit_LIS3DH *_lis = nullptr; ///< Pointer to LIS3DH sensor object
88+
};
89+
90+
#endif // DRV_LIS3DH_H

0 commit comments

Comments
 (0)