Skip to content

Commit eb0f088

Browse files
committed
WIP dido parsing
1 parent 98d9b41 commit eb0f088

6 files changed

Lines changed: 351 additions & 0 deletions

File tree

‎Cargo.lock‎

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ clio = { version = "0.3.5", features = ["clap-parse"] }
1515
csv = "1.3.0"
1616
hex = "0.4.3"
1717
modular-bitfield = "0.11.2"
18+
nom = "7.1.3"
1819
serde = { version = "1.0.204", features = ["derive"] }

‎configs/CoolHand.txt‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# extracted from: coolhand.cfg
2+
# on: Monday, September 4, 2017 at 10:10:10 Eastern Daylight Time
3+
# using dido v1.0.0 by stauf https://github.com/CoohLand/Dido
4+
#
5+
# (please edit carefully)
6+
#
7+
sticky_num=false # Enable Stick Num Key, false=Off, true=On
8+
sticky_shift=false # Enable Stick Shift Key, false=Off, true=On
9+
click_mode=true # Joystick Mouse Click, false=Right-Click, true=Left-Click
10+
key_repeat=true # Enable Key Repeat, false=Off, true=On
11+
direct_key=false # Enable Direct Key Mode, false=Off, true=On
12+
bluetooth_off=false # Disable Bluetooth Radio, false=Enabled, true=Disabled
13+
# --- end of options
14+
sleep=900 # Sleep Timeout (seconds)
15+
mouse_speed=10 # Mouse Acceleration Factor
16+
repeat_delay=100 # Key Repeat Delay (default 100)
17+
# --- end of settings
18+
mouse_mid=false
19+
0000:057 :# Keyboard Caps Lock
20+
mouse_right=false
21+
0000:043 :# Keyboard Tab
22+
mouse_left=false
23+
0000:String[6]:
24+
# --- end of header
25+
# alt control
26+
# ||
27+
# num |||| shift
28+
# ▼▼▼▼
29+
# Chord Format: NACS XXXX:HHH+LCLSLALGRCRSRARG:# comment
30+
# [ chord ] ▲ [ modifier ]
31+
# | LC = Left Ctrl
32+
# keycode in dec RG = Right GUI
33+
#
34+
R000:012 :# Keyboard i and I
35+
N R000:019+RC :# Keyboard p and P
36+
M000:008 :# Keyboard e and E
37+
N M000:034 :# Keyboard 5 and %
38+
L000:018 :# Keyboard o and O
39+
N L000:082 :# Keyboard UpArrow
40+
0R00:017 :# Keyboard n and N
41+
N 0R00:017+RC :# Keyboard n and N
42+
RR00:052 :# Keyboard ' and "
43+
N RR00:075 :# Keyboard PageUp
44+
MR00:054 :# Keyboard , and <
45+
0M00:023 :# Keyboard t and T
46+
N 0M00:055 :# Keyboard . and >
47+
RM00:032+RS :# Keyboard 3 and #
48+
MM00:055 :# Keyboard . and >
49+
N MM00:035 :# Keyboard 6 and ^
50+
0L00:021 :# Keyboard r and R
51+
N 0L00:081 :# Keyboard DownArrow
52+
ML00:051 :# Keyboard ; and :
53+
LL00:045 :# Keyboard - and _
54+
00R0:004 :# Keyboard a and A
55+
N 00R0:004+RC :# Keyboard a and A
56+
R0R0:013 :# Keyboard j and J
57+
M0R0:038+RS :# Keyboard 9 and (
58+
0RR0:009 :# Keyboard f and F
59+
N 0RR0:078 :# Keyboard PageDown
60+
RRR0:005 :# Keyboard b and B
61+
N RRR0:005+RC :# Keyboard b and B
62+
MRR0:047 :# Keyboard [ and {
63+
0MR0:034+RS :# Keyboard 5 and %
64+
MMR0:047+RS :# Keyboard [ and {
65+
LMR0:037+RS :# Keyboard 8 and *
66+
00M0:011 :# Keyboard h and H
67+
N 00M0:080 :# Keyboard LeftArrow
68+
M0M0:015 :# Keyboard l and L
69+
0RM0:030+RS :# Keyboard 1 and !
70+
0MM0:007 :# Keyboard d and D
71+
N 0MM0:007+RC :# Keyboard d and D
72+
MMM0:027 :# Keyboard x and X
73+
N MMM0:036 :# Keyboard 7 and &
74+
LMM0:040 :# Keyboard Return (ENTER)
75+
0LM0:056+RS :# Keyboard / and ?
76+
00L0:022 :# Keyboard s and S
77+
N 00L0:079 :# Keyboard RightArrow
78+
R0L0:036+RS :# Keyboard 7 and &
79+
M0L0:039+RS :# Keyboard 0 and )
80+
L0L0:014 :# Keyboard k and K
81+
0ML0:045+RS :# Keyboard - and _
82+
MML0:048+RS :# Keyboard ] and }
83+
0LL0:024 :# Keyboard u and U
84+
N 0LL0:021+RC :# Keyboard r and R
85+
MLL0:048 :# Keyboard ] and }
86+
LLL0:028 :# Keyboard y and Y
87+
N LLL0:041 :# Keyboard ESCAPE
88+
S LL00:045+RS :# Keyboard - and _
89+
000R:042 :# Keyboard DELETE (Backspace)
90+
R00R:006 :# Keyboard c and C
91+
N R00R:006+RC :# Keyboard c and C
92+
M00R:020 :# Keyboard q and Q
93+
L00R:056 :# Keyboard / and ?
94+
0R0R:053 :# Keyboard ` and ~
95+
RR0R:String[0]:
96+
00RR:052+RS :# Keyboard ' and "
97+
N 00RR:031 :# Keyboard 2 and @
98+
R0RR:029+RS :# Keyboard z and Z
99+
N R0RR:029+RC :# Keyboard z and Z
100+
M0RR:054+RS :# Keyboard , and <
101+
0RRR:029 :# Keyboard z and Z
102+
N 0RRR:032 :# Keyboard 3 and #
103+
RRRR:String[1]:
104+
N RRRR:033 :# Keyboard 4 and $
105+
MRRR:String[2]:
106+
00MR:035+RS :# Keyboard 6 and ^
107+
000M:083 :# Keypad Num Lock and Clear
108+
R00M:006+RC :# Keyboard c and C
109+
M00M:016 :# Keyboard m and M
110+
L00M:005+RC :# Keyboard b and B
111+
0M0M:049+RS :# Keyboard \ and |
112+
MM0M:046 :# Keyboard = and +
113+
00MM:051+RS :# Keyboard ; and :
114+
M0MM:String[3]:
115+
N M0MM:023+RC :# Keyboard t and T
116+
0MMM:010 :# Keyboard g and G
117+
MMMM:043 :# Keyboard Tab
118+
N MMMM:037 :# Keyboard 8 and *
119+
LMMM:String[4]:
120+
000L:044 :# Keyboard Spacebar
121+
R00L:049 :# Keyboard \ and |
122+
M00L:019 :# Keyboard p and P
123+
L00L:026 :# Keyboard w and W
124+
0L0L:053+RS :# Keyboard ` and ~
125+
LL0L:046+RS :# Keyboard = and +
126+
00ML:033+RS :# Keyboard 4 and $
127+
00LL:031+RS :# Keyboard 2 and @
128+
N 00LL:038 :# Keyboard 9 and (
129+
M0LL:055+RS :# Keyboard . and >
130+
L0LL:String[5]:
131+
N L0LL:025+RC :# Keyboard v and V
132+
0LLL:025 :# Keyboard v and V
133+
N 0LLL:039 :# Keyboard 0 and )
134+
LLLL:041 :# Keyboard ESCAPE
135+
N LLLL:030 :# Keyboard 1 and !
136+
# --- end of chords
137+
# String[0]="I "
138+
012+RS # Keyboard i and I
139+
044 # Keyboard Spacebar
140+
# String[1]="ing "
141+
012 # Keyboard i and I
142+
017 # Keyboard n and N
143+
010 # Keyboard g and G
144+
044 # Keyboard Spacebar
145+
# String[2]="and "
146+
004 # Keyboard a and A
147+
017 # Keyboard n and N
148+
007 # Keyboard d and D
149+
044 # Keyboard Spacebar
150+
# String[3]="the "
151+
023 # Keyboard t and T
152+
011 # Keyboard h and H
153+
008 # Keyboard e and E
154+
044 # Keyboard Spacebar
155+
# String[4]="tion "
156+
023 # Keyboard t and T
157+
012 # Keyboard i and I
158+
018 # Keyboard o and O
159+
017 # Keyboard n and N
160+
044 # Keyboard Spacebar
161+
# String[5]="you "
162+
028 # Keyboard y and Y
163+
018 # Keyboard o and O
164+
024 # Keyboard u and U
165+
044 # Keyboard Spacebar
166+
# String[6]="E~a"
167+
041 # Keyboard ESCAPE
168+
053+RS # Keyboard ` and ~
169+
004 # Keyboard a and A
170+
# --- end of config

