Browse Source

Fixed dot/digit fading code

Kevin Lee 5 years ago
parent
commit
faca5ff95e
3 changed files with 13 additions and 13 deletions
  1. 8 6
      Nixie_Firmware_Mbed/main.cpp
  2. 2 4
      Nixie_Firmware_Mbed/main.h
  3. 3 3
      Nixie_Firmware_Mbed/pca9685.cpp

+ 8 - 6
Nixie_Firmware_Mbed/main.cpp

@@ -34,10 +34,12 @@ Digit Dot;
 
 Timeout DotUpdateTimeout;
 void DotUpdateCallback(void) {
+    int ticks = DOT_FADE_DURATION_US / REFRESH_RATE_US;
+    int step = ((DOT_MAX - DOT_MIN) + ticks - 1) / ticks;
     if (Dot.CurrentState == Incrementing && Dot.Value < DOT_MAX) {
-        Dot.Value = (Dot.Value + DOT_FADE_STEP >= DOT_MAX) ? DOT_MAX : Dot.Value + DOT_FADE_STEP;
+        Dot.Value = (Dot.Value + step >= DOT_MAX) ? DOT_MAX : Dot.Value + step;
     } else if (Dot.CurrentState == Decrementing && Dot.Value > DOT_MIN) {
-        Dot.Value = (Dot.Value - DOT_FADE_STEP <= DOT_MIN) ? DOT_MIN : Dot.Value - DOT_FADE_STEP;
+        Dot.Value = (Dot.Value - step <= DOT_MIN) ? DOT_MIN : Dot.Value - step;
     }
     Dot.Updated = true;
 
@@ -51,14 +53,14 @@ void DotUpdateCallback(void) {
 #define TUBE_CALLBACK(x) \
 void Tube##x##UpdateCallback(void) { \
     int ticks = Tubes[x].FadeDuration / REFRESH_RATE_US; \
-    int step = (DOT_MAX + ticks - 1) / ticks; \
+    int step = ((DIGIT_MAX - DIGIT_MIN) + ticks - 1) / ticks; \
     bool activeTube = false; \
     for (int i = 0; i < NUM_DIGITS; i++) { \
         Digit *digit = &Tubes[x].Digits[i]; \
         if (digit->CurrentState == Incrementing && digit->Value <= DIGIT_MAX) { \
             digit->Value = (digit->Value + step >= DIGIT_MAX) ? DIGIT_MAX : digit->Value + step; \
             digit->Updated = true; \
-        } else if (digit->CurrentState == Decrementing && digit->Value >= DOT_MIN) { \
+        } else if (digit->CurrentState == Decrementing && digit->Value >= DIGIT_MIN) { \
             digit->Value = (digit->Value - step <= DIGIT_MIN) ? DIGIT_MIN : digit->Value - step; \
             digit->Updated = true; \
         } \
@@ -166,7 +168,7 @@ int main() {
     for (int i = 0; i < NUM_TUBES; i++) {
         for (int j = 0; j < NUM_DIGITS; j++) {
             Tubes[i].Digits[j].CurrentState = Decrementing;
-            Tubes[i].Digits[j].Value = 0;
+            Tubes[i].Digits[j].Value = DIGIT_MIN;
             Tubes[i].Digits[j].Updated = false;
         }
         Tubes[i].LastActiveDigit = -1;
@@ -176,7 +178,7 @@ int main() {
     }
 
     Dot.CurrentState = Decrementing;
-    Dot.Value = 0;
+    Dot.Value = DOT_MIN;
     Dot.Updated = false;
 
     RtcTick = false;

+ 2 - 4
Nixie_Firmware_Mbed/main.h

@@ -20,11 +20,9 @@
 
 #define REFRESH_RATE_US 1000  
 
-#define DOT_MIN PCA9685_Min_Brightness
-#define DOT_MAX PCA9685_Max_Brightness
+#define DOT_MIN 256
+#define DOT_MAX 2048
 #define DOT_FADE_DURATION_US 1000000
-#define DOT_FADE_TICK (DOT_FADE_DURATION_US / REFRESH_RATE_US)
-#define DOT_FADE_STEP ((DOT_MAX + DOT_FADE_TICK - 1) / DOT_FADE_TICK)
 
 #define DIGIT_MIN PCA9685_Min_Brightness
 #define DIGIT_MAX PCA9685_Max_Brightness

+ 3 - 3
Nixie_Firmware_Mbed/pca9685.cpp

@@ -71,9 +71,9 @@ void PCA9685_SetDigit(int Tube, int Digit, int Brightness) {
 void PCA9685_SetDot(int Brightness) {
     LED_CTRL reg = {0};
 
-    Brightness /= 4;
-    
-    if (Brightness == 0) {
+    if (Brightness >= PCA9685_Max_Brightness) {
+        reg.ON_FULL = 1;
+    } else if (Brightness == 0) {
         reg.OFF_FULL = 1;
     } else {
         reg.OFF = Brightness;