RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
TSL2591.h
Go to the documentation of this file.
1#pragma once
2#include <iostream>
3#include <wiringPiI2C.h>
4#include <unistd.h>
5#include <stdint.h>
6#include <math.h>
7#include <string>
8#include "BaseComponent.h"
9#include "../Data/SensorLevels.h"
10
11namespace RumPi
12{
13 //TSL2591 Constants
14 constexpr int TSL2591_ADDR = 0x29;
15 constexpr int TSL2591_COMMAND_BIT = 0xA0;
16 constexpr int TSL2591_ENABLE_REG = 0x00;
17 constexpr int TSL2591_CONTROL_REG = 0x01;
18 constexpr int TSL2591_C0DATA_L = 0x14;
19 constexpr int TSL2591_C1DATA_L = 0x16;
20
21 //Lux Coefficients
22 constexpr double TSL2591_LUX_DF = 408.0;
23 constexpr double TSL2591_LUX_COEFB = 1.64;
24
32 {
33 ATIME_100MS = 0x00,
34 ATIME_200MS = 0x01,
35 ATIME_300MS = 0x02,
36 ATIME_400MS = 0x03,
37 ATIME_500MS = 0x04,
38 ATIME_600MS = 0x05
39 };
40
48 {
49 GAIN_LOW = 0x00, // 1x
50 GAIN_MED = 0x10, // 25x
51 GAIN_HIGH = 0x20, // 428x
52 GAIN_MAX = 0x30 // 9876x
53 };
54
61 class TSL2591 : public BaseComponent
62 {
63 private:
70
71 //Lux boundaries for the light-level buckets (ascending). Defaulted in the ctor and overridden from the
72 //library config, so the dashboard's light categories can be tuned without a rebuild.
78
79 public:
94
106 virtual ~TSL2591();
107
119 virtual void TurnOn() override;
120
131 virtual void TurnOff() override;
132
144 virtual void ProcessRawValues() override;
145
158 virtual std::string ToString() const override;
159
175 virtual bool AdjustAndValidateSettings(const uint16_t ch0);
176
193 virtual float CalculateLux(const uint16_t ch0, const uint16_t ch1);
194
207 virtual float GetLux() const;
208
222 virtual E_LightLevel CalculateLightLevel() const;
223
238 virtual E_LightLevel CalculateLightLevel(const float lux) const;
239
256 void SetLightLevelCutoffs(float dark, float veryDim, float dim, float normal, float bright);
257
270 virtual std::string DetermineLightCategory() const;
271
287 virtual std::string DetermineLightCategory(const float lux) const;
288
303 virtual void SetTiming(const E_TSL2591_IntegrationTime integrationTimeMilliseconds, const E_TSL2591_AmplificationGain amplificationGain);
304
320 static float ConvertIntegrationTimeToDecimalMilliseconds(const E_TSL2591_IntegrationTime integrationTimeMilliseconds);
321
336 static E_TSL2591_IntegrationTime ConvertDecimalMillisecondsToIntegrationTime(const float decimalMilliseconds);
337
354
370 };
371} // namespace RumPi
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
TSL2591 high-dynamic-range digital light sensor (I2C), reading full-spectrum and IR channels to compu...
Definition: TSL2591.h:62
static float ConvertIntegrationTimeToDecimalMilliseconds(const E_TSL2591_IntegrationTime integrationTimeMilliseconds)
Converts an integration-time enum to its value in milliseconds.
Definition: TSL2591.cpp:479
virtual float CalculateLux(const uint16_t ch0, const uint16_t ch1)
Computes lux from the full-spectrum and IR channels, normalizing by counts-per-lux and rejecting IR,...
Definition: TSL2591.cpp:393
float myLightCutoffNormal
Upper lux bound of the "Normal" bucket.
Definition: TSL2591.h:76
virtual std::string DetermineLightCategory() const
Gets the short, human-readable light category for the current reading.
Definition: TSL2591.cpp:262
virtual float GetLux() const
Gets the most recently calculated lux value.
Definition: TSL2591.cpp:156
virtual void TurnOff() override
Turns the sensor off by disabling it over I2C (if it was initialized).
Definition: TSL2591.cpp:94
uint16_t myRawChannel1Reading
The most recent raw infrared (CH1) reading.
Definition: TSL2591.h:66
virtual void ProcessRawValues() override
Reads the raw CH0/CH1 channels, auto-adjusts gain if the reading is saturated or too dark (re-reading...
Definition: TSL2591.cpp:116
E_TSL2591_IntegrationTime myIntegrationTimeMilliseconds
How long the sensor integrates each reading.
Definition: TSL2591.h:68
static float ConvertAmplificationGainToDecimalMultiplier(const E_TSL2591_AmplificationGain amplificationGain)
Converts an amplification-gain enum to its decimal multiplier.
Definition: TSL2591.cpp:539
void SetLightLevelCutoffs(float dark, float veryDim, float dim, float normal, float bright)
Sets the ascending lux cutoffs that define the dark/very-dim/dim/normal/bright light-level buckets.
Definition: TSL2591.cpp:241
TSL2591(E_TSL2591_IntegrationTime integrationTimeMilliseconds=E_TSL2591_IntegrationTime::ATIME_100MS, E_TSL2591_AmplificationGain amplificationGain=E_TSL2591_AmplificationGain::GAIN_MED)
Builds a TSL2591 with the given integration time and gain, and default light-level cutoffs.
Definition: TSL2591.cpp:20
virtual void SetTiming(const E_TSL2591_IntegrationTime integrationTimeMilliseconds, const E_TSL2591_AmplificationGain amplificationGain)
Writes the combined integration-time and gain byte to the sensor's control register,...
Definition: TSL2591.cpp:449
virtual std::string ToString() const override
Serializes the raw CH0/CH1 readings, calculated lux, and current gain to a string.
Definition: TSL2591.cpp:601
E_TSL2591_AmplificationGain myAmplificationGain
The current amplification gain.
Definition: TSL2591.h:69
float myLightCutoffDim
Upper lux bound of the "Dim" bucket.
Definition: TSL2591.h:75
virtual void TurnOn() override
Initializes the sensor over I2C, powers on the ALS, applies the timing/gain, and lets the sensor wake...
Definition: TSL2591.cpp:62
float myLightCutoffDark
Upper lux bound of the "Dark" bucket.
Definition: TSL2591.h:73
virtual E_LightLevel CalculateLightLevel() const
Maps the most recent lux reading onto a 0 (pitch dark) to 5 (very bright) level.
Definition: TSL2591.cpp:174
float myLightCutoffVeryDim
Upper lux bound of the "Very Dim" bucket.
Definition: TSL2591.h:74
virtual ~TSL2591()
Destroys the sensor.
Definition: TSL2591.cpp:46
int myFileDescriptor
The open i2c device file descriptor.
Definition: TSL2591.h:64
static E_TSL2591_AmplificationGain ConvertDecimalMultiplierToAmplificationGain(const float decimalMultiplier)
Converts a decimal gain multiplier to the closest amplification-gain enum at or below it.
Definition: TSL2591.cpp:571
float myLightCutoffBright
Upper lux bound of the "Bright" bucket.
Definition: TSL2591.h:77
static E_TSL2591_IntegrationTime ConvertDecimalMillisecondsToIntegrationTime(const float decimalMilliseconds)
Converts a millisecond value to the closest integration-time enum at or below it.
Definition: TSL2591.cpp:498
virtual bool AdjustAndValidateSettings(const uint16_t ch0)
Auto-adjusts the amplification gain one step down when the channel is flooded (> 62000) or one step u...
Definition: TSL2591.cpp:325
uint16_t myRawChannel0Reading
The most recent raw full-spectrum (CH0) reading.
Definition: TSL2591.h:65
float myCalculatedLux
The most recently calculated lux value.
Definition: TSL2591.h:67
RumPi is the library's top-level facade.
Definition: AlertManager.h:6
constexpr int TSL2591_C1DATA_L
Definition: TSL2591.h:19
constexpr int TSL2591_COMMAND_BIT
Definition: TSL2591.h:15
constexpr int TSL2591_ADDR
Definition: TSL2591.h:14
E_TSL2591_AmplificationGain
This represents the amplification level, helpful for low/high light situations.
Definition: TSL2591.h:48
constexpr double TSL2591_LUX_COEFB
Definition: TSL2591.h:23
constexpr int TSL2591_CONTROL_REG
Definition: TSL2591.h:17
constexpr int TSL2591_C0DATA_L
Definition: TSL2591.h:18
E_LightLevel
How much light the sensor is seeing, darkest to brightest.
Definition: SensorLevels.h:17
constexpr double TSL2591_LUX_DF
Definition: TSL2591.h:22
constexpr int TSL2591_ENABLE_REG
Definition: TSL2591.h:16
E_TSL2591_IntegrationTime
This represents how long the sensor looks at the light in a reading.
Definition: TSL2591.h:32