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"
38
38
SI1145_DEFAULT_ADDRESS = const (0x60 )
39
39
SI1145_PART_ID = const (0x00 )
40
40
SI1145_HW_KEY = const (0x07 )
41
+ _COEFF_0 = const (0x13 )
42
+ _COEFF_1 = const (0x14 )
43
+ _COEFF_2 = const (0x15 )
44
+ _COEFF_3 = const (0x16 )
41
45
SI1145_PARAM_WR = const (0x17 )
42
46
SI1145_COMMAND = const (0x18 )
43
47
SI1145_RESPONSE = const (0x20 )
44
48
SI1145_ALS_VIS_DATA0 = const (0x22 )
49
+ SI1145_UV_INDEX_DATA0 = const (0x2C )
45
50
SI1145_PARAM_RD = const (0x2E )
46
51
47
52
# Commands (for COMMAND register)
58
63
class SI1145 :
59
64
"""Driver for the SI1145 UV, IR, Visible Light Sensor."""
60
65
66
+ _device_info = Struct (SI1145_PART_ID , "<BBB" )
67
+ _ucoeff_0 = Struct (_COEFF_0 , "<B" ) # TODO: const(coeff)
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 (SI1145_ALS_VIS_DATA0 , "<HH" )
72
+ _aux_data = Struct (SI1145_UV_INDEX_DATA0 , "<H" )
73
+
61
74
def __init__ (self , i2c , address = SI1145_DEFAULT_ADDRESS ):
62
- self ._i2c = i2c_device .I2CDevice (i2c , 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
80
self ._write_register (SI1145_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 ):
@@ -92,8 +107,32 @@ def als_enabled(self, enable):
92
107
def als (self ):
93
108
"""A two tuple of the Ambient Light System (ALS) visible and infrared raw sensor values."""
94
109
self ._send_command (SI1145_CMD_ALS_FORCE )
95
- data = self ._read_register (SI1145_ALS_VIS_DATA0 , 4 )
96
- return struct .unpack ("HH" , data )
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 (SI1145_RAM_CHLIST )
120
+ if enable :
121
+ chlist |= 0b01000000
122
+ else :
123
+ chlist &= ~ 0b01000000
124
+ self ._param_set (SI1145_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."""
@@ -126,12 +165,12 @@ def _send_command(self, command):
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