-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathinputmodule.rs
More file actions
1225 lines (1071 loc) · 39.6 KB
/
inputmodule.rs
File metadata and controls
1225 lines (1071 loc) · 39.6 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::thread;
use std::time::Duration;
use chrono::Local;
use image::codecs::gif::GifDecoder;
use image::{io::Reader as ImageReader, Luma};
use image::{AnimationDecoder, DynamicImage, ImageBuffer};
use rand::prelude::*;
use serialport::{SerialPort, SerialPortInfo, SerialPortType};
use crate::b1display::{B1Pattern, Fps, PowerMode};
use crate::c1minimal::Color;
use crate::font::{convert_font, convert_symbol};
use crate::ledmatrix::{Game, GameOfLifeStartParam, Pattern};
const FWK_MAGIC: &[u8] = &[0x32, 0xAC];
pub const FRAMEWORK_VID: u16 = 0x32AC;
pub const LED_MATRIX_PID: u16 = 0x0020;
pub const B1_LCD_PID: u16 = 0x0021;
type Brightness = u8;
// TODO: Use a shared enum with the firmware code
#[derive(Clone, Copy)]
#[repr(u8)]
enum Command {
Brightness = 0x00,
Pattern = 0x01,
Bootloader = 0x02,
Sleeping = 0x03,
Animate = 0x04,
Panic = 0x05,
DisplayBwImage = 0x06,
SendCol = 0x07,
CommitCols = 0x08,
_B1Reserved = 0x09,
StartGame = 0x10,
GameControl = 0x11,
_GameStatus = 0x12,
SetColor = 0x13,
DisplayOn = 0x14,
InvertScreen = 0x15,
SetPixelColumn = 0x16,
FlushFramebuffer = 0x17,
ClearRam = 0x18,
ScreenSaver = 0x19,
Fps = 0x1A,
PowerMode = 0x1B,
AnimationPeriod = 0x1C,
PwmFreq = 0x1E,
DebugMode = 0x1F,
Version = 0x20,
}
enum GameControlArg {
_Up = 0,
_Down = 1,
_Left = 2,
_Right = 3,
Exit = 4,
_SecondLeft = 5,
_SecondRight = 6,
}
const WIDTH: usize = 9;
const HEIGHT: usize = 34;
const SERIAL_TIMEOUT: Duration = Duration::from_millis(20);
fn match_serialdevs(
ports: &[SerialPortInfo],
requested: &Option<String>,
pid: Option<u16>,
) -> Vec<String> {
if let Some(requested) = requested {
for p in ports {
if requested == &p.port_name {
return vec![p.port_name.clone()];
}
}
vec![]
} else {
let mut compatible_devs = vec![];
let pids = if let Some(pid) = pid {
vec![pid]
} else {
// By default accept any type
vec![LED_MATRIX_PID, B1_LCD_PID, 0x22, 0xFF]
};
// Find all supported Framework devices
for p in ports {
if let SerialPortType::UsbPort(usbinfo) = &p.port_type {
// macOS creates a /dev/cu.* and /dev/tty.* device.
// The latter can only be used for reading, not writing, so we have to ignore it.
#[cfg(target_os = "macos")]
if !p.port_name.starts_with("/dev/tty.") {
continue;
}
if usbinfo.vid == FRAMEWORK_VID && pids.contains(&usbinfo.pid) {
compatible_devs.push(p.port_name.clone());
}
}
}
compatible_devs
}
}
/// Make sure device is awake before sending non sleep ignoring commands
fn enforce_awake(port: &mut Box<dyn SerialPort>) {
simple_cmd_port(port, Command::Sleeping, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
if response[0] == 0 {
return;
}
println!("Device is asleep. Waking up device.");
simple_cmd_port(port, Command::Sleeping, &[0]);
// Timeout increased thanks to ledmatrix taking up to 12 seconds to wake up.
port.set_timeout(Duration::from_secs(15))
.expect("Failed to set timeout");
simple_cmd_port(port, Command::Sleeping, &[]);
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
if response[0] == 1 {
panic!("Device did not respond to wake up command!");
}
port.set_timeout(SERIAL_TIMEOUT)
.expect("Failed to set timeout");
}
pub fn find_serialdevs(args: &crate::ClapCli, wait_for_device: bool) -> (Vec<String>, bool) {
let mut serialdevs: Vec<String>;
let mut waited = false;
loop {
let ports = serialport::available_ports().expect("No ports found!");
if args.list || args.verbose {
for p in &ports {
match &p.port_type {
SerialPortType::UsbPort(usbinfo) => {
println!("{}", p.port_name);
println!(" VID {:#06X}", usbinfo.vid);
println!(" PID {:#06X}", usbinfo.pid);
if let Some(sn) = &usbinfo.serial_number {
println!(" SN {}", sn);
}
if let Some(product) = &usbinfo.product {
// TODO: Seems to replace the spaces with underscore, not sure why
println!(" Product {}", product);
}
}
_ => {
//println!("{}", p.port_name);
//println!(" Unknown (PCI Port)");
}
}
}
}
serialdevs = match_serialdevs(
&ports,
&args.serial_dev,
args.command.as_ref().map(|x| x.to_pid()),
);
if serialdevs.is_empty() {
if wait_for_device {
// Waited at least once, that means the device was not present
// when the program started
waited = true;
// Try again after short wait
thread::sleep(Duration::from_millis(100));
continue;
} else {
return (vec![], waited);
}
} else {
break;
}
}
(serialdevs, waited)
}
/// Commands that interact with serial devices
pub fn serial_commands(args: &crate::ClapCli) {
let (serialdevs, waited): (Vec<String>, bool) = find_serialdevs(args, args.wait_for_device);
if serialdevs.is_empty() {
println!("Failed to find serial device. Please manually specify with --serial-dev");
return;
} else if args.wait_for_device && !waited {
println!("Device already present. No need to wait. Not executing command. Sleep 1s");
thread::sleep(Duration::from_millis(1000));
return;
}
match &args.command {
// TODO: Handle generic commands without code deduplication
Some(crate::Commands::LedMatrix(ledmatrix_args)) => {
for serialdev in &serialdevs {
if args.verbose {
println!("Selected serialdev: {:?}", serialdev);
}
if ledmatrix_args.bootloader {
bootloader_cmd(serialdev);
}
if let Some(sleeping_arg) = ledmatrix_args.sleeping {
sleeping_cmd(serialdev, sleeping_arg);
}
if let Some(brightness_arg) = ledmatrix_args.brightness {
brightness_cmd(serialdev, brightness_arg);
}
if let Some(percentage) = ledmatrix_args.percentage {
assert!(percentage <= 100);
percentage_cmd(serialdev, percentage);
}
if let Some(animate_arg) = ledmatrix_args.animate {
animate_cmd(serialdev, animate_arg);
}
if let Some(pattern) = ledmatrix_args.pattern {
pattern_cmd(serialdev, pattern);
}
if ledmatrix_args.all_brightnesses {
all_brightnesses_cmd(serialdev);
}
if ledmatrix_args.panic {
simple_cmd(serialdev, Command::Panic, &[0x00]);
}
if let Some(image_path) = &ledmatrix_args.image_bw {
display_bw_image_cmd(serialdev, image_path);
}
if let Some(image_path) = &ledmatrix_args.image_gray {
display_gray_image_cmd(serialdev, image_path);
}
if let Some(values) = &ledmatrix_args.eq {
eq_cmd(serialdev, values);
}
if let Some(s) = &ledmatrix_args.string {
show_string(serialdev, s);
}
if let Some(symbols) = &ledmatrix_args.symbols {
show_symbols(serialdev, symbols);
}
if let Some(game) = ledmatrix_args.start_game {
start_game_cmd(serialdev, game, ledmatrix_args.game_param);
}
if let Some(fps) = ledmatrix_args.animation_fps {
animation_fps_cmd(serialdev, fps);
}
if let Some(freq) = ledmatrix_args.pwm_freq {
pwm_freq_cmd(serialdev, freq);
}
if let Some(debug_mode) = ledmatrix_args.debug_mode {
debug_mode_cmd(serialdev, debug_mode);
}
if ledmatrix_args.stop_game {
simple_cmd(
serialdev,
Command::GameControl,
&[GameControlArg::Exit as u8],
);
}
if ledmatrix_args.version {
get_device_version(serialdev);
}
}
// Commands that block and need manual looping
if ledmatrix_args.blinking {
blinking_cmd(&serialdevs);
}
if ledmatrix_args.breathing {
breathing_cmd(&serialdevs);
}
if ledmatrix_args.random_eq {
random_eq_cmd(&serialdevs);
}
#[cfg(feature = "audio-visualizations")]
if ledmatrix_args.input_eq {
input_eq_cmd(&serialdevs);
}
if ledmatrix_args.clock {
clock_cmd(&serialdevs);
}
}
Some(crate::Commands::B1Display(b1display_args)) => {
for serialdev in &serialdevs {
if args.verbose {
println!("Selected serialdev: {:?}", serialdev);
}
if b1display_args.bootloader {
bootloader_cmd(serialdev);
}
if let Some(sleeping_arg) = b1display_args.sleeping {
sleeping_cmd(serialdev, sleeping_arg);
}
if b1display_args.panic {
simple_cmd(serialdev, Command::Panic, &[0x00]);
}
if b1display_args.version {
get_device_version(serialdev);
}
if let Some(display_on) = b1display_args.display_on {
display_on_cmd(serialdev, display_on);
}
if let Some(invert_screen) = b1display_args.invert_screen {
invert_screen_cmd(serialdev, invert_screen);
}
if let Some(screensaver_on) = b1display_args.screen_saver {
screensaver_cmd(serialdev, screensaver_on);
}
if let Some(fps) = b1display_args.fps {
fps_cmd(serialdev, fps);
}
if let Some(power_mode) = b1display_args.power_mode {
power_mode_cmd(serialdev, power_mode);
}
if let Some(fps) = b1display_args.animation_fps {
animation_fps_cmd(serialdev, fps);
}
if let Some(image_path) = &b1display_args.image {
b1display_bw_image_cmd(serialdev, image_path);
}
if let Some(image_path) = &b1display_args.animated_gif {
gif_cmd(serialdev, image_path);
}
if b1display_args.clear_ram {
simple_cmd(serialdev, Command::ClearRam, &[0x00]);
}
if let Some(pattern) = b1display_args.pattern {
b1_display_pattern(serialdev, pattern);
}
}
}
Some(crate::Commands::C1Minimal(c1minimal_args)) => {
for serialdev in &serialdevs {
if args.verbose {
println!("Selected serialdev: {:?}", serialdev);
}
if c1minimal_args.bootloader {
bootloader_cmd(serialdev);
}
if let Some(sleeping_arg) = c1minimal_args.sleeping {
sleeping_cmd(serialdev, sleeping_arg);
}
if c1minimal_args.panic {
simple_cmd(serialdev, Command::Panic, &[0x00]);
}
if c1minimal_args.version {
get_device_version(serialdev);
}
if let Some(color) = c1minimal_args.set_color {
set_color_cmd(serialdev, color);
}
}
}
_ => {}
}
}
fn get_device_version(serialdev: &str) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::Version, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let major = response[0];
let minor = (response[1] & 0xF0) >> 4;
let patch = response[1] & 0x0F;
let pre_release = response[2] == 1;
print!("Device Version: {major}.{minor}.{patch}");
if pre_release {
print!(" (Pre-Release)");
}
println!();
}
fn bootloader_cmd(serialdev: &str) {
simple_cmd(serialdev, Command::Bootloader, &[0x00]);
}
fn percentage_cmd(serialdev: &str, arg: u8) {
simple_cmd(
serialdev,
Command::Pattern,
&[Pattern::Percentage as u8, arg],
);
}
fn pattern_cmd(serialdev: &str, arg: Pattern) {
simple_cmd(serialdev, Command::Pattern, &[arg as u8]);
}
fn start_game_cmd(serialdev: &str, game: Game, param: Option<GameOfLifeStartParam>) {
match (game, param) {
(Game::GameOfLife, Some(param)) => {
simple_cmd(serialdev, Command::StartGame, &[game as u8, param as u8])
}
(Game::GameOfLife, None) => {
println!("To start Game of Life, provide a --game-param");
}
(_, _) => simple_cmd(serialdev, Command::StartGame, &[game as u8]),
}
}
fn simple_cmd_multiple(serialdevs: &Vec<String>, command: Command, args: &[u8]) {
for serialdev in serialdevs {
simple_cmd(serialdev, command, args);
}
}
fn simple_cmd(serialdev: &str, command: Command, args: &[u8]) {
let port_result = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open();
match port_result {
Ok(mut port) => simple_cmd_port(&mut port, command, args),
Err(error) => match error.kind {
serialport::ErrorKind::Io(std::io::ErrorKind::PermissionDenied) => panic!("Permission denied, couldn't access inputmodule serialport. Ensure that you have permission, for example using a udev rule or sudo."),
other_error => panic!("Couldn't open port: {:?}", other_error)
}
};
}
fn open_serialport(serialdev: &str) -> Box<dyn SerialPort> {
serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port")
}
fn simple_open_cmd(serialport: &mut Box<dyn SerialPort>, command: Command, args: &[u8]) {
simple_cmd_port(serialport, command, args);
}
fn simple_cmd_port(port: &mut Box<dyn SerialPort>, command: Command, args: &[u8]) {
let mut buffer: [u8; 64] = [0; 64];
buffer[..2].copy_from_slice(FWK_MAGIC);
buffer[2] = command as u8;
buffer[3..3 + args.len()].copy_from_slice(args);
port.write_all(&buffer[..3 + args.len()])
.expect("Write failed!");
}
fn sleeping_cmd(serialdev: &str, arg: Option<bool>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(goto_sleep) = arg {
simple_cmd_port(&mut port, Command::Sleeping, &[u8::from(goto_sleep)]);
} else {
simple_cmd_port(&mut port, Command::Sleeping, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let sleeping: bool = response[0] == 1;
println!("Currently sleeping: {sleeping}");
}
}
fn debug_mode_cmd(serialdev: &str, arg: Option<bool>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(enable_debug) = arg {
simple_cmd_port(&mut port, Command::DebugMode, &[u8::from(enable_debug)]);
} else {
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::DebugMode, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let debug_mode: bool = response[0] == 1;
println!("Debug Mode enabled: {debug_mode}");
}
}
fn brightness_cmd(serialdev: &str, arg: Option<u8>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(brightness) = arg {
simple_cmd_port(&mut port, Command::Brightness, &[brightness]);
} else {
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::Brightness, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let brightness: u8 = response[0];
println!("Current brightness: {brightness}");
}
}
fn animate_cmd(serialdev: &str, arg: Option<bool>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(animate) = arg {
simple_cmd_port(&mut port, Command::Animate, &[animate as u8]);
} else {
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::Animate, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let animating = response[0] == 1;
println!("Currently animating: {animating}");
}
}
/// Stage greyscale values for a single column. Must be committed with commit_cols()
fn send_col(port: &mut Box<dyn SerialPort>, x: u8, vals: &[u8]) {
let mut buffer: [u8; 64] = [0; 64];
buffer[0] = x;
buffer[1..vals.len() + 1].copy_from_slice(vals);
simple_cmd_port(port, Command::SendCol, &buffer[0..vals.len() + 1]);
}
/// Commit the changes from sending individual cols with send_col(), displaying the matrix.
/// This makes sure that the matrix isn't partially updated.
fn commit_cols(port: &mut Box<dyn SerialPort>) {
simple_cmd_port(port, Command::CommitCols, &[]);
}
///Increase the brightness with each pixel.
///Only 0-255 available, so it can't fill all 306 LEDs
fn all_brightnesses_cmd(serialdev: &str) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
for x in 0..WIDTH {
let mut vals: [u8; HEIGHT] = [0; HEIGHT];
for y in 0..HEIGHT {
let brightness = x + WIDTH * y;
vals[y] = if brightness > 255 { 0 } else { brightness } as u8;
}
send_col(&mut port, x as u8, &vals);
}
commit_cols(&mut port);
}
fn blinking_cmd(serialdevs: &Vec<String>) {
let duration = Duration::from_millis(500);
loop {
simple_cmd_multiple(serialdevs, Command::Brightness, &[0]);
thread::sleep(duration);
simple_cmd_multiple(serialdevs, Command::Brightness, &[200]);
thread::sleep(duration);
}
}
fn breathing_cmd(serialdevs: &Vec<String>) {
loop {
// Go quickly from 250 to 50
for i in 0..40 {
simple_cmd_multiple(serialdevs, Command::Brightness, &[250 - i * 5]);
thread::sleep(Duration::from_millis(25));
}
// Go slowly from 50 to 0
for i in 0..50 {
simple_cmd_multiple(serialdevs, Command::Brightness, &[50 - i]);
thread::sleep(Duration::from_millis(10));
}
// Go slowly from 0 to 50
for i in 0..50 {
simple_cmd_multiple(serialdevs, Command::Brightness, &[i]);
thread::sleep(Duration::from_millis(10));
}
// Go quickly from 50 to 250
for i in 0..40 {
simple_cmd_multiple(serialdevs, Command::Brightness, &[50 + i * 5]);
thread::sleep(Duration::from_millis(25));
}
}
}
/// Display an image in black and white
/// Confirmed working with PNG and GIF.
/// Must be 9x34 in size.
/// Sends everything in a single command
fn display_bw_image_cmd(serialdev: &str, image_path: &str) {
let mut vals: [u8; 39] = [0; 39];
let img = ImageReader::open(image_path)
.unwrap()
.decode()
.unwrap()
.to_luma8();
let width = img.width();
let height = img.height();
assert!(width == 9);
assert!(height == 34);
for (x, y, pixel) in img.enumerate_pixels() {
let brightness = pixel.0[0];
if brightness > 0xFF / 2 {
let i = (x as usize) + (y as usize) * WIDTH;
vals[i / 8] |= 1 << (i % 8);
}
}
simple_cmd(serialdev, Command::DisplayBwImage, &vals);
}
// Calculate pixel brightness from an RGB triple
fn pixel_to_brightness(pixel: &Luma<u8>) -> u8 {
let brightness = pixel.0[0];
// Poor man's scaling to make the greyscale pop better.
// Should find a good function.
if brightness > 200 {
brightness
} else if brightness > 150 {
((brightness as u32) * 10 / 8) as u8
} else if brightness > 100 {
brightness / 2
} else if brightness > 50 {
brightness
} else {
brightness * 2
}
}
/// Display an image in greyscale
/// Sends each 1x34 column and then commits => 10 commands
fn display_gray_image_cmd(serialdev: &str, image_path: &str) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
let img = ImageReader::open(image_path)
.unwrap()
.decode()
.unwrap()
.to_luma8();
let width = img.width();
let height = img.height();
assert!(width == 9);
assert!(height == 34);
for x in 0..WIDTH {
let mut vals: [u8; HEIGHT] = [0; HEIGHT];
for y in 0..HEIGHT {
let pixel = img.get_pixel(x as u32, y as u32);
vals[y] = pixel_to_brightness(pixel);
}
send_col(&mut port, x as u8, &vals)
}
commit_cols(&mut port);
}
/// Display an equlizer looking animation with random values.
fn random_eq_cmd(serialdevs: &Vec<String>) {
loop {
// Lower values more likely, makes it look nicer
//weights = [i*i for i in range(33, 0, -1)]
let population: Vec<u8> = (1..34).collect();
let mut rng = thread_rng();
let vals = population
.choose_multiple_weighted(&mut rng, 9, |item| (34 - item) ^ 2)
.unwrap()
.copied()
.collect::<Vec<_>>();
for serialdev in serialdevs {
eq_cmd(serialdev, vals.as_slice());
}
thread::sleep(Duration::from_millis(200));
}
}
#[cfg(feature = "audio-visualizations")]
/// The data-type for storing analyzer results
#[derive(Debug, Clone)]
pub struct AnalyzerResult {
spectrum: vis_core::analyzer::Spectrum<Vec<f32>>,
volume: f32,
beat: f32,
}
#[cfg(feature = "audio-visualizations")]
// Equalizer-like animation that expands as volume goes up and retracts as it goes down
fn input_eq_cmd(serialdevs: &Vec<String>) {
// Example from https://github.com/Rahix/visualizer2/blob/canon/README.md
// Initialize the logger. Take a look at the sources if you want to customize
// the logger.
vis_core::default_log();
// Load the default config source. More about config later on. You can also
// do this manually if you have special requirements.
vis_core::default_config();
// Initialize some analyzer-tools. These will be moved into the analyzer closure
// later on.
let mut analyzer = vis_core::analyzer::FourierBuilder::new()
.length(512)
.window(vis_core::analyzer::window::nuttall)
.plan();
let spectrum = vis_core::analyzer::Spectrum::new(vec![0.0; analyzer.buckets()], 0.0, 1.0);
let mut frames = vis_core::Visualizer::new(
AnalyzerResult {
spectrum,
volume: 0.0,
beat: 0.0,
},
// This closure is the "analyzer". It will be executed in a loop to always
// have the latest data available.
move |info, samples| {
analyzer.analyze(samples);
info.spectrum.fill_from(&analyzer.average());
info.volume = samples.volume(0.3) * 400.0;
info.beat = info.spectrum.slice(50.0, 100.0).max() * 0.01;
info
},
)
// Build the frame iterator which is the base of your loop later on
.frames();
for frame in frames.iter() {
// This is just a primitive example, your vis core belongs here
frame.info(|info| {
let sampled_volume = info.volume;
let limited_volume = sampled_volume.min(34.0);
let display_max_widths = [10.0, 14.0, 20.0, 28.0, 34.0, 28.0, 20.0, 14.0, 10.0];
let volumes_to_display = display_max_widths
.iter()
.map(|x| {
let computed_width = (limited_volume / 34.0) * x;
let next_lowest_odd = computed_width - (computed_width % 2.0) - 1.0;
next_lowest_odd as u8
})
.collect::<Vec<_>>();
for serialdev in serialdevs {
eq_cmd(serialdev, volumes_to_display.as_slice())
}
});
thread::sleep(Duration::from_millis(30));
}
}
/// Display 9 values in equalizer diagram starting from the middle, going up and down
/// TODO: Implement a commandline parameter for this
fn eq_cmd(serialdev: &str, vals: &[u8]) {
assert!(vals.len() <= WIDTH);
let mut matrix: [[Brightness; 34]; 9] = [[0; 34]; 9];
for (col, val) in vals[..9].iter().enumerate() {
let row: usize = 34 / 2;
let above: usize = (*val as usize) / 2;
let below = (*val as usize) - above;
for i in 0..above {
matrix[col][row + i] = 0xFF; // Set this LED to full brightness
}
for i in 0..below {
matrix[col][row - 1 - i] = 0xFF; // Set this LED to full brightness
}
}
render_matrix(serialdev, &matrix);
}
/// Show a black/white matrix
/// Send everything in a single command
fn render_matrix(serialdev: &str, matrix: &[[u8; 34]; 9]) {
// One bit for each LED, on or off
// 39 = ceil(34 * 9 / 8)
let mut vals: [u8; 39] = [0x00; 39];
for x in 0..9 {
for y in 0..34 {
let i = x + 9 * y;
if matrix[x][y] == 0xFF {
vals[i / 8] |= 1 << (i % 8);
}
}
}
simple_cmd(serialdev, Command::DisplayBwImage, &vals);
}
/// Render the current time and display.
/// Loops forever, updating every second
fn clock_cmd(serialdevs: &Vec<String>) {
loop {
let date = Local::now();
let current_time = date.format("%H:%M").to_string();
println!("Current Time = {current_time}");
for serialdev in serialdevs {
show_string(serialdev, ¤t_time);
}
thread::sleep(Duration::from_millis(1000));
}
}
/// Render a string with up to five letters
fn show_string(serialdev: &str, s: &str) {
let items: Vec<Vec<u8>> = s.chars().take(5).map(convert_font).collect();
show_font(serialdev, &items);
}
/// Render up to five 5x6 pixel font items
fn show_font(serialdev: &str, font_items: &[Vec<u8>]) {
let mut vals: [u8; 39] = [0x00; 39];
for (digit_i, digit_pixels) in font_items.iter().enumerate() {
let offset = digit_i * 7;
for pixel_x in 0..5 {
for pixel_y in 0..6 {
let pixel_value = digit_pixels[pixel_x + pixel_y * 5];
let i = (2 + pixel_x) + (9 * (pixel_y + offset));
if pixel_value == 1 {
vals[i / 8] |= 1 << (i % 8);
}
}
}
}
simple_cmd(serialdev, Command::DisplayBwImage, &vals);
}
/// Render a list of up to five symbols
/// Can use letters/numbers or symbol names, like 'sun', ':)'
fn show_symbols(serialdev: &str, symbols: &Vec<String>) {
println!("Symbols: {symbols:?}");
let font_items: Vec<Vec<u8>> = symbols.iter().map(|x| convert_symbol(x)).collect();
show_font(serialdev, &font_items);
}
fn display_on_cmd(serialdev: &str, arg: Option<bool>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(display_on) = arg {
simple_cmd_port(&mut port, Command::DisplayOn, &[display_on as u8]);
} else {
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::DisplayOn, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let on = response[0] == 1;
println!("Currently on: {on}");
}
}
fn invert_screen_cmd(serialdev: &str, arg: Option<bool>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(invert_on) = arg {
simple_cmd_port(&mut port, Command::InvertScreen, &[invert_on as u8]);
} else {
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::InvertScreen, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let inverted = response[0] == 1;
println!("Currently inverted: {inverted}");
}
}
fn screensaver_cmd(serialdev: &str, arg: Option<bool>) {
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
if let Some(display_on) = arg {
simple_cmd_port(&mut port, Command::ScreenSaver, &[display_on as u8]);
} else {
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::ScreenSaver, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let on = response[0] == 1;
println!("Currently on: {on}");
}
}
fn fps_cmd(serialdev: &str, arg: Option<Fps>) {
const HIGH_FPS_MASK: u8 = 0b00010000;
const LOW_FPS_MASK: u8 = 0b00000111;
let mut port = serialport::new(serialdev, 115_200)
.timeout(SERIAL_TIMEOUT)
.open()
.expect("Failed to open port");
enforce_awake(&mut port);
simple_cmd_port(&mut port, Command::Fps, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let current_fps = response[0];
if let Some(fps) = arg {
let power_mode = match fps {
Fps::Sixteen | Fps::ThirtyTwo => PowerMode::High,
_ => PowerMode::Low,
};
let fps_bits = match fps {
Fps::Quarter => current_fps & !LOW_FPS_MASK,
Fps::Half => (current_fps & !LOW_FPS_MASK) | 0b001,
Fps::One => (current_fps & !LOW_FPS_MASK) | 0b010,
Fps::Two => (current_fps & !LOW_FPS_MASK) | 0b011,
Fps::Four => (current_fps & !LOW_FPS_MASK) | 0b100,
Fps::Eight => (current_fps & !LOW_FPS_MASK) | 0b101,
Fps::Sixteen => current_fps & !HIGH_FPS_MASK,
Fps::ThirtyTwo => (current_fps & !HIGH_FPS_MASK) | 0b00010000,
};
set_power_mode(&mut port, power_mode);
simple_cmd_port(&mut port, Command::Fps, &[fps_bits]);
} else {
simple_cmd_port(&mut port, Command::PowerMode, &[]);
let mut response: Vec<u8> = vec![0; 32];
port.read_exact(response.as_mut_slice())
.expect("Found no data!");
let high = response[0] == 1;
let fps = if high {
if current_fps & HIGH_FPS_MASK == 0 {
16.0
} else {
32.0
}
} else {
let current_fps = current_fps & LOW_FPS_MASK;
if current_fps == 0 {
0.25
} else if current_fps == 1 {
0.5