Skip to content

Commit

Permalink
Showing 6 changed files with 235 additions and 4 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Adafruit WipperSnapper Beta
version=1.0.0-beta.28
version=1.0.0-beta.29
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino client for Adafruit.io WipperSnapper
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 SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, Adafruit MCP9808 Library, Adafruit MCP9600 Library
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit SleepyDog Library, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit DPS310, Adafruit SCD30, Sensirion I2C SCD4x, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit TSL2591 Library
2 changes: 1 addition & 1 deletion src/Wippersnapper.h
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@
#endif

#define WS_VERSION \
"1.0.0-beta.28" ///< WipperSnapper app. version (semver-formatted)
"1.0.0-beta.29" ///< WipperSnapper app. version (semver-formatted)

// Reserved Adafruit IO MQTT topics
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
40 changes: 39 additions & 1 deletion src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
@@ -265,6 +265,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_mcp9601->configureDriver(msgDeviceInitReq);
drivers.push_back(_mcp9601);
WS_DEBUG_PRINTLN("MCP9601 Initialized Successfully!");
} else if (strcmp("tsl2591", msgDeviceInitReq->i2c_device_name) == 0) {
_tsl2591 = new WipperSnapper_I2C_Driver_TSL2591(this->_i2c, i2cAddress);
if (!_tsl2591->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize TSL2591!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_tsl2591->configureDriver(msgDeviceInitReq);
drivers.push_back(_tsl2591);
WS_DEBUG_PRINTLN("TSL2591 Initialized Successfully!");
} else {
WS_DEBUG_PRINTLN("ERROR: I2C device type not found!")
_busStatusResponse =
@@ -317,6 +328,10 @@ void WipperSnapper_Component_I2C::updateI2CDeviceProperties(
drivers[i]->updateSensorAltitude(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
break;
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_LIGHT:
drivers[i]->updateSensorLight(
msgDeviceUpdateReq->i2c_device_properties[j].sensor_period);
break;
default:
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_UNSUPPORTED_SENSOR;
@@ -473,7 +488,7 @@ void WipperSnapper_Component_I2C::update() {
}
}

// AMBIENT_TEMPERATURE sensor
// OBJECT_TEMPERATURE sensor
curTime = millis();
if ((*iter)->sensorObjectTempPeriod() != 0L &&
curTime - (*iter)->sensorObjectTempPeriodPrv() >
@@ -588,6 +603,29 @@ void WipperSnapper_Component_I2C::update() {
}
}

// Light sensor
curTime = millis();
if ((*iter)->sensorLightPeriod() != 0L &&
curTime - (*iter)->SensorLightPeriodPrv() >
(*iter)->sensorLightPeriod()) {
if ((*iter)->getEventLight(&event)) {
WS_DEBUG_PRINT("Sensor 0x");
WS_DEBUG_PRINTHEX((*iter)->getI2CAddress());
WS_DEBUG_PRINTLN("");
WS_DEBUG_PRINT("\tLight: ");
WS_DEBUG_PRINT(event.light);
WS_DEBUG_PRINTLN(" lux");

// pack event data into msg
fillEventMessage(&msgi2cResponse, event.light,
wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_LIGHT);

(*iter)->setSensorLightPeriodPrv(curTime);
} else {
WS_DEBUG_PRINTLN("ERROR: Failed to get light sensor reading!");
}
}

// Did this driver obtain data from sensors?
if (msgi2cResponse.payload.resp_i2c_device_event.sensor_event_count == 0)
continue;
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
#include "drivers/WipperSnapper_I2C_Driver_MCP9601.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
#include "drivers/WipperSnapper_I2C_Driver_TSL2591.h"

#define I2C_TIMEOUT_MS 50 ///< Default I2C timeout, in milliseconds.

@@ -76,6 +77,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_BME280 *_bme280 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
WipperSnapper_I2C_Driver_MCP9601 *_mcp9601 = nullptr;
WipperSnapper_I2C_Driver_TSL2591 *_tsl2591 = nullptr;
};
extern Wippersnapper WS;

93 changes: 93 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
@@ -104,6 +104,10 @@ class WipperSnapper_I2C_Driver {
enableSensorObjectTemp();
setSensorObjectTempPeriod(
msgDeviceInitReq->i2c_device_properties[propertyIdx].sensor_period);
case wippersnapper_i2c_v1_SensorType_SENSOR_TYPE_LIGHT:
enableSensorLight();
setSensorLightPeriod(
msgDeviceInitReq->i2c_device_properties[propertyIdx].sensor_period);
default:
break;
}
@@ -762,6 +766,91 @@ class WipperSnapper_I2C_Driver {
setSensorObjectTempPeriod(period);
}

/**************************** SENSOR_TYPE: LIGHT
* ****************************/
/*******************************************************************************/
/*!
@brief Enables the device's object light sensor, if it exists.
*/
/*******************************************************************************/
virtual void enableSensorLight(){};

/*******************************************************************************/
/*!
@brief Disables the device's object light sensor, if it exists.
*/
/*******************************************************************************/
virtual void disableSensorLight() { _lightSensorPeriod = 0.0L; }

/*********************************************************************************/
/*!
@brief Base implementation - Returns the object light sensor's
period, if set.
@returns Time when the object light sensor should be polled, in
seconds.
*/
/*********************************************************************************/
virtual long sensorLightPeriod() { return _lightSensorPeriod; }

/*******************************************************************************/
/*!
@brief Set the object light sensor's return frequency.
@param period
The time interval at which to return new data from the
object light sensor.
*/
/*******************************************************************************/
virtual void setSensorLightPeriod(float period) {
if (period == 0)
disableSensorLight();
// Period is in seconds, cast it to long and convert it to milliseconds
_lightSensorPeriod = (long)period * 1000;
}

/*********************************************************************************/
/*!
@brief Base implementation - Returns the previous time interval at
which the light sensor was queried last.
@returns Time when the light sensor was last queried,
in seconds.
*/
/*********************************************************************************/
virtual long SensorLightPeriodPrv() { return _lightSensorPeriodPrv; }

/*******************************************************************************/
/*!
@brief Sets a timestamp for when the light sensor
was queried.
@param period
The time when the light sensor was queried last.
*/
/*******************************************************************************/
virtual void setSensorLightPeriodPrv(long period) {
_lightSensorPeriodPrv = period;
}

/*******************************************************************************/
/*!
@brief Base implementation - Reads a object light sensor and
converts the reading into the expected SI unit.
@param lightEvent
Light sensor reading, in meters.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
virtual bool getEventLight(sensors_event_t *lightEvent) { return false; }

/*******************************************************************************/
/*!
@brief Updates the properties of a object light sensor.
@param period
The time interval at which to return new data from the
light sensor.
*/
/*******************************************************************************/
virtual void updateSensorLight(float period) { setSensorLightPeriod(period); }

protected:
TwoWire *_i2c; ///< Pointer to the I2C driver's Wire object
uint16_t _sensorAddress; ///< The I2C driver's unique I2C address.
@@ -793,6 +882,10 @@ class WipperSnapper_I2C_Driver {
///< object temperature sensor's value.
long _objectTempSensorPeriodPrv = 0L; ///< The time when the object
///< temperature sensor was last read.
long _lightSensorPeriod = 0L; ///< The time period between reading the
///< light sensor's value.
long _lightSensorPeriodPrv = 0L; ///< The time when the light sensor
///< was last read.
};

#endif // WipperSnapper_I2C_Driver_H
98 changes: 98 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_TSL2591.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*!
* @file WipperSnapper_I2C_Driver_TSL2591.h
*
* Device driver for the TSL2591 digital luminosity (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) Brent Rubell 2022 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_TSL2591_H
#define WipperSnapper_I2C_Driver_TSL2591_H

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

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

/*******************************************************************************/
/*!
@brief Destructor for an TSL2591 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_TSL2591() { delete _tsl; }

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

// Configure TSL2591 sensor
// Note: This driver uses the default configuration from
// https://github.com/adafruit/Adafruit_TSL2591_Library/blob/master/examples/tsl2591/tsl2591.ino
_tsl->setGain(TSL2591_GAIN_MED); // 25x gain
_tsl->setTiming(TSL2591_INTEGRATIONTIME_300MS); // 300ms integration time
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) {
// Get sensor event
_tsl->getEvent(lightEvent);

// If lightEvent->light = 0 lux the sensor is probably saturated and no
// reliable data could be generated! or if lightEvent->light is +/-
// 4294967040 there was a float over/underflow
if ((lightEvent->light == 0) | (lightEvent->light > 4294966000.0) |
(lightEvent->light < -4294966000.0))
return false;

return true;
}

protected:
Adafruit_TSL2591 *_tsl; ///< Pointer to TSL2591 light sensor object
};

#endif // WipperSnapper_I2C_Driver_TSL2591

0 comments on commit 07ecff1

Please sign in to comment.