Skip to content

Commit 1713933

Browse files
committed
tweaks and some ci stuff
1 parent 0c7a180 commit 1713933

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

adafruit_si1145.py

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616
1717
**Hardware:**
1818
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>`_
2120
2221
**Software and Dependencies:**
2322
2423
* Adafruit CircuitPython firmware for the supported boards:
2524
https://circuitpython.org/downloads
2625
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
3227
"""
3328

3429
import time
@@ -51,56 +46,61 @@
5146

5247
# Commands (for COMMAND register)
5348
SI1145_CMD_PARAM_QUERY = const(0b10000000)
54-
SI1145_CMD_PARAM_SET = const(0b10100000)
49+
SI1145_CMD_PARAM_SET = const(0b10100000)
5550
SI1145_CMD_NOP = const(0b00000000)
5651
SI1145_CMD_RESET = const(0b00000001)
5752
SI1145_CMD_ALS_FORCE = const(0b00000110)
5853

5954
# RAM Parameter Offsets (use with PARAM_QUERY / PARAM_SET)
6055
SI1145_RAM_CHLIST = const(0x01)
6156

62-
class SI1145:
6357

58+
class SI1145:
6459
def __init__(self, i2c, address=SI1145_DEFAULT_ADDRESS):
6560
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:
6863
raise RuntimeError("Failed to find SI1145.")
6964
self.reset()
7065
self._write_register(SI1145_HW_KEY, 0x17)
7166
self._als_enabled = True
72-
self.als_enabled = True
67+
self.ALS_enabled = True
7368

7469
@property
7570
def device_info(self):
71+
"""A three tuple of part, revision, and sequencer ID"""
7672
return tuple(self._read_register(SI1145_PART_ID, 3))
7773

7874
@property
79-
def als_enabled(self):
75+
def ALS_enabled(self):
76+
"""The Ambient Light System enabled state."""
8077
return self._als_enabled
8178

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)
8582
if enable:
86-
current |= 0b00110000
83+
chlist |= 0b00110000
8784
else:
88-
current &= ~0b00110000
89-
self._param_set(SI1145_RAM_CHLIST, current)
85+
chlist &= ~0b00110000
86+
self._param_set(SI1145_RAM_CHLIST, chlist)
9087
self._als_enabled = enable
9188

9289
@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."""
9492
self._send_command(SI1145_CMD_ALS_FORCE)
9593
data = self._read_register(SI1145_ALS_VIS_DATA0, 4)
9694
return struct.unpack("HH", data)
9795

9896
def reset(self):
97+
"""Perform a software reset of the firmware."""
9998
self._send_command(SI1145_CMD_RESET)
100-
time.sleep(0.05) # doubling 25ms datasheet spec
99+
time.sleep(0.05) # doubling 25ms datasheet spec
101100

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)
104104

105105
def _param_query(self, param):
106106
self._send_command(SI1145_CMD_PARAM_QUERY | (param & 0x1F))
@@ -113,8 +113,8 @@ def _param_set(self, param, value):
113113
def _send_command(self, command):
114114
counter = self._read_register(SI1145_RESPONSE) & 0x0F
115115
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
118118
response = self._read_register(SI1145_RESPONSE)
119119
while counter == response & 0x0F:
120120
if response & 0xF0:
@@ -126,12 +126,10 @@ def _read_register(self, register, length=1):
126126
buffer = bytearray(length)
127127
with self._i2c as i2c:
128128
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
130130

131131
def _write_register(self, register, buffer):
132132
if isinstance(buffer, int):
133133
buffer = bytes([buffer])
134134
with self._i2c as i2c:
135135
i2c.write(bytes([register]) + buffer)
136-
137-

0 commit comments

Comments
 (0)