main.cpp 10 KB

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