16
16
17
17
**Hardware:**
18
18
19
- .. todo:: Add links to any specific hardware product page(s), or category page(s).
20
- Use unordered list & hyperlink rST inline format: "* `Link Text <url>`_"
19
+ * `Adafruit SI1145 Digital UV Index / IR / Visible Light Sensor <https://www.adafruit.com/product/1777>`_
21
20
22
21
**Software and Dependencies:**
23
22
24
23
* Adafruit CircuitPython firmware for the supported boards:
25
24
https://circuitpython.org/downloads
26
25
27
- .. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies
28
- based on the library's use of either.
29
-
30
- # * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
31
- # * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
26
+ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
32
27
"""
33
28
34
29
import time
51
46
52
47
# Commands (for COMMAND register)
53
48
SI1145_CMD_PARAM_QUERY = const (0b10000000 )
54
- SI1145_CMD_PARAM_SET = const (0b10100000 )
49
+ SI1145_CMD_PARAM_SET = const (0b10100000 )
55
50
SI1145_CMD_NOP = const (0b00000000 )
56
51
SI1145_CMD_RESET = const (0b00000001 )
57
52
SI1145_CMD_ALS_FORCE = const (0b00000110 )
58
53
59
54
# RAM Parameter Offsets (use with PARAM_QUERY / PARAM_SET)
60
55
SI1145_RAM_CHLIST = const (0x01 )
61
56
62
- class SI1145 :
63
57
58
+ class SI1145 :
64
59
def __init__ (self , i2c , address = SI1145_DEFAULT_ADDRESS ):
65
60
self ._i2c = i2c_device .I2CDevice (i2c , address )
66
- id , rev , seq = self .device_info
67
- if id != 69 or rev != 0 or seq != 8 :
61
+ dev_id , dev_rev , dev_seq = self .device_info
62
+ if dev_id != 69 or dev_rev != 0 or dev_seq != 8 :
68
63
raise RuntimeError ("Failed to find SI1145." )
69
64
self .reset ()
70
65
self ._write_register (SI1145_HW_KEY , 0x17 )
71
66
self ._als_enabled = True
72
- self .als_enabled = True
67
+ self .ALS_enabled = True
73
68
74
69
@property
75
70
def device_info (self ):
71
+ """A three tuple of part, revision, and sequencer ID"""
76
72
return tuple (self ._read_register (SI1145_PART_ID , 3 ))
77
73
78
74
@property
79
- def als_enabled (self ):
75
+ def ALS_enabled (self ):
76
+ """The Ambient Light System enabled state."""
80
77
return self ._als_enabled
81
78
82
- @als_enabled .setter
83
- def als_enabled (self , enable ):
84
- current = self ._param_query (SI1145_RAM_CHLIST )
79
+ @ALS_enabled .setter
80
+ def ALS_enabled (self , enable ):
81
+ chlist = self ._param_query (SI1145_RAM_CHLIST )
85
82
if enable :
86
- current |= 0b00110000
83
+ chlist |= 0b00110000
87
84
else :
88
- current &= ~ 0b00110000
89
- self ._param_set (SI1145_RAM_CHLIST , current )
85
+ chlist &= ~ 0b00110000
86
+ self ._param_set (SI1145_RAM_CHLIST , chlist )
90
87
self ._als_enabled = enable
91
88
92
89
@property
93
- def als (self ):
90
+ def ALS (self ):
91
+ """A two tuple of the Ambient Light System (ALS) visible and infrared raw sensor values."""
94
92
self ._send_command (SI1145_CMD_ALS_FORCE )
95
93
data = self ._read_register (SI1145_ALS_VIS_DATA0 , 4 )
96
94
return struct .unpack ("HH" , data )
97
95
98
96
def reset (self ):
97
+ """Perform a software reset of the firmware."""
99
98
self ._send_command (SI1145_CMD_RESET )
100
- time .sleep (0.05 ) # doubling 25ms datasheet spec
99
+ time .sleep (0.05 ) # doubling 25ms datasheet spec
101
100
102
- def nop (self ):
103
- self ._send_command (SI1145_CMD_NOP )
101
+ def clear_error (self ):
102
+ """Clear any existing error code."""
103
+ self ._send_command (SI1145_CMD_NOP )
104
104
105
105
def _param_query (self , param ):
106
106
self ._send_command (SI1145_CMD_PARAM_QUERY | (param & 0x1F ))
@@ -113,8 +113,8 @@ def _param_set(self, param, value):
113
113
def _send_command (self , command ):
114
114
counter = self ._read_register (SI1145_RESPONSE ) & 0x0F
115
115
self ._write_register (SI1145_COMMAND , command )
116
- if command == SI1145_CMD_NOP or command == SI1145_CMD_RESET :
117
- return
116
+ if command in ( SI1145_CMD_NOP , SI1145_CMD_RESET ) :
117
+ return 0
118
118
response = self ._read_register (SI1145_RESPONSE )
119
119
while counter == response & 0x0F :
120
120
if response & 0xF0 :
@@ -126,12 +126,10 @@ def _read_register(self, register, length=1):
126
126
buffer = bytearray (length )
127
127
with self ._i2c as i2c :
128
128
i2c .write_then_readinto (bytes ([register ]), buffer )
129
- return buffer [0 ] if length == 1 else buffer
129
+ return buffer [0 ] if length == 1 else buffer
130
130
131
131
def _write_register (self , register , buffer ):
132
132
if isinstance (buffer , int ):
133
133
buffer = bytes ([buffer ])
134
134
with self ._i2c as i2c :
135
135
i2c .write (bytes ([register ]) + buffer )
136
-
137
-
0 commit comments