-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.light.veml6075.spin2
270 lines (211 loc) · 7.92 KB
/
sensor.light.veml6075.spin2
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
257
258
259
260
261
262
263
264
265
266
267
268
269
{
----------------------------------------------------------------------------------------------------
Filename: sensor.light.veml6075.spin2
Description: Driver for the Vishay VEML6075 UVA/UVB sensor
Author: Jesse Burt
Started: Nov 14, 2019
Updated: Jun 7, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
CON
{ default I/O configuration - these can be overridden by the parent object }
SCL = 0
SDA = 1
I2C_FREQ = 100_000
{ dynamic() settings }
DYNAMIC_NORM = 0
DYNAMIC_HI = 1
{ opmode() modes }
CONT = 0
SINGLE = 1
SLAVE_WR = core.SLAVE_ADDR
SLAVE_RD = core.SLAVE_ADDR|1
DEF_SCL = 0
DEF_SDA = 1
DEF_HZ = 100_000
I2C_MAX_FREQ = core.I2C_MAX_FREQ
OBJ
i2c: "com.i2c" ' I2C engine
core: "core.con.veml6075" ' HW-specific constants
PUB null()
' This is not a top-level object
PUB start(): status
' Start using default I/O settings
return startx(SCL, SDA, I2C_FREQ)
PUB startx(SCL_PIN, SDA_PIN, I2C_HZ=DEF_HZ): status
' Start using custom settings
if ( lookdown(SCL_PIN: 0..63) and lookdown(SDA_PIN: 0..63) )
if ( status := i2c.init(SCL_PIN, SDA_PIN, I2C_HZ) )
waitus(core.T_POR)
i2c.reset() ' attempt to make startup
i2c.stop() ' more reliable
if ( dev_id() == core.DEV_ID_RESP )
return
' if this point is reached, something above failed
' Double check I/O pin assignments, connections, power
' Lastly - make sure you have at least one free core/cog
return FALSE
PUB stop()
' Stop the driver
powered(FALSE)
waitms(1)
i2c.deinit()
PUB preset_active()
' Enable sensor power, set to 100ms integration time, continuous measurements
powered(TRUE)
dynamic(DYNAMIC_NORM)
integr_time(100)
opmode(CONT)
PUB dev_id(): id
' Device ID of the chip
' Known values: $0026
id := 0
readreg(core.DEV_ID, 2, @id)
PUB dynamic(level): curr_lvl
' Set sensor dynamic
' Valid values: DYNAMIC_NORM (0), DYNAMIC_HI (1)
' Any other value polls the chip and returns the current setting
curr_lvl := 0
readreg(core.UV_CONF, 2, @curr_lvl)
case level
DYNAMIC_NORM, DYNAMIC_HI:
level <<= core.HD
level := ((curr_lvl & core.HD_MASK) | level)
writereg(core.UV_CONF, 2, @level)
other:
return ( (curr_lvl >> core.HD) & 1 )
PUB integr_time(itime): curr_itime
' Set sensor ADC integration time, in ms
' Valid values: 50, 100, 200, 400, 800
' Any other value polls the chip and returns the current setting
curr_itime := 0
readreg(core.UV_CONF, 2, @curr_itime)
case itime
50, 100, 200, 400, 800:
itime := lookdownz(itime: 50, 100, 200, 400, 800) << core.UV_IT
itime := ((curr_itime & core.UV_IT_MASK) | itime)
writereg(core.UV_CONF, 2, @itime)
other:
curr_itime := (curr_itime >> core.UV_IT) & core.UV_IT_BITS
return lookupz(curr_itime: 50, 100, 200, 400, 800)
PUB ir_data(): ir
' Read Infrared sensor data
' Returns: 16-bit word
readreg(core.UVCOMP2, 2, @ir)
PUB measure() | tmp
' Trigger a single measurement
' NOTE: For use when opmode() is set to SINGLE
tmp := 0
readreg(core.UV_CONF, 2, @tmp)
tmp.byte[0] |= (1 << core.UV_TRIG)
tmp.byte[1] := 0
writereg(core.UV_CONF, 2, @tmp)
PUB opmode(mode): curr_mode
' Set measurement mode
' Valid values:
' CONT (0): Continuous measurement mode
' SINGLE (1): Single-measurement mode
' Any other value polls the chip and returns the current setting
' NOTE: In SINGLE mode, measurements must be triggered manually using the
' measure() method
curr_mode := 0
readreg(core.UV_CONF, 2, @curr_mode)
case mode
CONT, SINGLE:
mode <<= core.UV_AF
mode := ((curr_mode & core.UV_AF_MASK) | mode)
writereg(core.UV_CONF, 2, @mode)
other:
return ( (curr_mode >> core.UV_AF) & 1 )
PUB powered(state): curr_state
' Power on sensor
' Valid values:
' TRUE (-1 or 1): Power on
' FALSE (0): Power off
' Any other value polls the chip and returns the current setting
curr_state := 0
readreg(core.UV_CONF, 2, @curr_state)
case abs(state)
0, 1:
state := (abs(state) ^ 1) & 1 ' logic on chip is inverted,
state := ((curr_state & core.SD_MASK) | state)
writereg(core.UV_CONF, 2, @state)
other: ' so flip the bit
return ( (curr_state & 1) == 1 )
PUB uva_data(): uva
' Read UV-A sensor data
' Returns: 16-bit word
readreg(core.UVA_DATA, 2, @uva)
PUB uvb_data(): uvb
' Read UV-B sensor data
' Returns: 16-bit word
readreg(core.UVB_DATA, 2, @uvb)
CON
{ Coefficients for calculating UV Index }
CO_A = 2_22
CO_B = 1_33
CO_C = 2_95
CO_D = 1_74
UVA_RESP = 0_001461
UVB_RESP = 0_002591
PUB uv_index(): uvidx | uva_raw, uva_comp, uvb_raw, uvb_comp, uvcomp1, uvcomp2
' Return UV Index, in hundredths of a point (e.g. 103 == 1.03)
uva_raw := uva_data() * 100
uvb_raw := uvb_data() * 100
uvcomp1 := white_data()
uvcomp2 := ir_data()
uva_comp := uva_raw - (CO_A * uvcomp1) - (CO_B * uvcomp2)
uvb_comp := uvb_raw - (CO_C * uvcomp1) - (CO_D * uvcomp2)
return (((uva_comp * UVA_RESP) + (uvb_comp * UVB_RESP)) / 2) / 1_000_000
PUB white_data(): w
' Read white/visible sensor data
' Returns: 16-bit word
readreg(core.UVCOMP1, 2, @w)
PRI present(): flag
' Flag indicating the device responds on the I2C bus
i2c.start()
flag := i2c.write(SLAVE_WR)
i2c.stop() ' <P> needed by this device
return ( flag == i2c.ACK )
PRI readreg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Read nr_bytes from slave device into ptr_buff
case reg_nr
core.UV_CONF, core.UVA_DATA..core.DEV_ID:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wait(SLAVE_RD)
i2c.rdblock_lsbf(ptr_buff, nr_bytes, i2c.NAK)
i2c.stop()
other:
return
PRI writereg(reg_nr, nr_bytes, ptr_buff) | cmd_pkt
' Write nr_bytes from ptr_buff to slave device
case reg_nr
core.UV_CONF:
cmd_pkt.byte[0] := SLAVE_WR
cmd_pkt.byte[1] := reg_nr
i2c.start()
i2c.wrblock_lsbf(@cmd_pkt, 2)
i2c.wrblock_lsbf(ptr_buff, nr_bytes)
i2c.stop()
other:
return
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}