A truly modular keyboard
C
firmware: remote Keybits relayed through other Keybits
Nathan28e942e5d7a4
diff --git a/firmware/Core/Inc/keymap.h b/firmware/Core/Inc/keymap.hindex de3217a..952a20e 100644--- a/firmware/Core/Inc/keymap.h+++ b/firmware/Core/Inc/keymap.h@@ -1,41 +1,30 @@/*- * Keymap: which HID modifiers and key each Keybit sends, keyed by the- * Keybit's 96-bit UID. Lives in RAM, remapped over the config protocol,- * saved to one flash page just below CPU2's secure area.+ * Keymap: what each Keybit sends (table in keytable.h), saved to one+ * flash page just below CPU2's secure area.*- * A 5 ms sequencer task watches keybits[] from main.c, sends a HID report- * whenever the set of held mapped keys changes, and emits the protocol's- * KEY+/KEY-/PRESS/SIDES events.+ * A 5 ms sequencer task watches keybits[] (touching Keybits) and+ * keybit_remotes[] (Keybits relayed through others) from main.c, sends a+ * HID report whenever the held mapped keys change, and emits the+ * protocol's KEY+/KEY-/PRESS/SIDES events.*/#ifndef KEYMAP_H#define KEYMAP_H+#include <stddef.h>#include <stdint.h>#include "keybit_bus.h"-#include "keyact.h"--#define KEYMAP_MAX 32-#define UID_LEN 12+#include "keytable.h"void KEYMAP_Init(void);-/* A Keybit maps to a keyboard key or a consumer usage, never both: each- * setter replaces the other (a hold stays). 0 on success, -1 when the- * table is full. key may be 0 (mods only). */-int KEYMAP_Set(const uint8_t uid[UID_LEN], uint8_t mods, uint8_t key);-int KEYMAP_SetConsumer(const uint8_t uid[UID_LEN], uint16_t usage);-/* Hold action, see keyact.h. mods 0 and key 0 clears it. */-int KEYMAP_SetHold(const uint8_t uid[UID_LEN], uint8_t mods, uint8_t key, uint16_t ms);-/* MACRO: up to KEYACT_MACRO_MAX (mods, key) taps, replaces MAP/MAPC. */-int KEYMAP_SetMacro(const uint8_t uid[UID_LEN], const uint8_t seq[][2], uint8_t len);-/* NULL when the Keybit has no entry. */-const keyact_t *KEYMAP_Lookup(const uint8_t uid[UID_LEN]);-void KEYMAP_Unmap(const uint8_t uid[UID_LEN]);-void KEYMAP_Clear(void);/* 0 on success (also when flash already matched), -1 on a flash error. */int KEYMAP_Save(void);-/* Keybits as of the last key task run, the same set the events describe. */+/* Touching Keybits as of the last key task run, the set the events describe. */const keybit_info_t *KEYMAP_Present(int *count);+/* The i-th KEY+ line for KEYS?: touching Keybits first, then the remote+ * ones that are present. 0 once i is past the end. */+int KEYMAP_KeyLine(int i, char *out, size_t n);+#endif /* KEYMAP_H */diff --git a/firmware/Core/Inc/keytable.h b/firmware/Core/Inc/keytable.hnew file mode 100644index 0000000..0068769--- /dev/null+++ b/firmware/Core/Inc/keytable.h@@ -0,0 +1,64 @@+/*+ * The keymap table: one keyact_t per Keybit, keyed by UID. No HAL, so the+ * host tests link the real thing (keymap.c adds flash and the key task).+ *+ * Direct Keybits are known by their full 96-bit UID. Keybits relayed+ * through others only report the low 32 bits, so an entry can also be+ * keyed by those alone (MAP uid=<8hex>). A short id and a full one match+ * when the low 32 bits agree; setting a full id on a short entry upgrades+ * it in place.+ */+#ifndef KEYTABLE_H+#define KEYTABLE_H++#include <stdint.h>+#include "keyact.h"++#define KEYMAP_MAX 32+#define UID_LEN 12+#define UID_SHORT_LEN 4++/* uid little endian; with len UID_SHORT_LEN only uid[0..3] count. */+typedef struct+{+ uint8_t uid[UID_LEN];+ uint8_t len;+} keyid_t;++/* 56 bytes, seven flash doublewords. */+typedef struct+{+ uint8_t uid[UID_LEN];+ uint8_t uid_len;+ uint8_t pad;+ keyact_t act;+} map_entry_t;++/* Also the flash image: one header doubleword, then the entries. */+typedef struct+{+ uint32_t magic;+ uint16_t version;+ uint16_t count;+ map_entry_t entries[KEYMAP_MAX];+} map_image_t;++extern map_image_t keytable;++/* A Keybit maps to a key, a consumer usage or a macro: each setter+ * replaces the others (a hold stays). 0 on success, -1 when the table is+ * full. key may be 0 (mods only). */+int KEYMAP_Set(const keyid_t *id, uint8_t mods, uint8_t key);+int KEYMAP_SetConsumer(const keyid_t *id, uint16_t usage);+/* Up to KEYACT_MACRO_MAX (mods, key) taps. */+int KEYMAP_SetMacro(const keyid_t *id, const uint8_t seq[][2], uint8_t len);+/* Hold action, see keyact.h. mods 0 and key 0 clears it. */+int KEYMAP_SetHold(const keyid_t *id, uint8_t mods, uint8_t key, uint16_t ms);+/* NULL when the Keybit has no entry. */+const keyact_t *KEYMAP_Lookup(const keyid_t *id);+void KEYMAP_Unmap(const keyid_t *id);+void KEYMAP_Clear(void);+/* Full UID of an entry whose low 32 bits are low, NULL if none. */+const uint8_t *KEYMAP_FullUid(uint32_t low);++#endif /* KEYTABLE_H */diff --git a/firmware/Core/Src/keymap.c b/firmware/Core/Src/keymap.cindex 1cee3f2..0718f33 100644--- a/firmware/Core/Src/keymap.c+++ b/firmware/Core/Src/keymap.c@@ -11,37 +11,31 @@#define KEYS_PERIOD_MS 5#define MAP_MAGIC 0x50414D4BUL /* "KMAP" */-#define MAP_VERSION 4 /* 2: kind + 16-bit code, 3: hold action, 4: macros */+#define MAP_VERSION 5 /* 2: kind + code, 3: hold, 4: macros, 5: short (32-bit) UIDs */+#define SEEN_MAX 16/* Owned by main.c's Keybit code, refreshed from its own poll calls in the* main loop. The key task runs from the same loop, so no locking. */extern keybit_info_t keybits[KEYBIT_MAX];extern int keybit_count;extern volatile uint32_t keybit_changed;--/* 56 bytes, seven flash doublewords. 32 of them plus the header are- * 1800 bytes, under half the 4 KB page. */-typedef struct {- uint8_t uid[UID_LEN];- keyact_t act;- uint8_t pad[2];-} map_entry_t;--/* Flash image: one header doubleword, then the entries. */-typedef struct {- uint32_t magic;- uint16_t version;- uint16_t count;- map_entry_t entries[KEYMAP_MAX];-} map_image_t;+extern keybit_remote_t keybit_remotes[KEYBIT_REMOTE_MAX];+extern int keybit_remote_count;_Static_assert(sizeof(map_entry_t) % 8 == 0, "map entries are written as doublewords");_Static_assert(sizeof(map_image_t) <= FLASH_PAGE_SIZE, "keymap must fit its flash page");-static map_image_t map __attribute__((aligned(8)));static keybit_info_t present[KEYBIT_MAX];-static keyact_key_t present_key[KEYBIT_MAX]; /* tap/hold state, follows present[] */static int present_count;+/* Tap/hold state for every key: direct Keybits in [0, KEYBIT_MAX)+ * following present[], remote ones at KEYBIT_MAX + their (fixed) index. */+static keyact_key_t key_state[KEYBIT_MAX + KEYBIT_REMOTE_MAX];+static uint8_t remote_shown[KEYBIT_REMOTE_MAX]; /* KEY+ sent, no KEY- yet */+static uint8_t remote_down[KEYBIT_REMOTE_MAX]; /* last PRESS sent */+/* Full UIDs of Keybits seen directly, so one that moves further out can+ * still be named in full. */+static uint8_t seen[SEEN_MAX][UID_LEN];+static int seen_count;static char sides[3]; /* last SIDES sent: L, R, T present */static uint8_t keys_timer_id;@@ -63,103 +57,6 @@ static uint32_t Map_Size(const map_image_t *m)return 8 + m->count * sizeof(map_entry_t);}-static int Find(const uint8_t uid[UID_LEN])-{- for (int i = 0; i < map.count; i++)- {- if (memcmp(map.entries[i].uid, uid, UID_LEN) == 0) return i;- }- return -1;-}--/* Entry for uid, created empty if needed. NULL when the table is full. */-static keyact_t *Entry(const uint8_t uid[UID_LEN])-{- int i = Find(uid);- if (i < 0)- {- if (map.count >= KEYMAP_MAX) return NULL;- i = map.count++;- memset(&map.entries[i], 0, sizeof(map_entry_t));- memcpy(map.entries[i].uid, uid, UID_LEN);- }- return &map.entries[i].act;-}--/* A Keybit has one mapping, so setting either kind replaces the other. */-static int Put(const uint8_t uid[UID_LEN], uint8_t kind, uint8_t mods, uint16_t code)-{- keyact_t *a = Entry(uid);- if (!a) return -1;- a->kind = kind;- a->mods = mods;- a->code = code;- a->macro_len = 0; /* MAP/MAPC replace a macro too */- return 0;-}--int KEYMAP_Set(const uint8_t uid[UID_LEN], uint8_t mods, uint8_t key)-{- return Put(uid, KEYACT_KEY, mods, key);-}--int KEYMAP_SetConsumer(const uint8_t uid[UID_LEN], uint16_t usage)-{- return Put(uid, KEYACT_CONSUMER, 0, usage);-}--int KEYMAP_SetMacro(const uint8_t uid[UID_LEN], const uint8_t seq[][2], uint8_t len)-{- if (len > KEYACT_MACRO_MAX) return -1;- keyact_t *a = Entry(uid);- if (!a) return -1;- a->kind = KEYACT_MACRO;- a->mods = 0;- a->code = 0;- a->macro_len = len;- memset(a->macro, 0, sizeof(a->macro));- memcpy(a->macro, seq, (size_t)len * 2);- return 0;-}--int KEYMAP_SetHold(const uint8_t uid[UID_LEN], uint8_t mods, uint8_t key, uint16_t ms)-{- if (!mods && !key)- {- int i = Find(uid);- if (i < 0) return 0;- keyact_t *a = &map.entries[i].act;- a->hold_mods = a->hold_key = 0;- a->hold_ms = 0;- if (a->kind == KEYACT_NONE) KEYMAP_Unmap(uid); /* nothing left */- return 0;- }- keyact_t *a = Entry(uid);- if (!a) return -1;- a->hold_mods = mods;- a->hold_key = key;- a->hold_ms = ms;- return 0;-}--const keyact_t *KEYMAP_Lookup(const uint8_t uid[UID_LEN])-{- int i = Find(uid);- return i < 0 ? NULL : &map.entries[i].act;-}--void KEYMAP_Unmap(const uint8_t uid[UID_LEN])-{- int i = Find(uid);- if (i < 0) return;- map.entries[i] = map.entries[--map.count];-}--void KEYMAP_Clear(void)-{- map.count = 0;-}-const keybit_info_t *KEYMAP_Present(int *count){*count = present_count;@@ -205,10 +102,10 @@ static int Flash_Op(uint8_t erase, uint32_t addr_or_page, uint64_t data)int KEYMAP_Save(void){- map.magic = MAP_MAGIC;- map.version = MAP_VERSION;- uint32_t size = Map_Size(&map);- if (memcmp(Map_Flash(), &map, size) == 0) return 0; /* nothing changed */+ keytable.magic = MAP_MAGIC;+ keytable.version = MAP_VERSION;+ uint32_t size = Map_Size(&keytable);+ if (memcmp(Map_Flash(), &keytable, size) == 0) return 0; /* nothing changed */int err = 0;while (LL_HSEM_1StepLock(HSEM, CFG_HW_FLASH_SEMID));@@ -221,7 +118,7 @@ int KEYMAP_Save(void)if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);uint32_t addr = (uint32_t)Map_Flash();- const uint64_t *src = (const uint64_t *)↦+ const uint64_t *src = (const uint64_t *)&keytable;for (uint32_t i = 0; i < size / 8 && !err; i++){err |= Flash_Op(0, addr + 8 * i, src[i]);@@ -229,7 +126,7 @@ int KEYMAP_Save(void)HAL_FLASH_Lock();LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);- if (err || memcmp(Map_Flash(), &map, size) != 0) return -1;+ if (err || memcmp(Map_Flash(), &keytable, size) != 0) return -1;return 0;}@@ -238,11 +135,11 @@ static void Map_Load(void)const map_image_t *f = Map_Flash();if (f->magic == MAP_MAGIC && f->version == MAP_VERSION && f->count <= KEYMAP_MAX){- memcpy(&map, f, Map_Size(f));+ memcpy(&keytable, f, Map_Size(f));}else{- map.count = 0;+ keytable.count = 0;}}@@ -255,12 +152,78 @@ static const keybit_info_t *Find_Keybit(const keybit_info_t *list, int n, constreturn NULL;}-/* Diff keybits[] against what we reported last time, by UID: indexes move- * every time main.c rescans the bus. */-static void Keys_Task(void)+static uint32_t Low32(const uint8_t uid[UID_LEN])+{+ return (uint32_t)uid[0] | (uint32_t)uid[1] << 8 | (uint32_t)uid[2] << 16 | (uint32_t)uid[3] << 24;+}++/* ponytail: the oldest of SEEN_MAX sightings is forgotten first */+static void Seen(const uint8_t uid[UID_LEN])+{+ int n = seen_count < SEEN_MAX ? seen_count : SEEN_MAX;+ for (int i = 0; i < n; i++)+ {+ if (memcmp(seen[i], uid, UID_LEN) == 0) return;+ }+ memcpy(seen[seen_count % SEEN_MAX], uid, UID_LEN);+ seen_count++;+}++/* A remote Keybit's UID as the protocol prints it: all 24 hex digits when+ * the keymap or an earlier direct sighting knows them, else the 8 it+ * reports. */+static void Remote_Uid(char *out, uint32_t low)+{+ const uint8_t *full = KEYMAP_FullUid(low);+ int n = seen_count < SEEN_MAX ? seen_count : SEEN_MAX;+ for (int i = 0; !full && i < n; i++)+ {+ if (Low32(seen[i]) == low) full = seen[i];+ }+ if (full) PROTO_FormatUid(out, full);+ else snprintf(out, 9, "%08lX", (unsigned long)low);+}++/* via is the address of the touching Keybit it came through; "?" if that+ * one dropped out of the last scan. */+static void Remote_Line(char *out, size_t n, const keybit_remote_t *r)+{+ char uid[UID_LEN * 2 + 1], via[UID_LEN * 2 + 1] = "?";+ Remote_Uid(uid, r->uid);+ for (int i = 0; i < present_count; i++)+ {+ if (present[i].addr == r->via) PROTO_FormatUid(via, present[i].uid);+ }+ snprintf(out, n, "KEY+ uid=%s at=X via=%s depth=%u", uid, via, r->depth);+}++int KEYMAP_KeyLine(int i, char *out, size_t n){char uid[UID_LEN * 2 + 1];- keybit_changed = 0; /* only main-loop code sets it, no ISR */+ if (i < present_count)+ {+ PROTO_FormatUid(uid, present[i].uid);+ snprintf(out, n, "KEY+ uid=%s at=%c v=%u", uid, present[i].at, present[i].fw); /* v: Keybit reg 0x01 */+ return 1;+ }+ i -= present_count;+ for (int r = 0; r < keybit_remote_count; r++)+ {+ if (remote_shown[r] && i-- == 0)+ {+ Remote_Line(out, n, &keybit_remotes[r]);+ return 1;+ }+ }+ return 0;+}++/* Direct Keybits: diff keybits[] against what we reported last time, by+ * UID, since indexes move every time main.c rescans the bus. */+static void Direct_Keys(void)+{+ char uid[UID_LEN * 2 + 1];+ char line[PROTO_LINE_MAX];for (int i = 0; i < present_count; i++){@@ -270,25 +233,37 @@ static void Keys_Task(void)PROTO_Send("KEY- uid=%s", uid);}}++ keybit_info_t old[KEYBIT_MAX];keyact_key_t state[KEYBIT_MAX];+ int old_count = present_count;+ memcpy(old, present, sizeof(keybit_info_t) * old_count);+ present_count = keybit_count;+ memcpy(present, keybits, sizeof(keybit_info_t) * keybit_count);+for (int i = 0; i < keybit_count; i++){- const keybit_info_t *old = Find_Keybit(present, present_count, keybits[i].uid);- uint8_t was_down = old ? old->key_down : 0;+ const keybit_info_t *was = Find_Keybit(old, old_count, keybits[i].uid);PROTO_FormatUid(uid, keybits[i].uid);- if (!old) PROTO_Send("KEY+ uid=%s at=%c v=%u", uid, keybits[i].at, keybits[i].fw); /* v: Keybit reg 0x01 */- if (was_down != keybits[i].key_down)+ if (!was)+ {+ Seen(keybits[i].uid);+ KEYMAP_KeyLine(i, line, sizeof(line));+ PROTO_Send("%s", line);+ }+ if ((was ? was->key_down : 0) != keybits[i].key_down){PROTO_Send("PRESS uid=%s down=%d", uid, keybits[i].key_down);}- if (old) state[i] = present_key[old - present];+ if (was) state[i] = key_state[was - old];else memset(&state[i], 0, sizeof(state[i]));state[i].down = keybits[i].key_down;}-- present_count = keybit_count;- memcpy(present, keybits, sizeof(keybit_info_t) * keybit_count);- memcpy(present_key, state, sizeof(keyact_key_t) * keybit_count);+ memcpy(key_state, state, sizeof(keyact_key_t) * keybit_count);+ for (int i = keybit_count; i < KEYBIT_MAX; i++)+ {+ memset(&key_state[i], 0, sizeof(key_state[i])); /* free slot */+ }char now[3] = {0, 0, 0};for (int i = 0; i < present_count; i++)@@ -302,14 +277,74 @@ static void Keys_Task(void)memcpy(sides, now, sizeof(sides));PROTO_Send("SIDES L=%d R=%d T=%d", sides[0], sides[1], sides[2]);}+}++/* Remote Keybits: entries never move, so their index is their identity. */+static void Remote_Keys(void)+{+ char uid[UID_LEN * 2 + 1];+ char line[PROTO_LINE_MAX];++ for (int r = 0; r < keybit_remote_count; r++)+ {+ keybit_remote_t *k = &keybit_remotes[r];+ keyact_key_t *s = &key_state[KEYBIT_MAX + r];+ uint8_t down = k->present && k->key_down;+ if (!k->dirty && k->present == remote_shown[r] && down == remote_down[r]) continue;+ k->dirty = 0;++ Remote_Uid(uid, k->uid);+ if (k->present && !remote_shown[r])+ {+ remote_shown[r] = 1;+ memset(s, 0, sizeof(*s));+ Remote_Line(line, sizeof(line), k);+ PROTO_Send("%s", line);+ }+ if (down != remote_down[r])+ {+ remote_down[r] = down;+ PROTO_Send("PRESS uid=%s down=%d", uid, down);+ }+ if (!k->present && remote_shown[r])+ {+ remote_shown[r] = 0;+ PROTO_Send("KEY- uid=%s", uid);+ }+ s->down = down;+ }+}++static void Keys_Task(void)+{+ keybit_changed = 0; /* only main-loop code sets it, no ISR */+ Direct_Keys();+ Remote_Keys();++ /* Direct Keybits by full UID, remote ones by their low 32 bits (which+ * also finds an entry stored with the full UID). Unmapped ones get NULL+ * and send nothing. */+ static const keyact_t *acts[KEYBIT_MAX + KEYBIT_REMOTE_MAX];+ keyid_t id;+ for (int i = 0; i < KEYBIT_MAX; i++)+ {+ acts[i] = NULL;+ if (i >= present_count) continue;+ memcpy(id.uid, present[i].uid, UID_LEN);+ id.len = UID_LEN;+ acts[i] = KEYMAP_Lookup(&id);+ }+ for (int r = 0; r < keybit_remote_count; r++)+ {+ memset(&id, 0, sizeof(id));+ for (int b = 0; b < UID_SHORT_LEN; b++) id.uid[b] = (uint8_t)(keybit_remotes[r].uid >> (8 * b));+ id.len = UID_SHORT_LEN;+ acts[KEYBIT_MAX + r] = remote_shown[r] ? KEYMAP_Lookup(&id) : NULL;+ }- /* Unmapped Keybits get a NULL action and send nothing. A Keybit that- * leaves while held just drops out of the reports. */- const keyact_t *acts[KEYBIT_MAX];- for (int i = 0; i < present_count; i++) acts[i] = KEYMAP_Lookup(present[i].uid);uint8_t report[8];uint16_t consumer;- KEYACT_Step(present_key, acts, present_count, HAL_GetTick(), report, &consumer);+ KEYACT_Step(key_state, acts, KEYBIT_MAX + keybit_remote_count, HAL_GetTick(), report, &consumer);HIDSAPP_SendKeyboard(report);HIDSAPP_SendConsumer(consumer);diff --git a/firmware/Core/Src/keytable.c b/firmware/Core/Src/keytable.cnew file mode 100644index 0000000..fdc82ea--- /dev/null+++ b/firmware/Core/Src/keytable.c@@ -0,0 +1,132 @@+#include <string.h>+#include "keytable.h"++map_image_t keytable __attribute__((aligned(8)));++static int Exact(const map_entry_t *e, const keyid_t *id)+{+ return e->uid_len == id->len && memcmp(e->uid, id->uid, id->len) == 0;+}++/* Low 32 bits agree and one side only has those. */+static int Loose(const map_entry_t *e, const keyid_t *id)+{+ return (e->uid_len == UID_SHORT_LEN || id->len == UID_SHORT_LEN)+ && memcmp(e->uid, id->uid, UID_SHORT_LEN) == 0;+}++static int Find(const keyid_t *id)+{+ for (int i = 0; i < keytable.count; i++)+ {+ if (Exact(&keytable.entries[i], id)) return i;+ }+ for (int i = 0; i < keytable.count; i++)+ {+ if (Loose(&keytable.entries[i], id)) return i;+ }+ return -1;+}++/* Entry for id, created empty if needed. NULL when the table is full. */+static keyact_t *Entry(const keyid_t *id)+{+ int i = Find(id);+ if (i < 0)+ {+ if (keytable.count >= KEYMAP_MAX) return NULL;+ i = keytable.count++;+ memset(&keytable.entries[i], 0, sizeof(map_entry_t));+ }+ map_entry_t *e = &keytable.entries[i];+ if (e->uid_len != UID_LEN) /* new, or a short entry now seen in full */+ {+ memcpy(e->uid, id->uid, id->len);+ e->uid_len = id->len;+ }+ return &e->act;+}++static int Put(const keyid_t *id, uint8_t kind, uint8_t mods, uint16_t code)+{+ keyact_t *a = Entry(id);+ if (!a) return -1;+ a->kind = kind;+ a->mods = mods;+ a->code = code;+ a->macro_len = 0; /* MAP/MAPC replace a macro too */+ return 0;+}++int KEYMAP_Set(const keyid_t *id, uint8_t mods, uint8_t key)+{+ return Put(id, KEYACT_KEY, mods, key);+}++int KEYMAP_SetConsumer(const keyid_t *id, uint16_t usage)+{+ return Put(id, KEYACT_CONSUMER, 0, usage);+}++int KEYMAP_SetMacro(const keyid_t *id, const uint8_t seq[][2], uint8_t len)+{+ if (len > KEYACT_MACRO_MAX) return -1;+ keyact_t *a = Entry(id);+ if (!a) return -1;+ a->kind = KEYACT_MACRO;+ a->mods = 0;+ a->code = 0;+ a->macro_len = len;+ memset(a->macro, 0, sizeof(a->macro));+ memcpy(a->macro, seq, (size_t)len * 2);+ return 0;+}++int KEYMAP_SetHold(const keyid_t *id, uint8_t mods, uint8_t key, uint16_t ms)+{+ if (!mods && !key)+ {+ int i = Find(id);+ if (i < 0) return 0;+ keyact_t *a = &keytable.entries[i].act;+ a->hold_mods = a->hold_key = 0;+ a->hold_ms = 0;+ if (a->kind == KEYACT_NONE) KEYMAP_Unmap(id); /* nothing left */+ return 0;+ }+ keyact_t *a = Entry(id);+ if (!a) return -1;+ a->hold_mods = mods;+ a->hold_key = key;+ a->hold_ms = ms;+ return 0;+}++const keyact_t *KEYMAP_Lookup(const keyid_t *id)+{+ int i = Find(id);+ return i < 0 ? NULL : &keytable.entries[i].act;+}++void KEYMAP_Unmap(const keyid_t *id)+{+ int i = Find(id);+ if (i < 0) return;+ keytable.entries[i] = keytable.entries[--keytable.count];+}++void KEYMAP_Clear(void)+{+ keytable.count = 0;+}++const uint8_t *KEYMAP_FullUid(uint32_t low)+{+ for (int i = 0; i < keytable.count; i++)+ {+ const map_entry_t *e = &keytable.entries[i];+ uint32_t v = (uint32_t)e->uid[0] | (uint32_t)e->uid[1] << 8 | (uint32_t)e->uid[2] << 16 | (uint32_t)e->uid[3] << 24;+ if (e->uid_len == UID_LEN && v == low) return e->uid;+ }+ return NULL;+}diff --git a/firmware/Core/Src/proto.c b/firmware/Core/Src/proto.cindex bb20a20..fd21982 100644--- a/firmware/Core/Src/proto.c+++ b/firmware/Core/Src/proto.c@@ -135,19 +135,34 @@ static int Arg_Seq(const char *args, uint8_t seq[KEYACT_MACRO_MAX][2])return -1; /* more than KEYACT_MACRO_MAX */}-/* uid= is printed most significant byte first, stored little endian. */-static int Arg_Uid(const char *args, uint8_t uid[UID_LEN])+/* uid= is 24 hex digits, or 8 for a relayed Keybit known only by its low+ * 32 bits. Printed most significant byte first, stored little endian. */+static int Arg_Uid(const char *args, keyid_t *id){uint8_t be[UID_LEN];- if (Arg_Hex(args, "uid", UID_LEN * 2, be) != 0) return -1;- for (int i = 0; i < UID_LEN; i++) uid[i] = be[UID_LEN - 1 - i];+ memset(id, 0, sizeof(*id));+ if (Arg_Hex(args, "uid", UID_LEN * 2, be) == 0) id->len = UID_LEN;+ else if (Arg_Hex(args, "uid", UID_SHORT_LEN * 2, be) == 0) id->len = UID_SHORT_LEN;+ else return -1;+ for (int i = 0; i < id->len; i++) id->uid[i] = be[id->len - 1 - i];return 0;}+/* Replies echo the uid in the form it was given. */+static void Format_Id(char *out, const keyid_t *id)+{+ if (id->len == UID_LEN)+ {+ PROTO_FormatUid(out, id->uid);+ return;+ }+ snprintf(out, 9, "%02X%02X%02X%02X", id->uid[3], id->uid[2], id->uid[1], id->uid[0]);+}+static void Handle_Line(proto_port_t *port, char *cmd){char uid_s[UID_LEN * 2 + 1];- uint8_t uid[UID_LEN];+ keyid_t id;char *args = strchr(cmd, ' ');if (args) *args++ = 0;else args = cmd + strlen(cmd);@@ -178,91 +193,89 @@ static void Handle_Line(proto_port_t *port, char *cmd)}else if (strcmp(cmd, "KEYS?") == 0){- int n;- const keybit_info_t *k = KEYMAP_Present(&n);- for (int i = 0; i < n; i++)+ char line[PROTO_LINE_MAX];+ for (int i = 0; KEYMAP_KeyLine(i, line, sizeof(line)); i++){- PROTO_FormatUid(uid_s, k[i].uid);- Reply(port, "KEY+ uid=%s at=%c v=%u", uid_s, k[i].at, k[i].fw);+ Reply(port, "%s", line);}Reply(port, "KEYS END");}else if (strcmp(cmd, "MAP") == 0){uint8_t mods, key;- if (Arg_Uid(args, uid) || Arg_Hex(args, "mods", 2, &mods) || Arg_Hex(args, "key", 2, &key))+ if (Arg_Uid(args, &id) || Arg_Hex(args, "mods", 2, &mods) || Arg_Hex(args, "key", 2, &key)){Reply(port, "ERR usage: MAP uid=<24hex> mods=<2hex> key=<2hex>");return;}- if (KEYMAP_Set(uid, mods, key) != 0)+ if (KEYMAP_Set(&id, mods, key) != 0){Reply(port, "ERR keymap full");return;}- PROTO_FormatUid(uid_s, uid);+ Format_Id(uid_s, &id);Reply(port, "OK MAP uid=%s", uid_s);}else if (strcmp(cmd, "MAPC") == 0){uint8_t usage[2];- if (Arg_Uid(args, uid) || Arg_Hex(args, "usage", 4, usage))+ if (Arg_Uid(args, &id) || Arg_Hex(args, "usage", 4, usage)){Reply(port, "ERR usage: MAPC uid=<24hex> usage=<4hex>");return;}- if (KEYMAP_SetConsumer(uid, (uint16_t)(usage[0] << 8 | usage[1])) != 0)+ if (KEYMAP_SetConsumer(&id, (uint16_t)(usage[0] << 8 | usage[1])) != 0){Reply(port, "ERR keymap full");return;}- PROTO_FormatUid(uid_s, uid);+ Format_Id(uid_s, &id);Reply(port, "OK MAPC uid=%s", uid_s);}else if (strcmp(cmd, "MACRO") == 0){uint8_t seq[KEYACT_MACRO_MAX][2];int n = -1;- if (Arg_Uid(args, uid) || (n = Arg_Seq(args, seq)) < 0)+ if (Arg_Uid(args, &id) || (n = Arg_Seq(args, seq)) < 0){Reply(port, "ERR usage: MACRO uid=<24hex> seq=<MMKK,...> (1-%d taps)", KEYACT_MACRO_MAX);return;}- if (KEYMAP_SetMacro(uid, (const uint8_t (*)[2])seq, (uint8_t)n) != 0)+ if (KEYMAP_SetMacro(&id, (const uint8_t (*)[2])seq, (uint8_t)n) != 0){Reply(port, "ERR keymap full");return;}- PROTO_FormatUid(uid_s, uid);+ Format_Id(uid_s, &id);Reply(port, "OK MACRO uid=%s", uid_s);}else if (strcmp(cmd, "HOLD") == 0){uint8_t mods, key;uint16_t ms = KEYACT_HOLD_MS_DEF;- if (Arg_Uid(args, uid) || Arg_Hex(args, "mods", 2, &mods) || Arg_Hex(args, "key", 2, &key)+ if (Arg_Uid(args, &id) || Arg_Hex(args, "mods", 2, &mods) || Arg_Hex(args, "key", 2, &key)|| (strstr(args, "ms=") && Arg_Dec(args, "ms", &ms))){Reply(port, "ERR usage: HOLD uid=<24hex> mods=<2hex> key=<2hex> ms=<dec>");return;}- if (KEYMAP_SetHold(uid, mods, key, ms) != 0)+ if (KEYMAP_SetHold(&id, mods, key, ms) != 0){Reply(port, "ERR keymap full");return;}- PROTO_FormatUid(uid_s, uid);+ Format_Id(uid_s, &id);Reply(port, "OK HOLD uid=%s", uid_s);}else if (strcmp(cmd, "MAP?") == 0){- if (Arg_Uid(args, uid))+ if (Arg_Uid(args, &id)){Reply(port, "ERR usage: MAP? uid=<24hex>");return;}- PROTO_FormatUid(uid_s, uid);- const keyact_t *a = KEYMAP_Lookup(uid);+ Format_Id(uid_s, &id);+ const keyact_t *a = KEYMAP_Lookup(&id);if (a && a->kind == KEYACT_CONSUMER){Reply(port, "MAPC uid=%s usage=%04X", uid_s, a->code);@@ -291,13 +304,13 @@ static void Handle_Line(proto_port_t *port, char *cmd)}else if (strcmp(cmd, "UNMAP") == 0){- if (Arg_Uid(args, uid))+ if (Arg_Uid(args, &id)){Reply(port, "ERR usage: UNMAP uid=<24hex>");return;}- KEYMAP_Unmap(uid);- PROTO_FormatUid(uid_s, uid);+ KEYMAP_Unmap(&id);+ Format_Id(uid_s, &id);Reply(port, "OK UNMAP uid=%s", uid_s);}else if (strcmp(cmd, "CLEAR") == 0)diff --git a/firmware/README.md b/firmware/README.mdindex 4e52112..133af4a 100644--- a/firmware/README.md+++ b/firmware/README.md@@ -68,7 +68,11 @@ an unmapped Keybit sends nothing. The map is set with the line protocol ineither the Nordic UART Service (6e400001-b5a3-f393-e0a9-e50e24dcca9e) or USB.A Keybit can also be a media key (`MAPC`), type a short macro (`MACRO`, up to16 taps), or do one thing on a tap and another while held (`HOLD`, like Escape-on tap and Ctrl on hold).+on tap and Ctrl on hold). Keybits further out, relayed through the ones+touching the keyword, work too. They only report the low 32 bits of their+UID, so they can be mapped with an 8 digit `uid=`. `BATT?` reads the+BQ25895's ADC for the battery level, which also goes to the BLE Battery+Service.`SAVE` writes it to the flash page just under CPU2's area (0x080CF000 with thefull stack).@@ -88,8 +92,9 @@ screen /dev/tty.usbmodem* 115200 # then type HELLO and enterThe parser and the tap/hold/macro logic have host checks:```sh-cc -std=c99 -Itest/stub -ICore/Inc test/proto_test.c Core/Src/proto.c -o /tmp/proto_test && /tmp/proto_test+cc -std=c99 -Itest/stub -ICore/Inc test/proto_test.c Core/Src/proto.c Core/Src/keytable.c -o /tmp/proto_test && /tmp/proto_testcc -std=c99 -ICore/Inc test/keyact_test.c Core/Src/keyact.c -o /tmp/keyact_test && /tmp/keyact_test+cc -std=c99 -Itest/stub -ICore/Inc test/batt_test.c Core/Src/batt.c -o /tmp/batt_test && /tmp/batt_test```### First flash: the CPU2 BLE stackdiff --git a/firmware/STM32Make.make b/firmware/STM32Make.makeindex 953b523..c8fd494 100644--- a/firmware/STM32Make.make+++ b/firmware/STM32Make.make@@ -106,6 +106,7 @@ Core/Src/batt.c \Core/Src/hw_timerserver.c \Core/Src/keyact.c \Core/Src/keymap.c \+Core/Src/keytable.c \Core/Src/proto.c \Core/Src/stm32_lpm_if.c \Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_rtc.c \diff --git a/firmware/test/proto_test.c b/firmware/test/proto_test.cindex 2ee08b8..ec66edf 100644--- a/firmware/test/proto_test.c+++ b/firmware/test/proto_test.c@@ -1,7 +1,8 @@/** Host check for the config protocol parser in Core/Src/proto.c, with the- * keymap and the two transports (NUS, USB CDC) replaced by small fakes.- * cc -std=c99 -Itest/stub -ICore/Inc test/proto_test.c Core/Src/proto.c -o /tmp/proto_test && /tmp/proto_test+ * real keymap table (keytable.c), and the Keybit bus, battery and the two+ * transports (NUS, USB CDC) replaced by small fakes.+ * cc -std=c99 -Itest/stub -ICore/Inc test/proto_test.c Core/Src/proto.c Core/Src/keytable.c -o /tmp/proto_test && /tmp/proto_test*/#include <assert.h>#include <stdio.h>@@ -17,62 +18,30 @@ static void write2(const char *data, uint16_t len) { strncat(out2, data, len); }static proto_port_t port = {.write = write1};static proto_port_t port2 = {.write = write2};-static struct { uint8_t uid[UID_LEN]; keyact_t act; } table[KEYMAP_MAX];-static int n;+/* The real keytable.c is linked; the bus side of keymap.c is faked. */static keybit_info_t present[2];--static int find(const uint8_t *uid)-{- for (int i = 0; i < n; i++) if (!memcmp(table[i].uid, uid, UID_LEN)) return i;- return -1;-}-static keyact_t *entry(const uint8_t *uid)-{- int i = find(uid);- if (i < 0) { if (n == KEYMAP_MAX) return NULL; i = n++; memset(&table[i], 0, sizeof(table[i])); memcpy(table[i].uid, uid, UID_LEN); }- return &table[i].act;-}-static int put(const uint8_t *uid, uint8_t kind, uint8_t mods, uint16_t code)-{- keyact_t *a = entry(uid);- if (!a) return -1;- a->kind = kind; a->mods = mods; a->code = code; a->macro_len = 0;- return 0;-}-int KEYMAP_Set(const uint8_t *uid, uint8_t mods, uint8_t key) { return put(uid, KEYACT_KEY, mods, key); }-int KEYMAP_SetConsumer(const uint8_t *uid, uint16_t usage) { return put(uid, KEYACT_CONSUMER, 0, usage); }-void KEYMAP_Unmap(const uint8_t *uid);-int KEYMAP_SetHold(const uint8_t *uid, uint8_t mods, uint8_t key, uint16_t ms)-{- if (!mods && !key)- {- int i = find(uid);- if (i < 0) return 0;- table[i].act.hold_mods = table[i].act.hold_key = 0; table[i].act.hold_ms = 0;- if (table[i].act.kind == KEYACT_NONE) KEYMAP_Unmap(uid);- return 0;- }- keyact_t *a = entry(uid);- if (!a) return -1;- a->hold_mods = mods; a->hold_key = key; a->hold_ms = ms;- return 0;-}-int KEYMAP_SetMacro(const uint8_t *uid, const uint8_t seq[][2], uint8_t len)-{- keyact_t *a = entry(uid);- if (!a) return -1;- a->kind = KEYACT_MACRO; a->mods = 0; a->code = 0; a->macro_len = len;- memcpy(a->macro, seq, (size_t)len * 2);- return 0;-}+static int remote_listed;const uint8_t keybit_image_version = 3;static batt_t batt_fake;const batt_t *BATT_Get(void) { return &batt_fake; }-const keyact_t *KEYMAP_Lookup(const uint8_t *uid) { int i = find(uid); return i < 0 ? NULL : &table[i].act; }-void KEYMAP_Unmap(const uint8_t *uid) { int i = find(uid); if (i >= 0) table[i] = table[--n]; }-void KEYMAP_Clear(void) { n = 0; }int KEYMAP_Save(void) { return 0; }const keybit_info_t *KEYMAP_Present(int *count) { *count = 2; return present; }+int KEYMAP_KeyLine(int i, char *out, size_t len)+{+ char uid[25];+ if (i < 2)+ {+ PROTO_FormatUid(uid, present[i].uid);+ snprintf(out, len, "KEY+ uid=%s at=%c v=%u", uid, present[i].at, present[i].fw);+ return 1;+ }+ if (i == 2 && remote_listed)+ {+ snprintf(out, len, "KEY+ uid=1234ABCD at=X via=%s depth=2", "0102030405060708090A0B0C");+ return 1;+ }+ return 0;+}static void expect(const char *in, const char *want){@@ -105,7 +74,7 @@ int main(void)expect("KEYS?\n", "KEY+ uid=" U " at=L v=2\nKEY+ uid=ABABABABABABABABABABABAB at=T v=0\nKEYS END\n");expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=00\n");expect("MAP uid=" U " mods=02 key=04\n", "OK MAP uid=" U "\n");- assert(table[0].uid[0] == 0x0C && table[0].uid[11] == 0x01); /* stored little endian */+ assert(keytable.entries[0].uid[0] == 0x0C && keytable.entries[0].uid[11] == 0x01); /* stored little endian */expect("MAP? uid=0102030405060708090a0b0c\n", "MAP uid=" U " mods=02 key=04\n");expect("MAP uid=" U " mods=01 key=00\n", "OK MAP uid=" U "\n");expect("MAP? uid=" U "\n", "MAP uid=" U " mods=01 key=00\n");@@ -114,20 +83,48 @@ int main(void)/* consumer keys: MAPC and MAP replace each other */expect("MAPC uid=" U " usage=00CD\n", "OK MAPC uid=" U "\n");- assert(table[0].act.kind == KEYACT_CONSUMER && table[0].act.code == 0x00CD);+ assert(keytable.entries[0].act.kind == KEYACT_CONSUMER && keytable.entries[0].act.code == 0x00CD);expect("MAP? uid=" U "\n", "MAPC uid=" U " usage=00CD\n");expect("MAP uid=" U " mods=00 key=04\n", "OK MAP uid=" U "\n");expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=04\n");expect("MAPC uid=" U " usage=00e9\n", "OK MAPC uid=" U "\n");expect("MAP? uid=" U "\n", "MAPC uid=" U " usage=00E9\n");- assert(n == 1);+ assert(keytable.count == 1);expect("UNMAP uid=" U "\n", "OK UNMAP uid=" U "\n");expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=00\n");expect("MAPC uid=" U " usage=E2\n", "ERR usage: MAPC uid=<24hex> usage=<4hex>\n");expect("MAPC uid=" U "\n", "ERR usage: MAPC uid=<24hex> usage=<4hex>\n");expect("MAPC uid=" U " usage=00E2\n", "OK MAPC uid=" U "\n");expect("CLEAR\n", "OK CLEAR\n");- assert(n == 0);+ assert(keytable.count == 0);++ /* Relayed Keybits: 8 hex digits, the low 32 bits of the UID */+ remote_listed = 1;+ expect("KEYS?\n", "KEY+ uid=" U " at=L v=2\nKEY+ uid=ABABABABABABABABABABABAB at=T v=0\n"+ "KEY+ uid=1234ABCD at=X via=" U " depth=2\nKEYS END\n");+ remote_listed = 0;+ expect("MAP uid=090a0b0c mods=00 key=04\n", "OK MAP uid=090A0B0C\n");+ assert(keytable.count == 1 && keytable.entries[0].uid_len == UID_SHORT_LEN && keytable.entries[0].uid[0] == 0x0C);+ expect("MAP? uid=090A0B0C\n", "MAP uid=090A0B0C mods=00 key=04\n");+ /* U's low 32 bits are 090A0B0C: the full UID finds the short entry */+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=04\n");+ assert(KEYMAP_FullUid(0x090A0B0C) == NULL);+ /* and mapping it in full upgrades that entry instead of adding one */+ expect("MAP uid=" U " mods=00 key=05\n", "OK MAP uid=" U "\n");+ assert(keytable.count == 1 && keytable.entries[0].uid_len == UID_LEN);+ assert(KEYMAP_FullUid(0x090A0B0C) == keytable.entries[0].uid);+ expect("MAP? uid=090A0B0C\n", "MAP uid=090A0B0C mods=00 key=05\n"); /* short still finds it */+ /* another full UID with the same low 32 bits is a different Keybit */+ expect("MAP? uid=FFFFFFFFFFFFFFFF090A0B0C\n", "MAP uid=FFFFFFFFFFFFFFFF090A0B0C mods=00 key=00\n");+ expect("MAPC uid=090A0B0C usage=00CD\n", "OK MAPC uid=090A0B0C\n"); /* edits the same entry */+ expect("MAP? uid=" U "\n", "MAPC uid=" U " usage=00CD\n");+ expect("UNMAP uid=090A0B0C\n", "OK UNMAP uid=090A0B0C\n");+ assert(keytable.count == 0);+ expect("HOLD uid=1234ABCD mods=02 key=00\n", "OK HOLD uid=1234ABCD\n");+ expect("MACRO uid=1234ABCD seq=0004\n", "OK MACRO uid=1234ABCD\n");+ expect("MAP? uid=1234ABCD\n", "MACRO uid=1234ABCD seq=0004\nHOLD uid=1234ABCD mods=02 key=00 ms=200\n");+ expect("MAP uid=1234ABCDEF mods=00 key=04\n", "ERR usage: MAP uid=<24hex> mods=<2hex> key=<2hex>\n");+ expect("CLEAR\n", "OK CLEAR\n");/* HOLD: tap does the MAP action, hold does this one */expect("HOLD uid=" U " mods=02 key=00 ms=250\n", "OK HOLD uid=" U "\n");@@ -146,15 +143,15 @@ int main(void)expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=00\n");expect("HOLD uid=" U " mods=02 key=00\n", "OK HOLD uid=" U "\n");expect("HOLD uid=" U " mods=00 key=00\n", "OK HOLD uid=" U "\n");- assert(n == 0); /* a hold-only entry goes away when cleared */+ assert(keytable.count == 0); /* a hold-only entry goes away when cleared */expect("HOLD uid=" U " mods=02 key=00\n", "OK HOLD uid=" U "\n");expect("CLEAR\n", "OK CLEAR\n");- assert(n == 0);+ assert(keytable.count == 0);/* MACRO: replaces MAP/MAPC, MAP? prints it back, UNMAP clears it */expect("MACRO uid=" U " seq=020B,0008,000F,000F,0012\n", "OK MACRO uid=" U "\n"); /* "Hello" */- assert(table[0].act.kind == KEYACT_MACRO && table[0].act.macro_len == 5);- assert(table[0].act.macro[0][0] == 0x02 && table[0].act.macro[0][1] == 0x0B);+ assert(keytable.entries[0].act.kind == KEYACT_MACRO && keytable.entries[0].act.macro_len == 5);+ assert(keytable.entries[0].act.macro[0][0] == 0x02 && keytable.entries[0].act.macro[0][1] == 0x0B);expect("MAP? uid=" U "\n", "MACRO uid=" U " seq=020B,0008,000F,000F,0012\n");expect("MACRO uid=" U " seq=0004\n", "OK MACRO uid=" U "\n");expect("MAP? uid=" U "\n", "MACRO uid=" U " seq=0004\n");@@ -194,7 +191,7 @@ int main(void)expect("BOGUS\n", "ERR unknown command\n");expect("\n", "");- n = KEYMAP_MAX;+ keytable.count = KEYMAP_MAX;expect("MAP uid=FFFFFFFFFFFFFFFFFFFFFFFF mods=00 key=04\n", "ERR keymap full\n");expect("MAPC uid=FFFFFFFFFFFFFFFFFFFFFFFF usage=00CD\n", "ERR keymap full\n");