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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use anyhow::{Result, anyhow};
pub mod buffer;
use crate::protocol;
use buffer::Buffer;
use palette::Srgb;
use serde_json::Value;
use std::cmp::max;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::mem;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("MetaParsingError")]
pub struct MetaParsingError;
#[derive(Debug)]
pub struct Button {
pub x: f64,
pub y: f64,
pub h: f64,
pub w: f64,
pub wire_x: u8,
pub wire_y: u8,
pub layout_options: Option<(u8, u8)>,
pub encoder: bool,
pub decal: bool,
pub color: Option<(u8, u8, u8)>,
}
impl Button {
pub fn scale(&self, scale: f64) -> Button {
Button {
x: self.x * scale,
y: self.y * scale,
h: self.h * scale,
w: self.w * scale,
wire_x: self.wire_x,
wire_y: self.wire_y,
layout_options: self.layout_options,
encoder: self.encoder,
decal: self.decal,
color: self.color,
}
}
}
fn matches(options: &[(u8, u8)], option: Option<(u8, u8)>) -> bool {
//return true;
match option {
Some(o) => {
if options.len() > o.1.into() {
options[o.0 as usize].1 == o.1
} else {
false
}
}
None => true,
}
}
pub fn get_encoders_count(keymap: &Value) -> Result<u8> {
let mut result = 0;
if let Some(rows) = keymap.as_array() {
for row in rows.iter() {
if let Some(items) = row.as_array() {
for item in items {
if let Value::String(label) = item {
let parts: Vec<_> = label.split("\n").collect();
if parts.len() > 9
&& parts[9].starts_with("e")
&& let Some((index, direction)) = parts[0].split_once(",")
&& direction == "0"
{
let index: u8 = index.parse()?;
result = max(result, index + 1);
}
}
}
}
}
}
Ok(result)
}
pub fn keymap_to_buttons(
keymap: &Value,
current_options: &protocol::LayoutOptions,
) -> Result<Vec<Button>> {
let via_options = current_options.via_options();
let mut option_groups = HashMap::<u8, (f64, f64)>::new();
let mut buttons = Vec::new();
let rows = keymap
.as_array()
.ok_or(anyhow!("keymap should be an array"))?;
let mut x_pos = 0f64;
let mut y_pos = 0f64;
let mut rx = 0f64;
let mut ry = 0f64;
let mut w = 1f64;
let mut h = 1f64;
let mut r = 0f64;
let mut decal = false;
let mut cluster: (f64, f64) = (0.0, 0.0);
let mut color: Option<(u8, u8, u8)> = None;
for row in rows.iter() {
match row.as_array() {
Some(items) => {
for item in items {
match item {
Value::Object(item) => {
for (key, value) in item {
match key.as_str() {
"x" => {
let x = value
.as_f64()
.ok_or(anyhow!("x should be a number"))?;
x_pos += x;
}
"y" => {
let y = value
.as_f64()
.ok_or(anyhow!("y should be a number"))?;
y_pos += y;
}
"w" => {
w = value.as_f64().ok_or(anyhow!("w should be a number"))?
}
"h" => {
h = value.as_f64().ok_or(anyhow!("h should be a number"))?
}
"r" => {
r = value.as_f64().ok_or(anyhow!("r should be a number"))?
}
"rx" => {
rx = value
.as_f64()
.ok_or(anyhow!("rx should be a number"))?;
cluster.0 = rx;
x_pos = cluster.0;
y_pos = cluster.1;
}
"ry" => {
ry = value
.as_f64()
.ok_or(anyhow!("ry should be a number"))?;
cluster.1 = ry;
x_pos = cluster.0;
y_pos = cluster.1;
}
"d" => {
decal =
value.as_bool().ok_or(anyhow!("d should be bool"))?
}
"c" => {
let rgb: Srgb<u8> = value
.as_str()
.ok_or(anyhow!("r should be a string"))?
.parse()?;
let (r, g, b) = rgb.into_components();
color = Some((r, b, g));
}
&_ => {
// println!("warning ignored value {:?} = {:?}", key, value)
}
}
}
}
Value::String(item) => {
// skip decals entirely
let labels: Vec<_> = item.split("\n").collect();
let (wire, option, encoder) = if labels.len() < 4 {
(labels[0], None, false)
} else if labels.len() < 10 {
(labels[0], Some(labels[3]), false)
} else {
(labels[0], Some(labels[3]), labels[9].starts_with("e"))
};
let (xx, yy): (u8, u8) = if let Some((xxx, yyy)) = wire.split_once(',')
{
if let (Ok(x), Ok(y)) = (xxx.parse(), yyy.parse()) {
(x, y)
} else {
(0, 0)
}
} else {
(0, 0)
};
let layout_options = match option {
Some(s) => {
if let Some((l, r)) = s.split_once(',') {
let (l, r) = (l.parse()?, r.parse()?);
if r == 0 {
option_groups.entry(l).or_insert((x_pos, y_pos));
}
Some((l, r))
} else {
None
}
}
None => None,
};
let but = if r == 0.0 && rx == 0.0 && ry == 0.0 {
/*
println!(
"p = {},{}, r = {:?}, rx = {:?}, ry = {:?}, x_pos = {:?}, y_pos = {:?}",
xx, yy, r, rx, ry, x_pos, y_pos,
);
*/
let bx = x_pos;
let by = y_pos;
let bw = w;
let bh = h;
Button {
x: bx,
y: by,
w: bw,
h: bh,
wire_x: xx,
wire_y: yy,
layout_options,
encoder,
decal,
color,
}
} else {
/*
println!(
"p = {},{}, r = {:?}, rx = {:?}, ry = {:?}, x_pos = {:?}, y_pos = {:?}, w = {:?}, h = {:?}",
xx, yy, r, rx, ry, x_pos, y_pos, w, h,
);
*/
let theta = -r.to_radians();
let theta_sin = theta.sin();
let theta_cos = theta.cos();
let bx;
let by;
let x = x_pos - rx;
let y = y_pos - ry;
if r >= 0.0 {
if r < 45.0 {
bx = x * theta_cos + y * theta_sin + rx;
by = -x * theta_sin + y * theta_cos + ry;
} else {
bx = x * theta_cos + y * theta_sin - h + rx;
by = -x * theta_sin + y * theta_cos + ry;
mem::swap(&mut w, &mut h);
}
} else {
// for negative angle rotate right corner
// and shift back -w
// otherwise mirrored part will be
// vertically shifted
if r > -45.0 {
bx = (x + w) * theta_cos + y * theta_sin - w + rx;
by = -(x + w) * theta_sin + y * theta_cos + ry;
} else {
bx = (x + w) * theta_cos + y * theta_sin + rx;
by = -(x + w) * theta_sin + y * theta_cos + ry;
mem::swap(&mut w, &mut h);
}
}
Button {
x: bx,
y: by,
w,
h,
wire_x: xx,
wire_y: yy,
layout_options: None,
encoder,
decal,
color,
}
//return Err(MetaParsingError.into());
};
if matches(&via_options, layout_options) || decal {
buttons.push(but);
}
x_pos += w;
w = 1.0;
h = 1.0;
decal = false;
//println!("! {:?} => {:?}", item.as_str().unwrap(), &but);
}
_ => {
return Err(MetaParsingError.into());
}
}
}
}
None => {
// sometimes first element is dict we should compensate y_pos increment
y_pos -= 1.0;
}
}
y_pos += 1.0;
x_pos = rx;
}
// this logic tries to follow via layout_options choices in a following way
// option_groups contains coordinates of first default (x, 0) button
// for first button code replaces current choice coordinates (x, current) with coordinates from
// option_groups then calculates and stores delta between default and current choice
// for following buttons it applies delta to current coordinates
let mut deltas = HashMap::new();
for button in &mut buttons {
match button.layout_options {
Some(option) => {
if matches(&via_options, Some(option)) {
match deltas.entry(option.0) {
Entry::Vacant(v) => {
if let Some((def_x, def_y)) = option_groups.get(&option.0) {
//println!("{:?} => {:?}", button, (def_x, def_y));
let dx = button.x - *def_x;
let dy = button.y - *def_y;
button.x = *def_x;
button.y = *def_y;
v.insert_entry((dx, dy));
}
}
Entry::Occupied(o) => {
let (dx, dy) = o.get();
//println!("{:?}", (dx, dy));
button.x -= dx;
button.y -= dy;
}
}
}
}
None => {
//do nothing
}
}
}
Ok(buttons)
}
pub fn render_and_dump(buttons: &Vec<Button>, labels: Option<HashMap<(u8, u8), String>>) {
let mut buff = Buffer::new();
for button in buttons {
if !button.decal {
let scale = 4.0;
let b = button.scale(scale);
let lu = (b.x.round() as usize, b.y.round() as usize);
let ru = ((b.x + b.w - 1.0).round() as usize, b.y.round() as usize);
let lb = (b.x.round() as usize, (b.y + b.h - 1.0).round() as usize);
let rb = (
(b.x + b.w - 1.0).round() as usize,
(b.y + b.h - 1.0).round() as usize,
);
if !b.encoder {
buff.put(lu.0, lu.1, '╔', &b.color);
for x in (lu.0 + 1)..ru.0 {
buff.put(x, lu.1, '═', &b.color);
}
buff.put(ru.0, ru.1, '╗', &b.color);
for y in (lu.1 + 1)..lb.1 {
buff.put(lu.0, y, '║', &b.color);
}
for y in (ru.1 + 1)..rb.1 {
buff.put(ru.0, y, '║', &b.color);
}
buff.put(lb.0, lb.1, '╚', &b.color);
for x in (lb.0 + 1)..rb.0 {
buff.put(x, lb.1, '═', &b.color);
}
buff.put(rb.0, rb.1, '╝', &b.color);
} else {
buff.put(lu.0, lu.1, '╭', &b.color);
for x in (lu.0 + 1)..ru.0 {
buff.put(x, lu.1, '─', &b.color);
}
buff.put(ru.0, ru.1, '╮', &b.color);
for y in (lu.1 + 1)..lb.1 {
buff.put(lu.0, y, '│', &b.color);
}
for y in (ru.1 + 1)..rb.1 {
buff.put(ru.0, y, '│', &b.color);
}
buff.put(lb.0, lb.1, '╰', &b.color);
for x in (lb.0 + 1)..rb.0 {
buff.put(x, lb.1, '─', &b.color);
}
buff.put(rb.0, rb.1, '╯', &b.color);
}
for x in (lu.0 + 1)..ru.0 {
for y in (lu.1 + 1)..lb.1 {
buff.put(x, y, ' ', &b.color);
}
}
let label_x_shift = if b.w < 3.0 { 1 } else { 0 };
let label_y_shift = if b.h < 3.0 { 1 } else { 0 };
match labels {
Some(ref labels) => {
if b.encoder {
let label = format!(
"{}{}",
b.wire_x,
match b.wire_y {
0 => '↺',
1 => '↻',
_ => 'x',
}
);
for (i, c) in label.chars().enumerate() {
buff.put(
lu.0 + 1 - label_x_shift + i,
lu.1 - label_y_shift + 1,
c,
&b.color,
);
}
} else {
match labels.get(&(b.wire_x, b.wire_y)) {
Some(label) => {
// FIXME comma treatment is too ugly :( but works
let mut we_got_comma = false;
for (line, chunk) in label.split(',').enumerate() {
if chunk.is_empty() {
if !we_got_comma {
buff.put(
lu.0 + 1 - label_x_shift + line,
lu.1 - label_y_shift + 1,
',',
&b.color,
);
we_got_comma = true;
}
} else {
for (i, c) in chunk.chars().enumerate() {
buff.put(
lu.0 + 1 - label_x_shift + i,
lu.1 + 1 - label_y_shift + line,
c,
&b.color,
);
}
}
}
}
None => {
// No label => empty button
}
}
}
}
None => {
let xx = format!("{}", b.wire_x);
let yy = format!("{}", b.wire_y);
for (i, c) in xx.chars().enumerate() {
buff.put(
lu.0 + 1 - label_x_shift + i,
lu.1 - label_y_shift + 1,
c,
&b.color,
);
}
for (i, c) in yy.chars().enumerate() {
buff.put(
lu.0 + 1 - label_x_shift + i,
lu.1 - label_y_shift + 2,
c,
&b.color,
);
}
}
}
}
}
buff.dump();
}