1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_LTR329_LTR303.h
3
+ *
4
+ * Device driver for the LTR329 + LTR303 (329+interrupt) light sensors.
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) Tyeth Gundry 2023 for Adafruit Industries.
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+ #ifndef WipperSnapper_I2C_Driver_LTR329_LTR303_H
16
+ #define WipperSnapper_I2C_Driver_LTR329_LTR303_H
17
+
18
+ #include " WipperSnapper_I2C_Driver.h"
19
+ #include < Adafruit_LTR329_LTR303.h>
20
+
21
+ /* *************************************************************************/
22
+ /* !
23
+ @brief Class that provides a driver interface for a LTR329/303 sensor.
24
+ */
25
+ /* *************************************************************************/
26
+ class WipperSnapper_I2C_Driver_LTR329_LTR303 : public WipperSnapper_I2C_Driver {
27
+ public:
28
+ /* ******************************************************************************/
29
+ /* !
30
+ @brief Constructor for a LTR329/303 sensor.
31
+ @param i2c
32
+ The I2C interface.
33
+ @param sensorAddress
34
+ The 7-bit I2C address of the sensor.
35
+ */
36
+ /* ******************************************************************************/
37
+ WipperSnapper_I2C_Driver_LTR329_LTR303 (TwoWire *i2c, uint16_t sensorAddress)
38
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
39
+ _i2c = i2c;
40
+ _sensorAddress = sensorAddress;
41
+ }
42
+
43
+ /* ******************************************************************************/
44
+ /* !
45
+ @brief Destructor for an LTR329/303 sensor.
46
+ */
47
+ /* ******************************************************************************/
48
+ ~WipperSnapper_I2C_Driver_LTR329_LTR303 () { delete _LTR329; }
49
+
50
+ /* ******************************************************************************/
51
+ /* !
52
+ @brief Initializes the LTR329/303 sensor and begins I2C.
53
+ @returns True if initialized successfully, False otherwise.
54
+ */
55
+ /* ******************************************************************************/
56
+ bool begin () {
57
+ _LTR329 = new Adafruit_LTR329 ();
58
+ // Attempt to initialize LTR329
59
+ if (!_LTR329->begin (_i2c))
60
+ return false ;
61
+
62
+ // Configure LTR329 sensor - tested on a dull British day Oct'23 @7kLux
63
+ // Matches similar lux value from LTR390 with default configuration.
64
+ _LTR329->setGain (LTR3XX_GAIN_48);
65
+ _LTR329->setIntegrationTime (LTR3XX_INTEGTIME_100);
66
+ _LTR329->setMeasurementRate (LTR3XX_MEASRATE_100);
67
+ _delayBetweenReads = 110 ; // 100ms measurement time + 10ms fudge-factor
68
+ return true ;
69
+ }
70
+
71
+ /* ******************************************************************************/
72
+ /* !
73
+ @brief Reads the LTR329's ambient light level ([Visible+IR] - IR-only)
74
+ @param lightEvent
75
+ Light sensor reading.
76
+ @returns True if the sensor event was obtained successfully, False
77
+ otherwise.
78
+ */
79
+ /* ******************************************************************************/
80
+ bool getEventLight (sensors_event_t *lightEvent) {
81
+ if (!_LTR329->newDataAvailable ())
82
+ return false ;
83
+
84
+ uint16_t visible_plus_ir, infrared;
85
+ _LTR329->readBothChannels (visible_plus_ir, infrared);
86
+ lightEvent->light = visible_plus_ir - infrared;
87
+ return true ;
88
+ }
89
+
90
+ /* ******************************************************************************/
91
+ /* !
92
+ @brief Reads the LTR329's infrared value into an event.
93
+ @param rawEvent
94
+ Pointer to an adafruit sensor event.
95
+ @returns True if the sensor event was obtained successfully, False
96
+ otherwise.
97
+ */
98
+ /* ******************************************************************************/
99
+ bool getEventRaw (sensors_event_t *rawEvent) {
100
+
101
+ if (!_LTR329->newDataAvailable ()) {
102
+ delay (
103
+ _delayBetweenReads); // Raw comes after Light event and needs new data
104
+ if (!_LTR329->newDataAvailable ())
105
+ return false ;
106
+ }
107
+
108
+ uint16_t visible_plus_ir, infrared;
109
+ _LTR329->readBothChannels (visible_plus_ir, infrared);
110
+ rawEvent->data [0 ] = (float )infrared;
111
+ return true ;
112
+ }
113
+
114
+ protected:
115
+ Adafruit_LTR329 *_LTR329; // /< Pointer to LTR329 light sensor object
116
+
117
+ /* *
118
+ * @brief The delay between consecutive reads in milliseconds.
119
+ */
120
+ uint16_t _delayBetweenReads;
121
+ };
122
+
123
+ #endif // WipperSnapper_I2C_Driver_LTR329_LTR303
0 commit comments