From d601ea253938c8e7aeda1ffa185ff550fad4ac7b Mon Sep 17 00:00:00 2001
From: Sebastian Krzyszkowiak <dos@dosowisko.net>
Date: Sat, 4 Nov 2023 02:29:31 +0100
Subject: [PATCH] i2c_scanner: Show scanning being in progress

---
 python_payload/apps/i2c_scanner/__init__.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/python_payload/apps/i2c_scanner/__init__.py b/python_payload/apps/i2c_scanner/__init__.py
index 871a83fbde..5a70d8d1ba 100644
--- a/python_payload/apps/i2c_scanner/__init__.py
+++ b/python_payload/apps/i2c_scanner/__init__.py
@@ -12,12 +12,16 @@ class I2CScanner(Application):
         super().__init__(app_ctx)
         self.input = InputController()
         self.qwiic = I2C(1, freq=400000)
+        self._pending_scan = False
+
+    def scan(self):
         self._devices = self.qwiic.scan()
         print("Found Devices:")
         print(self._devices)
 
     def on_enter(self, vm):
         super().on_enter(vm)
+        self._devices = None
 
     def draw(self, ctx: Context) -> None:
         # Get the default font
@@ -38,7 +42,10 @@ class I2CScanner(Application):
         ctx.font_size = 16
         ctx.move_to(0, yStart + yOffset * 1).text("Press OK to Re-Scan")
 
-        if len(self._devices) == 0:
+        if self._devices is None:
+            ctx.move_to(0, yStart + (3 * yOffset)).text("Scanning...")
+            self._pending_scan = True
+        elif len(self._devices) == 0:
             ctx.rgb(0.7, 0.0, 0.0)
             ctx.move_to(0, yStart + (3 * yOffset)).text("No Devices Found")
         else:
@@ -56,7 +63,11 @@ class I2CScanner(Application):
         super().think(ins, delta_ms)
 
         if self.input.buttons.app.middle.pressed:
-            self._devices = self.qwiic.scan()
+            self._devices = None
+
+        if self._pending_scan and not self.vm.transitioning:
+            self.scan()
+            self._pending_scan = False
 
 
 # For running with `mpremote run`:
-- 
GitLab