-
Notifications
You must be signed in to change notification settings - Fork 1
/
seeed_si115x.py
256 lines (221 loc) · 6.38 KB
/
seeed_si115x.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/python3
# -*- coding:utf-8 -*-
from grove.i2c import Bus
import logging
import time
import signal
import ctypes
#Registers,Parameters and commands
# commands
DEVICE_ADDRESS = 0x53
SI114X_QUERY = 0x80
SI114X_SET = 0xA0
SI114X_NOP = 0x0
SI114X_RESET = 0x01
SI114X_BUSADDR = 0x02
SI114X_PS_FORCE = 0x05
SI114X_GET_CAL = 0x12
SI114X_ALS_FORCE = 0x06
SI114X_PSALS_FORCE = 0x07
SI114X_PS_PAUSE = 0x09
SI114X_ALS_PAUSE = 0x0A
SI114X_PSALS_PAUSE = 0xB
SI114X_PS_AUTO = 0x0D
SI114X_ALS_AUTO = 0x0E
SI114X_PSALS_AUTO = 0x0F
# IIC REGISTERS
############################
RESET_CMD_CTR = 0x00
RESET_SW = 0x01
FORCE = 0x11
PAUSE = 0x12
START = 0x13
PART_ID = 0x00
REV_ID = 0x01
MFR_ID = 0x02
INFO_0 = 0x03
INFO_1 = 0x04
HOSTIN_3 = 0x07
HOSTIN_2 = 0x08
HOSTIN_0 = 0x0A
COMMAND = 0x0B
IRQ_ENABLE = 0x0F
RESPONSE_0 = 0x11
RESPONSE_1 = 0x10
IRQ_STATUS = 0x12
HOSTOUT_0 = 0x13
HOSTOUT_1 = 0x14
HOSTOUT_2 = 0x15
HOSTOUT_3 = 0x16
HOSTOUT_4 = 0x17
HOSTOUT_5 = 0x18
HOSTOUT_6 = 0x19
HOSTOUT_7 = 0x1A
HOSTOUT_8 = 0x1B
HOSTOUT_9 = 0x1C
HOSTOUT_10 = 0x1D
HOSTOUT_11 = 0x1E
HOSTOUT_12 = 0x1F
HOSTOUT_13 = 0x20
HOSTOUT_14 = 0x21
HOSTOUT_15 = 0x22
HOSTOUT_16 = 0x23
HOSTOUT_17 = 0x24
HOSTOUT_18 = 0x25
HOSTOUT_19 = 0x26
HOSTOUT_20 = 0x27
HOSTOUT_21 = 0x28
HOSTOUT_22 = 0x29
HOSTOUT_23 = 0x2A
HOSTOUT_24 = 0x2B
HOSTOUT_25 = 0x2C
I2C_ADDR = 0x00
CHAN_LIST = 0x01
ADCCONFIG_0 = 0x02
ADCSENS_0 = 0x03
ADCPOST_0 = 0x04
MEASCONFIG_0 = 0x05
ADCCONFIG_1 = 0x06
ADCSENS_1 = 0x07
ADCPOST_1 = 0x08
MEASCONFIG_1 = 0x09
ADCCONFIG_2 = 0x0A
ADCSENS_2 = 0x0B
ADCPOST_2 = 0x0C
MEASCONFIG_2 = 0x0D
ADCCONFIG_3 = 0x0E
ADCSENS_3 = 0x0F
ADCPOST_3 = 0x10
MEASCONFIG_3 = 0x11
ADCCONFIG_4 = 0x12
ADCSENS_4 = 0x13
ADCPOST_4 = 0x14
MEASCONFIG_4 = 0x15
ADCCONFIG_5 = 0x16
ADCSENS_5 = 0x17
ADCPOST_5 = 0x18
MEASCONFIG_5 = 0x19
MEASRATE_H = 0x1A
MEASRATE_L = 0x1B
MEASCOUNT_0 = 0x1C
MEASCOUNT_1 = 0x1D
MEASCOUNT_2 = 0x1E
LED1_A = 0x1F
LED1_B = 0x20
LED2_A = 0x21
LED2_B = 0x22
LED3_A = 0x23
LED3_B = 0x24
THRESHOLD0_H = 0x25
THRESHOLD0_L = 0x26
THRESHOLD1_H = 0x27
THRESHOLD1_L = 0x28
THRESHOLD2_H = 0x29
THRESHOLD2_L = 0x2A
BURST = 0x2B
############################
class grove_si115x(object):
def __init__(self,address = 0x53):
self.bus = Bus()
self.addr = address
self._logger = logging.getLogger('grove_si115x')
assert self.Begin() , "Please check if the I2C device insert in I2C of Base Hat"
def __exit__(self):
self.bus.close()
#Init the si1151 and begin to collect data
def Begin(self):
if self._ReadByte(PART_ID) != 0X51:
return False
self.send_command(START)
self.param_set(CHAN_LIST, 0B000010)
self.param_set(MEASRATE_H, 0)
self.param_set(MEASRATE_L, 1)
self.param_set(MEASCOUNT_0, 5)
self.param_set(MEASCOUNT_1, 10)
self.param_set(MEASCOUNT_2, 10)
self.param_set(THRESHOLD0_L, 200)
self.param_set(THRESHOLD0_H, 0)
try:
self.bus.write_byte_data(self.addr,IRQ_ENABLE,0B000010)
except OSError:
raise OSError("Please check if the I2C device insert in I2C of Base Hat")
conf_1 = [0B00000000,0B00000010,0B00000001,0B11000001]
self.param_set(ADCCONFIG_0 + 1, conf_1[0])
self.param_set(ADCSENS_0 + 1, conf_1[1])
self.param_set(ADCPOST_0 + 1, conf_1[2])
self.param_set(MEASCONFIG_0 + 1, conf_1[3])
self.param_set(ADCCONFIG_0 + 3, conf_1[0])
self.param_set(ADCSENS_0 + 3, conf_1[1])
self.param_set(ADCPOST_0 + 3, conf_1[2])
self.param_set(MEASCONFIG_0 + 3, conf_1[3])
return True
#reset the si1151
#inclue IRQ reg, command regs...
##############################################################
def send_command(self,code):
while True:
cmmnd_ctr = self.read_register(RESPONSE_0,1)
self.write_data(COMMAND, code)
self.read_register(RESPONSE_0,1)
r = self.read_register(RESPONSE_0,1)
if(r > cmmnd_ctr):
break
def write_data(self,Reg,Value):
try:
self.bus.write_byte_data(self.addr,Reg,Value)
except OSError:
raise OSError("Please check if the I2C device insert in I2C of Base Hat")
def read_register(self,Reg,Value):
try:
read_data = self.bus.read_i2c_block_data(self.addr,Reg,Value)
except OSError:
raise OSError("Please check if the I2C device insert in I2C of Base Hat")
return read_data
def param_set(self,loc,val):
register_P2 = loc | (0B10 << 6)
while True:
cmmnd_ctr = self.read_register(RESPONSE_0,1)
self.write_data(HOSTIN_0, val)
self.write_data(COMMAND, register_P2)
r = self.read_register(RESPONSE_0,1)
if(r > cmmnd_ctr):
break
def ReadHalfWord(self):
self.send_command(FORCE)
data0 = self.read_register(HOSTOUT_0,1)
data1 = self.read_register(HOSTOUT_1,1)
data3 = (194 - data0[0]) * 256 + abs((230 - data1[0]))
return abs(data3)
def ReadHalfWord_UV(self):
self.send_command(FORCE)
data0 = self.read_register(HOSTOUT_0,1)
data1 = self.read_register(HOSTOUT_1,1)
data3 = (194 - data0[0]) * 256 + abs((230 - data1[0]))
return abs((194 - data0[0])/10)
def ReadHalfWord_VISIBLE(self):
self.send_command(FORCE)
data0 = self.read_register(HOSTOUT_0,1)
data1 = self.read_register(HOSTOUT_1,1)
data3 = (194 - data0[0]) * 256 + abs((230 - data1[0]))
return abs(data3/3)
##############################################################
# read 8 bit data from Reg
def _ReadByte(self,Reg):
try:
read_data = self.bus.read_byte_data(self.addr,Reg)
except OSError:
raise OSError("Please check if the I2C device insert in I2C of Base Hat")
return read_data
def handler(signalnum, handler):
print("Please use Ctrl C to quit")
def main():
signal.signal(signal.SIGTSTP, handler) # Ctrl-z
signal.signal(signal.SIGQUIT, handler) # Ctrl-\
SI1151 = grove_si115x()
print("Please use Ctrl C to quit")
while True:
print('Visible %03d UV %.2f IR %03d' % (SI1151.ReadHalfWord_VISIBLE(), SI1151.ReadHalfWord_UV(), SI1151.ReadHalfWord()),end=" ")
print('\r', end='')
time.sleep(0.5)
if __name__ == "__main__":
main()