Browse Source

Prelim support for refreshing digits

Kevin Lee 5 years ago
parent
commit
852407bee6
2 changed files with 50 additions and 15 deletions
  1. 45 15
      Nixie_Firmware_Mbed/main.cpp
  2. 5 0
      Nixie_Firmware_Mbed/main.h

+ 45 - 15
Nixie_Firmware_Mbed/main.cpp

@@ -10,7 +10,7 @@
 I2C i2c(PA_10, PA_9);
 // SWO_Channel swo("swo");
 
-bool RtcTick, RefreshTick;
+bool RtcTick, RefreshTick, RngTick;
 bool StartupFlag, RuntimeFlag;
 
 typedef struct {
@@ -18,6 +18,7 @@ typedef struct {
     ushort PrevValue;   // Brightness of previous digit
     char NextDigit;     // Next digit to fade in
     ushort NextValue;   // Brightness of next digit
+    bool RefreshActive;
 } Digit;
 
 Digit Digits[NUM_DIGITS] = {0};  // Active per-tube digit setting
@@ -25,27 +26,33 @@ Digit Digits[NUM_DIGITS] = {0};  // Active per-tube digit setting
 int DotNextValue, DotPrevValue;
 bool DotIncrement = false;
 
-Ticker StartupTicker, RuntimeTicker;
-int StartupTickerIter, RuntimeTickerIter;
+Ticker StartupUpdateTicker, RuntimeUpdateTicker, RngUpdateTicker;
+int StartupTickerIter, RuntimeTickerIter, RngTickerIter;
 
-void RtcCallback(void) {
+void RtcTickCallback(void) {
     RtcTick = true;
 }
 
-void RefreshCallback(void) {
+void RefreshTickCallback(void) {
     RefreshTick = true;
 }
 
-void StartupTickerCallback(void) {
+void RngTickCallback(void) {
+    RngTick = true;
+}
+
+void StartupTickerUpdate(void) {
 
     for (int i = 0; i < NUM_DIGITS; i++) {
-        Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
+        if (!Digits[i].RefreshActive) {
+            Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
+        }
     }
 
     DotNextValue += FADE_TICK_STEP;
 
     if (--StartupTickerIter == 0) {
-        StartupTicker.detach();
+        StartupUpdateTicker.detach();
         for (int i = 0; i < NUM_DIGITS; i++) {
             Digits[i].PrevDigit = Digits[i].NextDigit;
         }
@@ -55,10 +62,10 @@ void StartupTickerCallback(void) {
     }
 }
 
-void RuntimeTickerCallback(void) {
+void RuntimeTickerUpdate(void) {
 
     for (int i = 0; i < NUM_DIGITS; i++) {
-        if(Digits[i].PrevDigit != Digits[i].NextDigit) {
+        if(!Digits[i].RefreshActive && Digits[i].PrevDigit != Digits[i].NextDigit) {
             Digits[i].PrevValue -= FADE_TICK_STEP;
             Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
         }
@@ -67,7 +74,7 @@ void RuntimeTickerCallback(void) {
     DotNextValue = DotIncrement ? DotNextValue + FADE_TICK_STEP: DotNextValue - FADE_TICK_STEP;
 
     if (--RuntimeTickerIter == 0) {
-        RuntimeTicker.detach();
+        RuntimeUpdateTicker.detach();
         for (int i = 0; i < NUM_DIGITS; i++) {
             Digits[i].PrevDigit = Digits[i].NextDigit;
         }
@@ -75,10 +82,19 @@ void RuntimeTickerCallback(void) {
     }
 }
 
+void RngTickerUpdate(void) {
+
+
+    if (--RngTickerIter == 0) {
+        RngUpdateTicker.detach();
+    }
+}
+
 int main() {
     
     RtcTick = false;
     RefreshTick = false;
+    RngTick = false;
     StartupFlag = true;
     RuntimeFlag = false;
 
@@ -90,7 +106,7 @@ int main() {
     
     TUSB322_Init();
     PCA9685_Init();
-    DS3231_Init(RtcCallback);
+    DS3231_Init(RtcTickCallback);
     
     // Enable HV PSU
     HV_EnableOutput(true);
@@ -105,7 +121,11 @@ int main() {
 
     // Setup a ticker to refresh the display at 1kHz
     Ticker refreshTicker;
-    refreshTicker.attach_us(RefreshCallback, 1000);
+    refreshTicker.attach_us(RefreshTickCallback, DIGIT_REFRESH_RATE_US);
+
+    // Kick off the RNG ticker at 0.1Hz
+    Ticker rngTicker;
+    rngTicker.attach(RngTickCallback, DIGIT_RNG_REFRESH_RATE_S);
 
     // DS3231_SetTime(0, 55, 6, true);
     // DS3231_SetDate(0, 2, 12, 18, 0);
@@ -165,7 +185,7 @@ int main() {
 
                 // Fade in the current time
                 StartupTickerIter = FADE_TICKS;
-                StartupTicker.attach(StartupTickerCallback, 1/FADE_TICKS);
+                StartupUpdateTicker.attach(StartupTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/StartupTickerIter);
                 StartupFlag = false;
             } 
 
@@ -173,9 +193,19 @@ int main() {
 
                 // Fade in the new time
                 RuntimeTickerIter = FADE_TICKS;
-                RuntimeTicker.attach(RuntimeTickerCallback, 1/FADE_TICKS);
+                RuntimeUpdateTicker.attach(RuntimeTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/RuntimeTickerIter);
             }
         }
+
+        if (RngTick) {
+
+            // Choose a random tube to refresh
+            Digits[rand() % 4].RefreshActive = true;
+
+            RngTickerIter = DIGIT_RNG_REFRESH_ITER * FADE_TICKS;
+            RngUpdateTicker.attach(RngTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/FADE_TICKS);
+            RngTick = false;
+        }
     }
 }
 

+ 5 - 0
Nixie_Firmware_Mbed/main.h

@@ -20,6 +20,11 @@
 #define FADE_TICKS 1024
 #define FADE_TICK_STEP (PCA9685_Max_Brightness / FADE_TICKS)
 
+#define DIGIT_REFRESH_RATE_US 1000
+
+#define DIGIT_RNG_REFRESH_RATE_S 30
+#define DIGIT_RNG_REFRESH_ITER 20
+
 void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length);
 void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length);