main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "mbed.h"
  2. #include "main.h"
  3. #include "SWO.h"
  4. #include "pca9685.h"
  5. #include "tusb322.h"
  6. #include "ds3231.h"
  7. #include "ioc.h"
  8. #include "animation.h"
  9. I2C i2c(PA_10, PA_9);
  10. // SWO_Channel swo("swo");
  11. bool RtcTick, RefreshTick, RngTick;
  12. bool StartupFlag, RuntimeFlag;
  13. typedef struct {
  14. char PrevDigit; // Previous digit to fade out
  15. ushort PrevValue; // Brightness of previous digit
  16. char NextDigit; // Next digit to fade in
  17. ushort NextValue; // Brightness of next digit
  18. bool RefreshActive;
  19. } Digit;
  20. Digit Digits[NUM_DIGITS] = {0}; // Active per-tube digit setting
  21. int DotNextValue, DotPrevValue;
  22. bool DotIncrement = false;
  23. Ticker StartupUpdateTicker, RuntimeUpdateTicker, RngUpdateTicker;
  24. int StartupTickerIter, RuntimeTickerIter, RngTickerIter;
  25. void RtcTickCallback(void) {
  26. RtcTick = true;
  27. }
  28. void RefreshTickCallback(void) {
  29. RefreshTick = true;
  30. }
  31. void RngTickCallback(void) {
  32. RngTick = true;
  33. }
  34. void StartupTickerUpdate(void) {
  35. for (int i = 0; i < NUM_DIGITS; i++) {
  36. if (!Digits[i].RefreshActive) {
  37. Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
  38. }
  39. }
  40. DotNextValue += FADE_TICK_STEP;
  41. if (--StartupTickerIter == 0) {
  42. StartupUpdateTicker.detach();
  43. for (int i = 0; i < NUM_DIGITS; i++) {
  44. Digits[i].PrevDigit = Digits[i].NextDigit;
  45. }
  46. // Switch to runtime mode once startup sequence has completed
  47. RuntimeFlag = true;
  48. }
  49. }
  50. void RuntimeTickerUpdate(void) {
  51. for (int i = 0; i < NUM_DIGITS; i++) {
  52. if(!Digits[i].RefreshActive && Digits[i].PrevDigit != Digits[i].NextDigit) {
  53. Digits[i].PrevValue -= FADE_TICK_STEP;
  54. Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
  55. }
  56. }
  57. DotNextValue = DotIncrement ? DotNextValue + FADE_TICK_STEP: DotNextValue - FADE_TICK_STEP;
  58. if (--RuntimeTickerIter == 0) {
  59. RuntimeUpdateTicker.detach();
  60. for (int i = 0; i < NUM_DIGITS; i++) {
  61. Digits[i].PrevDigit = Digits[i].NextDigit;
  62. }
  63. DotIncrement = !DotIncrement;
  64. }
  65. }
  66. void RngTickerUpdate(void) {
  67. if (--RngTickerIter == 0) {
  68. RngUpdateTicker.detach();
  69. }
  70. }
  71. int main() {
  72. RtcTick = false;
  73. RefreshTick = false;
  74. RngTick = false;
  75. StartupFlag = true;
  76. RuntimeFlag = false;
  77. // Start I2C at 400kHz for DS3231
  78. i2c.frequency(400000);
  79. // Start with HV PSU disabled
  80. HV_EnableOutput(false);
  81. TUSB322_Init();
  82. PCA9685_Init();
  83. DS3231_Init(RtcTickCallback);
  84. // Enable HV PSU
  85. HV_EnableOutput(true);
  86. // Set PCA9685 input voltage to highest possible
  87. PCA9685_SetVoltage(1.0);
  88. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  89. // Bump I2C frequency to 1MHz
  90. i2c.frequency(1000000);
  91. // Setup a ticker to refresh the display at 1kHz
  92. Ticker refreshTicker;
  93. refreshTicker.attach_us(RefreshTickCallback, DIGIT_REFRESH_RATE_US);
  94. // Kick off the RNG ticker at 0.1Hz
  95. Ticker rngTicker;
  96. rngTicker.attach(RngTickCallback, DIGIT_RNG_REFRESH_RATE_S);
  97. // DS3231_SetTime(0, 55, 6, true);
  98. // DS3231_SetDate(0, 2, 12, 18, 0);
  99. while(1) {
  100. // Animate_Cycle_Basic();
  101. // Animate_Cycle_Analog();
  102. // Animate_Cycle_Low_Pwm();
  103. // Animate_Cycle_Pwm();
  104. // Animate_Cycle_Fade();
  105. // Animate_Cycle_Fade_Random();
  106. // Animate_Cycle_Fast();
  107. // Animate_Cycle_Fast_Random();
  108. if (RefreshTick) {
  109. RefreshTick = false;
  110. // On refresh, update the display with new values
  111. // If brightness has changed, update both prevous and next digits with new values
  112. for (int i = 0; i < NUM_DIGITS; i++) {
  113. if (Digits[i].NextValue != Digits[i].PrevValue) {
  114. PCA9685_SetDigit(i, Digits[i].PrevDigit, Digits[i].PrevValue);
  115. PCA9685_SetDigit(i, Digits[i].NextDigit, Digits[i].NextValue);
  116. }
  117. }
  118. if (DotNextValue != DotPrevValue) {
  119. PCA9685_SetDot(DotNextValue);
  120. }
  121. // Update previous values to what was just set
  122. for (int i = 0; i < NUM_DIGITS; i++) {
  123. Digits[i].PrevValue = Digits[i].NextValue;
  124. }
  125. DotPrevValue = DotNextValue;
  126. }
  127. if (RtcTick) {
  128. RtcTick = false;
  129. // On RTC 1Hz ticks, get the time and begin update
  130. int nextSecond, nextMinute, nextHour;
  131. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  132. Digits[3].NextDigit = nextMinute % 10;
  133. Digits[2].NextDigit = nextMinute / 10;
  134. Digits[1].NextDigit = nextHour % 10;
  135. Digits[0].NextDigit = nextHour / 10;
  136. if (StartupFlag) {
  137. // Fade in the current time
  138. StartupTickerIter = FADE_TICKS;
  139. StartupUpdateTicker.attach(StartupTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/StartupTickerIter);
  140. StartupFlag = false;
  141. }
  142. if (RuntimeFlag) {
  143. // Fade in the new time
  144. RuntimeTickerIter = FADE_TICKS;
  145. RuntimeUpdateTicker.attach(RuntimeTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/RuntimeTickerIter);
  146. }
  147. }
  148. if (RngTick) {
  149. // Choose a random tube to refresh
  150. Digits[rand() % 4].RefreshActive = true;
  151. RngTickerIter = DIGIT_RNG_REFRESH_ITER * FADE_TICKS;
  152. RngUpdateTicker.attach(RngTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/FADE_TICKS);
  153. RngTick = false;
  154. }
  155. }
  156. }
  157. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  158. char buffer[I2C_MAX_BUFFER+1] = {0};
  159. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  160. buffer[0] = RegAddress;
  161. memcpy(&buffer[1], Data, Length);
  162. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  163. }
  164. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  165. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  166. i2c.read(DeviceAddress << 1, Data, Length);
  167. }