main.cpp 14 KB

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