Skip to content

A truly modular keyboard

C

keyword: don't lose taps shorter than the key poll

Nathandafde79e1d76
diff --git a/firmware/keyword/Core/Inc/keybit_bus.h b/firmware/keyword/Core/Inc/keybit_bus.h
index 5fdc013..16dc46e 100644
--- a/firmware/keyword/Core/Inc/keybit_bus.h
+++ b/firmware/keyword/Core/Inc/keybit_bus.h
@@ -21,6 +21,10 @@ typedef struct {
char keybit_side; /* the Keybit's own side touching us: N/E/S/W */
char at; /* where it is on the keyword: L, R or T, '?' if unknown */
uint8_t backwards; /* its sides with a Keybit on backwards: bit 0 N, 1 E, 2 S, 3 W */
+ /* Taps too short for the key poll to see, caught from the Keybit's own
+ * queued events: saw_down is set when a poll reads it down, pend when
+ * its press event arrived unseen, tap 1 = report a down next, 2 = then up. */
+ uint8_t saw_down, pend, tap;
} keybit_info_t;
/* A Keybit further out, known only through events relayed by its
@@ -33,6 +37,8 @@ typedef struct {
uint8_t backwards; /* it has a neighbour on backwards (side unknown) */
uint8_t via; /* address of the touching Keybit it came through */
uint8_t dirty; /* set on any change; the consumer clears it */
+ uint8_t fresh_down; /* went down in a relay batch the consumer hasn't seen yet */
+ uint8_t tap; /* a press and release came in one batch: 1 = report down, 2 = then up */
uint32_t last_seen; /* HAL_GetTick() */
} keybit_remote_t;
@@ -67,7 +73,7 @@ uint32_t keybit_bus_poll_keys(I2C_HandleTypeDef *i2c, keybit_info_t *k, int n);
/* Drain relayed events from the n touching Keybits into the remote table
* (r, with *rn entries in use) and age out silent ones. Run it with
* keybit_bus_poll_keys. Returns 1 if any entry changed. */
-int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, const keybit_info_t *k, int n,
+int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, keybit_info_t *k, int n,
keybit_remote_t *r, int *rn);
/* Plain register access for other devices on I2C3, such as the BQ25895
diff --git a/firmware/keyword/Core/Src/keybit_bus.c b/firmware/keyword/Core/Src/keybit_bus.c
index 23bc919..b714bad 100644
--- a/firmware/keyword/Core/Src/keybit_bus.c
+++ b/firmware/keyword/Core/Src/keybit_bus.c
@@ -16,6 +16,7 @@
#define REG_EV_POP 0x7B
#define EV_BATCH 8
#define EV_PRESSED 0x01
+#define EV_HEARTBEAT 0x02
#define EV_BACKWARDS 0x04
#define MAGIC 0x4B
@@ -145,6 +146,7 @@ int keybit_bus_scan(I2C_HandleTypeDef *i2c, keybit_info_t *out, int max)
if (!read_regs(i2c, a, REG_MAGIC, head, sizeof(head)) || head[0] != MAGIC) continue;
keybit_info_t *k = &out[n];
+ memset(k, 0, sizeof(*k));
/* Reading the UID also catches two Keybits on one address: the loser of
* the arbitration drops its address and turns up on the default one. */
if (!read_regs(i2c, a, REG_UID, k->uid, sizeof(k->uid))) continue;
@@ -165,6 +167,10 @@ uint32_t keybit_bus_poll_keys(I2C_HandleTypeDef *i2c, keybit_info_t *k, int n)
for (int i = 0; i < n; i++) {
uint8_t v;
if (!read_regs(i2c, k[i].addr, REG_KEY, &v, 1)) continue; /* next scan drops it */
+ if (v & 1) {
+ k[i].saw_down = 1;
+ k[i].pend = 0;
+ }
if ((v & 1) != k[i].key_down) {
k[i].key_down = v & 1;
changed |= 1u << i;
@@ -189,7 +195,7 @@ static keybit_remote_t *remote_slot(keybit_remote_t *r, int *rn, uint32_t uid)
return free_slot; /* NULL when the table is full */
}
-int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, const keybit_info_t *k, int n,
+int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, keybit_info_t *k, int n,
keybit_remote_t *r, int *rn)
{
int changed = 0;
@@ -208,16 +214,30 @@ int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, const keybit_info_t *k, int n,
uint32_t uid = uid32(&ev[e * 5]);
uint8_t flags = ev[e * 5 + 4];
- /* Touching Keybits queue their own events too; we already read
- * those directly. */
+ /* Touching Keybits queue their own key events too. Their key is read
+ * directly, but a tap shorter than the key poll only shows up here:
+ * a press the poll never saw, then its release, is a missed tap. */
int direct = 0;
- for (int j = 0; j < n; j++) if (uid32(k[j].uid) == uid) direct = 1;
+ for (int j = 0; j < n; j++) {
+ if (uid32(k[j].uid) != uid) continue;
+ direct = 1;
+ if (flags & EV_HEARTBEAT) continue; /* only key changes count */
+ if (flags & EV_PRESSED) {
+ if (!k[j].saw_down) k[j].pend = 1;
+ } else {
+ if (k[j].pend && !k[j].saw_down && !k[j].tap) k[j].tap = 1;
+ k[j].pend = 0;
+ }
+ }
if (direct) continue;
keybit_remote_t *x = remote_slot(r, rn, uid);
if (!x) continue;
uint8_t down = flags & EV_PRESSED;
uint8_t back = (flags & EV_BACKWARDS) != 0;
+ /* Down and up again before the keys task saw the down: keep it as a tap. */
+ if (down && !x->key_down) x->fresh_down = 1;
+ if (!down && x->key_down && x->fresh_down && !x->tap) x->tap = 1;
if (x->uid != uid || !x->present || x->key_down != down || x->backwards != back) {
x->dirty = 1;
changed = 1;
@@ -232,6 +252,9 @@ int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, const keybit_info_t *k, int n,
}
}
+ /* The next batch's events are measured against the polls until then. */
+ for (int i = 0; i < n; i++) k[i].saw_down = k[i].key_down;
+
for (int i = 0; i < *rn; i++) {
if (r[i].present && now - r[i].last_seen > KEYBIT_REMOTE_TIMEOUT_MS) {
r[i].present = 0;
diff --git a/firmware/keyword/Core/Src/keymap.c b/firmware/keyword/Core/Src/keymap.c
index 926c5db..3411ecd 100644
--- a/firmware/keyword/Core/Src/keymap.c
+++ b/firmware/keyword/Core/Src/keymap.c
@@ -256,6 +256,12 @@ static void Direct_Keys(void)
memcpy(old, present, sizeof(keybit_info_t) * old_count);
present_count = keybit_count;
memcpy(present, keybits, sizeof(keybit_info_t) * keybit_count);
+ /* A tap the key poll missed: down for one pass, then up on the next. */
+ for (int i = 0; i < keybit_count; i++)
+ {
+ if (keybits[i].tap == 1) { present[i].key_down = 1; keybits[i].tap = 2; }
+ else if (keybits[i].tap == 2) keybits[i].tap = 0;
+ }
for (int i = 0; i < keybit_count; i++)
{
@@ -267,9 +273,9 @@ static void Direct_Keys(void)
KEYMAP_KeyLine(i, line, sizeof(line));
PROTO_Send("%s", line);
}
- if ((was ? was->key_down : 0) != keybits[i].key_down)
+ if ((was ? was->key_down : 0) != present[i].key_down)
{
- PROTO_Send("PRESS uid=%s down=%d", uid, keybits[i].key_down);
+ PROTO_Send("PRESS uid=%s down=%d", uid, present[i].key_down);
}
if ((was ? was->backwards : 0) != keybits[i].backwards)
{
@@ -277,7 +283,7 @@ static void Direct_Keys(void)
}
if (was) state[i] = key_state[was - old];
else memset(&state[i], 0, sizeof(state[i]));
- state[i].down = keybits[i].key_down;
+ state[i].down = present[i].key_down;
}
memcpy(key_state, state, sizeof(keyact_key_t) * keybit_count);
for (int i = keybit_count; i < KEYBIT_MAX; i++)
@@ -309,8 +315,12 @@ static void Remote_Keys(void)
{
keybit_remote_t *k = &keybit_remotes[r];
keyact_key_t *s = &key_state[KEYBIT_MAX + r];
- uint8_t down = k->present && k->key_down;
+ uint8_t down = k->present && (k->key_down || k->tap == 1);
uint8_t back = k->present && k->backwards;
+ /* A tap that came and went in one relay batch: down this pass, up the next. */
+ if (k->tap == 1) { k->tap = 2; k->dirty = 1; }
+ else if (k->tap == 2) k->tap = 0;
+ if (down) k->fresh_down = 0;
if (!k->dirty && k->present == remote_shown[r] && down == remote_down[r] && back == remote_back[r]) continue;
k->dirty = 0;
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.c
index 6cf23ad..88b0a2f 100644
--- a/firmware/keyword/Core/Src/main.c
+++ b/firmware/keyword/Core/Src/main.c
@@ -506,7 +506,14 @@ static void Keybit_Bus_Poll(void)
int n = keybit_bus_scan(&hi2c3, found, KEYBIT_MAX);
for (int i = 0; i < keybit_count && n < KEYBIT_MAX; i++) {
int here = 0;
- for (int j = 0; j < n && !here; j++) here = memcmp(found[j].uid, keybits[i].uid, sizeof(found[j].uid)) == 0;
+ for (int j = 0; j < n && !here; j++)
+ if (memcmp(found[j].uid, keybits[i].uid, sizeof(found[j].uid)) == 0) {
+ here = 1;
+ /* keep its tap tracking across the rescan */
+ found[j].saw_down = keybits[i].saw_down;
+ found[j].pend = keybits[i].pend;
+ found[j].tap = keybits[i].tap;
+ }
if (!here && missed[i] < KEYBIT_MISSES_MAX) {
found[n] = keybits[i];
found_missed[n++] = (uint8_t)(missed[i] + 1);