-
Notifications
You must be signed in to change notification settings - Fork 0
/
chordgen.rb
246 lines (203 loc) · 5.94 KB
/
chordgen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# coding: utf-8
require 'pp'
$syms = {
# ЙЦУКЕН letters
'а' => 'RU_A',
'б' => 'RU_B',
'в' => 'RU_V',
'г' => 'RU_G',
'д' => 'RU_D',
'е' => 'RU_JE',
'ё' => 'RU_JO',
'ж' => 'RU_ZH',
'з' => 'RU_Z',
'и' => 'RU_I',
'й' => 'RU_J',
'к' => 'RU_K',
'л' => 'RU_L',
'м' => 'RU_M',
'н' => 'RU_N',
'о' => 'RU_O',
'п' => 'RU_P',
'р' => 'RU_R',
'с' => 'RU_S',
'т' => 'RU_T',
'у' => 'RU_U',
'ф' => 'RU_F',
'х' => 'RU_H',
'ц' => 'RU_TS',
'ч' => 'RU_CH',
'ш' => 'RU_SH',
'щ' => 'RU_SHCH',
'ъ' => 'RU_HARD',
'ы' => 'RU_Y',
'ь' => 'RU_SOFT',
'э' => 'RU_E',
'ю' => 'RU_JU',
'я' => 'RU_JA',
# First layer QWERTY symbols
'.' => 'KC_DOT',
',' => 'KC_COMM',
'/' => 'KC_SLSH',
'\\' => 'KC_BSLS',
'`' => 'KC_GRAVE',
'[' => 'KC_LBRC',
']' => 'KC_RBRC',
'-' => 'KC_MINS',
'=' => 'KC_EQUAL',
}
('a'..'z').each { |ch| $syms[ch] = "KC_#{ch.upcase}" }
('0'..'9').each { |ch| $syms[ch] = "KC_#{ch}" }
def kc(k)
$syms[k]
end
if ARGV.include? '-h' or ARGV.include? '--help'
puts <<-EOK
chordgen.rb
© Timur Ismagilov 2019
Chordgen is part of QMK Bonus project:
https://github.com/klavarog/qmk_bonus
-h, --help
Print this message and exit.
--count-chords
Print number of chords to generate and exit. Output is meant to be used as
value of #define COMBO_COUNT in your config.h. Chords will not be printed if
you use this option.
--config <path>
Explicitly set <path> as your config file. If you do not set it,
‘./chords.ini’ is used by default.
--print-syms
Pretty-print hashmap that contains character definitions after processing
config file and exit. Chords will not be printed if you use this option.
EOK
exit
end
should_count_chords = ARGV.include? '--count-chords'
should_print_syms = ARGV.include? '--print-syms'
config_path = ARGV.include?('--config') ?
ARGV[ARGV.index('--config') + 1] :
'./chords.ini'
class String
def ini_header?
/^\[(.*)\]$/.match?(self)
end
def as_ini_header
/^\[(.*)\]$/.match(self)[1]
end
def empty_line?
self.strip.empty? or self.start_with? '//'
end
def in_parens?
self[0] == '(' and self[-1] == ')'
end
def sans_parens
self[1..-2]
end
def scln
self.insert(-2, ';')
end
end
def brace_expr(title, block)
<<-EOK
#{title} {
#{block}
}
EOK
end
$chord_count = 0
class Chord
attr_reader :combo_event, :combo_array, :combo_key, :case_expr
def initialize(left_hand, right_hand)
$chord_count += 1
result_is_kc = not(left_hand.in_parens? or
($syms.include?(left_hand) and
kc(left_hand).in_parens?))
result = result_is_kc ?
"tap_code16(#{kc left_hand});\n" :
(($syms.include?(left_hand) and
kc(left_hand).in_parens?) ?
kc(left_hand).sans_parens :
left_hand)
result_id = (result.hash.abs + right_hand.hash.abs).to_s
combo_event_id = "combo_event_#{result_id}"
combo_array_id = "combo_array_#{result_id}"
keys = right_hand.chars.map { |k| kc k }
@combo_event = "#{combo_event_id}, //#{result.chomp}\n "
@combo_array =
brace_expr("const uint16_t PROGMEM #{combo_array_id}[] =",
"#{keys.join(', ')}, COMBO_END").scln
@combo_key =
"[#{combo_event_id}] = COMBO_ACTION(#{combo_array_id}),\n "
@case_expr =
brace_expr(" case #{combo_event_id}:",
brace_expr(' if (pressed)', result) + "return;")
end
end
class Layer
attr_reader :combo_events, :combo_arrays, :combo_keys, :combo_switch
def initialize(name, chords)
@combo_events = chords.map(&:combo_event).join
@combo_arrays = chords.map(&:combo_array).join
@combo_keys = chords.map(&:combo_key) .join
@combo_switch =
brace_expr(name == 'any' ? "if (1)" : "if (layer_state & (1 << #{name}))",
brace_expr(' switch (combo_index)',
chords.map(&:case_expr).join))
end
end
class ChordedKeeb
def initialize(layers)
@combo_events_enum =
brace_expr("enum combo_events", layers.map(&:combo_events).join).scln
@combo_arrays_declaration =
layers.map(&:combo_arrays).join
@combo_keys_array =
brace_expr("combo_t key_combos[COMBO_COUNT] =",
layers.map(&:combo_keys).join).scln
@process_combo_event =
brace_expr('void process_combo_event(uint8_t combo_index, bool pressed)',
layers.map(&:combo_switch).join(' else '))
end
def as_string
@combo_events_enum +
@combo_arrays_declaration +
@combo_keys_array +
@process_combo_event
end
end
read_result = []
curr_section = {:header => nil, :contents => []}
File.open(config_path) do |f|
while true
f.gets
read_result << curr_section and break if $_ == nil # On last line
next if $_.empty_line?
if $_.ini_header?
read_result << curr_section
curr_section = {header: $_.as_ini_header, contents: []}
else
tmp = $_.strip.split('=')
curr_section[:contents] << [tmp[0..-2].join("="), tmp[-1]]
end
end
end
# First element is promised to be invalid, remove it:
read_result.shift
# Array of all [override] sections.
overrides = read_result.select { |rr| rr[:header] == 'override'}
# Applying all overrides. Multiple [override] sections are supported.
overrides.inject(&:merge)[:contents].each { |ovr| $syms[ovr[0]] = ovr[1] }
layers = (read_result - overrides)
.delete_if {|l| l[:contents].empty? }
.map do |rr|
Layer.new(rr[:header],
rr[:contents].map { |c| Chord.new(c[0], c[1])})
end
puts $chord_count if should_count_chords
pp $syms if should_print_syms
if not (should_count_chords or should_print_syms)
puts '#ifndef KLAVAROG_CHORDGEN'
puts '#define KLAVAROG_CHORDGEN'
puts ChordedKeeb.new(layers).as_string
puts '#endif'
end