main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. int PwmStart;
  22. int PwmEnd;
  23. bool Updated;
  24. } Digit;
  25. typedef struct {
  26. Digit Digits[NUM_DIGITS];
  27. int LastActiveDigit;
  28. int RefreshLastDigit;
  29. bool RefreshActive;
  30. int FadeDuration;
  31. } Tube;
  32. Tube Tubes[NUM_TUBES]; // Active per-tube configuration
  33. Digit Dot;
  34. Timeout DotUpdateTimeout;
  35. void DotUpdateCallback(void) {
  36. int ticks = DOT_FADE_DURATION_US / REFRESH_RATE_US;
  37. int step = ((DOT_MAX - DOT_MIN) + ticks - 1) / ticks;
  38. if (Dot.CurrentState == Incrementing && Dot.Value < DOT_MAX) {
  39. Dot.Value = (Dot.Value + step >= DOT_MAX) ? DOT_MAX : Dot.Value + step;
  40. } else if (Dot.CurrentState == Decrementing && Dot.Value > DOT_MIN) {
  41. Dot.Value = (Dot.Value - step <= DOT_MIN) ? DOT_MIN : Dot.Value - step;
  42. }
  43. Dot.Updated = true;
  44. if (Dot.Value != DOT_MAX && Dot.Value != DOT_MIN) {
  45. DotUpdateTimeout.attach_us(DotUpdateCallback, REFRESH_RATE_US);
  46. }
  47. }
  48. // Macro the per-tube callback function as each callback needs to be defined as
  49. // a unique function (callbacks cannot have arguments and are not reentrant)
  50. #define TUBE_CALLBACK(x) \
  51. void Tube##x##UpdateCallback(void) { \
  52. int ticks = Tubes[x].FadeDuration / REFRESH_RATE_US; \
  53. int step = ((DIGIT_MAX - DIGIT_MIN) + ticks - 1) / ticks; \
  54. bool activeTube = false; \
  55. for (int i = 0; i < NUM_DIGITS; i++) { \
  56. Digit *digit = &Tubes[x].Digits[i]; \
  57. if (digit->CurrentState == Incrementing && digit->Value <= DIGIT_MAX) { \
  58. digit->Value = (digit->Value + step >= DIGIT_MAX) ? DIGIT_MAX : digit->Value + step; \
  59. digit->Updated = true; \
  60. } else if (digit->CurrentState == Decrementing && digit->Value >= DIGIT_MIN) { \
  61. digit->Value = (digit->Value - step <= DIGIT_MIN) ? DIGIT_MIN : digit->Value - step; \
  62. digit->Updated = true; \
  63. } \
  64. activeTube |= (digit->Value != DIGIT_MAX && digit->Value != DIGIT_MIN); \
  65. } \
  66. if (activeTube) { \
  67. Tube##x##UpdateTimeout.attach_us(Tube##x##UpdateCallback, REFRESH_RATE_US); \
  68. } \
  69. }
  70. Timeout Tube0UpdateTimeout;
  71. TUBE_CALLBACK(0)
  72. Timeout Tube1UpdateTimeout;
  73. TUBE_CALLBACK(1)
  74. Timeout Tube2UpdateTimeout;
  75. TUBE_CALLBACK(2)
  76. Timeout Tube3UpdateTimeout;
  77. TUBE_CALLBACK(3)
  78. // In the event that there are multiple PWM outputs at less than 100% duty cycle,
  79. // stagger the start time of each PWM to reduce the switch on surge current.
  80. void StaggerPwmOutput() {
  81. int validOutputs = 0;
  82. int totalOnTime = 0;
  83. int lastPwmEnd = 0;
  84. // Determine the number of active outputs as well as the total on-time across all outputs.
  85. // Ignore outputs that are off (min) or fully on (max) as they have no surge impact.
  86. for (int i = 0; i < NUM_TUBES; i++) {
  87. for (int j = 0; j < NUM_DIGITS; j++) {
  88. if (Tubes[i].Digits[j].Value != PCA9685_Min_Brightness &&
  89. Tubes[i].Digits[j].Value != PCA9685_Max_Brightness) {
  90. validOutputs++;
  91. totalOnTime += Tubes[i].Digits[j].Value;
  92. }
  93. }
  94. }
  95. if (Dot.Value != PCA9685_Min_Brightness && Dot.Value != PCA9685_Max_Brightness) {
  96. validOutputs++;
  97. totalOnTime += Dot.Value;
  98. }
  99. // If the total on-time across all outputs is less than one PWM period, stagger each
  100. // output such that the rise of one pulse begins at the end of the previous pulse.
  101. if (totalOnTime <= PCA9685_Max_Brightness) {
  102. for (int i = 0; i < NUM_TUBES; i++) {
  103. for (int j = 0; j < NUM_DIGITS; j++) {
  104. if (Tubes[i].Digits[j].Value != PCA9685_Min_Brightness &&
  105. Tubes[i].Digits[j].Value != PCA9685_Max_Brightness) {
  106. Tubes[i].Digits[j].PwmStart = lastPwmEnd;
  107. Tubes[i].Digits[j].PwmEnd = lastPwmEnd + Tubes[i].Digits[j].Value;
  108. lastPwmEnd = Tubes[i].Digits[j].PwmEnd;
  109. Tubes[i].Digits[j].Updated = true;
  110. }
  111. }
  112. }
  113. if (Dot.Value != PCA9685_Min_Brightness && Dot.Value != PCA9685_Max_Brightness) {
  114. Dot.PwmStart = lastPwmEnd;
  115. Dot.PwmEnd = lastPwmEnd + Dot.Value;
  116. lastPwmEnd = Dot.PwmEnd;
  117. Dot.Updated = true;
  118. }
  119. }
  120. else {
  121. // Compute the amount of overlap between all outputs
  122. int overlap = totalOnTime / (validOutputs - 1);
  123. // Compute the staggered output period for each output
  124. for (int i = 0; i < NUM_TUBES; i++) {
  125. for (int j = 0; j < NUM_DIGITS; j++) {
  126. if (Tubes[i].Digits[j].Value != PCA9685_Min_Brightness &&
  127. Tubes[i].Digits[j].Value != PCA9685_Max_Brightness) {
  128. Tubes[i].Digits[j].PwmStart = (lastPwmEnd == 0) ? 0 : lastPwmEnd - overlap;
  129. Tubes[i].Digits[j].PwmEnd = Tubes[i].Digits[j].PwmStart + Tubes[i].Digits[j].Value;
  130. lastPwmEnd = Tubes[i].Digits[j].PwmEnd;
  131. Tubes[i].Digits[j].Updated = true;
  132. }
  133. }
  134. }
  135. if (Dot.Value != PCA9685_Min_Brightness && Dot.Value != PCA9685_Max_Brightness) {
  136. Dot.PwmStart = (lastPwmEnd == 0) ? 0 : lastPwmEnd - overlap;
  137. Dot.PwmEnd = Dot.PwmStart + Dot.Value;
  138. lastPwmEnd = Dot.PwmEnd;
  139. Dot.Updated = true;
  140. }
  141. }
  142. }
  143. void FadeInOutDigit(int T, int D, int Duration, bool RngUpdate = false) {
  144. // If the tube is in the middle of a refresh sequence and a call comes
  145. // in to update the tube digit (for time), override the last value of
  146. // the refresh sequence with the new digit.
  147. if (Tubes[T].RefreshActive && !RngUpdate) {
  148. Tubes[T].RefreshLastDigit = D;
  149. }
  150. // Dont update if actively refreshing tube unless RngUpdate is set
  151. if ((!RngUpdate && !Tubes[T].RefreshActive) || (RngUpdate)) {
  152. for (int i = 0; i < NUM_DIGITS; i++) {
  153. Tubes[T].Digits[i].CurrentState = Decrementing;
  154. }
  155. if (D != -1) {
  156. Tubes[T].Digits[D].CurrentState = Incrementing;
  157. }
  158. Tubes[T].FadeDuration = Duration;
  159. Tubes[T].LastActiveDigit = D;
  160. if (T == 0) {
  161. Tube0UpdateTimeout.attach_us(Tube0UpdateCallback, REFRESH_RATE_US);
  162. } else if (T == 1) {
  163. Tube1UpdateTimeout.attach_us(Tube1UpdateCallback, REFRESH_RATE_US);
  164. } else if (T == 2) {
  165. Tube2UpdateTimeout.attach_us(Tube2UpdateCallback, REFRESH_RATE_US);
  166. } else if (T == 3) {
  167. Tube3UpdateTimeout.attach_us(Tube3UpdateCallback, REFRESH_RATE_US);
  168. }
  169. }
  170. }
  171. bool RtcTick, RefreshTick, RngTick;
  172. Ticker RefreshTicker;
  173. Timeout RngRefreshTimeout;
  174. // Callback from DS3231 interrupt (1Hz)
  175. void RtcInterruptCallback(void) {
  176. RtcTick = true;
  177. }
  178. // Callback from RefreshTicker (REFRESH_RATE_US)
  179. void RefreshTickerCallback(void) {
  180. RefreshTick = true;
  181. }
  182. void RngTickerCallback(void) {
  183. RngTick = true;
  184. }
  185. Timeout RngUpdateTimeout;
  186. int RngUpdateIteration;
  187. void RngUpdateCallback(void) {
  188. int newfadeDuration = DIGIT_FADE_DURATION_US - RngUpdateIteration * DIGIT_RNG_FADE_DURATION_US;
  189. if (newfadeDuration <= DIGIT_RNG_FADE_DURATION_US) newfadeDuration = DIGIT_RNG_FADE_DURATION_US;
  190. for (int i = 0; i < NUM_TUBES; i++) {
  191. if (Tubes[i].RefreshActive) {
  192. if (RngUpdateIteration) {
  193. int nextDigit;
  194. #ifdef REFRESH_SEQUENTIAL
  195. nextDigit = (Tubes[i].LastActiveDigit >= 9 || Tubes[i].LastActiveDigit < 0) ?
  196. 0 : Tubes[i].LastActiveDigit + 1;;
  197. #endif
  198. #ifdef REFRESH_RANDOM
  199. do {
  200. nextDigit = rand() % NUM_DIGITS;
  201. } while (nextDigit == Tubes[i].LastActiveDigit ||
  202. (RngUpdateIteration == 1 && nextDigit == Tubes[i].RefreshLastDigit));
  203. #endif
  204. FadeInOutDigit(i, nextDigit, newfadeDuration, true);
  205. } else {
  206. FadeInOutDigit(i, Tubes[i].RefreshLastDigit, newfadeDuration, true);
  207. Tubes[i].RefreshActive = false;
  208. }
  209. }
  210. }
  211. if (RngUpdateIteration-- != 0) {
  212. RngUpdateTimeout.attach_us(RngUpdateCallback, newfadeDuration);
  213. }
  214. }
  215. int main() {
  216. #ifdef REVISION_B
  217. serial.baud(115200);
  218. #endif
  219. // Initialize pointers in global data structure
  220. for (int i = 0; i < NUM_TUBES; i++) {
  221. for (int j = 0; j < NUM_DIGITS; j++) {
  222. Tubes[i].Digits[j].CurrentState = Decrementing;
  223. Tubes[i].Digits[j].Value = DIGIT_MIN;
  224. Tubes[i].Digits[j].Updated = false;
  225. }
  226. Tubes[i].LastActiveDigit = -1;
  227. Tubes[i].RefreshLastDigit = -1;
  228. Tubes[i].RefreshActive = true;
  229. Tubes[i].FadeDuration = 0;
  230. }
  231. Dot.CurrentState = Decrementing;
  232. Dot.Value = DOT_MIN;
  233. Dot.Updated = false;
  234. RtcTick = false;
  235. RefreshTick = false;
  236. RngTick = false;
  237. int nextSecond, nextMinute, nextHour;
  238. int prevMinute, prevHour;
  239. int day, date, month, year;
  240. bool startup = true;
  241. // Start I2C at 400kHz for DS3231
  242. i2c.frequency(400000);
  243. // Start with HV PSU disabled
  244. HV_EnableOutput(false);
  245. IO_Init();
  246. TUSB322_Init();
  247. PCA9685_Init();
  248. DS3231_Init(RtcInterruptCallback);
  249. // Enable HV PSU
  250. HV_EnableOutput(true);
  251. // Set PCA9685 input voltage to highest possible
  252. PCA9685_SetVoltage(1.0);
  253. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  254. // Bump I2C frequency to 1MHz
  255. // i2c.frequency(1000000);
  256. // Animate_Cycle_Basic(10);
  257. // Animate_Cycle_Analog(10);
  258. // Animate_Cycle_Low_Pwm(10);
  259. // Animate_Cycle_Pwm(10);
  260. // Animate_Cycle_Fade(10);
  261. // Animate_Cycle_Fade_Random(10);
  262. // Animate_Cycle_Fast(10);
  263. // Animate_Cycle_Fast_Random(100);
  264. // wait(3);
  265. // Set RTC time through the following steps:
  266. // 1. Build and flash firmware that sets time on boot
  267. // 2. Reset board (non-DFU) at programmed time to set RTC time
  268. // 3. Reset board in DFU mode
  269. // 4. Build and flash firmware that does not set time on boot
  270. // DS3231_SetTime(00, 13, 17);
  271. // DS3231_SetDate(SUNDAY, 1, 11, 20, 0);
  272. // Setup a ticker to refresh the display at 1kHz
  273. RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
  274. // Kick off the RNG timeout animation on startup
  275. int nextRngTimeout = DIGIT_RNG_REFRESH_INTERVAL + (rand() % DIGIT_RNG_REFRESH_VARIANCE);
  276. RngRefreshTimeout.attach(RngTickerCallback, nextRngTimeout);
  277. RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
  278. RngUpdateCallback();
  279. while(1) {
  280. // On every refresh tick, update the display with new values
  281. if (RefreshTick) {
  282. RefreshTick = false;
  283. StaggerPwmOutput();
  284. for (int i = 0; i < NUM_TUBES; i++) {
  285. for (int j = 0; j < NUM_DIGITS; j++) {
  286. if (Tubes[i].Digits[j].Updated) {
  287. if (Tubes[i].Digits[j].Value == PCA9685_Min_Brightness ||
  288. Tubes[i].Digits[j].Value == PCA9685_Max_Brightness) {
  289. PCA9685_SetDigit(i, j, Tubes[i].Digits[j].Value);
  290. } else {
  291. PCA9685_SetDigitPwm(i, j, Tubes[i].Digits[j].PwmStart, Tubes[i].Digits[j].PwmEnd);
  292. }
  293. Tubes[i].Digits[j].Updated = false;
  294. }
  295. }
  296. }
  297. if (Dot.Updated) {
  298. if (Dot.Value == PCA9685_Min_Brightness || Dot.Value == PCA9685_Max_Brightness) {
  299. PCA9685_SetDot(Dot.Value);
  300. } else {
  301. PCA9685_SetDotPwm(Dot.PwmStart, Dot.PwmEnd);
  302. }
  303. Dot.Updated = false;
  304. }
  305. }
  306. // On every RTC tick, read and process the current time
  307. if (RtcTick) {
  308. RtcTick = false;
  309. // Save the previous time to check if digit(s) has changed
  310. prevMinute = nextMinute;
  311. prevHour = nextHour;
  312. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  313. DS3231_GetDate(&day, &date, &month, &year);
  314. // Compensate for daylight savings time
  315. nextHour = IsDst(day, date, month, nextHour) ? (nextHour + 1) % 12 : nextHour % 12;
  316. if (nextHour == 0) nextHour = 12;
  317. // Update the display configuration based on the new/previous time
  318. if (startup || prevHour / 10 != nextHour / 10) {
  319. FadeInOutDigit(0, (nextHour / 10 != 0) ? nextHour / 10 : -1 , DIGIT_FADE_DURATION_US);
  320. }
  321. if (startup || prevHour % 10 != nextHour % 10) {
  322. FadeInOutDigit(1, nextHour % 10, DIGIT_FADE_DURATION_US);
  323. }
  324. if (startup || prevMinute / 10 != nextMinute / 10) {
  325. FadeInOutDigit(2, nextMinute / 10, DIGIT_FADE_DURATION_US);
  326. }
  327. if (startup || prevMinute % 10 != nextMinute % 10) {
  328. FadeInOutDigit(3, nextMinute % 10, DIGIT_FADE_DURATION_US);
  329. }
  330. Dot.CurrentState = (Dot.CurrentState == Decrementing) ? Incrementing : Decrementing;
  331. DotUpdateTimeout.attach_us(DotUpdateCallback, DOT_FADE_DURATION_US / REFRESH_RATE_US);
  332. // Clear the startup run-once flag after the first iteration
  333. startup = false;
  334. }
  335. if (RngTick) {
  336. RngTick = false;
  337. int refreshTube = rand() % NUM_TUBES;
  338. Tubes[refreshTube].RefreshLastDigit = Tubes[refreshTube].LastActiveDigit;
  339. Tubes[refreshTube].RefreshActive = true;
  340. RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
  341. RngUpdateCallback();
  342. nextRngTimeout = DIGIT_RNG_REFRESH_INTERVAL + (rand() % DIGIT_RNG_REFRESH_VARIANCE);
  343. RngRefreshTimeout.attach(RngTickerCallback, nextRngTimeout);
  344. }
  345. }
  346. }
  347. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  348. char buffer[I2C_MAX_BUFFER+1] = {0};
  349. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  350. buffer[0] = RegAddress;
  351. memcpy(&buffer[1], Data, Length);
  352. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  353. }
  354. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  355. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  356. i2c.read(DeviceAddress << 1, Data, Length);
  357. }