Skip to content

Commit 216640c

Browse files
authored
Merge pull request #8 from tekktrik/dev/add-uv-index
Add UV Index, use adafruit_register
2 parents 744d15f + b471723 commit 216640c

File tree

2 files changed

+78
-38
lines changed

2 files changed

+78
-38
lines changed

adafruit_si1145.py

Lines changed: 77 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,51 +27,66 @@
2727
"""
2828

2929
import time
30-
import struct
3130
from micropython import const
3231
from adafruit_bus_device import i2c_device
32+
from adafruit_register.i2c_struct import Struct
3333

3434
__version__ = "0.0.0-auto.0"
3535
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SI1145.git"
3636

3737
# Registers
38-
SI1145_DEFAULT_ADDRESS = const(0x60)
39-
SI1145_PART_ID = const(0x00)
40-
SI1145_HW_KEY = const(0x07)
41-
SI1145_PARAM_WR = const(0x17)
42-
SI1145_COMMAND = const(0x18)
43-
SI1145_RESPONSE = const(0x20)
44-
SI1145_ALS_VIS_DATA0 = const(0x22)
45-
SI1145_PARAM_RD = const(0x2E)
38+
_DEFAULT_ADDRESS = const(0x60)
39+
_PART_ID = const(0x00)
40+
_HW_KEY = const(0x07)
41+
_COEFF_0 = const(0x13)
42+
_COEFF_1 = const(0x14)
43+
_COEFF_2 = const(0x15)
44+
_COEFF_3 = const(0x16)
45+
_PARAM_WR = const(0x17)
46+
_COMMAND = const(0x18)
47+
_RESPONSE = const(0x20)
48+
_ALS_VIS_DATA0 = const(0x22)
49+
_UV_INDEX_DATA0 = const(0x2C)
50+
_PARAM_RD = const(0x2E)
4651

4752
# Commands (for COMMAND register)
48-
SI1145_CMD_PARAM_QUERY = const(0b10000000)
49-
SI1145_CMD_PARAM_SET = const(0b10100000)
50-
SI1145_CMD_NOP = const(0b00000000)
51-
SI1145_CMD_RESET = const(0b00000001)
52-
SI1145_CMD_ALS_FORCE = const(0b00000110)
53+
_CMD_PARAM_QUERY = const(0b10000000)
54+
_CMD_PARAM_SET = const(0b10100000)
55+
_CMD_NOP = const(0b00000000)
56+
_CMD_RESET = const(0b00000001)
57+
_CMD_ALS_FORCE = const(0b00000110)
5358

5459
# RAM Parameter Offsets (use with PARAM_QUERY / PARAM_SET)
55-
SI1145_RAM_CHLIST = const(0x01)
60+
_RAM_CHLIST = const(0x01)
5661

5762

5863
class SI1145:
5964
"""Driver for the SI1145 UV, IR, Visible Light Sensor."""
6065

61-
def __init__(self, i2c, address=SI1145_DEFAULT_ADDRESS):
62-
self._i2c = i2c_device.I2CDevice(i2c, address)
66+
_device_info = Struct(_PART_ID, "<BBB")
67+
_ucoeff_0 = Struct(_COEFF_0, "<B")
68+
_ucoeff_1 = Struct(_COEFF_1, "<B")
69+
_ucoeff_2 = Struct(_COEFF_2, "<B")
70+
_ucoeff_3 = Struct(_COEFF_3, "<B")
71+
_als_data = Struct(_ALS_VIS_DATA0, "<HH")
72+
_aux_data = Struct(_UV_INDEX_DATA0, "<H")
73+
74+
def __init__(self, i2c, address=_DEFAULT_ADDRESS):
75+
self.i2c_device = i2c_device.I2CDevice(i2c, address)
6376
dev_id, dev_rev, dev_seq = self.device_info
6477
if dev_id != 69 or dev_rev != 0 or dev_seq != 8:
6578
raise RuntimeError("Failed to find SI1145.")
6679
self.reset()
67-
self._write_register(SI1145_HW_KEY, 0x17)
80+
self._write_register(_HW_KEY, 0x17)
6881
self._als_enabled = True
69-
self.als_enabled = True
82+
self._uv_index_enabled = True
83+
self.als_enabled = self._als_enabled
84+
self.uv_index_enabled = self._uv_index_enabled
7085

7186
@property
7287
def device_info(self):
7388
"""A three tuple of part, revision, and sequencer ID"""
74-
return tuple(self._read_register(SI1145_PART_ID, 3))
89+
return self._device_info
7590

7691
@property
7792
def als_enabled(self):
@@ -80,58 +95,82 @@ def als_enabled(self):
8095

8196
@als_enabled.setter
8297
def als_enabled(self, enable):
83-
chlist = self._param_query(SI1145_RAM_CHLIST)
98+
chlist = self._param_query(_RAM_CHLIST)
8499
if enable:
85100
chlist |= 0b00110000
86101
else:
87102
chlist &= ~0b00110000
88-
self._param_set(SI1145_RAM_CHLIST, chlist)
103+
self._param_set(_RAM_CHLIST, chlist)
89104
self._als_enabled = enable
90105

91106
@property
92107
def als(self):
93108
"""A two tuple of the Ambient Light System (ALS) visible and infrared raw sensor values."""
94-
self._send_command(SI1145_CMD_ALS_FORCE)
95-
data = self._read_register(SI1145_ALS_VIS_DATA0, 4)
96-
return struct.unpack("HH", data)
109+
self._send_command(_CMD_ALS_FORCE)
110+
return self._als_data
111+
112+
@property
113+
def uv_index_enabled(self):
114+
"""The UV Index system enabled state"""
115+
return self._uv_index_enabled
116+
117+
@uv_index_enabled.setter
118+
def uv_index_enabled(self, enable):
119+
chlist = self._param_query(_RAM_CHLIST)
120+
if enable:
121+
chlist |= 0b01000000
122+
else:
123+
chlist &= ~0b01000000
124+
self._param_set(_RAM_CHLIST, chlist)
125+
self._als_enabled = enable
126+
127+
self._ucoeff_0 = 0x00
128+
self._ucoeff_1 = 0x02
129+
self._ucoeff_2 = 0x89
130+
self._ucoeff_3 = 0x29
131+
132+
@property
133+
def uv_index(self):
134+
"""The UV Index value"""
135+
return self._aux_data[0] / 100
97136

98137
def reset(self):
99138
"""Perform a software reset of the firmware."""
100-
self._send_command(SI1145_CMD_RESET)
139+
self._send_command(_CMD_RESET)
101140
time.sleep(0.05) # doubling 25ms datasheet spec
102141

103142
def clear_error(self):
104143
"""Clear any existing error code."""
105-
self._send_command(SI1145_CMD_NOP)
144+
self._send_command(_CMD_NOP)
106145

107146
def _param_query(self, param):
108-
self._send_command(SI1145_CMD_PARAM_QUERY | (param & 0x1F))
109-
return self._read_register(SI1145_PARAM_RD)
147+
self._send_command(_CMD_PARAM_QUERY | (param & 0x1F))
148+
return self._read_register(_PARAM_RD)
110149

111150
def _param_set(self, param, value):
112-
self._write_register(SI1145_PARAM_WR, value)
113-
self._send_command(SI1145_CMD_PARAM_SET | (param & 0x1F))
151+
self._write_register(_PARAM_WR, value)
152+
self._send_command(_CMD_PARAM_SET | (param & 0x1F))
114153

115154
def _send_command(self, command):
116-
counter = self._read_register(SI1145_RESPONSE) & 0x0F
117-
self._write_register(SI1145_COMMAND, command)
118-
if command in (SI1145_CMD_NOP, SI1145_CMD_RESET):
155+
counter = self._read_register(_RESPONSE) & 0x0F
156+
self._write_register(_COMMAND, command)
157+
if command in (_CMD_NOP, _CMD_RESET):
119158
return 0
120-
response = self._read_register(SI1145_RESPONSE)
159+
response = self._read_register(_RESPONSE)
121160
while counter == response & 0x0F:
122161
if response & 0xF0:
123162
raise RuntimeError("SI1145 Error: 0x{:02x}".format(response & 0xF0))
124-
response = self._read_register(SI1145_RESPONSE)
163+
response = self._read_register(_RESPONSE)
125164
return response
126165

127166
def _read_register(self, register, length=1):
128167
buffer = bytearray(length)
129-
with self._i2c as i2c:
168+
with self.i2c_device as i2c:
130169
i2c.write_then_readinto(bytes([register]), buffer)
131170
return buffer[0] if length == 1 else buffer
132171

133172
def _write_register(self, register, buffer):
134173
if isinstance(buffer, int):
135174
buffer = bytes([buffer])
136-
with self._i2c as i2c:
175+
with self.i2c_device as i2c:
137176
i2c.write(bytes([register]) + buffer)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-busdevice
7+
adafruit-circuitpython-register

0 commit comments

Comments
 (0)