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.

109 lines
4.0KB

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