main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. bool RtcTick, RefreshTick;
  12. bool StartupFlag, RuntimeFlag;
  13. typedef struct {
  14. char PrevDigit; // Previous digit to fade out
  15. ushort PrevValue; // Brightness of previous digit
  16. char NextDigit; // Next digit to fade in
  17. ushort NextValue; // Brightness of next digit
  18. } Digit;
  19. Digit Digits[NUM_DIGITS] = {0}; // Active per-tube digit setting
  20. int DotNextValue, DotPrevValue;
  21. bool DotIncrement = false;
  22. Ticker StartupTicker, RuntimeTicker;
  23. int StartupTickerIter, RuntimeTickerIter;
  24. void RtcCallback(void) {
  25. RtcTick = true;
  26. }
  27. void RefreshCallback(void) {
  28. RefreshTick = true;
  29. }
  30. void StartupTickerCallback(void) {
  31. for (int i = 0; i < NUM_DIGITS; i++) {
  32. Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
  33. }
  34. DotNextValue += FADE_TICK_STEP;
  35. if (--StartupTickerIter == 0) {
  36. StartupTicker.detach();
  37. for (int i = 0; i < NUM_DIGITS; i++) {
  38. Digits[i].PrevDigit = Digits[i].NextDigit;
  39. }
  40. // Switch to runtime mode once startup sequence has completed
  41. RuntimeFlag = true;
  42. }
  43. }
  44. void RuntimeTickerCallback(void) {
  45. for (int i = 0; i < NUM_DIGITS; i++) {
  46. if(Digits[i].PrevDigit != Digits[i].NextDigit) {
  47. Digits[i].PrevValue -= FADE_TICK_STEP;
  48. Digits[i].NextValue = (i == 0 && Digits[i].NextDigit == 0) ? 0 : Digits[i].NextValue + FADE_TICK_STEP;
  49. }
  50. }
  51. DotNextValue = DotIncrement ? DotNextValue + FADE_TICK_STEP: DotNextValue - FADE_TICK_STEP;
  52. if (--RuntimeTickerIter == 0) {
  53. RuntimeTicker.detach();
  54. for (int i = 0; i < NUM_DIGITS; i++) {
  55. Digits[i].PrevDigit = Digits[i].NextDigit;
  56. }
  57. DotIncrement = !DotIncrement;
  58. }
  59. }
  60. int main() {
  61. RtcTick = false;
  62. RefreshTick = false;
  63. StartupFlag = true;
  64. RuntimeFlag = false;
  65. // Start I2C at 400kHz for DS3231
  66. i2c.frequency(400000);
  67. // Start with HV PSU disabled
  68. HV_EnableOutput(false);
  69. TUSB322_Init();
  70. PCA9685_Init();
  71. DS3231_Init(RtcCallback);
  72. // Enable HV PSU
  73. HV_EnableOutput(true);
  74. // Set PCA9685 input voltage to highest possible
  75. PCA9685_SetVoltage(1.0);
  76. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  77. // Bump I2C frequency to 1MHz
  78. i2c.frequency(1000000);
  79. // Setup a ticker to refresh the display at 1kHz
  80. Ticker refreshTicker;
  81. refreshTicker.attach_us(RefreshCallback, 1000);
  82. // DS3231_SetTime(0, 55, 6, true);
  83. // DS3231_SetDate(0, 2, 12, 18, 0);
  84. while(1) {
  85. // Animate_Cycle_Basic();
  86. // Animate_Cycle_Analog();
  87. // Animate_Cycle_Low_Pwm();
  88. // Animate_Cycle_Pwm();
  89. // Animate_Cycle_Fade();
  90. // Animate_Cycle_Fade_Random();
  91. // Animate_Cycle_Fast();
  92. // Animate_Cycle_Fast_Random();
  93. if (RefreshTick) {
  94. RefreshTick = false;
  95. // On refresh, update the display with new values
  96. // If brightness has changed, update both prevous and next digits with new values
  97. for (int i = 0; i < NUM_DIGITS; i++) {
  98. if (Digits[i].NextValue != Digits[i].PrevValue) {
  99. PCA9685_SetDigit(i, Digits[i].PrevDigit, Digits[i].PrevValue);
  100. PCA9685_SetDigit(i, Digits[i].NextDigit, Digits[i].NextValue);
  101. }
  102. }
  103. if (DotNextValue != DotPrevValue) {
  104. PCA9685_SetDot(DotNextValue);
  105. }
  106. // Update previous values to what was just set
  107. for (int i = 0; i < NUM_DIGITS; i++) {
  108. Digits[i].PrevValue = Digits[i].NextValue;
  109. }
  110. DotPrevValue = DotNextValue;
  111. }
  112. if (RtcTick) {
  113. RtcTick = false;
  114. // On RTC 1Hz ticks, get the time and begin update
  115. int nextSecond, nextMinute, nextHour;
  116. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  117. Digits[3].NextDigit = nextMinute % 10;
  118. Digits[2].NextDigit = nextMinute / 10;
  119. Digits[1].NextDigit = nextHour % 10;
  120. Digits[0].NextDigit = nextHour / 10;
  121. if (StartupFlag) {
  122. // Fade in the current time
  123. StartupTickerIter = FADE_TICKS;
  124. StartupTicker.attach(StartupTickerCallback, 1/FADE_TICKS);
  125. StartupFlag = false;
  126. }
  127. if (RuntimeFlag) {
  128. // Fade in the new time
  129. RuntimeTickerIter = FADE_TICKS;
  130. RuntimeTicker.attach(RuntimeTickerCallback, 1/FADE_TICKS);
  131. }
  132. }
  133. }
  134. }
  135. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  136. char buffer[I2C_MAX_BUFFER+1] = {0};
  137. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  138. buffer[0] = RegAddress;
  139. memcpy(&buffer[1], Data, Length);
  140. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  141. }
  142. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  143. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  144. i2c.read(DeviceAddress << 1, Data, Length);
  145. }