‎src/buttons.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub enum TwiddlerButtons {
88
F4L, F4M, F4R,
99
}
1010

11+
#[derive(Debug)]
1112
pub struct ButtonState {
1213
pub t1: bool,
1314
pub t2: bool,

‎src/dido.rs‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
use std::io::{BufRead, Read, Seek};
2+
3+
use nom::{
4+
branch::alt,
5+
bytes::streaming::{tag, take},
6+
character::{
7+
complete::{alphanumeric0, digit0},
8+
streaming::{alpha0, alpha1, digit1, space0, space1},
9+
},
10+
combinator::{map, opt, value},
11+
error::{ErrorKind, VerboseError},
12+
number::{complete::be_u32, streaming::be_u16},
13+
sequence::{delimited, pair, preceded, separated_pair, terminated, tuple},
14+
Needed,
15+
};
16+
17+
use crate::{buttons, twiddler5::Config};
18+
19+
enum ParseState {
20+
Options,
21+
Settings,
22+
Header,
23+
Chords,
24+
Strings,
25+
Done,
26+
}
27+
28+
pub(crate) fn parse<R: Read + Seek>(reader: &mut R) -> Result<Chord, Box<dyn std::error::Error>> {
29+
let mut parse_state = ParseState::Options;
30+
31+
let buffered = std::io::BufReader::new(reader);
32+
33+
for line in buffered.lines() {
34+
match line {
35+
Ok(line) => {
36+
if line.contains("---") {
37+
match line.as_str() {
38+
"# --- end of options" => parse_state = ParseState::Settings,
39+
"# --- end of settings" => parse_state = ParseState::Header,
40+
"# --- end of header" => parse_state = ParseState::Chords,
41+
"# --- end of chords" => parse_state = ParseState::Strings,
42+
"# --- end of strings" => parse_state = ParseState::Done,
43+
_ => {}
44+
}
45+
46+
continue;
47+
}
48+
49+
if line.starts_with("#") {
50+
continue;
51+
}
52+
53+
match parse_state {
54+
ParseState::Options => {
55+
// todo: parse options
56+
}
57+
ParseState::Settings => {
58+
// todo: parse settings
59+
}
60+
ParseState::Header => {}
61+
ParseState::Chords => {
62+
let chord = parse_chord_line(line);
63+
}
64+
ParseState::Strings => {
65+
// parse strings
66+
}
67+
ParseState::Done => {
68+
// done
69+
}
70+
}
71+
}
72+
Err(e) => {
73+
println!("error: {:?}", e);
74+
}
75+
}
76+
}
77+
78+
Ok(Chord {
79+
hid_modifier: "N".to_owned(),
80+
buttons: "M000".to_owned(),
81+
keycode_dec: "034".to_owned(),
82+
modifiers: "".to_owned(),
83+
comment: "Keyboard 5 and %".to_owned(),
84+
})
85+
}
86+
87+
struct Chord {
88+
hid_modifier: String,
89+
buttons: String,
90+
keycode_dec: String,
91+
modifiers: String,
92+
comment: String,
93+
}
94+
95+
#[derive(Debug)]
96+
enum ChordOutput<'a> {
97+
StringIndex(&'a str),
98+
HidCode(&'a str),
99+
}
100+
101+
pub fn parse_chord_line(line: String) {
102+
// NACS XXXX:HHH+LCLSLALGRCRSRARG:# comment
103+
104+
let mut parser = nom::sequence::tuple((
105+
map(
106+
separated_pair(alpha0, space1, alphanumeric0),
107+
|(thumb, finger): (&str, &str)| {
108+
(buttons::parse_notation(
109+
thumb.to_string(),
110+
finger.to_string(),
111+
),)
112+
},
113+
),
114+
nom::character::streaming::char(':'),
115+
alt((
116+
delimited(
117+
tag("String["),
118+
map(digit0, ChordOutput::StringIndex),
119+
nom::character::streaming::char(']'),
120+
),
121+
map(digit0, ChordOutput::HidCode),
122+
)),
123+
opt(preceded(
124+
nom::character::streaming::char('+'),
125+
alphanumeric0,
126+
)),
127+
space0,
128+
nom::character::streaming::char(':'),
129+
));
130+
131+
assert!(matches!(
132+
parser("1.2"),
133+
Err(nom::Err::Error(VerboseError { .. }))
134+
));
135+
136+
let result = parser(line.as_str());
137+
138+
match result {
139+
Ok((comment, (button_state, _, hid, out_mods, _, _))) => {
140+
println!("button_state: {:?}", button_state);
141+
println!("hid: {:?}", hid);
142+
println!("out_mods: {:?}", out_mods);
143+
println!("comment: {:?}", comment);
144+
}
145+
Err(e) => {
146+
println!("error: {:?}", e);
147+
}
148+
}
149+
}
150+
151+
#[cfg(test)]
152+
mod tests {
153+
use super::*;
154+
155+
#[test]
156+
fn it_works() {
157+
parse_chord_line("N M000:034 :# Keyboard 5 and %".to_owned());
158+
parse_chord_line(" LMR0:037+RS :# Keyboard 8 and *".to_owned());
159+
parse_chord_line(" LMMM:String[4]:".to_owned());
160+
}
161+
}

‎src/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use twiddler6::HidCommand;
66

77
mod buttons;
88
mod csv;
9+
mod dido;
910
mod hid;
1011
mod twiddler5;
1112
mod twiddler6;

0 commit comments

Comments
 (0)