27
27
28
28
* Author(s): Carter Nelson
29
29
"""
30
+ from micropython import const
30
31
from adafruit_bus_device .i2c_device import I2CDevice
31
32
32
- TSL2561_DEFAULT_ADDRESS = const (0x39 )
33
- TSL2561_COMMAND_BIT = const (0x80 )
34
- TSL2561_WORD_BIT = const (0x20 )
33
+ _TSL2561_DEFAULT_ADDRESS = const (0x39 )
34
+ _TSL2561_COMMAND_BIT = const (0x80 )
35
+ _TSL2561_WORD_BIT = const (0x20 )
35
36
36
- TSL2561_CONTROL_POWERON = const (0x03 )
37
- TSL2561_CONTROL_POWEROFF = const (0x00 )
37
+ _TSL2561_CONTROL_POWERON = const (0x03 )
38
+ _TSL2561_CONTROL_POWEROFF = const (0x00 )
38
39
39
- TSL2561_REGISTER_CONTROL = const (0x00 )
40
- TSL2561_REGISTER_TIMING = const (0x01 )
41
- TSL2561_REGISTER_CHAN0_LOW = const (0x0C )
42
- TSL2561_REGISTER_CHAN1_LOW = const (0x0E )
43
- TSL2561_REGISTER_ID = const (0x0A )
40
+ _TSL2561_REGISTER_CONTROL = const (0x00 )
41
+ _TSL2561_REGISTER_TIMING = const (0x01 )
42
+ _TSL2561_REGISTER_CHAN0_LOW = const (0x0C )
43
+ _TSL2561_REGISTER_CHAN1_LOW = const (0x0E )
44
+ _TSL2561_REGISTER_ID = const (0x0A )
44
45
45
- TSL2561_GAIN_SCALE = (16 , 1 )
46
- TSL2561_TIME_SCALE = (1 / 0.034 , 1 / 0.252 , 1 )
47
- TSL2561_CLIP_THRESHOLD = (4900 , 37000 , 65000 )
46
+ _TSL2561_GAIN_SCALE = (16 , 1 )
47
+ _TSL2561_TIME_SCALE = (1 / 0.034 , 1 / 0.252 , 1 )
48
+ _TSL2561_CLIP_THRESHOLD = (4900 , 37000 , 65000 )
48
49
49
50
class TSL2561 ():
50
51
"""Class which provides interface to TSL2561 light sensor."""
51
52
52
- def __init__ (self , i2c , address = TSL2561_DEFAULT_ADDRESS , ** kwargs ):
53
+ def __init__ (self , i2c , address = _TSL2561_DEFAULT_ADDRESS , ** kwargs ):
53
54
self .buf = bytearray (3 )
54
55
self .i2c_device = I2CDevice (i2c , address )
56
+ partno , revno = self .id
57
+ # data sheet says TSL2561 = 0001, reality says 0101
58
+ if not partno == 5 :
59
+ raise RuntimeError ('Failed to find TSL2561! Part 0x%x Rev 0x%x' % (partno , revno ))
55
60
self .enabled = True
56
61
57
62
@property
58
63
def id (self ):
59
64
"""A tuple containing the part number and the revision number."""
60
- id = self ._read_register (TSL2561_REGISTER_ID )
65
+ id = self ._read_register (_TSL2561_REGISTER_ID )
61
66
partno = (id >> 4 ) & 0x0f
62
67
revno = id & 0x0f
63
68
return (partno , revno )
64
69
65
70
@property
66
71
def enabled (self ):
67
72
"""The state of the sensor."""
68
- return (self ._read_register (TSL2561_REGISTER_CONTROL ) & 0x03 ) != 0
73
+ return (self ._read_register (_TSL2561_REGISTER_CONTROL ) & 0x03 ) != 0
69
74
70
75
@enabled .setter
71
76
def enabled (self , enable ):
@@ -76,7 +81,7 @@ def enabled(self, enable):
76
81
self ._disable ()
77
82
78
83
@property
79
- def light (self ):
84
+ def lux (self ):
80
85
"""The computed lux value."""
81
86
return self ._compute_lux ()
82
87
@@ -99,31 +104,31 @@ def luminosity(self):
99
104
@property
100
105
def gain (self ):
101
106
"""The gain. 0:1x, 1:16x."""
102
- return self ._read_register (TSL2561_REGISTER_TIMING ) >> 4 & 0x01
107
+ return self ._read_register (_TSL2561_REGISTER_TIMING ) >> 4 & 0x01
103
108
104
109
@gain .setter
105
110
def gain (self , value ):
106
111
"""Set the gain. 0:1x, 1:16x."""
107
112
value &= 0x01
108
113
value <<= 4
109
- current = self ._read_register (TSL2561_REGISTER_TIMING )
110
- self .buf [0 ] = TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING
114
+ current = self ._read_register (_TSL2561_REGISTER_TIMING )
115
+ self .buf [0 ] = _TSL2561_COMMAND_BIT | _TSL2561_REGISTER_TIMING
111
116
self .buf [1 ] = (current & 0xef ) | value
112
117
with self .i2c_device as i2c :
113
118
i2c .write (self .buf , end = 2 )
114
119
115
120
@property
116
121
def integration_time (self ):
117
122
"""The integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual"""
118
- current = self ._read_register (TSL2561_REGISTER_TIMING )
123
+ current = self ._read_register (_TSL2561_REGISTER_TIMING )
119
124
return current & 0x03
120
125
121
126
@integration_time .setter
122
127
def integration_time (self , value ):
123
128
"""Set the integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual."""
124
129
value &= 0x03
125
- current = self ._read_register (TSL2561_REGISTER_TIMING )
126
- self .buf [0 ] = TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING
130
+ current = self ._read_register (_TSL2561_REGISTER_TIMING )
131
+ self .buf [0 ] = _TSL2561_COMMAND_BIT | _TSL2561_REGISTER_TIMING
127
132
self .buf [1 ] = (current & 0xfc ) | value
128
133
with self .i2c_device as i2c :
129
134
i2c .write (self .buf , end = 2 )
@@ -132,8 +137,8 @@ def _compute_lux(self):
132
137
"""Based on datasheet for FN package."""
133
138
ch0 , ch1 = self .luminosity
134
139
if ch0 == 0 : return None
135
- if ch0 > TSL2561_CLIP_THRESHOLD [self .integration_time ]: return None
136
- if ch1 > TSL2561_CLIP_THRESHOLD [self .integration_time ]: return None
140
+ if ch0 > _TSL2561_CLIP_THRESHOLD [self .integration_time ]: return None
141
+ if ch1 > _TSL2561_CLIP_THRESHOLD [self .integration_time ]: return None
137
142
ratio = ch1 / ch0
138
143
if ratio > 0 and ratio <= 0.50 :
139
144
lux = 0.0304 * ch0 - 0.062 * ch0 * ratio ** 1.4
@@ -149,21 +154,21 @@ def _compute_lux(self):
149
154
# is based on 16x gain and 402ms integration time. Need to scale
150
155
# result for other settings.
151
156
# Scale for gain.
152
- lux *= TSL2561_GAIN_SCALE [self .gain ]
157
+ lux *= _TSL2561_GAIN_SCALE [self .gain ]
153
158
# Scale for integration time.
154
- lux *= TSL2561_TIME_SCALE [self .integration_time ]
159
+ lux *= _TSL2561_TIME_SCALE [self .integration_time ]
155
160
return lux
156
161
157
162
def _enable (self ):
158
- self ._write_control_register (TSL2561_CONTROL_POWERON )
163
+ self ._write_control_register (_TSL2561_CONTROL_POWERON )
159
164
160
165
def _disable (self ):
161
- self ._write_control_register (TSL2561_CONTROL_POWEROFF )
166
+ self ._write_control_register (_TSL2561_CONTROL_POWEROFF )
162
167
163
168
def _read_register (self , reg , count = 1 ):
164
- self .buf [0 ] = TSL2561_COMMAND_BIT | reg
169
+ self .buf [0 ] = _TSL2561_COMMAND_BIT | reg
165
170
if count == 2 :
166
- self .buf [0 ] |= TSL2561_WORD_BIT
171
+ self .buf [0 ] |= _TSL2561_WORD_BIT
167
172
with self .i2c_device as i2c :
168
173
i2c .write (self .buf , end = 1 , stop = False )
169
174
i2c .read_into (self .buf , start = 1 )
@@ -173,15 +178,15 @@ def _read_register(self, reg, count=1):
173
178
return (self .buf [1 ], self .buf [2 ])
174
179
175
180
def _write_control_register (self , reg ):
176
- self .buf [0 ] = TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL
181
+ self .buf [0 ] = _TSL2561_COMMAND_BIT | _TSL2561_REGISTER_CONTROL
177
182
self .buf [1 ] = reg
178
183
with self .i2c_device as i2c :
179
184
i2c .write (self .buf , end = 2 )
180
185
181
186
def _read_broadband (self ):
182
- low , high = self ._read_register (TSL2561_REGISTER_CHAN0_LOW , 2 )
187
+ low , high = self ._read_register (_TSL2561_REGISTER_CHAN0_LOW , 2 )
183
188
return high << 8 | low
184
189
185
190
def _read_infrared (self ):
186
- low , high = self ._read_register (TSL2561_REGISTER_CHAN1_LOW , 2 )
191
+ low , high = self ._read_register (_TSL2561_REGISTER_CHAN1_LOW , 2 )
187
192
return high << 8 | low
0 commit comments