Skip to content
Snippets Groups Projects
Commit 5168edff authored by fuchsi*'s avatar fuchsi* Committed by rahix
Browse files

some cleanup: fixed pulse logging format, create folder 'ecg_logs' for output files

parent d3a2a1df
Branches
No related tags found
No related merge requests found
...@@ -143,17 +143,19 @@ def callback_ecg(datasets): ...@@ -143,17 +143,19 @@ def callback_ecg(datasets):
if update_screen >= DRAW_AFTER_SAMPLES: if update_screen >= DRAW_AFTER_SAMPLES:
draw_histogram() draw_histogram()
def write_pulse():
def append_to_file(fileprefix, content):
global write, pause_screen global write, pause_screen
# write to file # write to file
lt = utime.localtime(write) lt = utime.localtime(write)
filename = "/pulse-{:04d}-{:02d}-{:02d}_{:02d}{:02d}{:02d}.log".format(*lt) filename = "/ecg_logs/{}-{:04d}-{:02d}-{:02d}_{:02d}{:02d}{:02d}.log".format(
fileprefix, *lt
)
# write stuff to disk # write stuff to disk
try: try:
f = open(filename, "ab") f = open(filename, "ab")
print(utime.time(), pulse) f.write(content)
f.write(struct.pack("h", utime.time(), pulse))
f.close() f.close()
except OSError as e: except OSError as e:
print("Please check the file or filesystem", e) print("Please check the file or filesystem", e)
...@@ -170,32 +172,14 @@ def write_pulse(): ...@@ -170,32 +172,14 @@ def write_pulse():
print("Unexpected error, stop writing logfile") print("Unexpected error, stop writing logfile")
write = 0 write = 0
def write_filebuffer():
global write, filebuffer, pause_screen
# write to file
lt = utime.localtime(write)
filename = "/ecg-{:04d}-{:02d}-{:02d}_{:02d}{:02d}{:02d}.log".format(*lt)
# write stuff to disk def write_pulse():
try: append_to_file("pulse", struct.pack("ib", utime.time(), pulse))
f = open(filename, "ab")
f.write(filebuffer)
f.close()
except OSError as e:
print("Please check the file or filesystem", e)
write = 0
pause_screen = -1
disp.clear(COLOR_BACKGROUND)
disp.print("IO Error", posy=0, fg=COLOR_TEXT)
disp.print("Please check", posy=20, fg=COLOR_TEXT)
disp.print("your", posy=40, fg=COLOR_TEXT)
disp.print("filesystem", posy=60, fg=COLOR_TEXT)
disp.update()
close_sensor()
except:
print("Unexpected error, stop writing logfile")
write = 0
def write_filebuffer():
global filebuffer
append_to_file("ecg", filebuffer)
filebuffer = bytearray() filebuffer = bytearray()
...@@ -226,6 +210,10 @@ def toggle_write(): ...@@ -226,6 +210,10 @@ def toggle_write():
else: else:
filebuffer = bytearray() filebuffer = bytearray()
write = utime.time() write = utime.time()
try:
os.mkdir("ecg_logs")
except:
pass
disp.print("Start", posx=45, posy=20, fg=COLOR_TEXT) disp.print("Start", posx=45, posy=20, fg=COLOR_TEXT)
disp.print("logging", posx=30, posy=40, fg=COLOR_TEXT) disp.print("logging", posx=30, posy=40, fg=COLOR_TEXT)
...@@ -243,7 +231,6 @@ def toggle_pause(): ...@@ -243,7 +231,6 @@ def toggle_pause():
leds.clear() leds.clear()
def draw_leds(vmin, vmax): def draw_leds(vmin, vmax):
# vmin should be in [0, -1] # vmin should be in [0, -1]
# vmax should be in [0, 1] # vmax should be in [0, 1]
...@@ -336,11 +323,14 @@ def draw_histogram(): ...@@ -336,11 +323,14 @@ def draw_histogram():
) )
if pulse < 0: if pulse < 0:
disp.print( disp.print(
config.get_option("Mode") + ("+Bias" if config.get_option("Bias") else ""), config.get_option("Mode")
+ ("+Bias" if config.get_option("Bias") else ""),
posx=0, posx=0,
posy=0, posy=0,
fg=( fg=(
COLOR_MODE_FINGER if config.get_option("Mode") == MODE_FINGER else COLOR_MODE_USB COLOR_MODE_FINGER
if config.get_option("Mode") == MODE_FINGER
else COLOR_MODE_USB
), ),
) )
else: else:
...@@ -349,7 +339,9 @@ def draw_histogram(): ...@@ -349,7 +339,9 @@ def draw_histogram():
posx=0, posx=0,
posy=0, posy=0,
fg=( fg=(
COLOR_MODE_FINGER if config.get_option("Mode") == MODE_FINGER else COLOR_MODE_USB COLOR_MODE_FINGER
if config.get_option("Mode") == MODE_FINGER
else COLOR_MODE_USB
), ),
) )
...@@ -400,7 +392,6 @@ def main(): ...@@ -400,7 +392,6 @@ def main():
button_pressed["TOP_RIGHT"] = 1 button_pressed["TOP_RIGHT"] = 1
toggle_pause() toggle_pause()
# up # up
if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0: if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0:
button_pressed["TOP_RIGHT"] = 0 button_pressed["TOP_RIGHT"] = 0
...@@ -435,7 +426,9 @@ def main(): ...@@ -435,7 +426,9 @@ def main():
button_pressed["BOTTOM_RIGHT"] = 1 button_pressed["BOTTOM_RIGHT"] = 1
if pause_histogram: if pause_histogram:
histogram_offset -= config.get_option("Rate") / 2 histogram_offset -= config.get_option("Rate") / 2
histogram_offset -= histogram_offset % (config.get_option("Rate") / 2) histogram_offset -= histogram_offset % (
config.get_option("Rate") / 2
)
if histogram_offset < 0: if histogram_offset < 0:
histogram_offset = 0 histogram_offset = 0
else: else:
...@@ -445,7 +438,8 @@ def main(): ...@@ -445,7 +438,8 @@ def main():
close_sensor() # reset sensor in case mode or bias was changed TODO do not close sensor otherwise? close_sensor() # reset sensor in case mode or bias was changed TODO do not close sensor otherwise?
open_sensor() open_sensor()
pause_screen = 0 # start plotting graph again pause_screen = 0 # start plotting graph again
button_pressed["TOP_RIGHT"] = 1 # returning from menu was by pressing the TOP_RIGHT button # returning from menu was by pressing the TOP_RIGHT button
button_pressed["TOP_RIGHT"] = 1
# up # up
if button_pressed["BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0: if button_pressed["BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0:
......
...@@ -11,6 +11,7 @@ class Settings(simple_menu.Menu): ...@@ -11,6 +11,7 @@ class Settings(simple_menu.Menu):
def __init__(self): def __init__(self):
super().__init__([("return", False)]) super().__init__([("return", False)])
self.config_path = "/".join(__file__.split("/")[0:-1])
def on_select(self, value, index): def on_select(self, value, index):
if index == 0: if index == 0:
...@@ -34,7 +35,9 @@ class Settings(simple_menu.Menu): ...@@ -34,7 +35,9 @@ class Settings(simple_menu.Menu):
def ecg_settings(): def ecg_settings():
config = Settings() config = Settings()
config.add_option(("LEDs", config.add_option(
(
"LEDs",
itertools.cycle( itertools.cycle(
[ [
("off", {}), ("off", {}),
...@@ -42,57 +45,25 @@ def ecg_settings(): ...@@ -42,57 +45,25 @@ def ecg_settings():
("Bar", {"bar"}), ("Bar", {"bar"}),
("Full", {"pulse", "bar"}), ("Full", {"pulse", "bar"}),
] ]
),
) )
))
config.add_option(("Mode",
itertools.cycle(
[
("Finger", "Finger"),
("USB", "USB")
]
)
))
config.add_option(("Bias",
itertools.cycle(
[
("on", True),
("off", False)
]
)
))
config.add_option(("Filter",
itertools.cycle(
[
("HP", {"HP"}),
("off", {})
]
) )
)) config.add_option(("Mode", itertools.cycle([("Finger", "Finger"), ("USB", "USB")])))
config.add_option(("Rate", config.add_option(("Bias", itertools.cycle([("on", True), ("off", False)])))
config.add_option(("Filter", itertools.cycle([("HP", {"HP"}), ("off", {})])))
config.add_option(("Rate", itertools.cycle([("128Hz", 128), ("256Hz", 256)])))
config.add_option(("Window", itertools.cycle([("1x", 1), ("2x", 2), ("3x", 3)])))
config.add_option(
(
"Log",
itertools.cycle( itertools.cycle(
[ [
("128Hz", 128),
("256Hz", 256)
]
)
))
config.add_option(("Window",
itertools.cycle(
[
("1x", 1),
("2x", 2),
("3x", 3)
]
)
))
config.add_option(("Log",
itertools.cycle(
[
("full", {"graph", "pulse"}),
("graph", {"graph"}), ("graph", {"graph"}),
("pulse", {"pulse"}), ("pulse", {"pulse"}),
("full", {"graph", "pulse"}),
] ]
),
)
) )
))
return config return config
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment