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 )
38
+ _DEFAULT_ADDRESS = const (0x60 )
39
+ _PART_ID = const (0x00 )
40
+ _HW_KEY = const (0x07 )
41
41
_COEFF_0 = const (0x13 )
42
42
_COEFF_1 = const (0x14 )
43
43
_COEFF_2 = const (0x15 )
44
44
_COEFF_3 = const (0x16 )
45
- SI1145_PARAM_WR = const (0x17 )
46
- SI1145_COMMAND = const (0x18 )
47
- SI1145_RESPONSE = const (0x20 )
48
- SI1145_ALS_VIS_DATA0 = const (0x22 )
49
- SI1145_UV_INDEX_DATA0 = const (0x2C )
50
- SI1145_PARAM_RD = const (0x2E )
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 )
51
51
52
52
# Commands (for COMMAND register)
53
- SI1145_CMD_PARAM_QUERY = const (0b10000000 )
54
- SI1145_CMD_PARAM_SET = const (0b10100000 )
55
- SI1145_CMD_NOP = const (0b00000000 )
56
- SI1145_CMD_RESET = const (0b00000001 )
57
- 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 )
58
58
59
59
# RAM Parameter Offsets (use with PARAM_QUERY / PARAM_SET)
60
- SI1145_RAM_CHLIST = const (0x01 )
60
+ _RAM_CHLIST = const (0x01 )
61
61
62
62
63
63
class SI1145 :
64
64
"""Driver for the SI1145 UV, IR, Visible Light Sensor."""
65
65
66
- _device_info = Struct (SI1145_PART_ID , "<BBB" )
66
+ _device_info = Struct (_PART_ID , "<BBB" )
67
67
_ucoeff_0 = Struct (_COEFF_0 , "<B" )
68
68
_ucoeff_1 = Struct (_COEFF_1 , "<B" )
69
69
_ucoeff_2 = Struct (_COEFF_2 , "<B" )
70
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" )
71
+ _als_data = Struct (_ALS_VIS_DATA0 , "<HH" )
72
+ _aux_data = Struct (_UV_INDEX_DATA0 , "<H" )
73
73
74
- def __init__ (self , i2c , address = SI1145_DEFAULT_ADDRESS ):
74
+ def __init__ (self , i2c , address = _DEFAULT_ADDRESS ):
75
75
self .i2c_device = i2c_device .I2CDevice (i2c , address )
76
76
dev_id , dev_rev , dev_seq = self .device_info
77
77
if dev_id != 69 or dev_rev != 0 or dev_seq != 8 :
78
78
raise RuntimeError ("Failed to find SI1145." )
79
79
self .reset ()
80
- self ._write_register (SI1145_HW_KEY , 0x17 )
80
+ self ._write_register (_HW_KEY , 0x17 )
81
81
self ._als_enabled = True
82
82
self ._uv_index_enabled = True
83
83
self .als_enabled = self ._als_enabled
@@ -95,18 +95,18 @@ def als_enabled(self):
95
95
96
96
@als_enabled .setter
97
97
def als_enabled (self , enable ):
98
- chlist = self ._param_query (SI1145_RAM_CHLIST )
98
+ chlist = self ._param_query (_RAM_CHLIST )
99
99
if enable :
100
100
chlist |= 0b00110000
101
101
else :
102
102
chlist &= ~ 0b00110000
103
- self ._param_set (SI1145_RAM_CHLIST , chlist )
103
+ self ._param_set (_RAM_CHLIST , chlist )
104
104
self ._als_enabled = enable
105
105
106
106
@property
107
107
def als (self ):
108
108
"""A two tuple of the Ambient Light System (ALS) visible and infrared raw sensor values."""
109
- self ._send_command (SI1145_CMD_ALS_FORCE )
109
+ self ._send_command (_CMD_ALS_FORCE )
110
110
return self ._als_data
111
111
112
112
@property
@@ -116,12 +116,12 @@ def uv_index_enabled(self):
116
116
117
117
@uv_index_enabled .setter
118
118
def uv_index_enabled (self , enable ):
119
- chlist = self ._param_query (SI1145_RAM_CHLIST )
119
+ chlist = self ._param_query (_RAM_CHLIST )
120
120
if enable :
121
121
chlist |= 0b01000000
122
122
else :
123
123
chlist &= ~ 0b01000000
124
- self ._param_set (SI1145_RAM_CHLIST , chlist )
124
+ self ._param_set (_RAM_CHLIST , chlist )
125
125
self ._als_enabled = enable
126
126
127
127
self ._ucoeff_0 = 0x00
@@ -136,31 +136,31 @@ def uv_index(self):
136
136
137
137
def reset (self ):
138
138
"""Perform a software reset of the firmware."""
139
- self ._send_command (SI1145_CMD_RESET )
139
+ self ._send_command (_CMD_RESET )
140
140
time .sleep (0.05 ) # doubling 25ms datasheet spec
141
141
142
142
def clear_error (self ):
143
143
"""Clear any existing error code."""
144
- self ._send_command (SI1145_CMD_NOP )
144
+ self ._send_command (_CMD_NOP )
145
145
146
146
def _param_query (self , param ):
147
- self ._send_command (SI1145_CMD_PARAM_QUERY | (param & 0x1F ))
148
- return self ._read_register (SI1145_PARAM_RD )
147
+ self ._send_command (_CMD_PARAM_QUERY | (param & 0x1F ))
148
+ return self ._read_register (_PARAM_RD )
149
149
150
150
def _param_set (self , param , value ):
151
- self ._write_register (SI1145_PARAM_WR , value )
152
- 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 ))
153
153
154
154
def _send_command (self , command ):
155
- counter = self ._read_register (SI1145_RESPONSE ) & 0x0F
156
- self ._write_register (SI1145_COMMAND , command )
157
- 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 ):
158
158
return 0
159
- response = self ._read_register (SI1145_RESPONSE )
159
+ response = self ._read_register (_RESPONSE )
160
160
while counter == response & 0x0F :
161
161
if response & 0xF0 :
162
162
raise RuntimeError ("SI1145 Error: 0x{:02x}" .format (response & 0xF0 ))
163
- response = self ._read_register (SI1145_RESPONSE )
163
+ response = self ._read_register (_RESPONSE )
164
164
return response
165
165
166
166
def _read_register (self , register , length = 1 ):
0 commit comments