KeSp_controller/src/logic/config_io.rs
Mae PUGIN d1d10b7d73 feat: Binary protocol v2, config import/export, Tools tab, new layout format
- Migrate all serial commands from ASCII text to binary KS/KR frame protocol
  (SETLAYER, TD_LIST, COMBO_LIST, LEADER_LIST, KO_LIST, etc.)
- Add config import/export as JSON (keymaps, tap dances, combos, KO, leaders, macros)
- Merge Flash + Layout Preview into single Tools tab
- Replace WPF tree layout JSON format with flat groups+keys format:
  - Top-level "keys" for absolute positioning (thumbs, isolated keys)
  - "groups" with x/y/r transform, keys inside use local coordinates
  - Coordinates in units (1u = 50px), w/h default 1u, r default 0
- Layout auto-refresh (5s timer) for live preview while editing externally
- Pretty-print JSON in layout preview and export

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 21:30:04 +02:00

65 lines
1.5 KiB
Rust

/// Import/export keyboard configuration as JSON.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct KeyboardConfig {
pub version: u32,
pub layer_names: Vec<String>,
/// keymaps[layer][row][col] = keycode (u16)
pub keymaps: Vec<Vec<Vec<u16>>>,
pub tap_dances: Vec<TdConfig>,
pub combos: Vec<ComboConfig>,
pub key_overrides: Vec<KoConfig>,
pub leaders: Vec<LeaderConfig>,
pub macros: Vec<MacroConfig>,
}
#[derive(Serialize, Deserialize)]
pub struct TdConfig {
pub index: u8,
pub actions: [u16; 4],
}
#[derive(Serialize, Deserialize)]
pub struct ComboConfig {
pub index: u8,
pub r1: u8,
pub c1: u8,
pub r2: u8,
pub c2: u8,
pub result: u16,
}
#[derive(Serialize, Deserialize)]
pub struct KoConfig {
pub trigger_key: u8,
pub trigger_mod: u8,
pub result_key: u8,
pub result_mod: u8,
}
#[derive(Serialize, Deserialize)]
pub struct LeaderConfig {
pub index: u8,
pub sequence: Vec<u8>,
pub result: u8,
pub result_mod: u8,
}
#[derive(Serialize, Deserialize)]
pub struct MacroConfig {
pub slot: u8,
pub name: String,
/// Steps as "kc:mod,kc:mod,..." hex string
pub steps: String,
}
impl KeyboardConfig {
pub fn to_json(&self) -> Result<String, String> {
serde_json::to_string_pretty(self).map_err(|e| e.to_string())
}
pub fn from_json(json: &str) -> Result<Self, String> {
serde_json::from_str(json).map_err(|e| e.to_string())
}
}