main.cpp 5.1 KB

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