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
No related branches found
No related tags found
No related merge requests found
......@@ -143,17 +143,19 @@ def callback_ecg(datasets):
if update_screen >= DRAW_AFTER_SAMPLES:
draw_histogram()
def write_pulse():
def append_to_file(fileprefix, content):
global write, pause_screen
# write to file
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
try:
f = open(filename, "ab")
print(utime.time(), pulse)
f.write(struct.pack("h", utime.time(), pulse))
f.write(content)
f.close()
except OSError as e:
print("Please check the file or filesystem", e)
......@@ -170,32 +172,14 @@ def write_pulse():
print("Unexpected error, stop writing logfile")
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
try:
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_pulse():
append_to_file("pulse", struct.pack("ib", utime.time(), pulse))
def write_filebuffer():
global filebuffer
append_to_file("ecg", filebuffer)
filebuffer = bytearray()
......@@ -226,6 +210,10 @@ def toggle_write():
else:
filebuffer = bytearray()
write = utime.time()
try:
os.mkdir("ecg_logs")
except:
pass
disp.print("Start", posx=45, posy=20, fg=COLOR_TEXT)
disp.print("logging", posx=30, posy=40, fg=COLOR_TEXT)
......@@ -243,7 +231,6 @@ def toggle_pause():
leds.clear()
def draw_leds(vmin, vmax):
# vmin should be in [0, -1]
# vmax should be in [0, 1]
......@@ -336,11 +323,14 @@ def draw_histogram():
)
if pulse < 0:
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,
posy=0,
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:
......@@ -349,7 +339,9 @@ def draw_histogram():
posx=0,
posy=0,
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():
button_pressed["TOP_RIGHT"] = 1
toggle_pause()
# up
if button_pressed["TOP_RIGHT"] > 0 and v & buttons.TOP_RIGHT == 0:
button_pressed["TOP_RIGHT"] = 0
......@@ -435,7 +426,9 @@ def main():
button_pressed["BOTTOM_RIGHT"] = 1
if pause_histogram:
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:
histogram_offset = 0
else:
......@@ -445,7 +438,8 @@ def main():
close_sensor() # reset sensor in case mode or bias was changed TODO do not close sensor otherwise?
open_sensor()
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
if button_pressed["BOTTOM_RIGHT"] > 0 and v & buttons.BOTTOM_RIGHT == 0:
......
......@@ -11,6 +11,7 @@ class Settings(simple_menu.Menu):
def __init__(self):
super().__init__([("return", False)])
self.config_path = "/".join(__file__.split("/")[0:-1])
def on_select(self, value, index):
if index == 0:
......@@ -34,7 +35,9 @@ class Settings(simple_menu.Menu):
def ecg_settings():
config = Settings()
config.add_option(("LEDs",
config.add_option(
(
"LEDs",
itertools.cycle(
[
("off", {}),
......@@ -42,57 +45,25 @@ def ecg_settings():
("Bar", {"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(("Rate",
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(("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(
[
("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"}),
("pulse", {"pulse"}),
("full", {"graph", "pulse"}),
]
),
)
)
))
return config
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment