fix: Stop mixing binary and text serial protocols

Root cause of keyboard malfunction: binary SET/DELETE commands
followed by text QUERY commands confused the firmware state machine.

Changes:
- All create/delete (combo, leader, KO) back to TEXT protocol
  (matching stable egui desktop behavior)
- TD_SET kept binary (isolated, no text query after)
- 50ms delay between all consecutive serial commands
- Removed dead v2 check in refresh_advanced
- Added delay between KEYSTATS? and BIGRAMS? queries

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mae PUGIN 2026-04-07 10:50:18 +02:00
parent a8f621e3d6
commit ca21a6766d

View file

@ -877,7 +877,8 @@ fn main() {
let lines = ser.query_command("KEYSTATS?").unwrap_or_default(); let lines = ser.query_command("KEYSTATS?").unwrap_or_default();
let (data, max) = logic::parsers::parse_heatmap_lines(&lines); let (data, max) = logic::parsers::parse_heatmap_lines(&lines);
let _ = tx.send(BgMsg::HeatmapData(data, max)); let _ = tx.send(BgMsg::HeatmapData(data, max));
// Also fetch bigrams // Also fetch bigrams (delay to avoid serial confusion)
std::thread::sleep(std::time::Duration::from_millis(50));
let bigram_lines = ser.query_command("BIGRAMS?").unwrap_or_default(); let bigram_lines = ser.query_command("BIGRAMS?").unwrap_or_default();
let _ = tx.send(BgMsg::BigramLines(bigram_lines)); let _ = tx.send(BgMsg::BigramLines(bigram_lines));
}); });
@ -894,12 +895,7 @@ fn main() {
let tx = tx.clone(); let tx = tx.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
// Use binary protocol if v2, text fallback otherwise let queries = [("td", "TD?"), ("combo", "COMBO?"), ("leader", "LEADER?"), ("ko", "KO?"), ("bt", "BT?")];
let queries: &[(&str, &str)] = if ser.v2 {
&[("td", "TD?"), ("combo", "COMBO?"), ("leader", "LEADER?"), ("ko", "KO?"), ("bt", "BT?")]
} else {
&[("td", "TD?"), ("combo", "COMBO?"), ("leader", "LEADER?"), ("ko", "KO?"), ("bt", "BT?")]
};
for (tag, cmd) in queries { for (tag, cmd) in queries {
std::thread::sleep(std::time::Duration::from_millis(50)); std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command(cmd).unwrap_or_default(); let lines = ser.query_command(cmd).unwrap_or_default();
@ -917,10 +913,11 @@ fn main() {
window.global::<AdvancedBridge>().on_delete_combo(move |idx| { window.global::<AdvancedBridge>().on_delete_combo(move |idx| {
let serial = serial.clone(); let serial = serial.clone();
let tx = tx.clone(); let tx = tx.clone();
let payload = vec![idx as u8]; let cmd = logic::protocol::cmd_combodel(idx as u8);
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
let _ = ser.send_binary(logic::binary_protocol::cmd::COMBO_DELETE, &payload); let _ = ser.send_command(&cmd);
std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command("COMBO?").unwrap_or_default(); let lines = ser.query_command("COMBO?").unwrap_or_default();
let _ = tx.send(BgMsg::TextLines("combo".into(), lines)); let _ = tx.send(BgMsg::TextLines("combo".into(), lines));
}); });
@ -935,10 +932,11 @@ fn main() {
window.global::<AdvancedBridge>().on_delete_leader(move |idx| { window.global::<AdvancedBridge>().on_delete_leader(move |idx| {
let serial = serial.clone(); let serial = serial.clone();
let tx = tx.clone(); let tx = tx.clone();
let payload = vec![idx as u8]; let cmd = logic::protocol::cmd_leaderdel(idx as u8);
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
let _ = ser.send_binary(logic::binary_protocol::cmd::LEADER_DELETE, &payload); let _ = ser.send_command(&cmd);
std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command("LEADER?").unwrap_or_default(); let lines = ser.query_command("LEADER?").unwrap_or_default();
let _ = tx.send(BgMsg::TextLines("leader".into(), lines)); let _ = tx.send(BgMsg::TextLines("leader".into(), lines));
}); });
@ -953,10 +951,11 @@ fn main() {
window.global::<AdvancedBridge>().on_delete_ko(move |idx| { window.global::<AdvancedBridge>().on_delete_ko(move |idx| {
let serial = serial.clone(); let serial = serial.clone();
let tx = tx.clone(); let tx = tx.clone();
let payload = vec![idx as u8]; let cmd = logic::protocol::cmd_kodel(idx as u8);
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
let _ = ser.send_binary(logic::binary_protocol::cmd::KO_DELETE, &payload); let _ = ser.send_command(&cmd);
std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command("KO?").unwrap_or_default(); let lines = ser.query_command("KO?").unwrap_or_default();
let _ = tx.send(BgMsg::TextLines("ko".into(), lines)); let _ = tx.send(BgMsg::TextLines("ko".into(), lines));
}); });
@ -1063,12 +1062,13 @@ fn main() {
w.global::<AppState>().set_status_text("Pick both keys first".into()); w.global::<AppState>().set_status_text("Pick both keys first".into());
return; return;
} }
let payload = logic::binary_protocol::combo_set_payload(255, r1, c1, r2, c2, result); let cmd = logic::protocol::cmd_comboset(255, r1, c1, r2, c2, result);
let serial = serial.clone(); let serial = serial.clone();
let tx = tx.clone(); let tx = tx.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
let _ = ser.send_binary(logic::binary_protocol::cmd::COMBO_SET, &payload); let _ = ser.send_command(&cmd);
std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command("COMBO?").unwrap_or_default(); let lines = ser.query_command("COMBO?").unwrap_or_default();
let _ = tx.send(BgMsg::TextLines("combo".into(), lines)); let _ = tx.send(BgMsg::TextLines("combo".into(), lines));
}); });
@ -1087,12 +1087,13 @@ fn main() {
let trig_mod = mod_idx_to_byte(trig_mod_idx); let trig_mod = mod_idx_to_byte(trig_mod_idx);
let result = result_code as u8; let result = result_code as u8;
let res_mod = mod_idx_to_byte(res_mod_idx); let res_mod = mod_idx_to_byte(res_mod_idx);
let payload = logic::binary_protocol::ko_set_payload(255, trig, trig_mod, result, res_mod); let cmd = logic::protocol::cmd_koset(255, trig, trig_mod, result, res_mod);
let serial = serial.clone(); let serial = serial.clone();
let tx = tx.clone(); let tx = tx.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
let _ = ser.send_binary(logic::binary_protocol::cmd::KO_SET, &payload); let _ = ser.send_command(&cmd);
std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command("KO?").unwrap_or_default(); let lines = ser.query_command("KO?").unwrap_or_default();
let _ = tx.send(BgMsg::TextLines("ko".into(), lines)); let _ = tx.send(BgMsg::TextLines("ko".into(), lines));
}); });
@ -1119,12 +1120,13 @@ fn main() {
if count > 3 { sequence.push(adv.get_new_leader_seq3_code() as u8); } if count > 3 { sequence.push(adv.get_new_leader_seq3_code() as u8); }
let result = result_code as u8; let result = result_code as u8;
let result_mod = mod_idx_to_byte(mod_idx); let result_mod = mod_idx_to_byte(mod_idx);
let payload = logic::binary_protocol::leader_set_payload(255, &sequence, result, result_mod); let cmd = logic::protocol::cmd_leaderset(255, &sequence, result, result_mod);
let serial = serial.clone(); let serial = serial.clone();
let tx = tx.clone(); let tx = tx.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner());
let _ = ser.send_binary(logic::binary_protocol::cmd::LEADER_SET, &payload); let _ = ser.send_command(&cmd);
std::thread::sleep(std::time::Duration::from_millis(50));
let lines = ser.query_command("LEADER?").unwrap_or_default(); let lines = ser.query_command("LEADER?").unwrap_or_default();
let _ = tx.send(BgMsg::TextLines("leader".into(), lines)); let _ = tx.send(BgMsg::TextLines("leader".into(), lines));
}); });