Skip to content

Commit

Permalink
Add LTR390 sensor with switching mode example
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Oct 3, 2023
1 parent bd8a96a commit c13074d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino client for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_IO_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, 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 PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR390 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, 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 PM25 AQI Sensor, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ lib_deps =
adafruit/Adafruit DPS310
adafruit/Adafruit INA219
adafruit/Adafruit HTS221
adafruit/Adafruit LTR390 Library
adafruit/Adafruit PCT2075
adafruit/Adafruit SCD30
adafruit/Adafruit SGP30 Sensor
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_ina219->configureDriver(msgDeviceInitReq);
drivers.push_back(_ina219);
WS_DEBUG_PRINTLN("INA219 Initialized Successfully!");
} else if (strcmp("ltr390", msgDeviceInitReq->i2c_device_name) == 0) {
_ltr390 = new WipperSnapper_I2C_Driver_LTR390(this->_i2c, i2cAddress);
if (!_ltr390->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize LTR390");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_ltr390->configureDriver(msgDeviceInitReq);
drivers.push_back(_ltr390);
WS_DEBUG_PRINTLN("LTR390 Initialized Successfully!");
} else if (strcmp("sgp30", msgDeviceInitReq->i2c_device_name) == 0) {
_sgp30 = new WipperSnapper_I2C_Driver_SGP30(this->_i2c, i2cAddress);
if (!_sgp30->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS25HB.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS3XHW.h"
#include "drivers/WipperSnapper_I2C_Driver_LTR390.h"
#include "drivers/WipperSnapper_I2C_Driver_MAX17048.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#include "drivers/WipperSnapper_I2C_Driver_MPL115A2.h"
Expand Down Expand Up @@ -115,6 +116,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_BME680 *_bme680 = nullptr;
WipperSnapper_I2C_Driver_HTS221 *_hts221 = nullptr;
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
WipperSnapper_I2C_Driver_MPL115A2 *_mpl115a2 = nullptr;
WipperSnapper_I2C_Driver_MPRLS *_mprls = nullptr;
Expand Down
122 changes: 122 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_LTR390.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*!
* @file WipperSnapper_I2C_Driver_LTR390.h
*
* Device driver for the LTR390 light sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2023 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_LTR390_H
#define WipperSnapper_I2C_Driver_LTR390_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_LTR390.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a LTR390 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_LTR390 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for a LTR390 sensor.
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_LTR390(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an LTR390 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_LTR390() { delete _ltr390; }

/*******************************************************************************/
/*!
@brief Initializes the LTR390 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_ltr390 = new Adafruit_LTR390();
// Attempt to initialize LTR390
if (!_ltr390->begin(_i2c))
return false;

// Configure LTR390 sensor
// Note: This driver uses the default configuration from
// https://github.com/adafruit/Adafruit_LTR390/blob/master/examples/ltr390_test/ltr390_test.ino
_ltr390->setMode(LTR390_MODE_ALS);
_ltr390->setGain(LTR390_GAIN_3);
_ltr390->setResolution(LTR390_RESOLUTION_16BIT);
return true;
}

/*******************************************************************************/
/*!
@brief Performs a light sensor read using the Adafruit
Unified Sensor API.
@param lightEvent
Light sensor reading, in lux.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventLight(sensors_event_t *lightEvent) {
if (_ltr390->getMode() != LTR390_MODE_ALS){
_ltr390->setMode(LTR390_MODE_ALS);
delay(100);
}

if (!_ltr390->newDataAvailable())
return false;

lightEvent->light = _ltr390->readALS();
return true;
}


/*******************************************************************************/
/*!
@brief Reads the LTR390's UV value into an event.
@param rawEvent
Pointer to an adafruit sensor event.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *rawEvent) {
if (_ltr390->getMode() != LTR390_MODE_UVS){
_ltr390->setMode(LTR390_MODE_UVS);
delay(100);
}

if (!_ltr390->newDataAvailable())
return false;

rawEvent->data[0] = (float)_ltr390->readUVS();
return true;
}


protected:
Adafruit_LTR390 *_ltr390; ///< Pointer to LTR390 light sensor object
};

#endif // WipperSnapper_I2C_Driver_LTR390

0 comments on commit c13074d

Please sign in to comment.