main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 DigitA;
  15. ushort DigitAValue;
  16. char DigitB;
  17. ushort DigitBValue;
  18. bool DigitBActive;
  19. bool Updated;
  20. bool RefreshActive;
  21. } TUBE;
  22. TUBE Display[NUM_TUBES] = {0}; // Active per-tube digit setting
  23. int DotValue;
  24. bool DotUpdated = false;;
  25. bool DotIncrement = false;
  26. Ticker StartupUpdateTicker, RuntimeUpdateTicker, RngUpdateTicker;
  27. int StartupTickerIter, RuntimeTickerIter, RngTickerIter;
  28. void RtcTickCallback(void) {
  29. RtcTick = true;
  30. }
  31. void RefreshTickCallback(void) {
  32. RefreshTick = true;
  33. }
  34. void RngTickCallback(void) {
  35. RngTick = true;
  36. }
  37. void StartupTickerUpdate(void) {
  38. for (int i = 0; i < NUM_TUBES; i++) {
  39. Display[i].DigitAValue = (i == 0 && Display[i].DigitA == 0) ? 0 : Display[i].DigitAValue + FADE_TICK_STEP;
  40. Display[i].Updated = true;
  41. }
  42. DotValue += FADE_TICK_STEP;
  43. DotUpdated = true;
  44. if (--StartupTickerIter == 0) {
  45. StartupUpdateTicker.detach();
  46. for (int i = 0; i < NUM_TUBES; i++) {
  47. Display[i].DigitBActive = !Display[i].DigitBActive;
  48. }
  49. // Switch to runtime mode once startup sequence has completed
  50. RuntimeFlag = true;
  51. }
  52. }
  53. void RuntimeTickerUpdate(void) {
  54. for (int i = 0; i < NUM_TUBES; i++) {
  55. if(!Display[i].RefreshActive) {
  56. if (Display[i].DigitA != Display[i].DigitB) {
  57. if (Display[i].DigitBActive) {
  58. Display[i].DigitAValue -= FADE_TICK_STEP;
  59. Display[i].DigitBValue = (i == 0 && Display[i].DigitB == 0) ? 0 : Display[i].DigitBValue + FADE_TICK_STEP;
  60. } else {
  61. Display[i].DigitBValue -= FADE_TICK_STEP;
  62. Display[i].DigitAValue = (i == 0 && Display[i].DigitA == 0) ? 0 : Display[i].DigitAValue + FADE_TICK_STEP;
  63. }
  64. Display[i].Updated = true;
  65. }
  66. }
  67. }
  68. DotValue = DotIncrement ? DotValue + FADE_TICK_STEP: DotValue - FADE_TICK_STEP;
  69. DotUpdated = true;
  70. if (--RuntimeTickerIter == 0) {
  71. RuntimeUpdateTicker.detach();
  72. for (int i = 0; i < NUM_TUBES; i++) {
  73. if (Display[i].DigitA != Display[i].DigitB) {
  74. Display[i].DigitBActive = !Display[i].DigitBActive;
  75. }
  76. }
  77. DotIncrement = !DotIncrement;
  78. }
  79. }
  80. void RngTickerUpdate(void) {
  81. if (--RngTickerIter == 0) {
  82. RngUpdateTicker.detach();
  83. }
  84. }
  85. int main() {
  86. RtcTick = false;
  87. RefreshTick = false;
  88. RngTick = false;
  89. StartupFlag = true;
  90. RuntimeFlag = false;
  91. // Start I2C at 400kHz for DS3231
  92. i2c.frequency(400000);
  93. // Start with HV PSU disabled
  94. HV_EnableOutput(false);
  95. TUSB322_Init();
  96. PCA9685_Init();
  97. DS3231_Init(RtcTickCallback);
  98. // Enable HV PSU
  99. HV_EnableOutput(true);
  100. // Set PCA9685 input voltage to highest possible
  101. PCA9685_SetVoltage(1.0);
  102. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  103. // Bump I2C frequency to 1MHz
  104. i2c.frequency(1000000);
  105. // Setup a ticker to refresh the display at 1kHz
  106. Ticker refreshTicker;
  107. refreshTicker.attach_us(RefreshTickCallback, DIGIT_REFRESH_RATE_US);
  108. // Kick off the RNG ticker at 0.1Hz
  109. // Ticker rngTicker;
  110. // rngTicker.attach(RngTickCallback, DIGIT_RNG_REFRESH_RATE_S);
  111. // DS3231_SetTime(0, 55, 6, true);
  112. // DS3231_SetDate(0, 2, 12, 18, 0);
  113. while(1) {
  114. // Animate_Cycle_Basic();
  115. // Animate_Cycle_Analog();
  116. // Animate_Cycle_Low_Pwm();
  117. // Animate_Cycle_Pwm();
  118. // Animate_Cycle_Fade();
  119. // Animate_Cycle_Fade_Random();
  120. // Animate_Cycle_Fast();
  121. // Animate_Cycle_Fast_Random();
  122. if (RefreshTick) {
  123. RefreshTick = false;
  124. // On refresh, update the display with new values
  125. // If values has changed, update both prevous and next digits with new values
  126. for (int i = 0; i < NUM_TUBES; i++) {
  127. if (Display[i].Updated) {
  128. PCA9685_SetDigit(i, Display[i].DigitA, Display[i].DigitAValue);
  129. PCA9685_SetDigit(i, Display[i].DigitB, Display[i].DigitBValue);
  130. Display[i].Updated = false;
  131. }
  132. }
  133. if (DotUpdated) {
  134. PCA9685_SetDot(DotValue);
  135. DotUpdated = false;
  136. }
  137. }
  138. if (RtcTick) {
  139. RtcTick = false;
  140. // On RTC 1Hz ticks, get the time and begin update
  141. int nextSecond, nextMinute, nextHour;
  142. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  143. if (Display[3].DigitBActive) {
  144. Display[3].DigitB = nextMinute % 10;
  145. } else {
  146. Display[3].DigitA = nextMinute % 10;
  147. }
  148. if (Display[2].DigitBActive) {
  149. Display[2].DigitB = nextMinute / 10;
  150. } else {
  151. Display[2].DigitA = nextMinute / 10;
  152. }
  153. if (Display[1].DigitBActive) {
  154. Display[1].DigitB = nextHour % 10;
  155. } else {
  156. Display[1].DigitA = nextHour % 10;
  157. }
  158. if (Display[0].DigitBActive) {
  159. Display[0].DigitB = nextHour / 10;
  160. } else {
  161. Display[0].DigitA = nextHour / 10;
  162. }
  163. if (StartupFlag) {
  164. // Fade in the current time
  165. StartupTickerIter = FADE_TICKS;
  166. StartupUpdateTicker.attach(StartupTickerUpdate, (float)(DIGIT_REFRESH_RATE_US / 1000) / StartupTickerIter);
  167. StartupFlag = false;
  168. }
  169. if (RuntimeFlag) {
  170. // Fade in the new time
  171. RuntimeTickerIter = FADE_TICKS;
  172. RuntimeUpdateTicker.attach(RuntimeTickerUpdate, (float)(DIGIT_REFRESH_RATE_US / 1000) / RuntimeTickerIter);
  173. }
  174. }
  175. if (RngTick) {
  176. // Choose a random tube to refresh
  177. Display[rand() % 4].RefreshActive = true;
  178. RngTickerIter = DIGIT_RNG_REFRESH_ITER * FADE_TICKS;
  179. RngUpdateTicker.attach(RngTickerUpdate, (DIGIT_REFRESH_RATE_US/1000)/FADE_TICKS);
  180. RngTick = false;
  181. }
  182. }
  183. }
  184. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  185. char buffer[I2C_MAX_BUFFER+1] = {0};
  186. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  187. buffer[0] = RegAddress;
  188. memcpy(&buffer[1], Data, Length);
  189. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  190. }
  191. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  192. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  193. i2c.read(DeviceAddress << 1, Data, Length);
  194. }