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.

clock2.ino 4.1KB

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