Browse Source

Refactored constants

Kevin Lee 5 years ago
parent
commit
9d2e0bddfa
2 changed files with 41 additions and 36 deletions
  1. 38 36
      Nixie_Firmware_Mbed/main.cpp
  2. 3 0
      Nixie_Firmware_Mbed/main.h

+ 38 - 36
Nixie_Firmware_Mbed/main.cpp

@@ -10,21 +10,13 @@
 I2C i2c(PA_10, PA_9);
 // SWO_Channel swo("swo");
 
-bool RtcTick = false;
-bool RefreshTick = false;
-
-void RtcCallback(void) {
-    RtcTick = true;
-}
-
-void RefreshCallback(void) {
-    RefreshTick = true;
-}
+bool RtcTick, RefreshTick;
+bool StartupFlag, RuntimeFlag;
 
 typedef struct {
-    char Prev;          // Previous digit to fade out
+    char PrevDigit;     // Previous digit to fade out
     ushort PrevValue;   // Brightness of previous digit
-    char Next;          // Next digit to fade in
+    char NextDigit;     // Next digit to fade in
     ushort NextValue;   // Brightness of next digit
 } Digit;
 
@@ -33,47 +25,51 @@ Digit Digits[NUM_DIGITS] = {0};  // Active per-tube digit setting
 int DotNextValue, DotPrevValue;
 bool DotIncrement = false;
 
-bool StartupFlag = true;
-bool RuntimeFlag = false;
+Ticker StartupTicker, RuntimeTicker;
+int StartupTickerIter, RuntimeTickerIter;
+
+void RtcCallback(void) {
+    RtcTick = true;
+}
 
-Ticker StartupTicker;
-int StartupTickerIter = 1024;
+void RefreshCallback(void) {
+    RefreshTick = true;
+}
 
 void StartupTickerCallback(void) {
 
     for (int i = 0; i < NUM_DIGITS; i++) {
-        Digits[i].NextValue = (i == 0 && Digits[i].Next == 0) ? 0 : Digits[i].NextValue + 4;
+        Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
     }
 
-    DotNextValue += 4;
+    DotNextValue += FADE_TICK_STEP;
 
     if (--StartupTickerIter == 0) {
         StartupTicker.detach();
         for (int i = 0; i < NUM_DIGITS; i++) {
-            Digits[i].Prev = Digits[i].Next;
+            Digits[i].PrevDigit = Digits[i].NextDigit;
         }
+
+        // Switch to runtime mode once startup sequence has completed
         RuntimeFlag = true;
     }
 }
 
-Ticker RuntimeTicker;
-int RuntimeTickerIter = 1024;
-
 void RuntimeTickerCallback(void) {
 
     for (int i = 0; i < NUM_DIGITS; i++) {
-        if(Digits[i].Prev != Digits[i].Next) {
-            Digits[i].PrevValue -= 4;
-            Digits[i].NextValue = (i == 0 && Digits[i].Next == 0) ? 0 : Digits[i].NextValue + 4;
+        if(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;
         }
     }
 
-    DotNextValue = DotIncrement ? DotNextValue + 4 : DotNextValue - 4;
+    DotNextValue = DotIncrement ? DotNextValue + FADE_TICK_STEP: DotNextValue - FADE_TICK_STEP;
 
     if (--RuntimeTickerIter == 0) {
         RuntimeTicker.detach();
         for (int i = 0; i < NUM_DIGITS; i++) {
-            Digits[i].Prev = Digits[i].Next;
+            Digits[i].PrevDigit = Digits[i].NextDigit;
         }
         DotIncrement = !DotIncrement;
     }
@@ -81,6 +77,11 @@ void RuntimeTickerCallback(void) {
 
 int main() {
     
+    RtcTick = false;
+    RefreshTick = false;
+    StartupFlag = true;
+    RuntimeFlag = false;
+
     // Start I2C at 400kHz for DS3231 
     i2c.frequency(400000);
     
@@ -129,8 +130,8 @@ int main() {
             // If brightness has changed, update both prevous and next digits with new values
             for (int i = 0; i < NUM_DIGITS; i++) {
                 if (Digits[i].NextValue != Digits[i].PrevValue) {
-                    PCA9685_SetDigit(i, Digits[i].Prev, Digits[i].PrevValue);
-                    PCA9685_SetDigit(i, Digits[i].Next, Digits[i].NextValue);
+                    PCA9685_SetDigit(i, Digits[i].PrevDigit, Digits[i].PrevValue);
+                    PCA9685_SetDigit(i, Digits[i].NextDigit, Digits[i].NextValue);
                 }
             }
 
@@ -155,23 +156,24 @@ int main() {
             int nextSecond, nextMinute, nextHour;
             DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
 
-            Digits[3].Next = nextMinute % 10;
-            Digits[2].Next = nextMinute / 10;
-            Digits[1].Next = nextHour % 10;
-            Digits[0].Next = nextHour / 10;
+            Digits[3].NextDigit = nextMinute % 10;
+            Digits[2].NextDigit = nextMinute / 10;
+            Digits[1].NextDigit = nextHour % 10;
+            Digits[0].NextDigit = nextHour / 10;
 
             if (StartupFlag) {
 
                 // Fade in the current time
-                StartupTicker.attach_us(StartupTickerCallback, 900);
+                StartupTickerIter = FADE_TICKS;
+                StartupTicker.attach(StartupTickerCallback, 1/FADE_TICKS);
                 StartupFlag = false;
             } 
 
             if (RuntimeFlag) {
 
                 // Fade in the new time
-                RuntimeTickerIter = 1024;
-                RuntimeTicker.attach_us(RuntimeTickerCallback, 900);
+                RuntimeTickerIter = FADE_TICKS;
+                RuntimeTicker.attach(RuntimeTickerCallback, 1/FADE_TICKS);
             }
         }
     }

+ 3 - 0
Nixie_Firmware_Mbed/main.h

@@ -17,6 +17,9 @@
 
 #define I2C_MAX_BUFFER 20
 
+#define FADE_TICKS 1024
+#define FADE_TICK_STEP (PCA9685_Max_Brightness / FADE_TICKS)
+
 void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length);
 void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length);