main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 ComputePwmOffsets() {
  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].PwmStart = 0;
  106. Tubes[i].Digits[j].PwmEnd = 0;
  107. }
  108. else if (Tubes[i].Digits[j].Value == PCA9685_Max_Brightness) {
  109. Tubes[i].Digits[j].PwmStart = 0;
  110. Tubes[i].Digits[j].PwmEnd = PCA9685_Max_Brightness;
  111. }
  112. else {
  113. Tubes[i].Digits[j].PwmStart = lastPwmEnd;
  114. Tubes[i].Digits[j].PwmEnd = lastPwmEnd + Tubes[i].Digits[j].Value;
  115. lastPwmEnd = Tubes[i].Digits[j].PwmEnd;
  116. Tubes[i].Digits[j].Updated = true;
  117. }
  118. }
  119. }
  120. if (Dot.Value == PCA9685_Min_Brightness) {
  121. Dot.PwmStart = 0;
  122. Dot.PwmEnd = 0;
  123. }
  124. else if (Dot.Value == PCA9685_Max_Brightness) {
  125. Dot.PwmStart = 0;
  126. Dot.PwmEnd = PCA9685_Max_Brightness;
  127. }
  128. else {
  129. Dot.PwmStart = lastPwmEnd;
  130. Dot.PwmEnd = lastPwmEnd + Dot.Value;
  131. lastPwmEnd = Dot.PwmEnd;
  132. Dot.Updated = true;
  133. }
  134. }
  135. else {
  136. // Compute the amount of overlap between all outputs
  137. int overlap = (totalOnTime - PCA9685_Max_Brightness) / (validOutputs - 1);
  138. // Compute the staggered output period for each output
  139. for (int i = 0; i < NUM_TUBES; i++) {
  140. for (int j = 0; j < NUM_DIGITS; j++) {
  141. if (Tubes[i].Digits[j].Value == PCA9685_Min_Brightness) {
  142. Tubes[i].Digits[j].PwmStart = 0;
  143. Tubes[i].Digits[j].PwmEnd = 0;
  144. }
  145. else if (Tubes[i].Digits[j].Value == PCA9685_Max_Brightness) {
  146. Tubes[i].Digits[j].PwmStart = 0;
  147. Tubes[i].Digits[j].PwmEnd = PCA9685_Max_Brightness;
  148. }
  149. else {
  150. Tubes[i].Digits[j].PwmStart = (lastPwmEnd - overlap < 0) ? 0 : lastPwmEnd - overlap;
  151. Tubes[i].Digits[j].PwmEnd = Tubes[i].Digits[j].PwmStart + Tubes[i].Digits[j].Value;
  152. lastPwmEnd = Tubes[i].Digits[j].PwmEnd;
  153. Tubes[i].Digits[j].Updated = true;
  154. }
  155. }
  156. }
  157. if (Dot.Value == PCA9685_Min_Brightness) {
  158. Dot.PwmStart = 0;
  159. Dot.PwmEnd = 0;
  160. }
  161. else if (Dot.Value == PCA9685_Max_Brightness) {
  162. Dot.PwmStart = 0;
  163. Dot.PwmEnd = PCA9685_Max_Brightness;
  164. }
  165. else {
  166. Dot.PwmStart = (lastPwmEnd - overlap < 0) ? 0 : lastPwmEnd - overlap;
  167. Dot.PwmEnd = Dot.PwmStart + Dot.Value;
  168. lastPwmEnd = Dot.PwmEnd;
  169. Dot.Updated = true;
  170. }
  171. }
  172. }
  173. void FadeInOutDigit(int T, int D, int Duration, bool RngUpdate = false) {
  174. // If the tube is in the middle of a refresh sequence and a call comes
  175. // in to update the tube digit (for time), override the last value of
  176. // the refresh sequence with the new digit.
  177. if (Tubes[T].RefreshActive && !RngUpdate) {
  178. Tubes[T].RefreshLastDigit = D;
  179. }
  180. // Dont update if actively refreshing tube unless RngUpdate is set
  181. if ((!RngUpdate && !Tubes[T].RefreshActive) || (RngUpdate)) {
  182. for (int i = 0; i < NUM_DIGITS; i++) {
  183. Tubes[T].Digits[i].CurrentState = Decrementing;
  184. }
  185. if (D != -1) {
  186. Tubes[T].Digits[D].CurrentState = Incrementing;
  187. }
  188. Tubes[T].FadeDuration = Duration;
  189. Tubes[T].LastActiveDigit = D;
  190. if (T == 0) {
  191. Tube0UpdateTimeout.attach_us(Tube0UpdateCallback, REFRESH_RATE_US);
  192. } else if (T == 1) {
  193. Tube1UpdateTimeout.attach_us(Tube1UpdateCallback, REFRESH_RATE_US);
  194. } else if (T == 2) {
  195. Tube2UpdateTimeout.attach_us(Tube2UpdateCallback, REFRESH_RATE_US);
  196. } else if (T == 3) {
  197. Tube3UpdateTimeout.attach_us(Tube3UpdateCallback, REFRESH_RATE_US);
  198. }
  199. }
  200. }
  201. bool RtcTick, RefreshTick, RngTick;
  202. Ticker RefreshTicker;
  203. Timeout RngRefreshTimeout;
  204. // Callback from DS3231 interrupt (1Hz)
  205. void RtcInterruptCallback(void) {
  206. RtcTick = true;
  207. }
  208. // Callback from RefreshTicker (REFRESH_RATE_US)
  209. void RefreshTickerCallback(void) {
  210. RefreshTick = true;
  211. }
  212. void RngTickerCallback(void) {
  213. RngTick = true;
  214. }
  215. Timeout RngUpdateTimeout;
  216. int RngUpdateIteration;
  217. void RngUpdateCallback(void) {
  218. int newfadeDuration = DIGIT_FADE_DURATION_US - RngUpdateIteration * DIGIT_RNG_FADE_DURATION_US;
  219. if (newfadeDuration <= DIGIT_RNG_FADE_DURATION_US) newfadeDuration = DIGIT_RNG_FADE_DURATION_US;
  220. for (int i = 0; i < NUM_TUBES; i++) {
  221. if (Tubes[i].RefreshActive) {
  222. if (RngUpdateIteration) {
  223. int nextDigit;
  224. #ifdef REFRESH_SEQUENTIAL
  225. nextDigit = (Tubes[i].LastActiveDigit >= 9 || Tubes[i].LastActiveDigit < 0) ?
  226. 0 : Tubes[i].LastActiveDigit + 1;;
  227. #endif
  228. #ifdef REFRESH_RANDOM
  229. do {
  230. nextDigit = rand() % NUM_DIGITS;
  231. } while (nextDigit == Tubes[i].LastActiveDigit ||
  232. (RngUpdateIteration == 1 && nextDigit == Tubes[i].RefreshLastDigit));
  233. #endif
  234. FadeInOutDigit(i, nextDigit, newfadeDuration, true);
  235. } else {
  236. FadeInOutDigit(i, Tubes[i].RefreshLastDigit, newfadeDuration, true);
  237. Tubes[i].RefreshActive = false;
  238. }
  239. }
  240. }
  241. if (RngUpdateIteration-- != 0) {
  242. RngUpdateTimeout.attach_us(RngUpdateCallback, newfadeDuration);
  243. }
  244. }
  245. int main() {
  246. #ifdef REVISION_B
  247. serial.baud(115200);
  248. #endif
  249. // Initialize pointers in global data structure
  250. for (int i = 0; i < NUM_TUBES; i++) {
  251. for (int j = 0; j < NUM_DIGITS; j++) {
  252. Tubes[i].Digits[j].CurrentState = Decrementing;
  253. Tubes[i].Digits[j].Value = DIGIT_MIN;
  254. Tubes[i].Digits[j].Updated = false;
  255. }
  256. Tubes[i].LastActiveDigit = -1;
  257. Tubes[i].RefreshLastDigit = -1;
  258. Tubes[i].RefreshActive = true;
  259. Tubes[i].FadeDuration = 0;
  260. }
  261. Dot.CurrentState = Decrementing;
  262. Dot.Value = DOT_MIN;
  263. Dot.Updated = false;
  264. RtcTick = false;
  265. RefreshTick = false;
  266. RngTick = false;
  267. int nextSecond, nextMinute, nextHour;
  268. int prevMinute, prevHour;
  269. int day, date, month, year;
  270. bool startup = true;
  271. // Start I2C at 400kHz for DS3231
  272. i2c.frequency(400000);
  273. // Start with HV PSU disabled
  274. HV_EnableOutput(false);
  275. IO_Init();
  276. TUSB322_Init();
  277. PCA9685_Init();
  278. DS3231_Init(RtcInterruptCallback);
  279. // Enable HV PSU
  280. HV_EnableOutput(true);
  281. // Set PCA9685 input voltage to highest possible
  282. PCA9685_SetVoltage(1.0);
  283. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  284. // Bump I2C frequency to 1MHz
  285. // i2c.frequency(1000000);
  286. // Animate_Cycle_Basic(10);
  287. // Animate_Cycle_Analog(10);
  288. // Animate_Cycle_Low_Pwm(10);
  289. // Animate_Cycle_Pwm(10);
  290. // Animate_Cycle_Fade(10);
  291. // Animate_Cycle_Fade_Random(10);
  292. // Animate_Cycle_Fast(10);
  293. // Animate_Cycle_Fast_Random(100);
  294. // wait(3);
  295. // Set RTC time through the following steps:
  296. // 1. Build and flash firmware that sets time on boot
  297. // 2. Reset board (non-DFU) at programmed time to set RTC time
  298. // 3. Reset board in DFU mode
  299. // 4. Build and flash firmware that does not set time on boot
  300. // DS3231_SetTime(00, 13, 17);
  301. // DS3231_SetDate(SUNDAY, 1, 11, 20, 0);
  302. // Setup a ticker to refresh the display at 1kHz
  303. RefreshTicker.attach_us(RefreshTickerCallback, REFRESH_RATE_US);
  304. // Kick off the RNG timeout animation on startup
  305. int nextRngTimeout = DIGIT_RNG_REFRESH_INTERVAL + (rand() % DIGIT_RNG_REFRESH_VARIANCE);
  306. RngRefreshTimeout.attach(RngTickerCallback, nextRngTimeout);
  307. RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
  308. RngUpdateCallback();
  309. while(1) {
  310. // On every refresh tick, update the display with new values
  311. if (RefreshTick) {
  312. RefreshTick = false;
  313. ComputePwmOffsets();
  314. for (int i = 0; i < NUM_TUBES; i++) {
  315. for (int j = 0; j < NUM_DIGITS; j++) {
  316. if (Tubes[i].Digits[j].Updated) {
  317. PCA9685_SetDigitPwm(i, j, Tubes[i].Digits[j].PwmStart, Tubes[i].Digits[j].PwmEnd);
  318. Tubes[i].Digits[j].Updated = false;
  319. }
  320. }
  321. }
  322. if (Dot.Updated) {
  323. PCA9685_SetDotPwm(Dot.PwmStart, Dot.PwmEnd);
  324. Dot.Updated = false;
  325. }
  326. }
  327. // On every RTC tick, read and process the current time
  328. if (RtcTick) {
  329. RtcTick = false;
  330. // Save the previous time to check if digit(s) has changed
  331. prevMinute = nextMinute;
  332. prevHour = nextHour;
  333. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  334. DS3231_GetDate(&day, &date, &month, &year);
  335. // Compensate for daylight savings time
  336. nextHour = IsDst(day, date, month, nextHour) ? (nextHour + 1) % 12 : nextHour % 12;
  337. if (nextHour == 0) nextHour = 12;
  338. // Update the display configuration based on the new/previous time
  339. if (startup || prevHour / 10 != nextHour / 10) {
  340. FadeInOutDigit(0, (nextHour / 10 != 0) ? nextHour / 10 : -1 , DIGIT_FADE_DURATION_US);
  341. }
  342. if (startup || prevHour % 10 != nextHour % 10) {
  343. FadeInOutDigit(1, nextHour % 10, DIGIT_FADE_DURATION_US);
  344. }
  345. if (startup || prevMinute / 10 != nextMinute / 10) {
  346. FadeInOutDigit(2, nextMinute / 10, DIGIT_FADE_DURATION_US);
  347. }
  348. if (startup || prevMinute % 10 != nextMinute % 10) {
  349. FadeInOutDigit(3, nextMinute % 10, DIGIT_FADE_DURATION_US);
  350. }
  351. Dot.CurrentState = (Dot.CurrentState == Decrementing) ? Incrementing : Decrementing;
  352. DotUpdateTimeout.attach_us(DotUpdateCallback, DOT_FADE_DURATION_US / REFRESH_RATE_US);
  353. // Clear the startup run-once flag after the first iteration
  354. startup = false;
  355. }
  356. if (RngTick) {
  357. RngTick = false;
  358. int refreshTube = rand() % NUM_TUBES;
  359. Tubes[refreshTube].RefreshLastDigit = Tubes[refreshTube].LastActiveDigit;
  360. Tubes[refreshTube].RefreshActive = true;
  361. RngUpdateIteration = DIGIT_RNG_FADE_ITERATIONS;
  362. RngUpdateCallback();
  363. nextRngTimeout = DIGIT_RNG_REFRESH_INTERVAL + (rand() % DIGIT_RNG_REFRESH_VARIANCE);
  364. RngRefreshTimeout.attach(RngTickerCallback, nextRngTimeout);
  365. }
  366. }
  367. }
  368. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  369. char buffer[I2C_MAX_BUFFER+1] = {0};
  370. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  371. buffer[0] = RegAddress;
  372. memcpy(&buffer[1], Data, Length);
  373. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  374. }
  375. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  376. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  377. i2c.read(DeviceAddress << 1, Data, Length);
  378. }