A truly modular keyboard
C
firmware: MACRO, a Keybit types up to 16 taps
Nathan607d59d26250
diff --git a/firmware/Core/Inc/keyact.h b/firmware/Core/Inc/keyact.hindex 5a65362..6b128f0 100644--- a/firmware/Core/Inc/keyact.h+++ b/firmware/Core/Inc/keyact.h@@ -10,12 +10,15 @@#define KEYACT_TAP_MS 10 /* how long a tap stays in the reports */#define KEYACT_HOLD_MS_DEF 200+#define KEYACT_MACRO_MAX 16+#define KEYACT_MACRO_STEP_MS 5 /* press, release, press...: a tap every 10 ms */-enum { KEYACT_NONE = 0, KEYACT_KEY = 1, KEYACT_CONSUMER = 2 };+enum { KEYACT_NONE = 0, KEYACT_KEY = 1, KEYACT_CONSUMER = 2, KEYACT_MACRO = 3 };-/* 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+/* Stored per Keybit in the keymap and in flash. code is the keyboard+ * usage for KEYACT_KEY and the consumer usage for KEYACT_CONSUMER.+ * KEYACT_MACRO types macro[0..macro_len) as (mods, key) taps, once per+ * press. 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@@ -26,6 +29,8 @@ typedef structuint8_t hold_mods;uint8_t hold_key;uint16_t hold_ms;+ uint8_t macro_len;+ uint8_t macro[KEYACT_MACRO_MAX][2];} keyact_t;/* Per key runtime state, kept by the caller across calls (by UID). Thediff --git a/firmware/Core/Inc/keymap.h b/firmware/Core/Inc/keymap.hindex d591933..de3217a 100644--- a/firmware/Core/Inc/keymap.h+++ b/firmware/Core/Inc/keymap.h@@ -26,6 +26,8 @@ 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]);diff --git a/firmware/Core/Src/keyact.c b/firmware/Core/Src/keyact.cindex ff3ec41..dddf8f9 100644--- a/firmware/Core/Src/keyact.c+++ b/firmware/Core/Src/keyact.c@@ -16,15 +16,39 @@ typedef structuint32_t until;} pulse_t;+/* One macro plays at a time; its taps are copied so a remap mid-play+ * doesn't matter. */+static struct+{+ uint8_t len; /* 0 when idle */+ uint8_t idx;+ uint8_t pressed;+ uint32_t t;+ uint8_t seq[KEYACT_MACRO_MAX][2];+} player;+static pulse_t pulses[PULSES];static uint32_t press_counter;void KEYACT_Reset(void){memset(pulses, 0, sizeof(pulses));+ memset(&player, 0, sizeof(player));press_counter = 0;}+/* ponytail: a macro started while another plays is dropped, queue them+ * if that turns out to matter */+static void Macro_Start(const keyact_t *a, uint32_t now)+{+ if (player.len || !a->macro_len) return;+ player.len = a->macro_len > KEYACT_MACRO_MAX ? KEYACT_MACRO_MAX : a->macro_len;+ memcpy(player.seq, a->macro, sizeof(player.seq));+ player.idx = 0;+ player.pressed = 1;+ player.t = now;+}+static int Has_Hold(const keyact_t *a){return a && (a->hold_mods || a->hold_key);@@ -33,6 +57,11 @@ static int Has_Hold(const keyact_t *a)static void Pulse(const keyact_t *a, uint32_t now){if (!a || a->kind == KEYACT_NONE) return;+ if (a->kind == KEYACT_MACRO)+ {+ Macro_Start(a, now);+ return;+ }/* reuse the oldest slot if all are busy */pulse_t *p = &pulses[0];for (int i = 0; i < PULSES; i++)@@ -80,6 +109,7 @@ void KEYACT_Step(keyact_key_t *keys, const keyact_t *const *acts, int n, uint32_k->t0 = now;k->seq = ++press_counter;k->state = Has_Hold(a) ? ST_PENDING : ST_ACTIVE;+ if (k->state == ST_ACTIVE && a && a->kind == KEYACT_MACRO) Macro_Start(a, now);}if (k->down && k->state == ST_PENDING&& (uint32_t)(now - k->t0) >= (a->hold_ms ? a->hold_ms : KEYACT_HOLD_MS_DEF))@@ -118,4 +148,16 @@ void KEYACT_Step(keyact_key_t *keys, const keyact_t *const *acts, int n, uint32_Add(report, &slots, p->mods, p->key);if (p->usage) *consumer = p->usage;}++ if (player.len && (uint32_t)(now - player.t) >= KEYACT_MACRO_STEP_MS)+ {+ player.t = now;+ if (player.pressed) player.pressed = 0;+ else if (++player.idx >= player.len) player.len = 0;+ else player.pressed = 1;+ }+ if (player.len && player.pressed)+ {+ Add(report, &slots, player.seq[player.idx][0], player.seq[player.idx][1]);+ }}diff --git a/firmware/Core/Src/keymap.c b/firmware/Core/Src/keymap.cindex fc38b6b..6cf8bc7 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 3 /* 2: kind + 16-bit code, 3: hold action */+#define MAP_VERSION 4 /* 2: kind + 16-bit code, 3: hold action, 4: macros *//* 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,11 +18,12 @@ extern keybit_info_t keybits[KEYBIT_MAX];extern int keybit_count;extern volatile uint32_t keybit_changed;-/* 24 bytes, three flash doublewords. */+/* 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[4];+ uint8_t pad[2];} map_entry_t;/* Flash image: one header doubleword, then the entries. */@@ -92,6 +93,7 @@ static int Put(const uint8_t uid[UID_LEN], uint8_t kind, uint8_t mods, uint16_ta->kind = kind;a->mods = mods;a->code = code;+ a->macro_len = 0; /* MAP/MAPC replace a macro too */return 0;}@@ -105,6 +107,20 @@ 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)diff --git a/firmware/Core/Src/proto.c b/firmware/Core/Src/proto.cindex 893beec..6ad94bc 100644--- a/firmware/Core/Src/proto.c+++ b/firmware/Core/Src/proto.c@@ -108,6 +108,28 @@ static int Arg_Dec(const char *args, const char *name, uint16_t *out)return -1;}+/* seq=MMKK,MMKK,... (mods byte, key usage), 1 to KEYACT_MACRO_MAX taps.+ * Returns the count, or -1. */+static int Arg_Seq(const char *args, uint8_t seq[KEYACT_MACRO_MAX][2])+{+ const char *v = strstr(args, "seq=");+ if (!v || (v != args && v[-1] != ' ')) return -1;+ v += 4;+ for (int n = 0; n < KEYACT_MACRO_MAX; n++)+ {+ for (int i = 0; i < 4; i++)+ {+ if (Hex_Digit(v[i]) < 0) return -1;+ }+ seq[n][0] = (uint8_t)(Hex_Digit(v[0]) << 4 | Hex_Digit(v[1]));+ seq[n][1] = (uint8_t)(Hex_Digit(v[2]) << 4 | Hex_Digit(v[3]));+ v += 4;+ if (*v == 0 || *v == ' ') return n + 1;+ if (*v++ != ',') return -1;+ }+ 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]){@@ -185,6 +207,23 @@ 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, "MACRO") == 0)+ {+ uint8_t seq[KEYACT_MACRO_MAX][2];+ int n = -1;+ if (Arg_Uid(args, uid) || (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)+ {+ Reply(port, "ERR keymap full");+ return;+ }+ PROTO_FormatUid(uid_s, uid);+ Reply(port, "OK MACRO uid=%s", uid_s);+ }else if (strcmp(cmd, "HOLD") == 0){uint8_t mods, key;@@ -216,6 +255,19 @@ static void Handle_Line(proto_port_t *port, char *cmd){Reply(port, "MAPC uid=%s usage=%04X", uid_s, a->code);}+ else if (a && a->kind == KEYACT_MACRO)+ {+ /* 16 taps is 79 chars of seq, the line still fits PROTO_LINE_MAX */+ char seq_s[KEYACT_MACRO_MAX * 5];+ int len = 0;+ for (int i = 0; i < a->macro_len; i++)+ {+ len += snprintf(&seq_s[len], sizeof(seq_s) - (size_t)len, "%s%02X%02X",+ i ? "," : "", a->macro[i][0], a->macro[i][1]);+ }+ seq_s[len] = 0;+ Reply(port, "MACRO uid=%s seq=%s", uid_s, seq_s);+ }else{Reply(port, "MAP uid=%s mods=%02X key=%02X", uid_s, a ? a->mods : 0, a ? a->code : 0);diff --git a/firmware/test/keyact_test.c b/firmware/test/keyact_test.cindex b86b4f6..19c606d 100644--- a/firmware/test/keyact_test.c+++ b/firmware/test/keyact_test.c@@ -153,6 +153,46 @@ int main(void)KEYACT_Step(many, p7, 7, 1005, report, &consumer);for (int i = 2; i < 8; i++) assert(report[i] == 0x01);+ /* macro: each tap pressed for one 5 ms tick, released for the next */+ reset();+ const keyact_t hi = {.kind = KEYACT_MACRO, .macro_len = 3,+ .macro = {{0x02, 0x0B}, {0x00, 0x0C}, {0x00, 0x0C}}}; /* H i i */+ acts[0] = &hi;+ keys[0].down = 1;+ KEYACT_Step(keys, acts, 3, 1000, report, &consumer);+ assert(report[0] == 0x02 && has_key(0x0B));+ keys[0].down = 0; /* releasing the key doesn't stop the macro */+ KEYACT_Step(keys, acts, 3, 1005, report, &consumer);+ assert(report[0] == 0 && report[2] == 0);+ KEYACT_Step(keys, acts, 3, 1010, report, &consumer);+ assert(report[0] == 0 && has_key(0x0C));+ KEYACT_Step(keys, acts, 3, 1015, report, &consumer);+ assert(report[2] == 0); /* release between the two i's */+ KEYACT_Step(keys, acts, 3, 1020, report, &consumer);+ assert(has_key(0x0C));+ KEYACT_Step(keys, acts, 3, 1025, report, &consumer);+ KEYACT_Step(keys, acts, 3, 1030, report, &consumer);+ KEYACT_Step(keys, acts, 3, 1035, report, &consumer);+ assert(report[0] == 0 && report[2] == 0); /* done */++ /* a second press runs it again; held keys stay in the reports alongside */+ acts[1] = &a;+ keys[1].down = 1;+ keys[0].down = 1;+ KEYACT_Step(keys, acts, 3, 1040, report, &consumer);+ assert(has_key(0x0B) && has_key(0x04));++ /* macro as the tap action of a hold key */+ reset();+ const keyact_t hi_ctrl = {.kind = KEYACT_MACRO, .macro_len = 1, .macro = {{0x00, 0x0B}}, .hold_mods = 0x01};+ acts[0] = &hi_ctrl;+ keys[0].down = 1;+ run_to(1050);+ assert(report[2] == 0);+ keys[0].down = 0;+ KEYACT_Step(keys, acts, 3, 1055, report, &consumer);+ assert(has_key(0x0B) && report[0] == 0);+puts("keyact_test: ok");return 0;}diff --git a/firmware/test/proto_test.c b/firmware/test/proto_test.cindex 621ce69..67adb29 100644--- a/firmware/test/proto_test.c+++ b/firmware/test/proto_test.c@@ -35,7 +35,7 @@ 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->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); }@@ -56,6 +56,14 @@ int KEYMAP_SetHold(const uint8_t *uid, uint8_t mods, uint8_t key, uint16_t ms)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;+}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; }@@ -135,6 +143,34 @@ int main(void)expect("HOLD uid=" U " mods=02 key=00\n", "OK HOLD uid=" U "\n");expect("CLEAR\n", "OK CLEAR\n");assert(n == 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);+ 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");+ expect("MAP uid=" U " mods=00 key=05\n", "OK MAP uid=" U "\n"); /* MAP replaces it */+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=05\n");+ expect("MACRO uid=" U " seq=0004,0005\n", "OK MACRO uid=" U "\n");+ expect("MAPC uid=" U " usage=00CD\n", "OK MAPC uid=" U "\n"); /* so does MAPC */+ expect("MAP? uid=" U "\n", "MAPC uid=" U " usage=00CD\n");+ expect("MACRO uid=" U " seq=0004,0005\n", "OK MACRO uid=" U "\n");+ expect("UNMAP uid=" U "\n", "OK UNMAP uid=" U "\n");+ expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=00\n");+ /* all 16 taps, then a seventeenth is too many */+ expect("MACRO uid=" U " seq=0004,0005,0006,0007,0008,0009,000A,000B,000C,000D,000E,000F,0010,0011,0012,0013\n",+ "OK MACRO uid=" U "\n");+ expect("MAP? uid=" U "\n",+ "MACRO uid=" U " seq=0004,0005,0006,0007,0008,0009,000A,000B,000C,000D,000E,000F,0010,0011,0012,0013\n");+ expect("MACRO uid=" U " seq=0004,0005,0006,0007,0008,0009,000A,000B,000C,000D,000E,000F,0010,0011,0012,0013,0014\n",+ "ERR usage: MACRO uid=<24hex> seq=<MMKK,...> (1-16 taps)\n");+ expect("MACRO uid=" U " seq=\n", "ERR usage: MACRO uid=<24hex> seq=<MMKK,...> (1-16 taps)\n");+ expect("MACRO uid=" U " seq=004\n", "ERR usage: MACRO uid=<24hex> seq=<MMKK,...> (1-16 taps)\n");+ expect("MACRO uid=" U " seq=0004,\n", "ERR usage: MACRO uid=<24hex> seq=<MMKK,...> (1-16 taps)\n");+ expect("MACRO uid=" U " seq=0004;0005\n", "ERR usage: MACRO uid=<24hex> seq=<MMKK,...> (1-16 taps)\n");+ expect("CLEAR\n", "OK CLEAR\n");expect("SAVE\n", "OK SAVE\n");/* chunked like 20 byte BLE writes */