Browse Source

Enable periodic digit cycling

Kevin Lee 2 years ago
parent
commit
6ca96ace45
2 changed files with 21 additions and 16 deletions
  1. 7 6
      Nixie_Firmware_Rust/src/main.rs
  2. 14 10
      Nixie_Firmware_Rust/src/nixie.rs

+ 7 - 6
Nixie_Firmware_Rust/src/main.rs

@@ -88,6 +88,7 @@ fn main() -> ! {
         .hclk(64.mhz())
         .pclk1(64.mhz())
         .pclk2(64.mhz())
+        .hsi48(true)
         .freeze(&mut flash.acr, &mut pwr);
 
     // Configure delay timer that operates off systick timer
@@ -257,7 +258,7 @@ fn main() -> ! {
         CYCLE_TIMER.borrow(cs).replace(Some(cycle_timer));
     });
 
-    // let rng = dp.RNG.enable(&mut rcc.ahb2, clocks);
+    let rng = dp.RNG.enable(&mut rcc.ahb2, clocks);
 
     // Enable the high voltage power supply last
     hv_enable.set_high().unwrap();
@@ -268,13 +269,13 @@ fn main() -> ! {
     nixie::cycle_start(3);
 
     loop {
-        // let rng_delay =
-        //     nixie::RNG_REFRESH_INTERVAL + (rng.get_random_data() % nixie::RNG_REFRESH_VARIANCE);
+        // let rng_delay = nixie::CYCLE_REFRESH_INTERVAL + (rng.get_random_data() % nixie::CYCLE_REFRESH_VARIANCE);
+        let rng_delay = nixie::CYCLE_REFRESH_INTERVAL;
 
-        // delay_timer.delay_ms(rng_delay * 1000);
+        delay_timer.delay_ms(rng_delay * 1000);
 
-        // let tube = (rng.get_random_data() % 4) as usize;
-        // nixie::rng_refresh(tube);
+        let tube = (rng.get_random_data() % 4) as usize;
+        nixie::cycle_start(tube);
     }
 }
 

+ 14 - 10
Nixie_Firmware_Rust/src/nixie.rs

@@ -1,4 +1,4 @@
-use core::{cell::RefCell, ops::DerefMut, panic};
+use core::{cell::RefCell, ops::DerefMut};
 
 use cortex_m::interrupt::{free, Mutex};
 use stm32l4xx_hal::{
@@ -22,7 +22,7 @@ pub const PCA9685_SUB_CALL_1: u8 = 0x71; // Default disabled
 pub const PCA9685_SUB_CALL_2: u8 = 0x72; // Default disabled
 pub const PCA9685_SUB_CALL_3: u8 = 0x73; // Default disabled
 
-pub const DISPLAY_REFRESH_FPS: u32 = 1000;
+pub const DISPLAY_REFRESH_FPS: u32 = 500;
 
 pub const DIGIT_FADE_DURATION_MS: u32 = 1000;
 pub const CYCLE_FADE_DURATION_MS: u32 = 200;
@@ -279,11 +279,12 @@ struct CycleSettings {
     last_digit: Option<u32>,
     next_digit: u32,
     iteration: usize,
+    last_fade_duration: u32,
 }
 
 struct Tube {
     digits: [Digit; NUM_DIGITS],
-    last_active_digit: Option<u32>,
+    last_digit: Option<u32>,
     cycle: Option<CycleSettings>,
 }
 
@@ -292,7 +293,7 @@ impl Tube {
         const DIGIT_INIT: Digit = Digit::default();
         Self {
             digits: [DIGIT_INIT; 10],
-            last_active_digit: None,
+            last_digit: None,
             cycle: None,
         }
     }
@@ -309,6 +310,7 @@ impl Tube {
         if let Some(ref mut cycle) = self.cycle {
             if !cycle_cmd {
                 cycle.last_digit = digit;
+                cycle.last_fade_duration = fade_duration;
             }
         }
 
@@ -330,7 +332,7 @@ impl Tube {
                 }
             }
 
-            self.last_active_digit = digit;
+            self.last_digit = digit;
         }
     }
 }
@@ -455,7 +457,7 @@ impl Clock {
             tube.digits.iter_mut().for_each(|digit| {
 
                 if let Some(fade_duration) = digit.fade_duration {
-                    let ticks = fade_duration  * 1000 / DISPLAY_REFRESH_FPS;
+                    let ticks = fade_duration  * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
                     let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
                     update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
                 }
@@ -467,7 +469,7 @@ impl Clock {
             for (d, digit) in tube.digits.iter_mut().enumerate() {
 
                 if let Some(fade_duration) = digit.fade_duration {
-                    let ticks = fade_duration * 1000 / DISPLAY_REFRESH_FPS;
+                    let ticks = fade_duration  * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
                     let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
                     update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
                 }
@@ -483,7 +485,7 @@ impl Clock {
 
         // Handle dot
         if let Some(fade_duration) = self.dot.fade_duration {
-            let ticks = fade_duration * 1000 / DISPLAY_REFRESH_FPS;
+            let ticks = fade_duration  * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
             let steps = ((DOT_MAX_BRIGHTNESS - DOT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
             update_fn(&mut self.dot, DOT_MIN_BRIGHTNESS, DOT_MAX_BRIGHTNESS, steps);
         }
@@ -517,8 +519,9 @@ impl Clock {
                     cycle_ended = false;
                 } else {
                     let last_digit = cycle.last_digit;
+                    let last_fade = cycle.last_fade_duration;
                     tube.cycle = None;
-                    tube.fade_in_out_digit(last_digit, CYCLE_FADE_DURATION_MS, true);
+                    tube.fade_in_out_digit(last_digit, last_fade, false);
                 }
             }
         });
@@ -725,9 +728,10 @@ pub fn cycle_start(tube: usize) {
         let clock = clock_ref.deref_mut();
 
         clock.tubes[tube].cycle = Some(CycleSettings {
-            last_digit: clock.tubes[tube].last_active_digit,
+            last_digit: clock.tubes[tube].last_digit,
             next_digit: 0,
             iteration: CYCLE_ITERATIONS,
+            last_fade_duration: DIGIT_FADE_DURATION_MS,
         });
     });