From f02f89e26e6fb9fa81fa59c0bba0cb7d83a3c9e4 Mon Sep 17 00:00:00 2001
From: Timon <timon@diodes-delight.com>
Date: Thu, 2 Nov 2023 20:45:24 +0000
Subject: [PATCH] add default pins for I2C1 and I2C scanner app

---
 .../micropython/include/mpconfigboard.h       |  4 +-
 python_payload/apps/i2c_scanner/__init__.py   | 48 +++++++++++++++++++
 python_payload/apps/i2c_scanner/flow3r.toml   | 11 +++++
 3 files changed, 61 insertions(+), 2 deletions(-)
 create mode 100644 python_payload/apps/i2c_scanner/__init__.py
 create mode 100644 python_payload/apps/i2c_scanner/flow3r.toml

diff --git a/components/micropython/include/mpconfigboard.h b/components/micropython/include/mpconfigboard.h
index f3a24752ce..933be9981a 100644
--- a/components/micropython/include/mpconfigboard.h
+++ b/components/micropython/include/mpconfigboard.h
@@ -4,8 +4,8 @@
 #define MICROPY_ENABLE_FINALISER (1)
 #define MICROPY_PY_MACHINE_DAC (0)
 #define MICROPY_PY_MACHINE_PWM (1)
-#define MICROPY_HW_I2C0_SCL (9)
-#define MICROPY_HW_I2C0_SDA (8)
+#define MICROPY_HW_I2C1_SCL (45)
+#define MICROPY_HW_I2C1_SDA (17)
 #define MICROPY_HW_ESP32S3_EXTENDED_IO (0)
 
 #define MICROPY_ESP_IDF_4 1
diff --git a/python_payload/apps/i2c_scanner/__init__.py b/python_payload/apps/i2c_scanner/__init__.py
new file mode 100644
index 0000000000..e8bad7490b
--- /dev/null
+++ b/python_payload/apps/i2c_scanner/__init__.py
@@ -0,0 +1,48 @@
+# micropython imports
+from machine import I2C, Pin
+
+# flow3r imports
+from st3m.application import Application, ApplicationContext
+from ctx import Context
+from st3m.input import InputController, InputState
+
+
+class I2CScanner(Application):
+    def __init__(self, app_ctx: ApplicationContext) -> None:
+        super().__init__(app_ctx)
+        self.input = InputController()
+        self.qwiic = I2C(1, freq=400000)
+        self._devices = self.qwiic.scan()
+        print("Found Devices:")
+        print(self._devices)
+
+    def on_enter(self, vm):
+        super().on_enter(vm)
+
+    def draw(self, ctx: Context) -> None:
+        # Get the default font
+        ctx.font = ctx.get_font_name(1)
+        # Draw a black background
+        ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
+
+        ctx.font_size = 16
+
+        ctx.rgb(0.7, 0.7, 0.7)
+        ctx.text_align = ctx.MIDDLE
+        ctx.move_to(0, -62).text("Press OK to Re-Scan")
+        ctx.move_to(0, -42).text("Found Devices:")
+        for i, d in enumerate(self._devices):
+            ctx.move_to(0, -24 + (i * 12)).text(hex(d))
+
+    def think(self, ins: InputState, delta_ms: int) -> None:
+        super().think(ins, delta_ms)
+
+        if self.input.buttons.app.middle.pressed:
+            self._devices = self.qwiic.scan()
+
+
+# For running with `mpremote run`:
+if __name__ == "__main__":
+    import st3m.run
+
+    st3m.run.run_app(I2CScanner)
diff --git a/python_payload/apps/i2c_scanner/flow3r.toml b/python_payload/apps/i2c_scanner/flow3r.toml
new file mode 100644
index 0000000000..f0b976f32d
--- /dev/null
+++ b/python_payload/apps/i2c_scanner/flow3r.toml
@@ -0,0 +1,11 @@
+[app]
+name = "I2C/Qwiic Scanner"
+category = "Apps"
+
+[entry]
+class = "I2CScanner"
+
+[metadata]
+author = "Flow3r Badge Authors"
+license = "LGPL-3.0-only"
+url = "https://git.flow3r.garden/flow3r/flow3r-firmware"
-- 
GitLab