-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
tsl2561_driver.go
510 lines (437 loc) · 13.7 KB
/
tsl2561_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package i2c
import (
"fmt"
"time"
"gobot.io/x/gobot"
)
const (
// TSL2561AddressLow - the address of the device when address pin is low
TSL2561AddressLow = 0x29
// TSL2561AddressFloat - the address of the device when address pin is floating
TSL2561AddressFloat = 0x39
// TSL2561AddressHigh - the address of the device when address pin is high
TSL2561AddressHigh = 0x49
tsl2561CommandBit = 0x80 // Must be 1
tsl2561ClearBit = 0x40 // Clears any pending interrupt (write 1 to clear)
tsl2561WordBit = 0x20 // 1 = read/write word (rather than byte)
tsl2561BlockBit = 0x10 // 1 = using block read/write
tsl2561ControlPowerOn = 0x03
tsl2561ControlPowerOff = 0x00
tsl2561LuxLuxScale = 14 // Scale by 2^14
tsl2561LuxRatioScale = 9 // Scale ratio by 2^9
tsl2561LuxChScale = 10 // Scale channel values by 2^10
tsl2561LuxCHScaleTInt0 = 0x7517 // 322/11 * 2^tsl2561LUXCHSCALE
tsl2561LuxChScaleTInt1 = 0x0FE7 // 322/81 * 2^tsl2561LUXCHSCALE
// T, FN and CL package values
tsl2561LuxK1T = 0x0040 // 0.125 * 2^RATIO_SCALE
tsl2561LuxB1T = 0x01f2 // 0.0304 * 2^LUX_SCALE
tsl2561LuxM1T = 0x01be // 0.0272 * 2^LUX_SCALE
tsl2561LuxK2T = 0x0080 // 0.250 * 2^RATIO_SCALE
tsl2561LuxB2T = 0x0214 // 0.0325 * 2^LUX_SCALE
tsl2561LuxM2T = 0x02d1 // 0.0440 * 2^LUX_SCALE
tsl2561LuxK3T = 0x00c0 // 0.375 * 2^RATIO_SCALE
tsl2561LuxB3T = 0x023f // 0.0351 * 2^LUX_SCALE
tsl2561LuxM3T = 0x037b // 0.0544 * 2^LUX_SCALE
tsl2561LuxK4T = 0x0100 // 0.50 * 2^RATIO_SCALE
tsl2561LuxB4T = 0x0270 // 0.0381 * 2^LUX_SCALE
tsl2561LuxM4T = 0x03fe // 0.0624 * 2^LUX_SCALE
tsl2561LuxK5T = 0x0138 // 0.61 * 2^RATIO_SCALE
tsl2561LuxB5T = 0x016f // 0.0224 * 2^LUX_SCALE
tsl2561LuxM5T = 0x01fc // 0.0310 * 2^LUX_SCALE
tsl2561LuxK6T = 0x019a // 0.80 * 2^RATIO_SCALE
tsl2561LuxB6T = 0x00d2 // 0.0128 * 2^LUX_SCALE
tsl2561LuxM6T = 0x00fb // 0.0153 * 2^LUX_SCALE
tsl2561LuxK7T = 0x029a // 1.3 * 2^RATIO_SCALE
tsl2561LuxB7T = 0x0018 // 0.00146 * 2^LUX_SCALE
tsl2561LuxM7T = 0x0012 // 0.00112 * 2^LUX_SCALE
tsl2561LuxK8T = 0x029a // 1.3 * 2^RATIO_SCALE
tsl2561LuxB8T = 0x0000 // 0.000 * 2^LUX_SCALE
tsl2561LuxM8T = 0x0000 // 0.000 * 2^LUX_SCALE
// Auto-gain thresholds
tsl2561AgcTHi13MS = 4850 // Max value at Ti 13ms = 5047
tsl2561AgcTLo13MS = 100
tsl2561AgcTHi101MS = 36000 // Max value at Ti 101ms = 37177
tsl2561AgcTLo101MS = 200
tsl2561AgcTHi402MS = 63000 // Max value at Ti 402ms = 65535
tsl2561AgcTLo402MS = 500
// Clipping thresholds
tsl2561Clipping13MS = 4900
tsl2561Clipping101MS = 37000
tsl2561Clipping402MS = 65000
)
const (
tsl2561RegisterControl = 0x00
tsl2561RegisterTiming = 0x01
tsl2561RegisterThreshholdLLow = 0x02
tsl2561RegisterThreshholdLHigh = 0x03
tsl2561RegisterThreshholdHLow = 0x04
tsl2561RegisterThreshholdHHigh = 0x05
tsl2561RegisterInterrupt = 0x06
tsl2561RegisterCRC = 0x08
tsl2561RegisterID = 0x0A
tsl2561RegisterChan0Low = 0x0C
tsl2561RegisterChan0High = 0x0D
tsl2561RegisterChan1Low = 0x0E
tsl2561RegisterChan1High = 0x0F
)
// TSL2561IntegrationTime is the type of all valid integration time settings
type TSL2561IntegrationTime int
const (
// TSL2561IntegrationTime13MS integration time 13ms
TSL2561IntegrationTime13MS TSL2561IntegrationTime = iota // 13.7ms
// TSL2561IntegrationTime101MS integration time 101ms
TSL2561IntegrationTime101MS // 101ms
// TSL2561IntegrationTime402MS integration time 402ms
TSL2561IntegrationTime402MS // 402ms
)
// TSL2561Gain is the type of all valid gain settings
type TSL2561Gain int
const (
// TSL2561Gain1X gain == 1x
TSL2561Gain1X TSL2561Gain = 0x00 // No gain
// TSL2561Gain16X gain == 16x
TSL2561Gain16X = 0x10 // 16x gain
)
// TSL2561Driver is the gobot driver for the Adafruit Digital Luminosity/Lux/Light Sensor
//
// Datasheet: http://www.adafruit.com/datasheets/TSL2561.pdf
//
// Ported from the Adafruit driver at https://github.com/adafruit/Adafruit_TSL2561 by
// K. Townsend
type TSL2561Driver struct {
name string
connector Connector
connection Connection
Config
autoGain bool
gain TSL2561Gain
integrationTime TSL2561IntegrationTime
}
// NewTSL2561Driver creates a new driver for the TSL2561 device.
//
// Params:
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
// i2c.WithTSL2561Gain1X: sets the gain to 1X
// i2c.WithTSL2561Gain16X: sets the gain to 16X
// i2c.WithTSL2561AutoGain: turns on auto gain
// i2c.WithTSL2561IntegrationTime13MS: sets integration time to 13ms
// i2c.WithTSL2561IntegrationTime101MS: sets integration time to 101ms
// i2c.WithTSL2561IntegrationTime402MS: sets integration time to 402ms
//
func NewTSL2561Driver(conn Connector, options ...func(Config)) *TSL2561Driver {
driver := &TSL2561Driver{
name: gobot.DefaultName("TSL2561"),
connector: conn,
Config: NewConfig(),
integrationTime: TSL2561IntegrationTime402MS,
gain: TSL2561Gain1X,
autoGain: false,
}
for _, option := range options {
option(driver)
}
return driver
}
// WithTSL2561Gain1X option sets the TSL2561Driver gain to 1X
func WithTSL2561Gain1X(c Config) {
d, ok := c.(*TSL2561Driver)
if ok {
d.gain = TSL2561Gain1X
return
}
// TODO: return errors.New("Trying to set Gain for non-TSL2561Driver")
}
// WithTSL2561Gain16X option sets the TSL2561Driver gain to 16X
func WithTSL2561Gain16X(c Config) {
d, ok := c.(*TSL2561Driver)
if ok {
d.gain = TSL2561Gain16X
return
}
// TODO: return errors.New("Trying to set Gain for non-TSL2561Driver")
}
// WithTSL2561AutoGain option turns on TSL2561Driver auto gain
func WithTSL2561AutoGain(c Config) {
d, ok := c.(*TSL2561Driver)
if ok {
d.autoGain = true
return
}
// TODO: return errors.New("Trying to set Auto Gain for non-TSL2561Driver")
}
func withTSL2561IntegrationTime(iTime TSL2561IntegrationTime) func(Config) {
return func(c Config) {
d, ok := c.(*TSL2561Driver)
if ok {
d.integrationTime = iTime
return
}
// TODO: return errors.New("Trying to set integration time for non-TSL2561Driver")
}
}
// WithTSL2561IntegrationTime13MS option sets the TSL2561Driver integration time
// to 13ms
func WithTSL2561IntegrationTime13MS(c Config) {
withTSL2561IntegrationTime(TSL2561IntegrationTime13MS)(c)
}
// WithTSL2561IntegrationTime101MS option sets the TSL2561Driver integration time
// to 101ms
func WithTSL2561IntegrationTime101MS(c Config) {
withTSL2561IntegrationTime(TSL2561IntegrationTime101MS)(c)
}
// WithTSL2561IntegrationTime402MS option sets the TSL2561Driver integration time
// to 402ms
func WithTSL2561IntegrationTime402MS(c Config) {
withTSL2561IntegrationTime(TSL2561IntegrationTime402MS)(c)
}
// Name returns the name of the device.
func (d *TSL2561Driver) Name() string {
return d.name
}
// SetName sets the name of the device.
func (d *TSL2561Driver) SetName(name string) {
d.name = name
}
// Connection returns the connection of the device.
func (d *TSL2561Driver) Connection() gobot.Connection {
return d.connector.(gobot.Connection)
}
// Start initializes the device.
func (d *TSL2561Driver) Start() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
address := d.GetAddressOrDefault(TSL2561AddressFloat)
if d.connection, err = d.connector.GetConnection(address, bus); err != nil {
return err
}
if err = d.enable(); err != nil {
return err
}
var initialized byte
if initialized, err = d.connection.ReadByteData(tsl2561RegisterID); err != nil {
return err
} else if (initialized & 0x0A) == 0 {
return fmt.Errorf("TSL2561 device not found (0x%X)", initialized)
}
if err = d.SetIntegrationTime(d.integrationTime); err != nil {
return err
}
if err = d.SetGain(d.gain); err != nil {
return err
}
if err = d.disable(); err != nil {
return err
}
return nil
}
// Halt stops the device
func (d *TSL2561Driver) Halt() error {
return nil
}
// SetIntegrationTime sets integrations time for the TSL2561
func (d *TSL2561Driver) SetIntegrationTime(time TSL2561IntegrationTime) error {
if err := d.enable(); err != nil {
return err
}
timeGainVal := uint8(time) | uint8(d.gain)
if err := d.connection.WriteByteData(tsl2561CommandBit|tsl2561RegisterTiming, timeGainVal); err != nil {
return err
}
d.integrationTime = time
return d.disable()
}
// SetGain adjusts the TSL2561 gain (sensitivity to light)
func (d *TSL2561Driver) SetGain(gain TSL2561Gain) error {
if err := d.enable(); err != nil {
return err
}
timeGainVal := uint8(d.integrationTime) | uint8(gain)
if err := d.connection.WriteByteData(tsl2561CommandBit|tsl2561RegisterTiming, timeGainVal); err != nil {
return err
}
d.gain = gain
return d.disable()
}
// GetLuminocity gets the broadband and IR only values from the TSL2561,
// adjusting gain if auto-gain is enabled
func (d *TSL2561Driver) GetLuminocity() (broadband uint16, ir uint16, err error) {
// if auto gain disabled get a single reading and continue
if !d.autoGain {
broadband, ir, err = d.getData()
return
}
agcCheck := false
hi, lo := d.getHiLo()
// Read data until we find a valid range
valid := false
for {
broadband, ir, err = d.getData()
if err != nil {
return
}
// Run an auto-gain check if we haven't already done so
if !agcCheck {
if (broadband < lo) && (d.gain == TSL2561Gain1X) {
// increase gain and try again
err = d.SetGain(TSL2561Gain16X)
if err != nil {
return
}
agcCheck = true
} else if (broadband > hi) && (d.gain == TSL2561Gain16X) {
// drop gain and try again
err = d.SetGain(TSL2561Gain1X)
if err != nil {
return
}
agcCheck = true
} else {
// Reading is either valid, or we're already at the chips
// limits
valid = true
}
} else {
// If we've already adjusted the gain once, just return the new results.
// This avoids endless loops where a value is at one extreme pre-gain,
// and the the other extreme post-gain
valid = true
}
if valid {
break
}
}
return
}
// CalculateLux converts raw sensor values to the standard SI Lux equivalent.
// Returns 65536 if the sensor is saturated.
func (d *TSL2561Driver) CalculateLux(broadband uint16, ir uint16) (lux uint32) {
var channel1 uint32
var channel0 uint32
// Set cliplevel and scaling based on integration time
clipThreshold, chScale := d.getClipScaling()
// Saturated sensor
if (broadband > clipThreshold) || (ir > clipThreshold) {
return 65536
}
// Adjust scale for gain
if d.gain == TSL2561Gain1X {
chScale = chScale * 16
}
channel0 = (uint32(broadband) * chScale) >> tsl2561LuxChScale
channel1 = (uint32(ir) * chScale) >> tsl2561LuxChScale
// Find the ratio of the channel values (channel1 / channel0)
var ratio1 uint32
if channel0 != 0 {
ratio1 = (channel1 << (tsl2561LuxRatioScale + 1)) / channel0
}
// Round the ratio value
ratio := (ratio1 + 1) / 2
b, m := d.getBM(ratio)
temp := (channel0 * b) - (channel1 * m)
// Negative lux not allowed
if temp < 0 {
temp = 0
}
// Round lsb (2^(LUX_SCALE+1))
temp += (1 << (tsl2561LuxLuxScale - 1))
// Strip off fractional portion
lux = temp >> tsl2561LuxLuxScale
return lux
}
func (d *TSL2561Driver) enable() (err error) {
err = d.connection.WriteByteData(uint8(tsl2561CommandBit|tsl2561RegisterControl), tsl2561ControlPowerOn)
return err
}
func (d *TSL2561Driver) disable() (err error) {
err = d.connection.WriteByteData(uint8(tsl2561CommandBit|tsl2561RegisterControl), tsl2561ControlPowerOff)
return err
}
func (d *TSL2561Driver) getData() (broadband uint16, ir uint16, err error) {
if err = d.enable(); err != nil {
return
}
d.waitForADC()
// Reads a two byte value from channel 0 (visible + infrared)
broadband, err = d.connection.ReadWordData(tsl2561CommandBit | tsl2561WordBit | tsl2561RegisterChan0Low)
if err != nil {
return
}
// Reads a two byte value from channel 1 (infrared)
ir, err = d.connection.ReadWordData(tsl2561CommandBit | tsl2561WordBit | tsl2561RegisterChan1Low)
if err != nil {
return
}
err = d.disable()
return
}
func (d *TSL2561Driver) getHiLo() (hi, lo uint16) {
switch d.integrationTime {
case TSL2561IntegrationTime13MS:
hi = tsl2561AgcTHi13MS
lo = tsl2561AgcTLo13MS
case TSL2561IntegrationTime101MS:
hi = tsl2561AgcTHi101MS
lo = tsl2561AgcTLo101MS
case TSL2561IntegrationTime402MS:
hi = tsl2561AgcTHi402MS
lo = tsl2561AgcTLo402MS
}
return
}
func (d *TSL2561Driver) getClipScaling() (clipThreshold uint16, chScale uint32) {
switch d.integrationTime {
case TSL2561IntegrationTime13MS:
clipThreshold = tsl2561Clipping13MS
chScale = tsl2561LuxCHScaleTInt0
case TSL2561IntegrationTime101MS:
clipThreshold = tsl2561Clipping101MS
chScale = tsl2561LuxChScaleTInt1
case TSL2561IntegrationTime402MS:
clipThreshold = tsl2561Clipping402MS
chScale = (1 << tsl2561LuxChScale)
}
return
}
func (d *TSL2561Driver) getBM(ratio uint32) (b uint32, m uint32) {
switch {
case (ratio >= 0) && (ratio <= tsl2561LuxK1T):
b = tsl2561LuxB1T
m = tsl2561LuxM1T
case (ratio <= tsl2561LuxK2T):
b = tsl2561LuxB2T
m = tsl2561LuxM2T
case (ratio <= tsl2561LuxK3T):
b = tsl2561LuxB3T
m = tsl2561LuxM3T
case (ratio <= tsl2561LuxK4T):
b = tsl2561LuxB4T
m = tsl2561LuxM4T
case (ratio <= tsl2561LuxK5T):
b = tsl2561LuxB5T
m = tsl2561LuxM5T
case (ratio <= tsl2561LuxK6T):
b = tsl2561LuxB6T
m = tsl2561LuxM6T
case (ratio <= tsl2561LuxK7T):
b = tsl2561LuxB7T
m = tsl2561LuxM7T
case (ratio > tsl2561LuxK8T): // TODO: there is a gap here...
b = tsl2561LuxB8T
m = tsl2561LuxM8T
}
return
}
func (d *TSL2561Driver) waitForADC() {
switch d.integrationTime {
case TSL2561IntegrationTime13MS:
time.Sleep(15 * time.Millisecond)
case TSL2561IntegrationTime101MS:
time.Sleep(120 * time.Millisecond)
case TSL2561IntegrationTime402MS:
time.Sleep(450 * time.Millisecond)
}
return
}