From 1ca99c295ca33d759292271a11ebcd6ea908d627 Mon Sep 17 00:00:00 2001
From: Sébastien Dailly <sebastien@dailly.me>
Date: Wed, 30 Apr 2025 08:44:23 +0200
Subject: Update the screen when the pad is disconnected

---
 json_layer.py | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

(limited to 'json_layer.py')

diff --git a/json_layer.py b/json_layer.py
index 4d4c69c..91699b5 100644
--- a/json_layer.py
+++ b/json_layer.py
@@ -88,12 +88,24 @@ class Layer(object):
     """ Layer as an object.
         This class gives the property name in addition of the keymap list
     """
+
     def __init__(self, jdata):
-        self.load(jdata)
+        json_data = json.loads(jdata.strip())
+        self.load(json_data)
 
     def load(self, json_data):
-        jdata = json.loads(json_data.strip())
-        for name, keys in jdata.items():
+        """ Load the json dictionnary into the layer. The dictionnary shall be
+            either
+            - a string:   The key named will be sent: "A"
+            - a list: All the keys will be chained in a single stroke:
+              ["^", "A"]
+            - a dictionnary:  Used to create custom sequences:
+              {"seq": ["/", "W", "C", "ENTER"]}
+            - null:   The key will do nothing.
+
+            The dictionnary must be one of the operators declared above.
+        """
+        for name, keys in json_data.items():
             self.name = name
             self.keys = list(map(key_of_json, keys))
 
@@ -131,6 +143,10 @@ class JsonLayer(Module):
         if not data:
             return
 
+        if not data.connected:
+            keyboard.keymap[0].name = "Disconnected"
+            return
+
         # Nothing to parse.
         if data.in_waiting <= 0:
             return
@@ -139,7 +155,8 @@ class JsonLayer(Module):
             return
 
         try:
-            keyboard.keymap[0].load(line.decode())
+            jdata = json.loads(line.decode().strip())
+            keyboard.keymap[0].load(jdata)
         except Exception as err:
             print(err)
 
-- 
cgit v1.2.3