@@ -5,6 +5,7 @@ use chrono::Local;
5
5
use image:: codecs:: gif:: GifDecoder ;
6
6
use image:: { io:: Reader as ImageReader , Luma } ;
7
7
use image:: { AnimationDecoder , DynamicImage , ImageBuffer } ;
8
+ use rand:: prelude:: * ;
8
9
use serialport:: { SerialPort , SerialPortInfo , SerialPortType } ;
9
10
10
11
use crate :: b1display:: { B1Pattern , Fps , PowerMode } ;
@@ -620,6 +621,26 @@ fn display_gray_image_cmd(serialdev: &str, image_path: &str) {
620
621
commit_cols ( & mut port) ;
621
622
}
622
623
624
+ /// Display an equlizer looking animation with random values.
625
+ fn random_eq_cmd ( serialdevs : & Vec < String > ) {
626
+ loop {
627
+ // Lower values more likely, makes it look nicer
628
+ //weights = [i*i for i in range(33, 0, -1)]
629
+ let population: Vec < u8 > = ( 1 ..34 ) . collect ( ) ;
630
+ let mut rng = thread_rng ( ) ;
631
+ let vals = population
632
+ . choose_multiple_weighted ( & mut rng, 9 , |item| ( 34 - item) ^ 2 )
633
+ . unwrap ( )
634
+ . copied ( )
635
+ . collect :: < Vec < _ > > ( ) ;
636
+ for serialdev in serialdevs {
637
+ eq_cmd ( serialdev, vals. as_slice ( ) ) ;
638
+ }
639
+ thread:: sleep ( Duration :: from_millis ( 200 ) ) ;
640
+ }
641
+ }
642
+
643
+
623
644
/// The data-type for storing analyzer results
624
645
#[ derive( Debug , Clone ) ]
625
646
pub struct AnalyzerResult {
@@ -628,75 +649,6 @@ pub struct AnalyzerResult {
628
649
beat : f32 ,
629
650
}
630
651
631
- /// Display an equlizer looking animation with random values.
632
- fn random_eq_cmd ( serialdevs : & Vec < String > ) {
633
- // Example from https://github.com/Rahix/visualizer2/blob/canon/README.md
634
-
635
- // Initialize the logger. Take a look at the sources if you want to customize
636
- // the logger.
637
- vis_core:: default_log ( ) ;
638
-
639
- // Load the default config source. More about config later on. You can also
640
- // do this manually if you have special requirements.
641
- vis_core:: default_config ( ) ;
642
-
643
- // Initialize some analyzer-tools. These will be moved into the analyzer closure
644
- // later on.
645
- let mut analyzer = vis_core:: analyzer:: FourierBuilder :: new ( )
646
- . length ( 512 )
647
- . window ( vis_core:: analyzer:: window:: nuttall)
648
- . plan ( ) ;
649
-
650
- let spectrum = vis_core:: analyzer:: Spectrum :: new ( vec ! [ 0.0 ; analyzer. buckets( ) ] , 0.0 , 1.0 ) ;
651
-
652
- let mut frames = vis_core:: Visualizer :: new (
653
- AnalyzerResult {
654
- spectrum,
655
- volume : 0.0 ,
656
- beat : 0.0 ,
657
- } ,
658
- // This closure is the "analyzer". It will be executed in a loop to always
659
- // have the latest data available.
660
- move |info, samples| {
661
- analyzer. analyze ( samples) ;
662
-
663
- info. spectrum . fill_from ( & analyzer. average ( ) ) ;
664
- info. volume = samples. volume ( 0.3 ) * 400.0 ;
665
- info. beat = info. spectrum . slice ( 50.0 , 100.0 ) . max ( ) * 0.01 ;
666
- info
667
- } ,
668
- )
669
- // Build the frame iterator which is the base of your loop later on
670
- . frames ( ) ;
671
-
672
- for frame in frames. iter ( ) {
673
- // This is just a primitive example, your vis core belongs here
674
-
675
- frame. info ( |info| {
676
- // for n in 0..info.volume as usize {
677
- // print!("{}",n);
678
- // }
679
- for serialdev in serialdevs {
680
- eq_cmd (
681
- serialdev,
682
- & [
683
- info. volume as u8 ,
684
- info. volume as u8 ,
685
- info. volume as u8 ,
686
- info. volume as u8 ,
687
- info. volume as u8 ,
688
- info. volume as u8 ,
689
- info. volume as u8 ,
690
- info. volume as u8 ,
691
- info. volume as u8 ,
692
- ] ,
693
- )
694
- }
695
- } ) ;
696
- thread:: sleep ( Duration :: from_millis ( 30 ) ) ;
697
- }
698
- }
699
-
700
652
// Equalizer-like animation that expands as volume goes up and retracts as it goes down
701
653
fn input_eq_cmd ( serialdevs : & Vec < String > ) {
702
654
// Example from https://github.com/Rahix/visualizer2/blob/canon/README.md
0 commit comments