From 08f65e56ce6adb718e8f018eea653a35b83f4bc8 Mon Sep 17 00:00:00 2001
From: Daniel Hoffend <dh@dotlan.net>
Date: Sat, 7 Sep 2019 01:12:32 +0200
Subject: [PATCH] make led modes iterable in the python way

---
 preload/apps/ecg/__init__.py | 37 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 19 deletions(-)

diff --git a/preload/apps/ecg/__init__.py b/preload/apps/ecg/__init__.py
index 6e2ed15f..bd5bf97b 100644
--- a/preload/apps/ecg/__init__.py
+++ b/preload/apps/ecg/__init__.py
@@ -6,6 +6,7 @@ import buttons
 import max30001
 import math
 import struct
+import itertools
 
 WIDTH = 160
 HEIGHT = 80
@@ -24,11 +25,17 @@ COLOR_MODE_FINGER = [0, 255, 0]
 COLOR_MODE_USB = [0, 0, 255]
 COLOR_WRITE_FG = [255, 255, 255]
 COLOR_WRITE_BG = [255, 0, 0]
-LED_FLAG_BAR = 1
-LED_FLAG_PULSE = 2
 
 current_mode = MODE_FINGER
-led_mode = LED_FLAG_BAR + LED_FLAG_PULSE
+modes = itertools.cycle(
+    [
+        ({"bar", "pulse"}, {"text": "Top + Pulse", "posx": 0}),
+        ({}, {"text": "off", "posx": 55}),
+        ({"bar"}, {"text": "Top Only", "posx": 25}),
+        ({"pulse"}, {"text": "Pulse Only", "posx": 5}),
+    ]
+)
+led_mode = next(modes)[0]
 history = []
 filebuffer = bytearray()
 write = 0
@@ -240,21 +247,13 @@ def toggle_pause():
 
 
 def toggle_leds():
-    global led_mode, disp, pause_screen, leds
-    led_mode = (led_mode + 1) % 4
+    global led_mode, disp, pause_screen, leds, modes
+    led_mode, display_args = next(modes)
 
     pause_screen = utime.time_ms() + 250
     disp.clear(COLOR_BACKGROUND)
     disp.print("LEDs", posx=50, posy=20, fg=COLOR_TEXT)
-    if not led_mode:
-        disp.print("off", posx=55, posy=40, fg=COLOR_TEXT)
-    elif led_mode == LED_FLAG_BAR:
-        disp.print("Top only", posx=25, posy=40, fg=COLOR_TEXT)
-    elif led_mode == LED_FLAG_PULSE:
-        disp.print("Pulse only", posx=5, posy=40, fg=COLOR_TEXT)
-    elif led_mode == LED_FLAG_BAR + LED_FLAG_PULSE:
-        disp.print("Top + Pulse", posx=0, posy=40, fg=COLOR_TEXT)
-
+    disp.print(**display_args, posy=40, fg=COLOR_TEXT)
     disp.update()
     leds.clear()
 
@@ -265,11 +264,11 @@ def draw_leds(vmin, vmax):
     global pulse, samples_since_last_pulse, last_pulse_blink
 
     # stop blinking
-    if not led_mode:
+    if not bool(led_mode):
         return
 
     # update led bar
-    if led_mode & LED_FLAG_BAR:
+    if "bar" in led_mode:
         for i in reversed(range(6)):
             leds.prep_hsv(
                 5 + i, COLORS[5 + i] if vmin <= 0 and i <= vmin * -6 else (0, 0, 0)
@@ -281,13 +280,13 @@ def draw_leds(vmin, vmax):
 
     # blink red on pulse
     if (
-        led_mode & LED_FLAG_PULSE
+        "pulse" in led_mode
         and pulse > 0
         and samples_since_last_pulse < last_pulse_blink
     ):
         for i in range(4):
             leds.prep(11 + i, (255, 0, 0))
-    elif led_mode & LED_FLAG_PULSE:
+    elif "pulse" in led_mode:
         for i in range(4):
             leds.prep(11 + i, (0, 0, 0))
     last_pulse_blink = samples_since_last_pulse
@@ -387,7 +386,7 @@ def main():
         "       Pause >", posx=0, posy=28, fg=COLOR_MODE_FINGER, font=display.FONT16
     )
     disp.print(
-        " Mode / Bias >", posx=0, posy=44, fg=COLOR_MODE_USB, font=display.FONT16
+        "   Mode/Bias >", posx=0, posy=44, fg=COLOR_MODE_USB, font=display.FONT16
     )
     disp.print(
         "< LED/WriteLog", posx=0, posy=64, fg=COLOR_WRITE_BG, font=display.FONT16
-- 
GitLab