main.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. typedef enum {
  12. Decrementing = 0,
  13. Incrementing = 1,
  14. } DigitState;
  15. typedef struct {
  16. DigitState CurrentState;
  17. int Value;
  18. bool Updated;
  19. } Digit;
  20. typedef struct {
  21. Digit Digits[NUM_DIGITS];
  22. int LastActiveDigit;
  23. int RefreshLastDigit;
  24. bool Refresh;
  25. int FadeDuration;
  26. } Tube;
  27. Tube Tubes[NUM_TUBES]; // Active per-tube configuration
  28. Digit Dot;
  29. Timeout DotUpdateTimeout;
  30. void DotUpdateCallback(void) {
  31. if (Dot.CurrentState == Incrementing && Dot.Value < DOT_MAX) {
  32. Dot.Value = (Dot.Value + DOT_FADE_STEP >= DOT_MAX) ? DOT_MAX : Dot.Value + DOT_FADE_STEP;
  33. } else if (Dot.CurrentState == Decrementing && Dot.Value > DOT_MIN) {
  34. Dot.Value = (Dot.Value - DOT_FADE_STEP <= DOT_MIN) ? DOT_MIN : Dot.Value - DOT_FADE_STEP;
  35. }
  36. Dot.Updated = true;
  37. if (Dot.Value != DOT_MAX && Dot.Value != DOT_MIN) {
  38. DotUpdateTimeout.attach_us(DotUpdateCallback, REFRESH_RATE_US);
  39. }
  40. }
  41. // Macro the per-tube callback function as each callback needs to be defined as
  42. // a unique function (callbacks cannot have arguments and are not reentrant)
  43. #define TUBE_CALLBACK(x) \
  44. void Tube##x##UpdateCallback(void) { \
  45. int ticks = Tubes[x].FadeDuration / REFRESH_RATE_US; \
  46. int step = (DOT_MAX + ticks - 1) / ticks; \
  47. bool activeTube = false; \
  48. for (int i = 0; i < NUM_DIGITS; i++) { \
  49. Digit *digit = &Tubes[x].Digits[i]; \
  50. if (digit->CurrentState == Incrementing && digit->Value <= DIGIT_MAX) { \
  51. digit->Value = (digit->Value + step >= DIGIT_MAX) ? DIGIT_MAX : digit->Value + step; \
  52. digit->Updated = true; \
  53. } else if (digit->CurrentState == Decrementing && digit->Value >= DOT_MIN) { \
  54. digit->Value = (digit->Value - step <= DIGIT_MIN) ? DIGIT_MIN : digit->Value - step; \
  55. digit->Updated = true; \
  56. } \
  57. activeTube |= (digit->Value != DIGIT_MAX && digit->Value != DIGIT_MIN); \
  58. } \
  59. if (activeTube) { \
  60. Tube##x##UpdateTimeout.attach_us(Tube##x##UpdateCallback, REFRESH_RATE_US); \
  61. } \
  62. }
  63. Timeout Tube0UpdateTimeout;
  64. TUBE_CALLBACK(0)
  65. Timeout Tube1UpdateTimeout;
  66. TUBE_CALLBACK(1)
  67. Timeout Tube2UpdateTimeout;
  68. TUBE_CALLBACK(2)
  69. Timeout Tube3UpdateTimeout;
  70. TUBE_CALLBACK(3)
  71. void FadeInOutDigit(int T, int D, int Duration) {
  72. for (int i = 0; i < NUM_DIGITS; i++) {
  73. Tubes[T].Digits[i].CurrentState = Decrementing;
  74. }
  75. if (D != -1) {
  76. Tubes[T].Digits[D].CurrentState = Incrementing;
  77. }
  78. Tubes[T].FadeDuration = Duration;
  79. Tubes[T].LastActiveDigit = D;
  80. if (T == 0) {
  81. Tube0UpdateTimeout.attach_us(Tube0UpdateCallback, REFRESH_RATE_US);
  82. } else if (T == 1) {
  83. Tube1UpdateTimeout.attach_us(Tube1UpdateCallback, REFRESH_RATE_US);
  84. } else if (T == 2) {
  85. Tube2UpdateTimeout.attach_us(Tube2UpdateCallback, REFRESH_RATE_US);
  86. } else if (T == 3) {
  87. Tube3UpdateTimeout.attach_us(Tube3UpdateCallback, REFRESH_RATE_US);
  88. }
  89. }
  90. bool RtcTick, RefreshTick, RngTick;
  91. Ticker RefreshTicker, RngRefreshTicker;
  92. // Callback from DS3231 interrupt (1Hz)
  93. void RtcInterruptCallback(void) {
  94. RtcTick = true;
  95. }
  96. // Callback from RefreshTicker (REFRESH_RATE_US)
  97. void RefreshTickerCallback(void) {
  98. RefreshTick = true;
  99. }
  100. void RngTickerCallback(void) {
  101. RngTick = true;
  102. }
  103. Timeout RngUpdateTimeout;
  104. int RngUpdateIteration;
  105. void RngUpdateCallback(void) {
  106. for (int i = 0; i < NUM_TUBES; i++) {
  107. if (Tubes[i].Refresh) {
  108. if (RngUpdateIteration) {
  109. int nextDigit;
  110. do {
  111. nextDigit = rand() % NUM_DIGITS;
  112. } while (nextDigit == Tubes[i].LastActiveDigit ||
  113. (RngUpdateIteration == 1 && nextDigit == Tubes[i].RefreshLastDigit));
  114. FadeInOutDigit(i, nextDigit, DIGIT_RNG_FADE_DURATION_US);
  115. } else {
  116. FadeInOutDigit(i, Tubes[i].RefreshLastDigit, DIGIT_RNG_FADE_DURATION_US);
  117. Tubes[i].Refresh = false;
  118. }
  119. }
  120. }
  121. if (RngUpdateIteration-- != 0) {
  122. RngUpdateTimeout.attach_us(RngUpdateCallback, DIGIT_RNG_FADE_DURATION_US);
  123. }
  124. }
  125. int main() {
  126. // Initialize pointers in global data structure
  127. for (int i = 0; i < NUM_TUBES; i++) {
  128. for (int j = 0; j < NUM_DIGITS; j++) {
  129. Tubes[i].Digits[j].CurrentState = Decrementing;
  130. Tubes[i].Digits[j].Value = 0;
  131. Tubes[i].Digits[j].Updated = false;
  132. }
  133. Tubes[i].LastActiveDigit = -1;
  134. Tubes[i].Refresh = false;
  135. Tubes[i].FadeDuration = 0;
  136. }
  137. Dot.CurrentState = Decrementing;
  138. Dot.Value = 0;
  139. Dot.Updated = false;
  140. RtcTick = false;
  141. RefreshTick = false;
  142. RngTick = false;
  143. int nextSecond, nextMinute, nextHour;
  144. int prevMinute, prevHour;
  145. bool startup = true;
  146. // Start I2C at 400kHz for DS3231
  147. i2c.frequency(400000);
  148. // Start with HV PSU disabled
  149. HV_EnableOutput(false);
  150. TUSB322_Init();
  151. PCA9685_Init();
  152. DS3231_Init(RtcInterruptCallback);
  153. // Enable HV PSU
  154. HV_EnableOutput(true);
  155. // Set PCA9685 input voltage to highest possible
  156. PCA9685_SetVoltage(1.0);
  157. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  158. // Bump I2C frequency to 1MHz
  159. // i2c.frequency(1000000);
  160. // Setup a ticker to refresh the display at 1kHz
  161. RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
  162. // Kick off the RNG ticker at 0.1Hz
  163. RngRefreshTicker.attach(RngTickerCallback, DIGIT_RNG_REFRESH_RATE);
  164. // DS3231_SetTime(0, 55, 6, true);
  165. // DS3231_SetDate(0, 2, 12, 18, 0);
  166. while(1) {
  167. // Animate_Cycle_Basic();
  168. // Animate_Cycle_Analog();
  169. // Animate_Cycle_Low_Pwm();
  170. // Animate_Cycle_Pwm();
  171. // Animate_Cycle_Fade();
  172. // Animate_Cycle_Fade_Random();
  173. // Animate_Cycle_Fast();
  174. // Animate_Cycle_Fast_Random();
  175. // On every refresh tick, update the display with new values
  176. if (RefreshTick) {
  177. RefreshTick = false;
  178. for (int i = 0; i < NUM_TUBES; i++) {
  179. for (int j = 0; j < NUM_DIGITS; j++) {
  180. if (Tubes[i].Digits[j].Updated) {
  181. PCA9685_SetDigit(i, j, Tubes[i].Digits[j].Value);
  182. Tubes[i].Digits[j].Updated = false;
  183. }
  184. }
  185. }
  186. if (Dot.Updated) {
  187. PCA9685_SetDot(Dot.Value);
  188. Dot.Updated = false;
  189. }
  190. }
  191. // On every RTC tick, read and process the current time
  192. if (RtcTick) {
  193. RtcTick = false;
  194. // Save the previous time to check if digit(s) has changed
  195. prevMinute = nextMinute;
  196. prevHour = nextHour;
  197. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  198. // Update the display configuration based on the new/previous time
  199. if (startup || prevHour / 10 != nextHour / 10) {
  200. FadeInOutDigit(0, (nextHour / 10 != 0) ? nextHour / 10 : -1 , DIGIT_FADE_DURATION_US);
  201. }
  202. if (startup || prevHour % 10 != nextHour % 10) {
  203. FadeInOutDigit(1, nextHour % 10, DIGIT_FADE_DURATION_US);
  204. }
  205. if (startup || prevMinute / 10 != nextMinute / 10) {
  206. FadeInOutDigit(2, nextMinute / 10, DIGIT_FADE_DURATION_US);
  207. }
  208. if (startup || prevMinute % 10 != nextMinute % 10) {
  209. FadeInOutDigit(3, nextMinute % 10, DIGIT_FADE_DURATION_US);
  210. }
  211. Dot.CurrentState = (Dot.CurrentState == Decrementing) ? Incrementing : Decrementing;
  212. DotUpdateTimeout.attach_us(DotUpdateCallback, DOT_FADE_DURATION_US / REFRESH_RATE_US);
  213. // Clear the startup run-once flag after the first iteration
  214. startup = false;
  215. }
  216. if (RngTick) {
  217. RngTick = false;
  218. int refreshTube = rand() % NUM_TUBES;
  219. Tubes[refreshTube].RefreshLastDigit = Tubes[refreshTube].LastActiveDigit;
  220. Tubes[refreshTube].Refresh = true;
  221. RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
  222. RngUpdateCallback();
  223. }
  224. }
  225. }
  226. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  227. char buffer[I2C_MAX_BUFFER+1] = {0};
  228. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  229. buffer[0] = RegAddress;
  230. memcpy(&buffer[1], Data, Length);
  231. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  232. }
  233. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  234. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  235. i2c.read(DeviceAddress << 1, Data, Length);
  236. }