27
27
"""
28
28
29
29
import time
30
- import struct
31
30
from micropython import const
32
31
from adafruit_bus_device import i2c_device
32
+ from adafruit_register .i2c_struct import Struct
33
33
34
34
__version__ = "0.0.0-auto.0"
35
35
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SI1145.git"
36
36
37
37
# 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 )
46
51
47
52
# 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 )
53
58
54
59
# RAM Parameter Offsets (use with PARAM_QUERY / PARAM_SET)
55
- SI1145_RAM_CHLIST = const (0x01 )
60
+ _RAM_CHLIST = const (0x01 )
56
61
57
62
58
63
class SI1145 :
59
64
"""Driver for the SI1145 UV, IR, Visible Light Sensor."""
60
65
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 )
63
76
dev_id , dev_rev , dev_seq = self .device_info
64
77
if dev_id != 69 or dev_rev != 0 or dev_seq != 8 :
65
78
raise RuntimeError ("Failed to find SI1145." )
66
79
self .reset ()
67
- self ._write_register (SI1145_HW_KEY , 0x17 )
80
+ self ._write_register (_HW_KEY , 0x17 )
68
81
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
70
85
71
86
@property
72
87
def device_info (self ):
73
88
"""A three tuple of part, revision, and sequencer ID"""
74
- return tuple ( self ._read_register ( SI1145_PART_ID , 3 ))
89
+ return self ._device_info
75
90
76
91
@property
77
92
def als_enabled (self ):
@@ -80,58 +95,82 @@ def als_enabled(self):
80
95
81
96
@als_enabled .setter
82
97
def als_enabled (self , enable ):
83
- chlist = self ._param_query (SI1145_RAM_CHLIST )
98
+ chlist = self ._param_query (_RAM_CHLIST )
84
99
if enable :
85
100
chlist |= 0b00110000
86
101
else :
87
102
chlist &= ~ 0b00110000
88
- self ._param_set (SI1145_RAM_CHLIST , chlist )
103
+ self ._param_set (_RAM_CHLIST , chlist )
89
104
self ._als_enabled = enable
90
105
91
106
@property
92
107
def als (self ):
93
108
"""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
97
136
98
137
def reset (self ):
99
138
"""Perform a software reset of the firmware."""
100
- self ._send_command (SI1145_CMD_RESET )
139
+ self ._send_command (_CMD_RESET )
101
140
time .sleep (0.05 ) # doubling 25ms datasheet spec
102
141
103
142
def clear_error (self ):
104
143
"""Clear any existing error code."""
105
- self ._send_command (SI1145_CMD_NOP )
144
+ self ._send_command (_CMD_NOP )
106
145
107
146
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 )
110
149
111
150
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 ))
114
153
115
154
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 ):
119
158
return 0
120
- response = self ._read_register (SI1145_RESPONSE )
159
+ response = self ._read_register (_RESPONSE )
121
160
while counter == response & 0x0F :
122
161
if response & 0xF0 :
123
162
raise RuntimeError ("SI1145 Error: 0x{:02x}" .format (response & 0xF0 ))
124
- response = self ._read_register (SI1145_RESPONSE )
163
+ response = self ._read_register (_RESPONSE )
125
164
return response
126
165
127
166
def _read_register (self , register , length = 1 ):
128
167
buffer = bytearray (length )
129
- with self ._i2c as i2c :
168
+ with self .i2c_device as i2c :
130
169
i2c .write_then_readinto (bytes ([register ]), buffer )
131
170
return buffer [0 ] if length == 1 else buffer
132
171
133
172
def _write_register (self , register , buffer ):
134
173
if isinstance (buffer , int ):
135
174
buffer = bytes ([buffer ])
136
- with self ._i2c as i2c :
175
+ with self .i2c_device as i2c :
137
176
i2c .write (bytes ([register ]) + buffer )
0 commit comments