From 1db3a1310d7b7604cca598f410a99d78defc04a7 Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Tue, 22 Dec 2020 02:58:08 +0100
Subject: [PATCH] feat(hid): Improve demo app

---
 preload/apps/hid.py | 121 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 113 insertions(+), 8 deletions(-)

diff --git a/preload/apps/hid.py b/preload/apps/hid.py
index 25a134ba5..f4e9e9465 100644
--- a/preload/apps/hid.py
+++ b/preload/apps/hid.py
@@ -2,17 +2,122 @@ import buttons
 import color
 import display
 import ble_hid
+import bhi160
+
+from adafruit_hid.keyboard import Keyboard
+from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
+from adafruit_hid.keycode import Keycode
+
+from adafruit_hid.mouse import Mouse
+
 from adafruit_hid.consumer_control_code import ConsumerControlCode
 from adafruit_hid.consumer_control import ConsumerControl
 
 
+import time
+
+
+def keyboard_demo():
+    disp = display.open()
+    disp.clear()
+    disp.print("  card10", posy=0)
+    disp.print(" Keyboard", posy=20)
+    disp.print("F19", posy=60, fg=color.BLUE)
+    disp.print("Backspace", posy=40, posx=20, fg=color.RED)
+    disp.print("Type", posy=60, posx=100, fg=color.GREEN)
+    disp.update()
+
+    k = Keyboard(ble_hid.devices)
+    kl = KeyboardLayoutUS(k)
+
+    b_old = buttons.read()
+    while True:
+        b_new = buttons.read()
+        if not b_old == b_new:
+            print(b_new)
+            b_old = b_new
+            if b_new == buttons.TOP_RIGHT:
+                # print("back")  # for debug in REPL
+                k.send(Keycode.BACKSPACE)
+
+            elif b_new == buttons.BOTTOM_RIGHT:
+                # use keyboard_layout for words
+                kl.write("Demo with a long text to show how fast a card10 can type!")
+
+            elif b_new == buttons.BOTTOM_LEFT:
+                # add shift modifier
+                k.send(Keycode.SHIFT, Keycode.F19)
+
+
+def mouse_demo():
+    disp = display.open()
+    disp.clear()
+    disp.print("  card10", posy=0)
+    disp.print("  Mouse", posy=20)
+    disp.print("Left", posy=60, fg=color.BLUE)
+    disp.print("Midd", posy=40, posx=100, fg=color.RED)
+    disp.print("Right", posy=60, posx=80, fg=color.GREEN)
+    disp.update()
+
+    m = Mouse(ble_hid.devices)
+
+    def send_report(samples):
+        if len(samples) > 0:
+            x = -int(samples[0].z)
+            y = -int(samples[0].y)
+            print("Reporting", x, y)
+
+            m.move(x, y)
+
+    sensor = bhi160.BHI160Orientation(sample_rate=10, callback=send_report)
+
+    b_old = buttons.read()
+    while True:
+        b_new = buttons.read()
+        if not b_old == b_new:
+            print(b_new)
+            b_old = b_new
+            if b_new == buttons.TOP_RIGHT:
+                m.click(Mouse.MIDDLE_BUTTON)
+            elif b_new == buttons.BOTTOM_RIGHT:
+                m.click(Mouse.RIGHT_BUTTON)
+            elif b_new == buttons.BOTTOM_LEFT:
+                m.click(Mouse.LEFT_BUTTON)
+
+
+def control_demo():
+    disp = display.open()
+    disp.clear()
+    disp.print("  card10", posy=0)
+    disp.print("  Control", posy=20)
+    disp.print("Play", posy=60, fg=color.BLUE)
+    disp.print("Vol+", posy=40, posx=100, fg=color.RED)
+    disp.print("Vol-", posy=60, posx=100, fg=color.GREEN)
+    disp.update()
+
+    cc = ConsumerControl(ble_hid.devices)
+
+    b_old = buttons.read()
+    while True:
+        b_new = buttons.read()
+        if not b_old == b_new:
+            print(b_new)
+            b_old = b_new
+            if b_new == buttons.TOP_RIGHT:
+                cc.send(ConsumerControlCode.VOLUME_INCREMENT)
+            elif b_new == buttons.BOTTOM_RIGHT:
+                cc.send(ConsumerControlCode.VOLUME_DECREMENT)
+            elif b_new == buttons.BOTTOM_LEFT:
+                cc.send(ConsumerControlCode.PLAY_PAUSE)
+
+
 disp = display.open()
 disp.clear()
-disp.print("  card10", posy=0)
-disp.print("  Remote", posy=20)
-disp.print("Play", posy=60, fg=color.BLUE)
-disp.print("Vol+", posy=40, posx=100, fg=color.RED)
-disp.print("Vol-", posy=60, posx=100, fg=color.GREEN)
+disp.print("card10 HID", posy=0)
+disp.print("   Demo", posy=20)
+disp.print("KBD", posy=60, fg=color.BLUE)
+disp.print("Mouse", posy=40, posx=80, fg=color.RED)
+disp.print("Control", posy=60, posx=60, fg=color.GREEN)
 disp.update()
 
 cc = ConsumerControl(ble_hid.devices)
@@ -24,8 +129,8 @@ while True:
         print(b_new)
         b_old = b_new
         if b_new == buttons.TOP_RIGHT:
-            cc.send(ConsumerControlCode.VOLUME_INCREMENT)
+            mouse_demo()
         elif b_new == buttons.BOTTOM_RIGHT:
-            cc.send(ConsumerControlCode.VOLUME_DECREMENT)
+            control_demo()
         elif b_new == buttons.BOTTOM_LEFT:
-            cc.send(ConsumerControlCode.PLAY_PAUSE)
+            keyboard_demo()
-- 
GitLab