main.cpp 5.3 KB

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