-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAX30102-VGA-Demo.spin2
210 lines (165 loc) · 7.14 KB
/
MAX30102-VGA-Demo.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
{
----------------------------------------------------------------------------------------------------
Filename: MAX30102-VGA-Demo.spin2
Description: Preliminary demo of the MAX30102 driver
Displays an auto-adjusting chart of
SpO2 and HR data (raw ADC counts only)
Author: Jesse Burt
Started: Jul 2, 2020
Updated: Oct 26, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
CON
_clkfreq = 180_000_000
_xtlfreq = 20_000_000
PRESCALE = 10
OBJ
time: "time"
ser: "com.serial.terminal.ansi" | SER_BAUD=2_000_000
sensor: "sensor.bio.pulse-ox.max30102" | SCL=0, SDA=1, I2C_FREQ=100_000
vga: "display.vga.bitmap-8bpp" | BASEPIN=0
VAR
long _settings_changed
long _i_red, _i_ir, _ir_offset, _red_offset, _div
long _red_data, _ir_data, _last_red, _last_ir, _die_temp
long _key_stack[50], _acq_stack[50]
byte _max30102_cog
PUB main() | x
setup()
vga.fgcolor(vga.MAX_COLOR)
repeat
repeat x from 0 to vga.XMAX
vga.line(x-1, vga.YMAX-_last_ir, x, vga.YMAX-_ir_data, vga.MAX_COLOR)
vga.line(x-1, vga.YMAX-_last_red, x, vga.YMAX-_red_data, %%300)
' vga.plot(x, _ir_data #> 0, vga.MAX_COLOR)
' vga.plot(x, _red_data #> 0, %%300)
if ( _settings_changed )
display_settings()
quit ' settings changed; restart
waitms(10)
vga.box(x+1, 0, x+5, vga.YMAX, 0, TRUE) ' erase ahead of the plot
' Scroll View
repeat
vga.wait_vsync()
vga.plot(vga.XMAX-2, _ir_data #> 0, vga.MAX_COLOR)
vga.plot(vga.XMAX-2, _red_data #> 0, %%300)
vga.scroll_left(0, 0, vga.XMAX, vga.YMAX)
if ( _settings_changed )
display_settings()
PUB display_settings()
ser.pos_xy(0, 7)
ser.printf(@"IR: %d, Red: %d \n\r", sensor.last_ir(), sensor.last_red() )
ser.printf(@"Red current: %d \n\r", _i_red)
ser.printf(@"IR current: %d \n\r", _i_ir)
ser.printf(@"IR offset: %d \n\r", _ir_offset)
ser.printf(@"Red offset: %d \n\r", _red_offset)
ser.printf(@"Div: %d\n\r", _div)
ser.printf(@"Die temp: %d\n\r", _die_temp)
_settings_changed := FALSE
PUB cog_acquire() | tmp[2], irlc, irhc, rlc, rhc
_max30102_cog := sensor.start()
' Initial settings
_i_red := 4_000 ' Red LED current (uA)
_i_ir := 4_000 ' IR LED current
_ir_offset := 37_000
_red_offset := 32_000
_div := 70
sensor.preset_oxysat()
sensor.red_led_current(_i_red)
sensor.ir_led_current(_i_ir)
repeat
repeat
until sensor.ppg_data_rdy()
sensor.fifo_read(@tmp)
_last_ir := _ir_data
_last_red := _red_data
_ir_data := ((sensor.last_ir()-_ir_offset)*PRESCALE)/_div ' Prescale to preserve precision
_red_data := ((sensor.last_red()-_red_offset)*PRESCALE)/_div ' then shrink it down
' Auto-scaling:
if ( _ir_data <= 0 ) ' track how long data goes off-screen. If the
irlc++ ' threshold is reached, change the visual offset
if ( irlc >= 10 ) ' to bring the chart back on-screen
_ir_offset -= 250
irlc := 0
_settings_changed := TRUE ' Notify the main cog
if ( _red_data <= 0 )
rhc++
if ( rhc >= 10 )
_red_offset -= 250
rhc := 0
_settings_changed := TRUE
if ( _ir_data >= vga.YMAX )
irhc++
if ( irhc >= 10 )
_ir_offset += 250
irhc := 0
_settings_changed := TRUE
if ( _red_data >= vga.YMAX )
rlc++
if ( rlc >= 10 )
_red_offset += 250
rlc := 0
_settings_changed := TRUE
if ( _settings_changed ) ' If any settings are changed, update the sensor's
sensor.red_led_current(_i_red) ' LED current limits - they might've changed
sensor.ir_led_current(_i_ir)
_die_temp := sensor.temperature() ' Update sensor die temperature
PUB cog_key_input() | key
repeat
key := ser.getchar()
case key
"=": ' Change LED current (both IR and red)
_i_red := (_i_red + 0_200) <# 51_000
_i_ir := (_i_ir + 0_200) <# 51_000
sensor.red_led_current(_i_red)
sensor.ir_led_current(_i_ir)
"-":
_i_red := (_i_red - 0_200) #> 0
_i_ir := (_i_ir - 0_200) #> 0
sensor.red_led_current(_i_red)
sensor.ir_led_current(_i_ir)
"I": ' Manually change IR data chart offset
_ir_offset := (_ir_offset + 250) <# 2_621_440
"i":
_ir_offset := (_ir_offset - 250) #> 65_000
"R": ' Manually change Red data chart offset
_red_offset := (_red_offset + 250) <# 2_621_440
"r":
_red_offset := (_red_offset - 250) #> 65_000
"D": ' Manually change chart scale divisor
_div := (_div + 1) <# 10_0
"d":
_div := (_div - 1) #> 1_0
" ":
other:
next
_settings_changed := TRUE
PUB setup()
ser.start()
ser.clear()
ser.strln(@"Serial terminal started")
vga.start()
ser.strln(@"VGA Bitmap driver started")
vga.clear()
cogspin(NEWCOG, cog_key_input(), @_key_stack)
cogspin(NEWCOG, cog_acquire(), @_acq_stack)
repeat
until _max30102_cog ' wait for the sensor cog to be ready
ser.strln(@"MAX30102 driver started")
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.
}