/
max30102.go
403 lines (339 loc) · 8.92 KB
/
max30102.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package max30102
import (
"errors"
"fmt"
"time"
"periph.io/x/periph/conn/i2c"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/host"
)
var (
// ErrNotDevice throws an error when the device part ID does not match a
// MAX30102 signature (0x15).
ErrNotDevice error = errors.New("max30102: part ID does not match (0x15)")
)
// Device defines a MAX30102 device.
type Device struct {
dev *i2c.Dev
bus i2c.BusCloser
}
// New returns a new MAX30102 device. By default, this sets the LED pulse
// amplitude to 2.4mA, with a pulse width of 411us and a sample rate of 100
// samples/s.
//
// Argument "busName" can be used to specify the exact bus to use ("/dev/i2c-2", "I2C2", "2").
// Argument "addr" can be used to specify alternative address if default (0x57) is unavailable and changed.
// If "busName" argument is specified as an empty string "" the first available bus will be used.
func New(busName string, addr uint16) (*Device, error) {
if _, err := host.Init(); err != nil {
return nil, fmt.Errorf("max30102: could not initialize host: %w", err)
}
bus, err := i2creg.Open(busName)
if err != nil {
return nil, fmt.Errorf("max30102: could not open I2C bus: %w", err)
}
if addr == 0 {
addr = Addr
}
dev := &i2c.Dev{
Addr: addr,
Bus: bus,
}
d := &Device{
dev: dev,
bus: bus,
}
part, err := d.Read(RegPartID)
if err != nil {
return nil, fmt.Errorf("max30102: could not get part ID: %w", err)
}
if part != PartID {
return nil, ErrNotDevice
}
err = d.Reset()
if err != nil {
return nil, fmt.Errorf("max30102: could not reset device: %w", err)
}
if _, err = d.Options(
RedPulseAmp(2.8),
IRPulseAmp(2.8),
PulseWidth(PW411),
SampleRate(SR100),
InterruptEnable(NewFIFOData|AlmostFull),
AlmostFullValue(0),
Mode(ModeSpO2),
); err != nil {
return nil, fmt.Errorf("max30102: could not initialize device: %w", err)
}
d.drain()
return d, nil
}
// Close closes the device and cleans after itself.
func (d *Device) Close() {
d.Shutdown()
d.bus.Close()
}
// RevID returns the revision ID of the device.
func (d *Device) RevID() (byte, error) {
rev, err := d.Read(RegRevID)
if err != nil {
return 0, fmt.Errorf("max30102: could not get revision ID: %w", err)
}
return rev, nil
}
func (d *Device) waitUntil(reg, flag byte, bit byte) error {
switch bit {
case 1:
for {
state, err := d.Read(reg)
if err != nil {
return fmt.Errorf("could not wait for %v in %v to be %v", flag, reg, bit)
} else if state&flag != 0 {
//fmt.Printf("%#x = %#b\n", reg, state)
return nil
}
}
case 0:
for {
if state, err := d.Read(reg); err != nil {
return fmt.Errorf("could not wait for %v in %v to be %v", flag, reg, bit)
} else if state&flag == 0 {
//fmt.Printf("%#x = %#b\n", reg, state)
return nil
}
}
}
return fmt.Errorf("invalid bit %v, it should be 1 or 0", bit)
}
func (d *Device) tempEnable() error {
if err := d.Write(TempCfg, TempEna); err != nil {
return fmt.Errorf("max30102: could not enable temperature: %w", err)
}
return nil
}
func (d *Device) tempReady() (bool, error) {
state, err := d.Read(TempCfg)
if err != nil {
return false, fmt.Errorf("max30102: could not read temperature state: %w", err)
}
return (state & TempEna) == 0, nil
}
// Temperature returns the current temperature of the device.
func (d *Device) Temperature() (float64, error) {
if err := d.tempEnable(); err != nil {
return 0, err
}
if err := d.waitUntil(TempCfg, TempEna, 0); err != nil {
return 0, err
}
i, err := d.Read(TempInt)
if err != nil {
return 0, fmt.Errorf("max30102: could not read integer part of temperature: %w", err)
}
f, err := d.Read(TempFrac)
if err != nil {
return 0, fmt.Errorf("max30102: could not read fractional part of temperature: %w", err)
}
return float64(int8(i)) + (float64(f) * 0.0625), nil
}
// Read reads a single byte from a register.
func (d *Device) Read(reg byte) (byte, error) {
b := make([]byte, 1)
if err := d.dev.Tx([]byte{reg}, b); err != nil {
return 0, fmt.Errorf("max30102: could not read byte: %w", err)
}
return b[0], nil
}
// ReadBytes reads n bytes from a register.
func (d *Device) ReadBytes(reg byte, n int) ([]byte, error) {
b := make([]byte, n)
if err := d.dev.Tx([]byte{reg}, b); err != nil {
return nil, fmt.Errorf("max30102: could not read %d bytes: %w", n, err)
}
return b, nil
}
// Write writes a byte to a register.
func (d *Device) Write(reg, data byte) error {
n, err := d.dev.Write([]byte{reg, data})
if err != nil {
return err
}
n-- // remove register write
if n != 1 {
return fmt.Errorf("write: wrong number of bytes written: want %d, got %d", 1, n)
}
return nil
}
// Reset resets the device. All configurations, thresholds, and data registers
// are reset to their power-on state.
func (d *Device) Reset() error {
if err := d.Write(ModeCfg, ResetControl); err != nil {
return fmt.Errorf("max30102: could not reset: %w", err)
}
if err := d.waitUntil(ModeCfg, ResetControl, 0); err != nil {
return fmt.Errorf("max30102: could not reset: %w", err)
}
return nil
}
// IRRed returns the value of the red LED and IR LED. The values are normalized
// from 0.0 to 1.0.
func (d *Device) IRRed() (ir, red float64, err error) {
const msbMask byte = 0b0000_0011
err = d.waitUntil(IntStat1, NewFIFOData, 1)
if err != nil {
return 0, 0, err
}
bytes, err := d.ReadBytes(FIFOData, 6)
if err != nil {
return 0, 0, err
}
ir = float64(
int(bytes[3]&msbMask)<<16|
int(bytes[4])<<8|
int(bytes[5])) / maxADC
red = float64(
int(bytes[0]&msbMask)<<16|
int(bytes[1])<<8|
int(bytes[2])) / maxADC
return ir, red, nil
}
// IRRedBatch returns a batch of IR and red LED values based on the AlmostFull
// flag. The amount of data returned can be configured by setting the
// AlmostFullValue leftover value, which is set to 0 by default. Therefore,
// this function returns 32 samples by default.
func (d *Device) IRRedBatch() (ir, red []float64, err error) {
const maxADC = 262143
const msbMask byte = 0b0000_0011
err = d.drain()
if err != nil {
return nil, nil, fmt.Errorf("max30102: could not empty FIFO: %w", err)
}
err = d.waitUntil(IntStat1, AlmostFull, 1)
if err != nil {
return nil, nil, fmt.Errorf("max30102: error waiting for almost full interrupt: %w", err)
}
n, err := d.available()
if err != nil {
return nil, nil, fmt.Errorf("max30102: error reading available data: %w", err)
}
ir = make([]float64, n)
red = make([]float64, n)
for i := 0; i < n; i++ {
bytes, err := d.ReadBytes(FIFOData, 6)
if err != nil {
return nil, nil, err
}
irData := float64(
int(bytes[3]&msbMask)<<16|
int(bytes[4])<<8|
int(bytes[5])) / maxADC
redData := float64(
int(bytes[0]&msbMask)<<16|
int(bytes[1])<<8|
int(bytes[2])) / maxADC
ir[i] = irData
red[i] = redData
}
return ir, red, nil
}
func (d *Device) drain() error {
n, err := d.available()
if err != nil {
return err
}
for i := 0; i < n; i++ {
_, err := d.ReadBytes(FIFOData, 6)
if err != nil {
return err
}
}
return nil
}
func (d *Device) available() (int, error) {
wr, err := d.Read(FIFOWrPtr)
if err != nil {
return 0, nil
}
rd, err := d.Read(FIFORdPtr)
if err != nil {
return 0, nil
}
if wr == rd {
return 32, nil
}
return (int(wr) + 32 - int(rd)) % 32, nil
}
// Calibrate auto-calibrates the current of each LED.
func (d *Device) Calibrate() error {
var ir []float64
var red []float64
var err error
irAmp := 0.0
redAmp := 0.0
if _, err = d.Options(
IRPulseAmp(irAmp),
RedPulseAmp(redAmp),
); err != nil {
return fmt.Errorf("max30102: could not calibrate sensor: %w", err)
}
for mean(ir) < 0.4 {
if irAmp >= 5 {
break
}
irAmp += 0.5
if _, err = d.Options(
IRPulseAmp(irAmp),
); err != nil {
return fmt.Errorf("max30102: could not calibrate sensor: %w", err)
}
time.Sleep(40 * time.Millisecond)
ir, red, err = d.IRRedBatch()
if err != nil {
return fmt.Errorf("max30102: could not calibrate sensor: %w", err)
}
}
for mean(red) < 0.4 {
if redAmp >= 5 {
break
}
redAmp += 0.5
if _, err = d.Options(
RedPulseAmp(redAmp),
); err != nil {
return fmt.Errorf("max30102: could not calibrate sensor: %w", err)
}
time.Sleep(40 * time.Millisecond)
ir, red, err = d.IRRedBatch()
if err != nil {
return fmt.Errorf("max30102: could not calibrate sensor: %w", err)
}
}
fmt.Println("calibration:")
fmt.Printf(" irAmp = %.1fmA\n", irAmp)
fmt.Printf(" redAmp = %.1fmA\n", redAmp)
return nil
}
func mean(a []float64) float64 {
if len(a) == 0 {
return 0
}
r := 0.0
for _, v := range a {
r += v
}
return r / float64(len(a))
}
// Shutdown sets the device into power-save mode.
func (d *Device) Shutdown() error {
_, err := d.config(ModeCfg, ^modeSHDN, modeSHDN)
return err
}
// Startup wakes the device from power-save mode.
func (d *Device) Startup() error {
_, err := d.config(ModeCfg, ^modeSHDN, ^modeSHDN)
return err
}
func (d *Device) debugRegister(reg byte) {
b, _ := d.Read(reg)
fmt.Printf("%#x = %#x (%#b)\n", reg, b, b)
}