From febcd1face7fcc9e80ed8b7474d1bbcd70323bcf Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sun, 29 Dec 2019 11:54:36 +0100
Subject: [PATCH] fix(bhi160-app): Only open the currently displayed sensor

Don't open all 4 virtual sensors all time time; only open the one which
is actually displayed.  This prevents the storm of queue-full message on
the serial terminal.

Signed-off-by: Rahix <rahix@rahix.de>
---
 preload/apps/bhi160/__init__.py | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/preload/apps/bhi160/__init__.py b/preload/apps/bhi160/__init__.py
index c1f43250..a6de8fc2 100644
--- a/preload/apps/bhi160/__init__.py
+++ b/preload/apps/bhi160/__init__.py
@@ -14,19 +14,23 @@ STATUS_COLORS = [
     color.GREEN,
 ]
 
-with contextlib.ExitStack() as cx:
-    disp = cx.enter_context(display.open())
 
+def sensors(**args):
+    while True:
+        with bhi160.BHI160Orientation(**args) as s:
+            yield s, "Orientation"
+        with bhi160.BHI160Accelerometer(**args) as s:
+            yield s, "Accel"
+        with bhi160.BHI160Gyroscope(**args) as s:
+            yield s, "Gyro"
+        with bhi160.BHI160Magnetometer(**args) as s:
+            yield s, "Magnetic"
+
+
+with display.open() as disp:
     args = {"sample_rate": 10, "sample_buffer_len": 20}
 
-    sensor_iter = itertools.cycle(
-        [
-            (cx.enter_context(bhi160.BHI160Orientation(**args)), "Orientation"),
-            (cx.enter_context(bhi160.BHI160Accelerometer(**args)), "Accel"),
-            (cx.enter_context(bhi160.BHI160Gyroscope(**args)), "Gyro"),
-            (cx.enter_context(bhi160.BHI160Magnetometer(**args)), "Magnetic"),
-        ]
-    )
+    sensor_iter = sensors(**args)
     sensor, sensor_name = next(sensor_iter)
 
     for ev in simple_menu.button_events(timeout=0.1):
-- 
GitLab