A truly modular keyboard
C
firmware: HOLD, tap for the mapped key and hold for another
Nathan98f03e6ca8da
diff --git a/firmware/Core/Inc/keyact.h b/firmware/Core/Inc/keyact.hnew file mode 100644index 0000000..5a65362--- /dev/null+++ b/firmware/Core/Inc/keyact.h@@ -0,0 +1,49 @@+/*+ * What a Keybit does when pressed, and the state machine that turns held+ * keys into HID reports. No HAL in here so it runs in the host test+ * (test/keyact_test.c).+ */+#ifndef KEYACT_H+#define KEYACT_H++#include <stdint.h>++#define KEYACT_TAP_MS 10 /* how long a tap stays in the reports */+#define KEYACT_HOLD_MS_DEF 200++enum { KEYACT_NONE = 0, KEYACT_KEY = 1, KEYACT_CONSUMER = 2 };++/* Stored per Keybit in the keymap (and in flash, so keep it 4-byte sized).+ * code is the keyboard usage for KEYACT_KEY and the consumer usage for+ * KEYACT_CONSUMER. With a hold set (hold_mods or hold_key non zero) a+ * press shorter than hold_ms fires the normal action as a tap on release,+ * a longer one holds hold_mods/hold_key down until release. */+typedef struct+{+ uint8_t kind;+ uint8_t mods;+ uint16_t code;+ uint8_t hold_mods;+ uint8_t hold_key;+ uint16_t hold_ms;+} keyact_t;++/* Per key runtime state, kept by the caller across calls (by UID). The+ * caller only writes down; zero it when a key first appears. */+typedef struct+{+ uint8_t down;+ uint8_t was;+ uint8_t state;+ uint32_t seq;+ uint32_t t0;+} keyact_key_t;++/* One step: keys[i] is driven by acts[i] (NULL when unmapped). Builds the+ * 8 byte keyboard report and the consumer usage to send now. */+void KEYACT_Step(keyact_key_t *keys, const keyact_t *const *acts, int n, uint32_t now,+ uint8_t report[8], uint16_t *consumer);++void KEYACT_Reset(void);++#endif /* KEYACT_H */diff --git a/firmware/Core/Inc/keymap.h b/firmware/Core/Inc/keymap.hindex d132651..d591933 100644--- a/firmware/Core/Inc/keymap.h+++ b/firmware/Core/Inc/keymap.h@@ -12,22 +12,22 @@#include <stdint.h>#include "keybit_bus.h"+#include "keyact.h"#define KEYMAP_MAX 32#define UID_LEN 12void KEYMAP_Init(void);-enum { KEYMAP_NONE = 0, KEYMAP_KEY = 1, KEYMAP_CONSUMER = 2 };-/* A Keybit maps to a keyboard key or a consumer usage, never both: each- * setter replaces whatever was there. 0 on success, -1 when the table is- * full. key may be 0 (mods only). */+ * 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);-/* Returns the kind. code is the keyboard usage or the consumer usage,- * mods and code are 0 when unmapped. */-int KEYMAP_Get(const uint8_t uid[UID_LEN], uint8_t *mods, uint16_t *code);+/* 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);+/* 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. */diff --git a/firmware/Core/Src/keyact.c b/firmware/Core/Src/keyact.cnew file mode 100644index 0000000..ff3ec41--- /dev/null+++ b/firmware/Core/Src/keyact.c@@ -0,0 +1,121 @@+#include <string.h>+#include "keyact.h"++enum { ST_IDLE, ST_ACTIVE, ST_PENDING, ST_HOLDING };++#define PULSES 4++/* Taps fired on release: a key or usage that stays in the reports for+ * KEYACT_TAP_MS and then goes away on its own. */+typedef struct+{+ uint8_t used;+ uint8_t mods;+ uint8_t key;+ uint16_t usage;+ uint32_t until;+} pulse_t;++static pulse_t pulses[PULSES];+static uint32_t press_counter;++void KEYACT_Reset(void)+{+ memset(pulses, 0, sizeof(pulses));+ press_counter = 0;+}++static int Has_Hold(const keyact_t *a)+{+ return a && (a->hold_mods || a->hold_key);+}++static void Pulse(const keyact_t *a, uint32_t now)+{+ if (!a || a->kind == KEYACT_NONE) return;+ /* reuse the oldest slot if all are busy */+ pulse_t *p = &pulses[0];+ for (int i = 0; i < PULSES; i++)+ {+ if (!pulses[i].used) { p = &pulses[i]; break; }+ if ((int32_t)(pulses[i].until - p->until) < 0) p = &pulses[i];+ }+ p->used = 1;+ p->mods = a->kind == KEYACT_KEY ? a->mods : 0;+ p->key = a->kind == KEYACT_KEY ? (uint8_t)a->code : 0;+ p->usage = a->kind == KEYACT_CONSUMER ? a->code : 0;+ p->until = now + KEYACT_TAP_MS;+}++/* 6KRO: modifiers OR together, the first six keys fill the slots and a+ * seventh turns every slot into ErrorRollOver as HID asks. */+static void Add(uint8_t report[8], int *slots, uint8_t mods, uint8_t key)+{+ report[0] |= mods;+ if (!key) return;+ for (int s = 0; s < 6 && s < *slots; s++)+ {+ if (report[2 + s] == key) return;+ }+ if (*slots < 6) report[2 + *slots] = key;+ else memset(&report[2], 0x01, 6);+ (*slots)++;+}++void KEYACT_Step(keyact_key_t *keys, const keyact_t *const *acts, int n, uint32_t now,+ uint8_t report[8], uint16_t *consumer)+{+ int slots = 0;+ uint32_t consumer_seq = 0;+ memset(report, 0, 8);+ *consumer = 0;++ for (int i = 0; i < n; i++)+ {+ keyact_key_t *k = &keys[i];+ const keyact_t *a = acts[i];++ if (k->down && !k->was)+ {+ k->t0 = now;+ k->seq = ++press_counter;+ k->state = Has_Hold(a) ? ST_PENDING : ST_ACTIVE;+ }+ if (k->down && k->state == ST_PENDING+ && (uint32_t)(now - k->t0) >= (a->hold_ms ? a->hold_ms : KEYACT_HOLD_MS_DEF))+ {+ k->state = ST_HOLDING;+ }+ if (!k->down && k->was)+ {+ if (k->state == ST_PENDING) Pulse(a, now); /* short press: tap */+ k->state = ST_IDLE;+ }+ k->was = k->down;++ if (!k->down || !a) continue;+ if (k->state == ST_HOLDING)+ {+ Add(report, &slots, a->hold_mods, a->hold_key);+ }+ else if (k->state == ST_ACTIVE)+ {+ if (a->kind == KEYACT_KEY) Add(report, &slots, a->mods, (uint8_t)a->code);+ /* most recently pressed consumer key wins */+ if (a->kind == KEYACT_CONSUMER && k->seq > consumer_seq)+ {+ consumer_seq = k->seq;+ *consumer = a->code;+ }+ }+ }++ for (int i = 0; i < PULSES; i++)+ {+ pulse_t *p = &pulses[i];+ if (!p->used) continue;+ if ((int32_t)(now - p->until) >= 0) { p->used = 0; continue; }+ Add(report, &slots, p->mods, p->key);+ if (p->usage) *consumer = p->usage;+ }+}diff --git a/firmware/Core/Src/keymap.c b/firmware/Core/Src/keymap.cindex 6fd5a11..fc38b6b 100644--- a/firmware/Core/Src/keymap.c+++ b/firmware/Core/Src/keymap.c@@ -10,7 +10,7 @@#define KEYS_PERIOD_MS 5#define MAP_MAGIC 0x50414D4BUL /* "KMAP" */-#define MAP_VERSION 2 /* 2: entries carry a kind and a 16-bit code */+#define MAP_VERSION 3 /* 2: kind + 16-bit code, 3: hold action *//* 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. */@@ -18,13 +18,11 @@ extern keybit_info_t keybits[KEYBIT_MAX];extern int keybit_count;extern volatile uint32_t keybit_changed;-/* 16 bytes, two flash doublewords. code is the keyboard usage for- * KEYMAP_KEY and the consumer page usage for KEYMAP_CONSUMER. */+/* 24 bytes, three flash doublewords. */typedef struct {uint8_t uid[UID_LEN];- uint8_t kind;- uint8_t mods;- uint16_t code;+ keyact_t act;+ uint8_t pad[4];} map_entry_t;/* Flash image: one header doubleword, then the entries. */@@ -35,10 +33,12 @@ typedef struct {map_entry_t entries[KEYMAP_MAX];} map_image_t;+_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 uint32_t present_seq[KEYBIT_MAX]; /* press order while held, 0 when up */-static uint32_t press_counter;+static keyact_key_t present_key[KEYBIT_MAX]; /* tap/hold state, follows present[] */static int present_count;static char sides[3]; /* last SIDES sent: L, R, T present */static uint8_t keys_timer_id;@@ -70,38 +70,65 @@ static int Find(const uint8_t uid[UID_LEN])return -1;}-/* 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)+/* 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 -1;+ 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);}- map.entries[i].kind = kind;- map.entries[i].mods = mods;- map.entries[i].code = code;+ 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;return 0;}int KEYMAP_Set(const uint8_t uid[UID_LEN], uint8_t mods, uint8_t key){- return Put(uid, KEYMAP_KEY, mods, key);+ return Put(uid, KEYACT_KEY, mods, key);}int KEYMAP_SetConsumer(const uint8_t uid[UID_LEN], uint16_t usage){- return Put(uid, KEYMAP_CONSUMER, 0, usage);+ return Put(uid, KEYACT_CONSUMER, 0, usage);}-int KEYMAP_Get(const uint8_t uid[UID_LEN], uint8_t *mods, uint16_t *code)+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);- *mods = i < 0 ? 0 : map.entries[i].mods;- *code = i < 0 ? 0 : map.entries[i].code;- return i < 0 ? KEYMAP_NONE : map.entries[i].kind;+ return i < 0 ? NULL : &map.entries[i].act;}void KEYMAP_Unmap(const uint8_t uid[UID_LEN])@@ -226,7 +253,7 @@ static void Keys_Task(void)PROTO_Send("KEY- uid=%s", uid);}}- uint32_t seq[KEYBIT_MAX];+ keyact_key_t state[KEYBIT_MAX];for (int i = 0; i < keybit_count; i++){const keybit_info_t *old = Find_Keybit(present, present_count, keybits[i].uid);@@ -237,14 +264,14 @@ static void Keys_Task(void){PROTO_Send("PRESS uid=%s down=%d", uid, keybits[i].key_down);}- if (!keybits[i].key_down) seq[i] = 0;- else if (!was_down) seq[i] = ++press_counter;- else seq[i] = present_seq[old - present];+ if (old) state[i] = present_key[old - present];+ 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_seq, seq, sizeof(uint32_t) * keybit_count);+ memcpy(present_key, state, sizeof(keyact_key_t) * keybit_count);char now[3] = {0, 0, 0};for (int i = 0; i < present_count; i++)@@ -259,33 +286,13 @@ static void Keys_Task(void)PROTO_Send("SIDES L=%d R=%d T=%d", sides[0], sides[1], sides[2]);}- /* 6KRO: modifiers OR together, the first six keys fill the slots and a- * seventh turns every slot into ErrorRollOver as HID asks. */- uint8_t report[8] = {0};- int slots = 0;- uint16_t consumer = 0; /* most recently pressed consumer key wins */- uint32_t consumer_seq = 0;- for (int i = 0; i < present_count; i++)- {- if (!present[i].key_down) continue;- int m = Find(present[i].uid);- if (m < 0) continue; /* unmapped Keybits send nothing */- if (map.entries[m].kind == KEYMAP_CONSUMER)- {- if (present_seq[i] > consumer_seq)- {- consumer_seq = present_seq[i];- consumer = map.entries[m].code;- }- continue;- }- report[0] |= map.entries[m].mods;- uint8_t key = (uint8_t)map.entries[m].code;- if (!key) continue;- if (slots < 6) report[2 + slots] = key;- else memset(&report[2], 0x01, 6);- slots++;- }+ /* 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);HIDSAPP_SendKeyboard(report);HIDSAPP_SendConsumer(consumer);}diff --git a/firmware/Core/Src/proto.c b/firmware/Core/Src/proto.cindex 0154d94..893beec 100644--- a/firmware/Core/Src/proto.c+++ b/firmware/Core/Src/proto.c@@ -90,6 +90,24 @@ static int Arg_Hex(const char *args, const char *name, int digits, uint8_t *out)return -1;}+/* Finds "name=" and parses 1 to 5 decimal digits after it. 0 on success. */+static int Arg_Dec(const char *args, const char *name, uint16_t *out)+{+ size_t nl = strlen(name);+ for (const char *p = args; (p = strstr(p, name)) != NULL; p += nl)+ {+ if ((p != args && p[-1] != ' ') || p[nl] != '=') continue;+ const char *v = p + nl + 1;+ uint32_t n = 0;+ int digits = 0;+ for (; *v >= '0' && *v <= '9'; v++, digits++) n = n * 10 + (uint32_t)(*v - '0');+ if (digits == 0 || digits > 5 || n > 0xFFFF || (*v != 0 && *v != ' ')) return -1;+ *out = (uint16_t)n;+ return 0;+ }+ return -1;+}+/* uid= is printed most significant byte first, stored little endian. */static int Arg_Uid(const char *args, uint8_t uid[UID_LEN]){@@ -167,23 +185,44 @@ static void Handle_Line(proto_port_t *port, char *cmd)PROTO_FormatUid(uid_s, uid);Reply(port, "OK MAPC 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)+ || (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)+ {+ Reply(port, "ERR keymap full");+ return;+ }+ PROTO_FormatUid(uid_s, uid);+ Reply(port, "OK HOLD uid=%s", uid_s);+ }else if (strcmp(cmd, "MAP?") == 0){- uint8_t mods;- uint16_t code;if (Arg_Uid(args, uid)){Reply(port, "ERR usage: MAP? uid=<24hex>");return;}PROTO_FormatUid(uid_s, uid);- if (KEYMAP_Get(uid, &mods, &code) == KEYMAP_CONSUMER)+ const keyact_t *a = KEYMAP_Lookup(uid);+ if (a && a->kind == KEYACT_CONSUMER){- Reply(port, "MAPC uid=%s usage=%04X", uid_s, code);+ Reply(port, "MAPC uid=%s usage=%04X", uid_s, a->code);}else{- Reply(port, "MAP uid=%s mods=%02X key=%02X", uid_s, mods, code);+ Reply(port, "MAP uid=%s mods=%02X key=%02X", uid_s, a ? a->mods : 0, a ? a->code : 0);+ }+ if (a && (a->hold_mods || a->hold_key))+ {+ Reply(port, "HOLD uid=%s mods=%02X key=%02X ms=%u", uid_s, a->hold_mods, a->hold_key, a->hold_ms);}}else if (strcmp(cmd, "UNMAP") == 0)diff --git a/firmware/STM32Make.make b/firmware/STM32Make.makeindex 927d5b7..0987446 100644--- a/firmware/STM32Make.make+++ b/firmware/STM32Make.make@@ -103,6 +103,7 @@ Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_rcc.c \Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_usb.c \Core/Src/app_entry.c \Core/Src/hw_timerserver.c \+Core/Src/keyact.c \Core/Src/keymap.c \Core/Src/proto.c \Core/Src/stm32_lpm_if.c \diff --git a/firmware/test/keyact_test.c b/firmware/test/keyact_test.cnew file mode 100644index 0000000..b86b4f6--- /dev/null+++ b/firmware/test/keyact_test.c@@ -0,0 +1,158 @@+/*+ * Host check for the tap/hold state machine in Core/Src/keyact.c.+ * cc -std=c99 -ICore/Inc test/keyact_test.c Core/Src/keyact.c -o /tmp/keyact_test && /tmp/keyact_test+ */+#include <assert.h>+#include <stdio.h>+#include <string.h>+#include "keyact.h"++static keyact_key_t keys[3];+static const keyact_t *acts[3];+static uint8_t report[8];+static uint16_t consumer;+static uint32_t now;++/* run 5 ms ticks until t, like keymap.c does */+static void run_to(uint32_t t)+{+ for (; now <= t; now += 5) KEYACT_Step(keys, acts, 3, now, report, &consumer);+ now -= 5;+}++static int has_key(uint8_t key)+{+ for (int i = 2; i < 8; i++) if (report[i] == key) return 1;+ return 0;+}++static void reset(void)+{+ KEYACT_Reset();+ memset(keys, 0, sizeof(keys));+ memset(acts, 0, sizeof(acts));+ now = 1000;+}++int main(void)+{+ const keyact_t a = {.kind = KEYACT_KEY, .code = 0x04};+ const keyact_t b = {.kind = KEYACT_KEY, .mods = 0x02, .code = 0x05};+ const keyact_t vol = {.kind = KEYACT_CONSUMER, .code = 0x00E9};+ const keyact_t mute = {.kind = KEYACT_CONSUMER, .code = 0x00E2};+ /* tap: Escape, hold: left ctrl */+ const keyact_t esc_ctrl = {.kind = KEYACT_KEY, .code = 0x29, .hold_mods = 0x01, .hold_ms = 200};++ /* plain key: in the report exactly while held */+ reset();+ acts[0] = &a;+ run_to(1010);+ assert(!has_key(0x04));+ keys[0].down = 1;+ run_to(1020);+ assert(has_key(0x04) && report[0] == 0);+ run_to(2000);+ assert(has_key(0x04));+ keys[0].down = 0;+ run_to(2005);+ assert(!has_key(0x04));++ /* two keys plus modifiers */+ reset();+ acts[0] = &a;+ acts[1] = &b;+ keys[0].down = keys[1].down = 1;+ run_to(1000);+ assert(has_key(0x04) && has_key(0x05) && report[0] == 0x02);++ /* unmapped keys send nothing */+ reset();+ keys[2].down = 1;+ run_to(1100);+ assert(report[0] == 0 && report[2] == 0 && consumer == 0);++ /* consumer: most recent press wins, falls back to the older one */+ reset();+ acts[0] = &vol;+ acts[1] = &mute;+ keys[0].down = 1;+ run_to(1000);+ assert(consumer == 0x00E9);+ keys[1].down = 1;+ run_to(1010);+ assert(consumer == 0x00E2);+ keys[1].down = 0;+ run_to(1020);+ assert(consumer == 0x00E9);+ keys[0].down = 0;+ run_to(1030);+ assert(consumer == 0);++ /* hold key, short press: nothing while held, a tap of Escape on release */+ reset();+ acts[0] = &esc_ctrl;+ keys[0].down = 1;+ run_to(1150);+ assert(!has_key(0x29) && report[0] == 0);+ keys[0].down = 0;+ run_to(1155);+ assert(has_key(0x29) && report[0] == 0);+ run_to(1155 + KEYACT_TAP_MS);+ assert(!has_key(0x29));++ /* hold key, long press: ctrl from 200 ms until release, no Escape */+ reset();+ acts[0] = &esc_ctrl;+ keys[0].down = 1;+ run_to(1195);+ assert(report[0] == 0);+ run_to(1200);+ assert(report[0] == 0x01 && !has_key(0x29));+ run_to(1600);+ assert(report[0] == 0x01);+ keys[0].down = 0;+ run_to(1605);+ assert(report[0] == 0 && !has_key(0x29));+ run_to(1700);+ assert(!has_key(0x29));++ /* ctrl held + another key = ctrl-a (the usual home row mod use) */+ reset();+ acts[0] = &esc_ctrl;+ acts[1] = &a;+ keys[0].down = 1;+ run_to(1250);+ keys[1].down = 1;+ run_to(1260);+ assert(report[0] == 0x01 && has_key(0x04));++ /* hold on a consumer key: tap sends the usage briefly */+ reset();+ const keyact_t vol_shift = {.kind = KEYACT_CONSUMER, .code = 0x00E9, .hold_mods = 0x02};+ acts[0] = &vol_shift;+ keys[0].down = 1;+ run_to(1050);+ assert(consumer == 0);+ keys[0].down = 0;+ run_to(1055);+ assert(consumer == 0x00E9);+ run_to(1055 + KEYACT_TAP_MS);+ assert(consumer == 0);++ /* seven keys: ErrorRollOver in every slot */+ reset();+ static keyact_key_t many[7];+ static const keyact_t k7[7] = {+ {.kind = KEYACT_KEY, .code = 4}, {.kind = KEYACT_KEY, .code = 5}, {.kind = KEYACT_KEY, .code = 6},+ {.kind = KEYACT_KEY, .code = 7}, {.kind = KEYACT_KEY, .code = 8}, {.kind = KEYACT_KEY, .code = 9},+ {.kind = KEYACT_KEY, .code = 10}};+ const keyact_t *p7[7];+ for (int i = 0; i < 7; i++) { p7[i] = &k7[i]; many[i].down = 1; }+ KEYACT_Step(many, p7, 6, 1000, report, &consumer);+ assert(report[2] == 4 && report[7] == 9);+ KEYACT_Step(many, p7, 7, 1005, report, &consumer);+ for (int i = 2; i < 8; i++) assert(report[i] == 0x01);++ puts("keyact_test: ok");+ return 0;+}diff --git a/firmware/test/proto_test.c b/firmware/test/proto_test.cindex a71e53d..621ce69 100644--- a/firmware/test/proto_test.c+++ b/firmware/test/proto_test.c@@ -16,7 +16,7 @@ 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], kind, mods; uint16_t code; } table[KEYMAP_MAX];+static struct { uint8_t uid[UID_LEN]; keyact_t act; } table[KEYMAP_MAX];static int n;static keybit_info_t present[2];@@ -25,21 +25,38 @@ 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 int put(const uint8_t *uid, uint8_t kind, uint8_t mods, uint16_t code)+static keyact_t *entry(const uint8_t *uid){int i = find(uid);- if (i < 0) { if (n == KEYMAP_MAX) return -1; i = n++; memcpy(table[i].uid, uid, UID_LEN); }- table[i].kind = kind; table[i].mods = mods; table[i].code = code;+ 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;return 0;}-int KEYMAP_Set(const uint8_t *uid, uint8_t mods, uint8_t key) { return put(uid, KEYMAP_KEY, mods, key); }-int KEYMAP_SetConsumer(const uint8_t *uid, uint16_t usage) { return put(uid, KEYMAP_CONSUMER, 0, usage); }-int KEYMAP_Get(const uint8_t *uid, uint8_t *mods, uint16_t *code)+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){- int i = find(uid);- *mods = i < 0 ? 0 : table[i].mods; *code = i < 0 ? 0 : table[i].code;- return i < 0 ? KEYMAP_NONE : table[i].kind;+ 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;}+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; }@@ -82,7 +99,7 @@ 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].kind == KEYMAP_CONSUMER && table[0].code == 0x00CD);+ assert(table[0].act.kind == KEYACT_CONSUMER && table[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");@@ -96,6 +113,28 @@ int main(void)expect("MAPC uid=" U " usage=00E2\n", "OK MAPC uid=" U "\n");expect("CLEAR\n", "OK CLEAR\n");assert(n == 0);++ /* 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");+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=00\nHOLD uid=" U " mods=02 key=00 ms=250\n");+ expect("MAP uid=" U " mods=00 key=29\n", "OK MAP uid=" U "\n"); /* MAP keeps the hold */+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=29\nHOLD uid=" U " mods=02 key=00 ms=250\n");+ expect("HOLD uid=" U " mods=00 key=E0\n", "OK HOLD uid=" U "\n"); /* ms defaults to 200 */+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=29\nHOLD uid=" U " mods=00 key=E0 ms=200\n");+ expect("HOLD uid=" U " mods=00 key=00\n", "OK HOLD uid=" U "\n"); /* clears it */+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=29\n");+ expect("HOLD uid=" U " mods=02 key=00 ms=70000\n", "ERR usage: HOLD uid=<24hex> mods=<2hex> key=<2hex> ms=<dec>\n");+ expect("HOLD uid=" U " mods=02 key=00 ms=\n", "ERR usage: HOLD uid=<24hex> mods=<2hex> key=<2hex> ms=<dec>\n");+ expect("HOLD uid=" U " key=04\n", "ERR usage: HOLD uid=<24hex> mods=<2hex> key=<2hex> ms=<dec>\n");+ expect("HOLD uid=" U " mods=02 key=00 ms=300\n", "OK HOLD uid=" U "\n");+ expect("UNMAP uid=" U "\n", "OK UNMAP uid=" U "\n"); /* UNMAP drops the hold too */+ 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 */+ expect("HOLD uid=" U " mods=02 key=00\n", "OK HOLD uid=" U "\n");+ expect("CLEAR\n", "OK CLEAR\n");+ assert(n == 0);expect("SAVE\n", "OK SAVE\n");/* chunked like 20 byte BLE writes */