main.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 LED;
  12. void RtcCallback(void) {
  13. LED = !LED;
  14. LED_SetOutput(LED);
  15. }
  16. int main() {
  17. // Start I2C at 400kHz for DS3231
  18. i2c.frequency(400000);
  19. // Start with HV PSU disabled
  20. HV_EnableOutput(false);
  21. TUSB322_Init();
  22. PCA9685_Init();
  23. DS3231_Init(RtcCallback);
  24. // Enable HV PSU
  25. HV_EnableOutput(true);
  26. // Set PCA9685 input voltage to highest possible
  27. PCA9685_SetVoltage(1.0);
  28. // swo.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  29. // Bump I2C frequency to 1MHz for PCA9685
  30. // i2c.frequency(1000000);
  31. // DS3231_SetTime(0, 55, 6, true);
  32. // DS3231_SetDate(0, 2, 12, 18, 0);
  33. int currMinute = 0, currHour = 0;
  34. int nextSecond, nextMinute, nextHour;
  35. while(1) {
  36. DS3231_GetTime(&nextSecond, &nextMinute, &nextHour);
  37. if (nextMinute != currMinute) {
  38. PCA9685_SetDigit(3, currMinute % 10, PCA9685_Min_Brightness);
  39. PCA9685_SetDigit(2, currMinute / 10, PCA9685_Min_Brightness);
  40. PCA9685_SetDigit(3, nextMinute % 10, PCA9685_Max_Brightness);
  41. PCA9685_SetDigit(2, nextMinute / 10, PCA9685_Max_Brightness);
  42. currMinute = nextMinute;
  43. }
  44. if (nextHour != currHour) {
  45. PCA9685_SetDigit(1, currHour % 10, PCA9685_Min_Brightness);
  46. PCA9685_SetDigit(0, currHour / 10, PCA9685_Min_Brightness);
  47. PCA9685_SetDigit(1, nextHour % 10, PCA9685_Max_Brightness);
  48. PCA9685_SetDigit(0, nextHour / 10, PCA9685_Max_Brightness);
  49. currHour = nextHour;
  50. }
  51. wait(0.1);
  52. // Animate_Cycle_Basic();
  53. // Animate_Cycle_Analog();
  54. // Animate_Cycle_Low_Pwm();
  55. // Animate_Cycle_Pwm();
  56. // Animate_Cycle_Fade();
  57. // Animate_Cycle_Fade_Random();
  58. // Animate_Cycle_Fast();
  59. // Animate_Cycle_Fast_Random();
  60. }
  61. }
  62. void I2C_Write(int DeviceAddress, char RegAddress, char *Data, int Length) {
  63. char buffer[I2C_MAX_BUFFER+1] = {0};
  64. if (Length > I2C_MAX_BUFFER) LED_Fault(1);
  65. buffer[0] = RegAddress;
  66. memcpy(&buffer[1], Data, Length);
  67. i2c.write(DeviceAddress << 1, buffer, Length + 1);
  68. }
  69. void I2C_Read(int DeviceAddress, char RegAddress, char *Data, int Length) {
  70. i2c.write(DeviceAddress << 1, &RegAddress, 1);
  71. i2c.read(DeviceAddress << 1, Data, Length);
  72. }