Commit ea5a30d9 authored by Daniel Hoffend's avatar Daniel Hoffend
Browse files

feat(ecg-app): add pause-mode

with longer 1s press on the TOP_RIGHT buttont you can pause the screen
update. You can then scroll through the increases 4s buffer with
BUTTON_LEFT and BOTTOM_RIGHT. Another press on TOP_RIGHT brings you back
into the normal mode.

The logging operation doesn't get interrupted when pausing the screen.
parent 00b4b33b
...@@ -8,9 +8,9 @@ import struct ...@@ -8,9 +8,9 @@ import struct
WIDTH = 160 WIDTH = 160
HEIGHT = 80 HEIGHT = 80
OFFSET = 50 OFFSET_Y = 49
ECG_RATE = 128 ECG_RATE = 128
HISTORY_MAX = ECG_RATE * 2 HISTORY_MAX = ECG_RATE * 4
DRAW_AFTER_SAMPLES = 5 DRAW_AFTER_SAMPLES = 5
SCALE_FACTOR = 30 SCALE_FACTOR = 30
MODE_USB = "USB" MODE_USB = "USB"
...@@ -31,16 +31,21 @@ write = 0 ...@@ -31,16 +31,21 @@ write = 0
bias = True bias = True
update_screen = 0 update_screen = 0
pause_screen = 0 pause_screen = 0
pause_histogram = False
histogram_offset = 0
sensor = 0 sensor = 0
disp = display.open() disp = display.open()
def callback_ecg(datasets): def callback_ecg(datasets):
global update_screen, history, filebuffer, write global update_screen, history, filebuffer, write
update_screen += len(datasets)
# update histogram datalist
if pause_histogram == False:
if len(datasets) > 0: if len(datasets) > 0:
for value in datasets: for value in datasets:
history.append(value) history.append(value)
update_screen += len(datasets)
if len(history) > HISTORY_MAX: if len(history) > HISTORY_MAX:
r = len(history) - HISTORY_MAX r = len(history) - HISTORY_MAX
for i in range(r): for i in range(r):
...@@ -56,9 +61,7 @@ def callback_ecg(datasets): ...@@ -56,9 +61,7 @@ def callback_ecg(datasets):
# don't update on every callback # don't update on every callback
if update_screen >= DRAW_AFTER_SAMPLES: if update_screen >= DRAW_AFTER_SAMPLES:
# print("history: %i, filebuffer: %i" % (len(history), len(filebuffer)))
draw_histogram() draw_histogram()
update_screen = 0
def write_filebuffer(): def write_filebuffer():
...@@ -76,7 +79,6 @@ def write_filebuffer(): ...@@ -76,7 +79,6 @@ def write_filebuffer():
) )
# write stuff to disk # write stuff to disk
# print("Write %i bytes to %s" % (len(filebuffer), filename))
try: try:
f = open(filename, "ab") f = open(filename, "ab")
f.write(filebuffer) f.write(filebuffer)
...@@ -149,8 +151,18 @@ def toggle_write(): ...@@ -149,8 +151,18 @@ def toggle_write():
return return
def toggle_pause():
global pause_histogram, histogram_offset, history
if pause_histogram == True:
pause_histogram = False
history = []
else:
pause_histogram = True
histogram_offset = 0
def draw_histogram(): def draw_histogram():
global disp, history, current_mode, bias, write, pause_screen global disp, history, current_mode, bias, write, pause_screen, update_screen
# skip rendering due to message beeing shown # skip rendering due to message beeing shown
if pause_screen == -1: if pause_screen == -1:
...@@ -164,28 +176,55 @@ def draw_histogram(): ...@@ -164,28 +176,55 @@ def draw_histogram():
disp.clear(COLOR_BACKGROUND) disp.clear(COLOR_BACKGROUND)
# offset in pause_histogram mode
samples = len(history)
s_start = samples - (histogram_offset + (ECG_RATE * 2))
s_end = samples - (histogram_offset + 1)
s_draw = s_end - (WIDTH - 1)
# get max value and calc scale # get max value and calc scale
value_max = 0 value_max = 0
for value in history: for i, value in enumerate(history):
if abs(value) > value_max: if i >= s_start and i <= s_end and abs(value) > value_max:
value_max = abs(value) value_max = abs(value)
scale = SCALE_FACTOR / (value_max if value_max > 0 else 1) scale = SCALE_FACTOR / (value_max if value_max > 0 else 1)
# draw histogram # draw histogram
old = False old = False
x = 0 x = 0
samples = len(history)
for i, value in enumerate(history): for i, value in enumerate(history):
if old == False: if old == False:
old = (x, int(value * scale) + OFFSET) old = value
x += 1 x += 1
continue continue
elif i > samples - WIDTH: elif i > s_start and i > s_draw and i < s_end:
disp.line(old[0], old[1], x, int(value * scale) + OFFSET, col=COLOR_LINE)
old = (x, int(value * scale) + OFFSET) oldy = int(old * scale)
if oldy < -SCALE_FACTOR:
oldy = -SCALE_FACTOR
elif oldy > SCALE_FACTOR:
oldy = SCALE_FACTOR
disp.line(
x - 1, oldy + OFFSET_Y, x, int(value * scale) + OFFSET_Y, col=COLOR_LINE
)
old = value
x += 1 x += 1
# draw text: mode/bias/write # draw text: mode/bias/write
if pause_histogram == True:
disp.print(
"Pause"
+ (
" -%0.1fs" % (histogram_offset / ECG_RATE)
if histogram_offset > 0
else ""
),
posx=0,
posy=0,
fg=COLOR_TEXT,
)
else:
disp.print( disp.print(
current_mode + ("+Bias" if bias else ""), current_mode + ("+Bias" if bias else ""),
posx=0, posx=0,
...@@ -200,9 +239,12 @@ def draw_histogram(): ...@@ -200,9 +239,12 @@ def draw_histogram():
disp.print("LOG", posx=0, posy=60, fg=COLOR_WRITE_FG, bg=COLOR_WRITE_BG) disp.print("LOG", posx=0, posy=60, fg=COLOR_WRITE_FG, bg=COLOR_WRITE_BG)
disp.update() disp.update()
update_screen = 0
def main(): def main():
global pause_histogram, histogram_offset
# show button layout # show button layout
disp.clear(COLOR_BACKGROUND) disp.clear(COLOR_BACKGROUND)
disp.print(" BUTTONS ", posx=0, posy=0, fg=COLOR_TEXT) disp.print(" BUTTONS ", posx=0, posy=0, fg=COLOR_TEXT)
...@@ -215,24 +257,68 @@ def main(): ...@@ -215,24 +257,68 @@ def main():
# start ecg # start ecg
open_sensor() open_sensor()
while True: while True:
button_pressed = False button_pressed = {"BOTTOM_LEFT": 0, "BOTTOM_RIGHT": 0, "TOP_RIGHT": 0}
while True: while True:
v = buttons.read( v = buttons.read(
buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT
) )
if v == 0:
button_pressed = False
if not button_pressed and v & buttons.BOTTOM_LEFT != 0: # BUTTOM LEFT
button_pressed = True
if button_pressed["BOTTOM_LEFT"] == 0 and v & buttons.BOTTOM_LEFT != 0:
button_pressed["BOTTOM_LEFT"] = utime.time_ms()
if pause_histogram == False:
toggle_write() toggle_write()
else:
l = len(history)
histogram_offset += ECG_RATE / 2
if l - histogram_offset < WIDTH:
histogram_offset = l - WIDTH
elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0: if button_pressed["BOTTOM_LEFT"] > 0 and v & buttons.BOTTOM_LEFT == 0:
button_pressed = True duration = utime.time_ms() - button_pressed["BOTTOM_LEFT"]
toggle_bias() button_pressed["BOTTOM_LEFT"] = 0
# BUTTOM RIGHT
elif not button_pressed and v & buttons.TOP_RIGHT != 0: if button_pressed["BOTTOM_RIGHT"] == 0 and v & buttons.BOTTOM_RIGHT != 0:
button_pressed = True button_pressed["BOTTOM_RIGHT"] = utime.time_ms()
if pause_histogram == False:
toggle_bias()
else:
histogram_offset -= ECG_RATE / 2
histogram_offset -= histogram_offset % (ECG_RATE / 2)
if histogram_offset < 0:
histogram_offset = 0
if button_pressed["BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0:
duration = utime.time_ms() - button_pressed["BOTTOM_RIGHT"]
button_pressed["BOTTOM_RIGHT"] = 0
# TOP RIGHT
# down, and still pressed
if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT != 0:
duration = utime.time_ms() - button_pressed["TOP_RIGHT"]
if duration > 1000:
button_pressed["TOP_RIGHT"] = -1
toggle_pause()
# register down event
elif button_pressed["TOP_RIGHT"] == 0 and v & buttons.TOP_RIGHT != 0:
button_pressed["TOP_RIGHT"] = utime.time_ms()
# register up event but event already called
if button_pressed["TOP_RIGHT"] == -1 and v & buttons.TOP_RIGHT == 0:
button_pressed["TOP_RIGHT"] = 0
# register normal up event
elif button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0:
duration = utime.time_ms() - button_pressed["TOP_RIGHT"]
button_pressed["TOP_RIGHT"] = 0
if pause_histogram == True:
toggle_pause()
else:
toggle_mode() toggle_mode()
pass pass
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment