main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Tick = false;
  12. void RtcCallback(void) {
  13. Tick = true;
  14. }
  15. int main() {
  16. // Start I2C at 400kHz for DS3231
  17. i2c.frequency(400000);
  18. // Start with HV PSU disabled
  19. HV_EnableOutput(false);
  20. TUSB322_Init();
  21. PCA9685_Init();
  22. DS3231_Init(RtcCallback);
  23. // Enable HV PSU
  24. HV_EnableOutput(true);
  25. // Set PCA9685 input voltage to highest possible
  26. PCA9685_SetVoltage(1.0);
  27. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  28. // Bump I2C frequency to 1MHz
  29. i2c.frequency(1000000);
  30. // DS3231_SetTime(0, 55, 6, true);
  31. // DS3231_SetDate(0, 2, 12, 18, 0);
  32. int currMinute = 0, currHour = 0;
  33. int nextSecond, nextMinute, nextHour;
  34. int rngTick = 0;
  35. while(1) {
  36. // Animate_Cycle_Basic();
  37. // Animate_Cycle_Analog();
  38. // Animate_Cycle_Low_Pwm();
  39. // Animate_Cycle_Pwm();
  40. // Animate_Cycle_Fade();
  41. // Animate_Cycle_Fade_Random();
  42. // Animate_Cycle_Fast();
  43. // Animate_Cycle_Fast_Random();
  44. rngTick = rand() % 60;
  45. if (Tick) {
  46. Tick = false;
  47. rngTick--;
  48. if (rngTick == 0) {
  49. rngTick = rand() % 60;
  50. }
  51. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  52. // Fade dot/digits to show the updated time
  53. for (int i = 0; i < PCA9685_Max_Brightness; i += 4) {
  54. if (nextMinute % 10 != currMinute % 10) {
  55. PCA9685_SetDigit(3, currMinute % 10, PCA9685_Max_Brightness - i);
  56. PCA9685_SetDigit(3, nextMinute % 10, PCA9685_Min_Brightness + i);
  57. }
  58. if (nextMinute / 10 != currMinute / 10) {
  59. PCA9685_SetDigit(2, currMinute / 10, PCA9685_Max_Brightness - i);
  60. PCA9685_SetDigit(2, nextMinute / 10, PCA9685_Min_Brightness + i);
  61. }
  62. if (nextHour % 10 != currHour % 10 ) {
  63. PCA9685_SetDigit(1, currHour % 10, PCA9685_Max_Brightness - i);
  64. PCA9685_SetDigit(1, nextHour % 10, PCA9685_Min_Brightness + i);
  65. }
  66. if (nextHour / 10 != currHour / 10) {
  67. PCA9685_SetDigit(0, currHour / 10, PCA9685_Max_Brightness - i);
  68. if (nextHour / 10 != 0) {
  69. PCA9685_SetDigit(0, nextHour / 10, PCA9685_Min_Brightness + i);
  70. }
  71. }
  72. PCA9685_SetDot(nextSecond % 2 ? i : PCA9685_Max_Brightness - i);
  73. wait(0.0007);
  74. }
  75. currMinute = nextMinute;
  76. currHour = nextHour;
  77. }
  78. // Delay a bit to avoid constant polling
  79. wait(0.001);
  80. }
  81. }
  82. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  83. char buffer[I2C_MAX_BUFFER+1] = {0};
  84. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  85. buffer[0] = RegAddress;
  86. memcpy(&buffer[1], Data, Length);
  87. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  88. }
  89. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  90. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  91. i2c.read(DeviceAddress << 1, Data, Length);
  92. }