SMK-37 v2.5.6: the encoders count fast turns

The stock decoder wanted each quadrature state to hold two frames (3.2 ms) and three valid
states in a row: a quick spin (above ~75 states a second) lost whole detents, or all of
them. Now a plain state machine: every new state counts at once (bounce cancels), a missed
state counts two the way the knob was going, four counts make a detent, and back at the
rest state the remainder rounds so the count stays on the clicks.
tests/smk_encoder_test.c (registered): slow, fast, missed states, bounce, half a detent.

Everything is behind BOARD_SMK37: the FM-1 build is main's v2.5.1, byte for byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
This commit is contained in:
Claude committed 2026-11-09 19:03:08 -03:00
1 parent fc9ac04c6e
commit 0c888fa4cc
4 files changed
+117 -20

No files matched your search

+36 -18
View File
@@ -387,33 +387,51 @@ static void smk__buttons(void)
}
}
static void smk__encoders(void) /* stock decoder: 3-state window, 4 counts a cycle */
/* The encoders (FMini-1 2.5.6, the owner's report: a fast turn counted little or nothing). Each one is read once a frame
* (1.6 ms: row 7 of its column). The stock decoder wanted a state to hold for two frames (3.2 ms) and three valid states
* in a row, so above ~75 states a second (a quick spin) it dropped whole detents. This one is a plain quadrature state
* machine: every new state counts at once (+1 / -1 for a neighbour, the Gray code; contact bounce is a step there and back
* and cancels), and a state two away (one missed between two frames) counts two in the direction it was going. Each
* four counts are a detent; back at the rest state (the one seen at power-on: these detent on one state of the four)
* what is left rounds (2 or 3 counts: one more, less: none), so it never drifts off the clicks.
* enc_last: the state now (0xFF: not read yet); enc_hist: the rest state (bits 0-1) and the last direction (bit 2 +,
* bit 3 -); enc_cnt: the counts since the last detent, + = B leads */
static const int8_t SMK_QDEC[16] = {0, -1, 1, 9, 1, 0, 9, -1, -1, 9, 0, 1, 9, 1, -1, 0}; /* [prev << 2 | cur]; 9 = skipped */
static void smk__encoders(void)
{
uint32_t e;
for (e = 0; e < FM1_NENC; e++) {
uint32_t cur = smk__closed(SMK_ENCC[e][0]) | smk__closed(SMK_ENCC[e][1]) << 1, h;
if (fm1_in.enc_last[e] == 0xFF) { /* first frame */
uint32_t cur = smk__closed(SMK_ENCC[e][0]) | smk__closed(SMK_ENCC[e][1]) << 1, prev = fm1_in.enc_last[e], hs;
int32_t d, c;
if (prev == 0xFFu) { /* first frame: the rest state */
fm1_in.enc_last[e] = (uint8_t)cur;
fm1_in.enc_hist[e] = (uint8_t)(cur * 0x15u);
fm1_in.enc_hist[e] = (uint8_t)cur;
continue;
}
if (cur != fm1_in.enc_last[e]) { /* must hold for two frames */
fm1_in.enc_last[e] = (uint8_t)cur;
if (cur == prev)
continue;
}
if (cur == (fm1_in.enc_hist[e] & 3u))
continue;
h = ((uint32_t)fm1_in.enc_hist[e] << 2 | cur) & 0x3Fu;
fm1_in.enc_hist[e] = (uint8_t)h;
if (h == 0x07u || h == 0x1Eu || h == 0x21u || h == 0x38u)
fm1_in.enc_cnt[e]--; /* A leads */
else if (h == 0x0Bu || h == 0x12u || h == 0x2Du || h == 0x34u)
fm1_in.enc_cnt[e]++; /* B leads */
if (fm1_in.enc_cnt[e] >= SMK_ENC_DIV || fm1_in.enc_cnt[e] <= -SMK_ENC_DIV) { /* one step a detent */
int32_t s = fm1_in.enc_cnt[e] / SMK_ENC_DIV;
fm1_in.enc_last[e] = (uint8_t)cur;
hs = fm1_in.enc_hist[e];
d = SMK_QDEC[prev << 2 | cur];
if (d == 9) /* one state missed: two more the way it was going (none known: none) */
d = (hs & 4u) ? 2 : (hs & 8u) ? -2 : 0;
if (d > 0)
hs = (hs & 3u) | 4u;
else if (d < 0)
hs = (hs & 3u) | 8u;
fm1_in.enc_hist[e] = (uint8_t)hs;
c = fm1_in.enc_cnt[e] + d;
if (c >= SMK_ENC_DIV || c <= -SMK_ENC_DIV) { /* a whole cycle: a detent (also when the rest state was missed) */
int32_t s = c / SMK_ENC_DIV;
fm1_in.enc_steps[e] = (int16_t)(fm1_in.enc_steps[e] + s);
fm1_in.enc_cnt[e] = (int16_t)(fm1_in.enc_cnt[e] - s * SMK_ENC_DIV);
c -= s * SMK_ENC_DIV;
}
if (cur == (hs & 3u)) { /* back at rest: what is left of a cycle rounds to the detent */
if (c >= 2 || c <= -2)
fm1_in.enc_steps[e] = (int16_t)(fm1_in.enc_steps[e] + (c > 0 ? 1 : -1));
c = 0;
}
fm1_in.enc_cnt[e] = (int16_t)c;
}
}
+2 -2
View File
@@ -20,8 +20,8 @@
#if BOARD_SMK37 /* the SMK-37 build has its own version (tools/build.py, the installer workflow read it) */
#define FMINI_VER_MAJ 2u
#define FMINI_VER_MIN 5u
#define FMINI_VER_PATCH 5u
#define FMINI_SMK_VERSION "v2.5.5"
#define FMINI_VER_PATCH 6u
#define FMINI_SMK_VERSION "v2.5.6"
#define FMINI_VERSION FMINI_SMK_VERSION
#else
#define FMINI_VER_MAJ 2u
+2
View File
@@ -271,6 +271,8 @@ run "SMK-37 Pro flash map: allow-lists never reach the head, BTIF/USRTRIM, USR,
$CC -DBOARD_SMK37=1 -Ifirmware/hal -o "$OUT/smk_owner_test" tests/smk_owner_test.c
run "SMK-37 Pro store owner tag: another firmware's store cleared, ours kept, nothing else written" "$OUT/smk_owner_test"
$CC -Itests/smk_fake -o "$OUT/smk_midi_out_test" tests/smk_midi_out_test.c
$CC -DBOARD_SMK37=1 -Ifirmware/hal -o "$OUT/smk_encoder_test" tests/smk_encoder_test.c
run "SMK-37 encoders: one count a detent at any speed, a missed state, bounce" "$OUT/smk_encoder_test"
run "SMK-37 TRS MIDI OUT: packets to bytes, whole messages in the ring, one byte a tick, quiet after a dead UART" "$OUT/smk_midi_out_test"
python3 tools/fm1pkg_make.py --board smk37 "$APP" build/loader/ota.bin "$OUT/smk_new.fwsc" >/dev/null
python3 tools/fm1pkg_make.py --board smk37 "$OUT/old_app.bin" build/loader/ota.bin "$OUT/smk_old.fwsc" >/dev/null
+77
View File
@@ -0,0 +1,77 @@
/* SPDX-License-Identifier: GPL-3.0-only */
/* SMK-37 encoders (firmware/hal/smk_input.h smk__encoders): detents at any speed, bounce, a missed state, both ways.
* cc -DBOARD_SMK37=1 -Ifirmware/hal -o smk_encoder_test tests/smk_encoder_test.c */
#include <stdio.h>
#include "smk_input.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); fails++; } } while (0)
static void set_state(uint32_t e, uint32_t st) /* bit 0 A, bit 1 B: their matrix contacts */
{
uint32_t i;
for (i = 0; i < 2u; i++) {
uint32_t code = SMK_ENCC[e][i];
if ((st >> i) & 1u)
fm1_in.raw[code >> 4] |= 1u << (code & 15u);
else
fm1_in.raw[code >> 4] &= ~(1u << (code & 15u));
}
}
static int32_t take(uint32_t e) { int32_t s = fm1_in.enc_steps[e]; fm1_in.enc_steps[e] = 0; return s; }
/* turn encoder e by n detents (sign = way), frames_per_state frames each state (0: some states missed) */
static const uint8_t CW[4] = {0, 2, 3, 1}; /* the +1 way: 0 -> 2 -> 3 -> 1 -> 0 */
static void turn(uint32_t e, int32_t n, uint32_t fps, uint32_t skip_every)
{
int32_t i, k = 0, dir = n > 0 ? 1 : -1, steps = (n > 0 ? n : -n) * 4;
uint32_t f, idx = 0;
for (i = 0; i < steps; i++) {
idx = (idx + 4u + (uint32_t)dir) % 4u;
if (skip_every && ++k % (int32_t)skip_every == 0 && i != steps - 1)
continue; /* this state is never seen (too fast for the frame) */
set_state(e, CW[idx]);
for (f = 0; f < (fps ? fps : 1u); f++)
smk__encoders();
}
}
int main(void)
{
uint32_t e = 3;
set_state(e, 0);
smk__encoders(); /* the first frame: rest state 0 */
turn(e, 5, 3, 0);
CHECK(take(e) == 5, "slow: 5 detents");
turn(e, -5, 3, 0);
CHECK(take(e) == -5, "slow back: -5");
turn(e, 20, 1, 0);
CHECK(take(e) == 20, "fast (one frame a state): 20");
turn(e, -20, 1, 0);
CHECK(take(e) == -20, "fast back: -20");
turn(e, 30, 1, 3);
CHECK(take(e) == 30, "faster (every third state missed): 30");
turn(e, -1, 2, 0); /* (a turn back starts slow: a missed state is read the way
* the knob was going) */
turn(e, -29, 1, 2);
CHECK(take(e) == -30, "faster back (every second state missed): -30");
{ /* bounce at rest and half a detent there and back: nothing */
set_state(e, 2); smk__encoders(); set_state(e, 0); smk__encoders();
set_state(e, 2); smk__encoders(); set_state(e, 3); smk__encoders(); set_state(e, 2); smk__encoders();
set_state(e, 0); smk__encoders();
CHECK(take(e) == 0, "bounce / half a detent: nothing");
}
{ /* bouncy contacts while turning (each state flickers back once): still one per detent */
int32_t i;
for (i = 0; i < 6; i++) {
set_state(e, 2); smk__encoders(); set_state(e, 0); smk__encoders(); set_state(e, 2); smk__encoders();
set_state(e, 3); smk__encoders(); set_state(e, 1); smk__encoders(); set_state(e, 3); smk__encoders();
set_state(e, 1); smk__encoders(); set_state(e, 0); smk__encoders();
}
CHECK(take(e) == 6, "bouncy: 6 detents");
}
if (fails)
return 1;
printf("smk_encoder_test: ok\n");
return 0;
}