From ca21a6766ddfe54f1b29de720de0f2a4a3f1976a Mon Sep 17 00:00:00 2001 From: Mae PUGIN <48982737+mornepousse@users.noreply.github.com> Date: Tue, 7 Apr 2026 10:50:18 +0200 Subject: [PATCH] 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) --- src/main.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index d910653..3115023 100644 --- a/src/main.rs +++ b/src/main.rs @@ -877,7 +877,8 @@ fn main() { let lines = ser.query_command("KEYSTATS?").unwrap_or_default(); let (data, max) = logic::parsers::parse_heatmap_lines(&lines); 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 _ = tx.send(BgMsg::BigramLines(bigram_lines)); }); @@ -894,12 +895,7 @@ fn main() { let tx = tx.clone(); std::thread::spawn(move || { let mut ser = serial.lock().unwrap_or_else(|e| e.into_inner()); - // Use binary protocol if v2, text fallback otherwise - 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?")] - }; + let queries = [("td", "TD?"), ("combo", "COMBO?"), ("leader", "LEADER?"), ("ko", "KO?"), ("bt", "BT?")]; for (tag, cmd) in queries { std::thread::sleep(std::time::Duration::from_millis(50)); let lines = ser.query_command(cmd).unwrap_or_default(); @@ -917,10 +913,11 @@ fn main() { window.global::().on_delete_combo(move |idx| { let serial = serial.clone(); let tx = tx.clone(); - let payload = vec![idx as u8]; + let cmd = logic::protocol::cmd_combodel(idx as u8); std::thread::spawn(move || { 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 _ = tx.send(BgMsg::TextLines("combo".into(), lines)); }); @@ -935,10 +932,11 @@ fn main() { window.global::().on_delete_leader(move |idx| { let serial = serial.clone(); let tx = tx.clone(); - let payload = vec![idx as u8]; + let cmd = logic::protocol::cmd_leaderdel(idx as u8); std::thread::spawn(move || { 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 _ = tx.send(BgMsg::TextLines("leader".into(), lines)); }); @@ -953,10 +951,11 @@ fn main() { window.global::().on_delete_ko(move |idx| { let serial = serial.clone(); let tx = tx.clone(); - let payload = vec![idx as u8]; + let cmd = logic::protocol::cmd_kodel(idx as u8); std::thread::spawn(move || { 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 _ = tx.send(BgMsg::TextLines("ko".into(), lines)); }); @@ -1063,12 +1062,13 @@ fn main() { w.global::().set_status_text("Pick both keys first".into()); 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 tx = tx.clone(); std::thread::spawn(move || { 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 _ = tx.send(BgMsg::TextLines("combo".into(), lines)); }); @@ -1087,12 +1087,13 @@ fn main() { let trig_mod = mod_idx_to_byte(trig_mod_idx); let result = result_code as u8; 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 tx = tx.clone(); std::thread::spawn(move || { 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 _ = 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); } let result = result_code as u8; 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 tx = tx.clone(); std::thread::spawn(move || { 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 _ = tx.send(BgMsg::TextLines("leader".into(), lines)); });