main.cpp 11 KB

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