@@ -92,6 +92,13 @@ const unsigned char BMPx8x_OverSampling = 3;
92
92
#define HTU21D_TEMP_DELAY 70 /* Maximum required measuring time for a complete temperature read */
93
93
#define HTU21D_HUM_DELAY 36 /* Maximum required measuring time for a complete humidity read */
94
94
95
+ // TSL2561 registers
96
+ #define TSL2561_ADDRESS 0x39 /* I2C address */
97
+ #define TSL2561_INIT 0x03 /* start integrations */
98
+ #define TSL2561_Channel0 0xAC /* IR+Visible lux */
99
+ #define TSL2561_Channel1 0xAE /* IR only lux */
100
+
101
+
95
102
I2C::I2C (const int ID, const int Mode1)
96
103
{
97
104
switch (Mode1)
@@ -102,6 +109,9 @@ I2C::I2C(const int ID, const int Mode1)
102
109
case 2 :
103
110
device = " HTU21D" ;
104
111
break ;
112
+ case 3 :
113
+ device = " TSL2561" ;
114
+ break ;
105
115
}
106
116
107
117
m_stoprequested = false ;
@@ -119,9 +129,6 @@ I2C::~I2C()
119
129
120
130
bool I2C::StartHardware ()
121
131
{
122
- #ifndef __arm__
123
- return false ;
124
- #endif
125
132
m_stoprequested = false ;
126
133
if (device == " BMP085" )
127
134
{
@@ -158,6 +165,12 @@ void I2C::Do_Work()
158
165
int msec_counter = 0 ;
159
166
int sec_counter = I2C_READ_INTERVAL - 5 ;
160
167
_log.Log (LOG_STATUS, " %s: Worker started..." , device.c_str ());
168
+
169
+ if (device == " TSL2561" )
170
+ {
171
+ TSL2561_Init ();
172
+ }
173
+
161
174
while (!m_stoprequested)
162
175
{
163
176
sleep_milliseconds (500 );
@@ -183,6 +196,10 @@ void I2C::Do_Work()
183
196
{
184
197
HTU21D_ReadSensorDetails ();
185
198
}
199
+ else if (device == " TSL2561" )
200
+ {
201
+ TSL2561_ReadSensorDetails ();
202
+ }
186
203
}
187
204
catch (...)
188
205
{
@@ -228,7 +245,7 @@ int I2C::i2c_Open(const char *I2CBusName)
228
245
#endif
229
246
}
230
247
231
- // BMP085 & BMP180 Specific code
248
+ // BMP085, BMP180, HTU and TSL common code
232
249
233
250
int I2C::ReadInt (int fd, uint8_t *devValues, uint8_t startReg, uint8_t bytesToRead)
234
251
{
@@ -244,6 +261,10 @@ int I2C::ReadInt(int fd, uint8_t *devValues, uint8_t startReg, uint8_t bytesToRe
244
261
struct i2c_msg htu_read_reg[1 ] = {
245
262
{ HTU21D_ADDRESS, I2C_M_RD, bytesToRead, devValues }
246
263
};
264
+ struct i2c_msg tsl_read_reg[2 ] = {
265
+ { TSL2561_ADDRESS, 0 , 1 , &startReg },
266
+ { TSL2561_ADDRESS, I2C_M_RD, bytesToRead, devValues }
267
+ };
247
268
248
269
// Build a register read command
249
270
// Requires a one complete message containing a command
@@ -258,6 +279,11 @@ int I2C::ReadInt(int fd, uint8_t *devValues, uint8_t startReg, uint8_t bytesToRe
258
279
messagebuffer.nmsgs = 1 ;
259
280
messagebuffer.msgs = htu_read_reg; // load the 'read__reg' message into the buffer
260
281
}
282
+ else if (device == " TSL2561" )
283
+ {
284
+ messagebuffer.nmsgs = 2 ;
285
+ messagebuffer.msgs = tsl_read_reg; // load the 'read__reg' message into the buffer
286
+ }
261
287
262
288
rc = ioctl (fd, I2C_RDWR, &messagebuffer); // Send the buffer to the bus and returns a send status
263
289
if (rc < 0 ) {
@@ -282,6 +308,9 @@ int I2C::WriteCmd(int fd, uint8_t devAction)
282
308
struct i2c_msg htu_write_reg[1 ] = {
283
309
{ HTU21D_ADDRESS, 0 , 1 , datatosend }
284
310
};
311
+ struct i2c_msg tsl_write_reg[1 ] = {
312
+ { TSL2561_ADDRESS, 0 , 1 , datatosend }
313
+ };
285
314
286
315
if (device == " BMP085" )
287
316
{
@@ -298,6 +327,13 @@ int I2C::WriteCmd(int fd, uint8_t devAction)
298
327
// Requires one complete message containing a reg address and command
299
328
messagebuffer.msgs = htu_write_reg; // load the 'write__reg' message into the buffer
300
329
}
330
+ else if (device == " TSL2561" )
331
+ {
332
+ datatosend[0 ] = devAction;
333
+ // Build a register write command
334
+ // Requires one complete message containing a reg address and command
335
+ messagebuffer.msgs = tsl_write_reg; // load the 'write__reg' message into the buffer
336
+ }
301
337
302
338
messagebuffer.nmsgs = 1 ; // One message/action
303
339
rc = ioctl (fd, I2C_RDWR, &messagebuffer); // Send the buffer to the bus and returns a send status
@@ -415,6 +451,46 @@ void I2C::HTU21D_ReadSensorDetails()
415
451
SendTempHumSensor (1 , 255 , temperature, round (humidity), " TempHum" );
416
452
}
417
453
454
+ // TSL2561 functions
455
+ void I2C::TSL2561_Init ()
456
+ {
457
+ #ifdef __arm__
458
+ int fd = i2c_Open (m_ActI2CBus.c_str ());
459
+ if (fd < 0 ) {
460
+ _log.Log (LOG_ERROR, " %s: Error opening device!..." , device.c_str ());
461
+ return ;
462
+ }
463
+ if (WriteCmd (fd, TSL2561_INIT) != 0 ) {
464
+ _log.Log (LOG_ERROR, " %s: Error initializing device!..." , device.c_str ());
465
+ }
466
+ close (fd);
467
+ #endif
468
+ }
469
+
470
+ void I2C::TSL2561_ReadSensorDetails ()
471
+ {
472
+ float lux;
473
+ uint8_t rValues[2 ];
474
+
475
+ #ifndef __arm__
476
+ lux = 1984 ;
477
+ #else
478
+ int fd = i2c_Open (m_ActI2CBus.c_str ());
479
+ if (fd < 0 ) {
480
+ _log.Log (LOG_ERROR, " %s: Error opening device!..." , device.c_str ());
481
+ return ;
482
+ }
483
+ if (ReadInt (fd, rValues, TSL2561_Channel0, 2 ) != 0 ) {
484
+ _log.Log (LOG_ERROR, " %s: Error reading lux!..." , device.c_str ());
485
+ close (fd);
486
+ return ;
487
+ }
488
+ close (fd);
489
+ lux = rValues[1 ] * 256.0 + rValues[0 ];
490
+ #endif
491
+ SendLuxSensor (0 , 0 , 255 , lux, " Lux" );
492
+ }
493
+
418
494
// BMP085 functions
419
495
int I2C::bmp_Calibration (int fd)
420
496
{
0 commit comments