Adafruit HTS221 Library
Adafruit_TSL2591.h
Go to the documentation of this file.
1 /**************************************************************************/
17 /**************************************************************************/
18 
19 #ifndef _TSL2591_H_
20 #define _TSL2591_H_
21 
22 #include <Adafruit_I2CDevice.h>
23 #include <Adafruit_Sensor.h>
24 #include <Arduino.h>
25 
26 #define TSL2591_VISIBLE (2)
27 #define TSL2591_INFRARED (1)
28 #define TSL2591_FULLSPECTRUM (0)
29 
30 #define TSL2591_ADDR (0x29)
31 
32 #define TSL2591_COMMAND_BIT \
33  (0xA0)
34 
35 #define TSL2591_CLEAR_INT (0xE7)
37 #define TSL2591_TEST_INT (0xE4)
39 
40 #define TSL2591_WORD_BIT (0x20)
41 #define TSL2591_BLOCK_BIT (0x10)
42 
43 #define TSL2591_ENABLE_POWEROFF (0x00)
44 #define TSL2591_ENABLE_POWERON (0x01)
45 #define TSL2591_ENABLE_AEN \
46  (0x02)
47 #define TSL2591_ENABLE_AIEN \
49  (0x10)
50 #define TSL2591_ENABLE_NPIEN \
52  (0x80)
53 
55 #define TSL2591_LUX_DF (408.0F)
56 #define TSL2591_LUX_COEFB (1.64F)
57 #define TSL2591_LUX_COEFC (0.59F)
58 #define TSL2591_LUX_COEFD (0.86F)
59 
60 enum {
62  TSL2591_REGISTER_ENABLE = 0x00, // Enable register
63  TSL2591_REGISTER_CONTROL = 0x01, // Control register
64  TSL2591_REGISTER_THRESHOLD_AILTL = 0x04, // ALS low threshold lower byte
65  TSL2591_REGISTER_THRESHOLD_AILTH = 0x05, // ALS low threshold upper byte
66  TSL2591_REGISTER_THRESHOLD_AIHTL = 0x06, // ALS high threshold lower byte
67  TSL2591_REGISTER_THRESHOLD_AIHTH = 0x07, // ALS high threshold upper byte
68  TSL2591_REGISTER_THRESHOLD_NPAILTL =
69  0x08, // No Persist ALS low threshold lower byte
70  TSL2591_REGISTER_THRESHOLD_NPAILTH =
71  0x09, // No Persist ALS low threshold higher byte
72  TSL2591_REGISTER_THRESHOLD_NPAIHTL =
73  0x0A, // No Persist ALS high threshold lower byte
74  TSL2591_REGISTER_THRESHOLD_NPAIHTH =
75  0x0B, // No Persist ALS high threshold higher byte
76  TSL2591_REGISTER_PERSIST_FILTER = 0x0C, // Interrupt persistence filter
77  TSL2591_REGISTER_PACKAGE_PID = 0x11, // Package Identification
78  TSL2591_REGISTER_DEVICE_ID = 0x12, // Device Identification
79  TSL2591_REGISTER_DEVICE_STATUS = 0x13, // Internal Status
80  TSL2591_REGISTER_CHAN0_LOW = 0x14, // Channel 0 data, low byte
81  TSL2591_REGISTER_CHAN0_HIGH = 0x15, // Channel 0 data, high byte
82  TSL2591_REGISTER_CHAN1_LOW = 0x16, // Channel 1 data, low byte
83  TSL2591_REGISTER_CHAN1_HIGH = 0x17, // Channel 1 data, high byte
84 };
85 
87 typedef enum {
88  TSL2591_INTEGRATIONTIME_100MS = 0x00, // 100 millis
89  TSL2591_INTEGRATIONTIME_200MS = 0x01, // 200 millis
90  TSL2591_INTEGRATIONTIME_300MS = 0x02, // 300 millis
91  TSL2591_INTEGRATIONTIME_400MS = 0x03, // 400 millis
92  TSL2591_INTEGRATIONTIME_500MS = 0x04, // 500 millis
93  TSL2591_INTEGRATIONTIME_600MS = 0x05, // 600 millis
95 
97 typedef enum {
98  // bit 7:4: 0
99  TSL2591_PERSIST_EVERY = 0x00, // Every ALS cycle generates an interrupt
100  TSL2591_PERSIST_ANY = 0x01, // Any value outside of threshold range
101  TSL2591_PERSIST_2 = 0x02, // 2 consecutive values out of range
102  TSL2591_PERSIST_3 = 0x03, // 3 consecutive values out of range
103  TSL2591_PERSIST_5 = 0x04, // 5 consecutive values out of range
104  TSL2591_PERSIST_10 = 0x05, // 10 consecutive values out of range
105  TSL2591_PERSIST_15 = 0x06, // 15 consecutive values out of range
106  TSL2591_PERSIST_20 = 0x07, // 20 consecutive values out of range
107  TSL2591_PERSIST_25 = 0x08, // 25 consecutive values out of range
108  TSL2591_PERSIST_30 = 0x09, // 30 consecutive values out of range
109  TSL2591_PERSIST_35 = 0x0A, // 35 consecutive values out of range
110  TSL2591_PERSIST_40 = 0x0B, // 40 consecutive values out of range
111  TSL2591_PERSIST_45 = 0x0C, // 45 consecutive values out of range
112  TSL2591_PERSIST_50 = 0x0D, // 50 consecutive values out of range
113  TSL2591_PERSIST_55 = 0x0E, // 55 consecutive values out of range
114  TSL2591_PERSIST_60 = 0x0F, // 60 consecutive values out of range
116 
118 typedef enum {
119  TSL2591_GAIN_LOW = 0x00,
123 } tsl2591Gain_t;
124 
125 /**************************************************************************/
130 /**************************************************************************/
131 class Adafruit_TSL2591 : public Adafruit_Sensor {
132 public:
133  Adafruit_TSL2591(int32_t sensorID = -1);
134  ~Adafruit_TSL2591();
135 
136  boolean begin(TwoWire *theWire, uint8_t addr = TSL2591_ADDR);
137  boolean begin(uint8_t addr = TSL2591_ADDR);
138  void enable(void);
139  void disable(void);
140 
141  float calculateLux(uint16_t ch0, uint16_t ch1);
142  void setGain(tsl2591Gain_t gain);
143  void setTiming(tsl2591IntegrationTime_t integration);
144  uint16_t getLuminosity(uint8_t channel);
145  uint32_t getFullLuminosity();
146 
149 
150  // Interrupt
151  void clearInterrupt(void);
152  void registerInterrupt(uint16_t lowerThreshold, uint16_t upperThreshold,
153  tsl2591Persist_t persist);
154  uint8_t getStatus();
155 
156  /* Unified Sensor API Functions */
157  bool getEvent(sensors_event_t *);
158  void getSensor(sensor_t *);
159 
160 private:
161  Adafruit_I2CDevice *i2c_dev = NULL;
162 
163  void write8(uint8_t r);
164  void write8(uint8_t r, uint8_t v);
165  uint16_t read16(uint8_t reg);
166  uint8_t read8(uint8_t reg);
167 
168  tsl2591IntegrationTime_t _integration;
169  tsl2591Gain_t _gain;
170  int32_t _sensorID;
171  uint8_t _addr;
172 
173  boolean _initialized;
174 };
175 #endif
low gain (1x)
Definition: Adafruit_TSL2591.h:120
void getSensor(sensor_t *)
Gets the overall sensor_t data including the type, range and resulution.
Definition: Adafruit_TSL2591.cpp:467
#define TSL2591_ADDR
Default I2C address.
Definition: Adafruit_TSL2591.h:30
medium gain (428x)
Definition: Adafruit_TSL2591.h:122
void setTiming(tsl2591IntegrationTime_t integration)
Setter for sensor integration time setting.
Definition: Adafruit_TSL2591.cpp:191
Class that stores state and functions for interacting with TSL2591 Light Sensor.
Definition: Adafruit_TSL2591.h:131
bool getEvent(sensors_event_t *)
Gets the most recent sensor event.
Definition: Adafruit_TSL2591.cpp:435
Adafruit_TSL2591(int32_t sensorID=-1)
Instantiates a new Adafruit TSL2591 class.
Definition: Adafruit_TSL2591.cpp:58
void setGain(tsl2591Gain_t gain)
Setter for sensor light gain.
Definition: Adafruit_TSL2591.cpp:164
void registerInterrupt(uint16_t lowerThreshold, uint16_t upperThreshold, tsl2591Persist_t persist)
Set up the interrupt to go off when light level is outside the lower/upper range. ...
Definition: Adafruit_TSL2591.cpp:366
void clearInterrupt(void)
Clear interrupt status.
Definition: Adafruit_TSL2591.cpp:393
medium gain (25x)
Definition: Adafruit_TSL2591.h:121
float calculateLux(uint16_t ch0, uint16_t ch1)
Calculates the visible Lux based on the two light sensors.
Definition: Adafruit_TSL2591.cpp:220
tsl2591Gain_t
Enumeration for the sensor gain.
Definition: Adafruit_TSL2591.h:118
void disable(void)
Disables the chip, so it&#39;s in power down mode.
Definition: Adafruit_TSL2591.cpp:146
boolean begin(TwoWire *theWire, uint8_t addr=TSL2591_ADDR)
Setups the I2C interface and hardware, identifies if chip is found.
Definition: Adafruit_TSL2591.cpp:81
tsl2591Persist_t
Enumeration for the persistance filter (for interrupts)
Definition: Adafruit_TSL2591.h:97
uint16_t getLuminosity(uint8_t channel)
Reads the raw data from the channel.
Definition: Adafruit_TSL2591.cpp:336
void enable(void)
Enables the chip, so it&#39;s ready to take readings.
Definition: Adafruit_TSL2591.cpp:128
uint8_t getStatus()
Gets the most recent sensor event from the hardware status register.
Definition: Adafruit_TSL2591.cpp:412
tsl2591Gain_t getGain()
Getter for sensor light gain.
Definition: Adafruit_TSL2591.cpp:183
uint32_t getFullLuminosity()
Reads the raw data from both light channels.
Definition: Adafruit_TSL2591.cpp:300
tsl2591IntegrationTime_t getTiming()
Getter for sensor integration time setting.
Definition: Adafruit_TSL2591.cpp:210
tsl2591IntegrationTime_t
Enumeration for the sensor integration timing.
Definition: Adafruit_TSL2591.h:87