58
58
59
59
# Register and command constants:
60
60
# pylint: disable=bad-whitespace
61
- _COMMAND_BIT = const (0x80 )
62
- _REGISTER_ENABLE = const (0x00 )
63
- _REGISTER_ATIME = const (0x01 )
64
- _REGISTER_AILT = const (0x04 )
65
- _REGISTER_AIHT = const (0x06 )
66
- _REGISTER_ID = const (0x12 )
67
- _REGISTER_APERS = const (0x0c )
68
- _REGISTER_CONTROL = const (0x0f )
61
+ _COMMAND_BIT = const (0x80 )
62
+ _REGISTER_ENABLE = const (0x00 )
63
+ _REGISTER_ATIME = const (0x01 )
64
+ _REGISTER_AILT = const (0x04 )
65
+ _REGISTER_AIHT = const (0x06 )
66
+ _REGISTER_ID = const (0x12 )
67
+ _REGISTER_APERS = const (0x0C )
68
+ _REGISTER_CONTROL = const (0x0F )
69
69
_REGISTER_SENSORID = const (0x12 )
70
- _REGISTER_STATUS = const (0x13 )
71
- _REGISTER_CDATA = const (0x14 )
72
- _REGISTER_RDATA = const (0x16 )
73
- _REGISTER_GDATA = const (0x18 )
74
- _REGISTER_BDATA = const (0x1a )
75
- _ENABLE_AIEN = const (0x10 )
76
- _ENABLE_WEN = const (0x08 )
77
- _ENABLE_AEN = const (0x02 )
78
- _ENABLE_PON = const (0x01 )
79
- _GAINS = (1 , 4 , 16 , 60 )
70
+ _REGISTER_STATUS = const (0x13 )
71
+ _REGISTER_CDATA = const (0x14 )
72
+ _REGISTER_RDATA = const (0x16 )
73
+ _REGISTER_GDATA = const (0x18 )
74
+ _REGISTER_BDATA = const (0x1A )
75
+ _ENABLE_AIEN = const (0x10 )
76
+ _ENABLE_WEN = const (0x08 )
77
+ _ENABLE_AEN = const (0x02 )
78
+ _ENABLE_PON = const (0x01 )
79
+ _GAINS = (1 , 4 , 16 , 60 )
80
80
_CYCLES = (0 , 1 , 2 , 3 , 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 , 50 , 55 , 60 )
81
81
_INTEGRATION_TIME_THRESHOLD_LOW = 2.4
82
82
_INTEGRATION_TIME_THRESHOLD_HIGH = 614.4
@@ -100,7 +100,7 @@ def __init__(self, i2c, address=0x29):
100
100
# Check sensor ID is expectd value.
101
101
sensor_id = self ._read_u8 (_REGISTER_SENSORID )
102
102
if sensor_id not in (0x44 , 0x10 ):
103
- raise RuntimeError (' Could not find sensor, check wiring!' )
103
+ raise RuntimeError (" Could not find sensor, check wiring!" )
104
104
105
105
@property
106
106
def lux (self ):
@@ -122,9 +122,9 @@ def color_rgb_bytes(self):
122
122
if clear == 0 :
123
123
return (0 , 0 , 0 )
124
124
# pylint: disable=bad-whitespace
125
- red = int (pow ((int ((r / clear ) * 256 ) / 255 ), 2.5 ) * 255 )
126
- green = int (pow ((int ((g / clear ) * 256 ) / 255 ), 2.5 ) * 255 )
127
- blue = int (pow ((int ((b / clear ) * 256 ) / 255 ), 2.5 ) * 255 )
125
+ red = int (pow ((int ((r / clear ) * 256 ) / 255 ), 2.5 ) * 255 )
126
+ green = int (pow ((int ((g / clear ) * 256 ) / 255 ), 2.5 ) * 255 )
127
+ blue = int (pow ((int ((b / clear ) * 256 ) / 255 ), 2.5 ) * 255 )
128
128
# Handle possible 8-bit overflow
129
129
if red > 255 :
130
130
red = 255
@@ -163,8 +163,7 @@ def active(self, val):
163
163
time .sleep (0.003 )
164
164
self ._write_u8 (_REGISTER_ENABLE , enable | _ENABLE_PON | _ENABLE_AEN )
165
165
else :
166
- self ._write_u8 (_REGISTER_ENABLE ,
167
- enable & ~ (_ENABLE_PON | _ENABLE_AEN ))
166
+ self ._write_u8 (_REGISTER_ENABLE , enable & ~ (_ENABLE_PON | _ENABLE_AEN ))
168
167
169
168
@property
170
169
def integration_time (self ):
@@ -173,12 +172,21 @@ def integration_time(self):
173
172
174
173
@integration_time .setter
175
174
def integration_time (self , val ):
176
- if not _INTEGRATION_TIME_THRESHOLD_LOW <= val <= _INTEGRATION_TIME_THRESHOLD_HIGH :
177
- raise ValueError ("Integration Time must be between '{0}' and '{1}'" .format (
178
- _INTEGRATION_TIME_THRESHOLD_LOW , _INTEGRATION_TIME_THRESHOLD_HIGH ))
175
+ if (
176
+ not _INTEGRATION_TIME_THRESHOLD_LOW
177
+ <= val
178
+ <= _INTEGRATION_TIME_THRESHOLD_HIGH
179
+ ):
180
+ raise ValueError (
181
+ "Integration Time must be between '{0}' and '{1}'" .format (
182
+ _INTEGRATION_TIME_THRESHOLD_LOW , _INTEGRATION_TIME_THRESHOLD_HIGH
183
+ )
184
+ )
179
185
cycles = int (val / 2.4 )
180
- self ._integration_time = cycles * 2.4 # pylint: disable=attribute-defined-outside-init
181
- self ._write_u8 (_REGISTER_ATIME , 256 - cycles )
186
+ self ._integration_time = (
187
+ cycles * 2.4
188
+ ) # pylint: disable=attribute-defined-outside-init
189
+ self ._write_u8 (_REGISTER_ATIME , 256 - cycles )
182
190
183
191
@property
184
192
def gain (self ):
@@ -190,7 +198,9 @@ def gain(self):
190
198
@gain .setter
191
199
def gain (self , val ):
192
200
if val not in _GAINS :
193
- raise ValueError ("Gain should be one of the following values: {0}" .format (_GAINS ))
201
+ raise ValueError (
202
+ "Gain should be one of the following values: {0}" .format (_GAINS )
203
+ )
194
204
self ._write_u8 (_REGISTER_CONTROL , _GAINS .index (val ))
195
205
196
206
@property
@@ -203,9 +213,11 @@ def interrupt(self):
203
213
@interrupt .setter
204
214
def interrupt (self , val ):
205
215
if val :
206
- raise ValueError ("Interrupt should be set to False in order to clear the interrupt" )
216
+ raise ValueError (
217
+ "Interrupt should be set to False in order to clear the interrupt"
218
+ )
207
219
with self ._device :
208
- self ._device .write (b' \xe6 ' )
220
+ self ._device .write (b" \xe6 " )
209
221
210
222
@property
211
223
def color_raw (self ):
@@ -215,21 +227,24 @@ def color_raw(self):
215
227
was_active = self .active
216
228
self .active = True
217
229
while not self ._valid ():
218
- time .sleep ((self ._integration_time + 0.9 )/ 1000.0 )
219
- data = tuple (self ._read_u16 (reg ) for reg in (
220
- _REGISTER_RDATA ,
221
- _REGISTER_GDATA ,
222
- _REGISTER_BDATA ,
223
- _REGISTER_CDATA ,
224
- ))
230
+ time .sleep ((self ._integration_time + 0.9 ) / 1000.0 )
231
+ data = tuple (
232
+ self ._read_u16 (reg )
233
+ for reg in (
234
+ _REGISTER_RDATA ,
235
+ _REGISTER_GDATA ,
236
+ _REGISTER_BDATA ,
237
+ _REGISTER_CDATA ,
238
+ )
239
+ )
225
240
self .active = was_active
226
241
return data
227
242
228
243
@property
229
244
def cycles (self ):
230
245
"""The persistence cycles of the sensor."""
231
246
if self ._read_u8 (_REGISTER_ENABLE ) & _ENABLE_AIEN :
232
- return _CYCLES [self ._read_u8 (_REGISTER_APERS ) & 0x0f ]
247
+ return _CYCLES [self ._read_u8 (_REGISTER_APERS ) & 0x0F ]
233
248
return - 1
234
249
235
250
@cycles .setter
@@ -239,7 +254,9 @@ def cycles(self, val):
239
254
self ._write_u8 (_REGISTER_ENABLE , enable & ~ (_ENABLE_AIEN ))
240
255
else :
241
256
if val not in _CYCLES :
242
- raise ValueError ("Only the following cycles are permitted: {0}" .format (_CYCLES ))
257
+ raise ValueError (
258
+ "Only the following cycles are permitted: {0}" .format (_CYCLES )
259
+ )
243
260
self ._write_u8 (_REGISTER_ENABLE , enable | _ENABLE_AIEN )
244
261
self ._write_u8 (_REGISTER_APERS , _CYCLES .index (val ))
245
262
@@ -280,13 +297,13 @@ def _temperature_and_lux_dn40(self):
280
297
R , G , B , C = self .color_raw
281
298
282
299
# Device specific values (DN40 Table 1 in Appendix I)
283
- GA = self .glass_attenuation # Glass Attenuation Factor
284
- DF = 310.0 # Device Factor
285
- R_Coef = 0.136 # |
286
- G_Coef = 1.0 # | used in lux computation
287
- B_Coef = - 0.444 # |
288
- CT_Coef = 3810 # Color Temperature Coefficient
289
- CT_Offset = 1391 # Color Temperatuer Offset
300
+ GA = self .glass_attenuation # Glass Attenuation Factor
301
+ DF = 310.0 # Device Factor
302
+ R_Coef = 0.136 # |
303
+ G_Coef = 1.0 # | used in lux computation
304
+ B_Coef = - 0.444 # |
305
+ CT_Coef = 3810 # Color Temperature Coefficient
306
+ CT_Offset = 1391 # Color Temperatuer Offset
290
307
291
308
# Analog/Digital saturation (DN40 3.5)
292
309
SATURATION = 65535 if 256 - ATIME > 63 else 1024 * (256 - ATIME )
@@ -300,7 +317,7 @@ def _temperature_and_lux_dn40(self):
300
317
return None , None
301
318
302
319
# IR Rejection (DN40 3.1)
303
- IR = (R + G + B - C ) / 2 if R + G + B > C else 0.
320
+ IR = (R + G + B - C ) / 2 if R + G + B > C else 0.0
304
321
R2 = R - IR
305
322
G2 = G - IR
306
323
B2 = B - IR
@@ -342,16 +359,14 @@ def _read_u8(self, address):
342
359
# Read an 8-bit unsigned value from the specified 8-bit address.
343
360
with self ._device as i2c :
344
361
self ._BUFFER [0 ] = (address | _COMMAND_BIT ) & 0xFF
345
- i2c .write_then_readinto (self ._BUFFER , self ._BUFFER ,
346
- out_end = 1 , in_end = 1 )
362
+ i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 1 )
347
363
return self ._BUFFER [0 ]
348
364
349
365
def _read_u16 (self , address ):
350
366
# Read a 16-bit unsigned value from the specified 8-bit address.
351
367
with self ._device as i2c :
352
368
self ._BUFFER [0 ] = (address | _COMMAND_BIT ) & 0xFF
353
- i2c .write_then_readinto (self ._BUFFER , self ._BUFFER ,
354
- out_end = 1 , in_end = 2 )
369
+ i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 2 )
355
370
return (self ._BUFFER [1 ] << 8 ) | self ._BUFFER [0 ]
356
371
357
372
def _write_u8 (self , address , val ):
0 commit comments