PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.1KB

  1. /*
  2. A similar clock example but this time uses some RA8875 gauges library function!
  3. */
  4. #include <SPI.h>
  5. #include <RA8875.h>
  6. #define RA8875_CS 10 //see below...
  7. #define RA8875_RESET 9//any pin or nothing!
  8. /*
  9. DUE can use: 4,10,52(o 53? not remember)
  10. */
  11. RA8875 tft = RA8875(RA8875_CS, RA8875_RESET);
  12. int16_t clockPos[3] = {0, 0, 50}; //x,y,r
  13. uint8_t currentTime[3] = {0, 0, 0}; //hh,mm,ss
  14. //background,face,hh,mm,ss
  15. const uint16_t clockColors[5] = {RA8875_BLACK, RA8875_BLACK, RA8875_CYAN, RA8875_BLUE, RA8875_RED};
  16. //houX,houY,minX,minY,secX,secY
  17. uint16_t oldPos[6] = {0, 0, 0, 0, 0, 0};
  18. unsigned long targetTime = 0;
  19. void setup() {
  20. // Serial.begin(38400);
  21. // long unsigned debug_start = millis ();
  22. // while (!Serial && ((millis () - debug_start) <= 5000)) ;
  23. tft.begin(RA8875_800x480);
  24. clockPos[0] = tft.width() / 2;
  25. clockPos[1] = tft.height() / 2;
  26. if (tft.isPortrait()){
  27. clockPos[2] = (tft.width()/2) - 20;
  28. } else {
  29. clockPos[2] = (tft.height()/2) - 20;
  30. }
  31. //clockPos[2] = 128;
  32. oldPos[0] = clockPos[0]; oldPos[1] = clockPos[1];
  33. oldPos[2] = clockPos[0]; oldPos[3] = clockPos[1];
  34. oldPos[4] = clockPos[0]; oldPos[5] = clockPos[1];
  35. tft.brightness(150);
  36. drawGauge(clockPos, 0, 360);
  37. //get current time from compiler
  38. currentTime[0] = conv2d(__TIME__);
  39. currentTime[1] = conv2d(__TIME__ + 3);
  40. currentTime[2] = conv2d(__TIME__ + 6);
  41. targetTime = millis() + 1000;
  42. }
  43. void loop() {
  44. if (targetTime < millis()) {
  45. targetTime = millis() + 1000;
  46. currentTime[2]++;
  47. if (currentTime[2] == 60) {
  48. currentTime[2] = 0;
  49. currentTime[1]++;
  50. if (currentTime[1] > 59) {
  51. currentTime[1] = 0;
  52. currentTime[0]++;
  53. if (currentTime[0] > 23) currentTime[0] = 0;
  54. }
  55. }
  56. drawClockHands(clockPos, currentTime, clockColors);
  57. }
  58. }
  59. void drawGauge(int16_t pos[], uint16_t from, uint16_t to) {
  60. tft.drawCircle(pos[0], pos[1], pos[2], RA8875_WHITE); //draw instrument container
  61. tft.drawCircle(pos[0], pos[1], pos[2] + 1, RA8875_WHITE); //draw instrument container
  62. tft.roundGaugeTicker(pos[0], pos[1], pos[2], from, to, 1.3, RA8875_WHITE);
  63. if (pos[2] > 15) tft.roundGaugeTicker(pos[0], pos[1], pos[2], from + 15, to - 15, 1.1, RA8875_WHITE); //draw minor ticks
  64. }
  65. static uint8_t conv2d(const char* p) {
  66. uint8_t v = 0;
  67. if ('0' <= *p && *p <= '9') v = *p - '0';
  68. return 10 * v + *++p - '0';
  69. }
  70. void drawClockHands(int16_t pos[], uint8_t curTime[], const uint16_t colors[]) {
  71. uint8_t hlen = (pos[2] / 2 - pos[2] / 12);
  72. uint8_t mlen = pos[2] / 2 - pos[2] / 4;
  73. uint8_t slen = pos[2] / 2 - pos[2] / 4;
  74. float hx = cos(((curTime[0] * 30 + (curTime[1] * 6 + (curTime[2] * 6) * 0.01666667) * 0.0833333) - 90) * 0.0174532925);
  75. float hy = sin(((curTime[0] * 30 + (curTime[1] * 6 + (curTime[2] * 6) * 0.01666667) * 0.0833333) - 90) * 0.0174532925);
  76. float mx = cos(((curTime[1] * 6 + (curTime[2] * 6) * 0.01666667) - 90) * 0.0174532925);
  77. float my = sin(((curTime[1] * 6 + (curTime[2] * 6) * 0.01666667) - 90) * 0.0174532925);
  78. float sx = cos(((curTime[2] * 6) - 90) * 0.0174532925);
  79. float sy = sin(((curTime[2] * 6) - 90) * 0.0174532925);
  80. // Erase just old hand positions
  81. tft.drawLine(oldPos[0], oldPos[1], pos[0] + 1, pos[1] + 1, colors[1]);
  82. tft.drawLine(oldPos[2], oldPos[3], pos[0] + 1, pos[1] + 1, colors[1]);
  83. tft.drawLine(oldPos[4], oldPos[5], pos[0] + 1, pos[1] + 1, colors[1]);
  84. // Draw new hand positions
  85. tft.drawLine(hx * (pos[2] - hlen) + pos[0] + 1, hy * (pos[2] - hlen) + pos[1] + 1, pos[0] + 1, pos[1] + 1, colors[2]);
  86. tft.drawLine(mx * (pos[2] - mlen) + pos[0] + 1, my * (pos[2] - mlen) + pos[1] + 1, pos[0] + 1, pos[1] + 1, colors[3]);
  87. tft.drawLine(sx * (pos[2] - slen) + pos[0] + 1, sy * (pos[2] - slen) + pos[1] + 1, pos[0] + 1, pos[1] + 1, colors[4]);
  88. tft.fillCircle(pos[0] + 1, pos[1] + 1, 3, colors[4]);
  89. // Update old x&y coords
  90. oldPos[4] = sx * (pos[2] - slen) + pos[0] + 1;
  91. oldPos[5] = sy * (pos[2] - slen) + pos[1] + 1;
  92. oldPos[2] = mx * (pos[2] - mlen) + pos[0] + 1;
  93. oldPos[3] = my * (pos[2] - mlen) + pos[1] + 1;
  94. oldPos[0] = hx * (pos[2] - hlen) + pos[0] + 1;
  95. oldPos[1] = hy * (pos[2] - hlen) + pos[1] + 1;
  96. }