main.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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, bool HighPrio = false) {
  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 the tube is in the middle of a refresh sequence and a call comes
  81. // in to update the tube digit (for time), override the last value of
  82. // the refresh sequence with the new digit.
  83. if (Tubes[T].Refresh && HighPrio) {
  84. Tubes[T].RefreshLastDigit = D;
  85. }
  86. if (T == 0) {
  87. Tube0UpdateTimeout.attach_us(Tube0UpdateCallback, REFRESH_RATE_US);
  88. } else if (T == 1) {
  89. Tube1UpdateTimeout.attach_us(Tube1UpdateCallback, REFRESH_RATE_US);
  90. } else if (T == 2) {
  91. Tube2UpdateTimeout.attach_us(Tube2UpdateCallback, REFRESH_RATE_US);
  92. } else if (T == 3) {
  93. Tube3UpdateTimeout.attach_us(Tube3UpdateCallback, REFRESH_RATE_US);
  94. }
  95. }
  96. bool RtcTick, RefreshTick, RngTick;
  97. Ticker RefreshTicker, RngRefreshTicker;
  98. // Callback from DS3231 interrupt (1Hz)
  99. void RtcInterruptCallback(void) {
  100. RtcTick = true;
  101. }
  102. // Callback from RefreshTicker (REFRESH_RATE_US)
  103. void RefreshTickerCallback(void) {
  104. RefreshTick = true;
  105. }
  106. void RngTickerCallback(void) {
  107. RngTick = true;
  108. }
  109. Timeout RngUpdateTimeout;
  110. int RngUpdateIteration;
  111. void RngUpdateCallback(void) {
  112. for (int i = 0; i < NUM_TUBES; i++) {
  113. if (Tubes[i].Refresh) {
  114. if (RngUpdateIteration) {
  115. int nextDigit;
  116. do {
  117. nextDigit = rand() % NUM_DIGITS;
  118. } while (nextDigit == Tubes[i].LastActiveDigit ||
  119. (RngUpdateIteration == 1 && nextDigit == Tubes[i].RefreshLastDigit));
  120. FadeInOutDigit(i, nextDigit, DIGIT_RNG_FADE_DURATION_US);
  121. } else {
  122. FadeInOutDigit(i, Tubes[i].RefreshLastDigit, DIGIT_RNG_FADE_DURATION_US);
  123. Tubes[i].Refresh = false;
  124. }
  125. }
  126. }
  127. if (RngUpdateIteration-- != 0) {
  128. RngUpdateTimeout.attach_us(RngUpdateCallback, DIGIT_RNG_FADE_DURATION_US);
  129. }
  130. }
  131. int main() {
  132. // Initialize pointers in global data structure
  133. for (int i = 0; i < NUM_TUBES; i++) {
  134. for (int j = 0; j < NUM_DIGITS; j++) {
  135. Tubes[i].Digits[j].CurrentState = Decrementing;
  136. Tubes[i].Digits[j].Value = 0;
  137. Tubes[i].Digits[j].Updated = false;
  138. }
  139. Tubes[i].LastActiveDigit = -1;
  140. Tubes[i].Refresh = false;
  141. Tubes[i].FadeDuration = 0;
  142. }
  143. Dot.CurrentState = Decrementing;
  144. Dot.Value = 0;
  145. Dot.Updated = false;
  146. RtcTick = false;
  147. RefreshTick = false;
  148. RngTick = false;
  149. int nextSecond, nextMinute, nextHour;
  150. int prevMinute, prevHour;
  151. bool startup = true;
  152. // Start I2C at 400kHz for DS3231
  153. i2c.frequency(400000);
  154. // Start with HV PSU disabled
  155. HV_EnableOutput(false);
  156. TUSB322_Init();
  157. PCA9685_Init();
  158. DS3231_Init(RtcInterruptCallback);
  159. // Enable HV PSU
  160. HV_EnableOutput(true);
  161. // Set PCA9685 input voltage to highest possible
  162. PCA9685_SetVoltage(1.0);
  163. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  164. // Bump I2C frequency to 1MHz
  165. // i2c.frequency(1000000);
  166. // Setup a ticker to refresh the display at 1kHz
  167. RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
  168. // Kick off the RNG ticker at 0.1Hz
  169. RngRefreshTicker.attach(RngTickerCallback, DIGIT_RNG_REFRESH_RATE);
  170. // DS3231_SetTime(0, 55, 6, true);
  171. // DS3231_SetDate(0, 2, 12, 18, 0);
  172. while(1) {
  173. // Animate_Cycle_Basic();
  174. // Animate_Cycle_Analog();
  175. // Animate_Cycle_Low_Pwm();
  176. // Animate_Cycle_Pwm();
  177. // Animate_Cycle_Fade();
  178. // Animate_Cycle_Fade_Random();
  179. // Animate_Cycle_Fast();
  180. // Animate_Cycle_Fast_Random();
  181. // On every refresh tick, update the display with new values
  182. if (RefreshTick) {
  183. RefreshTick = false;
  184. for (int i = 0; i < NUM_TUBES; i++) {
  185. for (int j = 0; j < NUM_DIGITS; j++) {
  186. if (Tubes[i].Digits[j].Updated) {
  187. PCA9685_SetDigit(i, j, Tubes[i].Digits[j].Value);
  188. Tubes[i].Digits[j].Updated = false;
  189. }
  190. }
  191. }
  192. if (Dot.Updated) {
  193. PCA9685_SetDot(Dot.Value);
  194. Dot.Updated = false;
  195. }
  196. }
  197. // On every RTC tick, read and process the current time
  198. if (RtcTick) {
  199. RtcTick = false;
  200. // Save the previous time to check if digit(s) has changed
  201. prevMinute = nextMinute;
  202. prevHour = nextHour;
  203. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  204. // Update the display configuration based on the new/previous time
  205. if (startup || prevHour / 10 != nextHour / 10) {
  206. FadeInOutDigit(0, (nextHour / 10 != 0) ? nextHour / 10 : -1 , DIGIT_FADE_DURATION_US, true);
  207. }
  208. if (startup || prevHour % 10 != nextHour % 10) {
  209. FadeInOutDigit(1, nextHour % 10, DIGIT_FADE_DURATION_US, true);
  210. }
  211. if (startup || prevMinute / 10 != nextMinute / 10) {
  212. FadeInOutDigit(2, nextMinute / 10, DIGIT_FADE_DURATION_US, true);
  213. }
  214. if (startup || prevMinute % 10 != nextMinute % 10) {
  215. FadeInOutDigit(3, nextMinute % 10, DIGIT_FADE_DURATION_US, true);
  216. }
  217. Dot.CurrentState = (Dot.CurrentState == Decrementing) ? Incrementing : Decrementing;
  218. DotUpdateTimeout.attach_us(DotUpdateCallback, DOT_FADE_DURATION_US / REFRESH_RATE_US);
  219. // Clear the startup run-once flag after the first iteration
  220. startup = false;
  221. }
  222. if (RngTick) {
  223. RngTick = false;
  224. int refreshTube = rand() % NUM_TUBES;
  225. Tubes[refreshTube].RefreshLastDigit = Tubes[refreshTube].LastActiveDigit;
  226. Tubes[refreshTube].Refresh = true;
  227. RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
  228. RngUpdateCallback();
  229. }
  230. }
  231. }
  232. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  233. char buffer[I2C_MAX_BUFFER+1] = {0};
  234. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  235. buffer[0] = RegAddress;
  236. memcpy(&buffer[1], Data, Length);
  237. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  238. }
  239. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  240. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  241. i2c.read(DeviceAddress << 1, Data, Length);
  242. }