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.

153 lines
4.4KB

  1. /*
  2. A really simple analog clock
  3. Works with Arduino 1.0.6 IDE, Arduino 1.6.x
  4. */
  5. #include <SPI.h>
  6. #include <RA8875.h>
  7. /*
  8. Teensy3.x and Arduino's
  9. You are using 4 wire SPI here, so:
  10. MOSI: 11//Teensy3.x
  11. MISO: 12//Teensy3.x
  12. SCK: 13//Teensy3.x
  13. the rest of pin below:
  14. */
  15. #define RA8875_CS 10 //any digital pin
  16. #define RA8875_RESET 9//any pin or nothing!
  17. RA8875 tft = RA8875(RA8875_CS, RA8875_RESET); //Teensy3/arduino's
  18. uint16_t ccenterx, ccentery; //center x,y of the clock
  19. const uint16_t cradius = 110;//radius of the clock
  20. const float scosConst = 0.0174532925;
  21. float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;
  22. float sdeg = 0, mdeg = 0, hdeg = 0;
  23. uint16_t osx, osy, omx, omy, ohx, ohy;
  24. uint16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0;
  25. uint32_t targetTime = 0;// for next 1 second timeout
  26. uint8_t hh, mm, ss; //containers for current time
  27. void drawClockFace() {
  28. tft.fillCircle(ccenterx, ccentery, cradius, RA8875_BLUE);
  29. tft.fillCircle(ccenterx, ccentery, cradius - 4, RA8875_BLACK);
  30. // Draw 12 lines
  31. for (int i = 0; i < 360; i += 30) {
  32. sx = cos((i - 90) * scosConst);
  33. sy = sin((i - 90) * scosConst);
  34. x0 = sx * (cradius - 4) + ccenterx;
  35. yy0 = sy * (cradius - 4) + ccentery;
  36. x1 = sx * (cradius - 11) + ccenterx;
  37. yy1 = sy * (cradius - 11) + ccentery;
  38. tft.drawLine(x0, yy0, x1, yy1, RA8875_BLUE);
  39. }
  40. }
  41. static uint8_t conv2d(const char* p) {
  42. uint8_t v = 0;
  43. if ('0' <= *p && *p <= '9') v = *p - '0';
  44. return 10 * v + *++p - '0';
  45. }
  46. void setup(void) {
  47. // begin display: Choose from: RA8875_480x272, RA8875_800x480, RA8875_800x480ALT, Adafruit_480x272, Adafruit_800x480
  48. tft.begin(RA8875_800x480);
  49. tft.setTextColor(RA8875_WHITE, RA8875_BLACK);
  50. ccenterx = tft.width() / 2;
  51. ccentery = tft.height() / 2;
  52. osx = ccenterx;
  53. osy = ccentery;
  54. omx = ccenterx;
  55. omy = ccentery;
  56. ohx = ccenterx;
  57. ohy = ccentery;
  58. drawClockFace();// Draw clock face
  59. //get current time from compiler
  60. hh = conv2d(__TIME__);
  61. mm = conv2d(__TIME__ + 3);
  62. ss = conv2d(__TIME__ + 6);
  63. targetTime = millis() + 1000;
  64. }
  65. void drawClockHands(uint8_t h, uint8_t m, uint8_t s) {
  66. // Pre-compute hand degrees, x & y coords for a fast screen update
  67. sdeg = s * 6; // 0-59 -> 0-354
  68. mdeg = m * 6 + sdeg * 0.01666667; // 0-59 -> 0-360 - includes seconds
  69. hdeg = h * 30 + mdeg * 0.0833333; // 0-11 -> 0-360 - includes minutes and seconds
  70. hx = cos((hdeg - 90) * scosConst);
  71. hy = sin((hdeg - 90) * scosConst);
  72. mx = cos((mdeg - 90) * scosConst);
  73. my = sin((mdeg - 90) * scosConst);
  74. sx = cos((sdeg - 90) * scosConst);
  75. sy = sin((sdeg - 90) * scosConst);
  76. // Erase just old hand positions
  77. tft.drawLine(ohx, ohy, ccenterx + 1, ccentery + 1, RA8875_BLACK);
  78. tft.drawLine(omx, omy, ccenterx + 1, ccentery + 1, RA8875_BLACK);
  79. tft.drawLine(osx, osy, ccenterx + 1, ccentery + 1, RA8875_BLACK);
  80. // Draw new hand positions
  81. tft.drawLine(hx * (cradius - 28) + ccenterx + 1, hy * (cradius - 28) + ccentery + 1, ccenterx + 1, ccentery + 1, RA8875_WHITE);
  82. tft.drawLine(mx * (cradius - 17) + ccenterx + 1, my * (cradius - 17) + ccentery + 1, ccenterx + 1, ccentery + 1, RA8875_WHITE);
  83. tft.drawLine(sx * (cradius - 14) + ccenterx + 1, sy * (cradius - 14) + ccentery + 1, ccenterx + 1, ccentery + 1, RA8875_RED);
  84. tft.fillCircle(ccenterx + 1, ccentery + 1, 3, RA8875_RED);
  85. // Update old x&y coords
  86. osx = sx * (cradius - 14) + ccenterx + 1;
  87. osy = sy * (cradius - 14) + ccentery + 1;
  88. omx = mx * (cradius - 17) + ccenterx + 1;
  89. omy = my * (cradius - 17) + ccentery + 1;
  90. ohx = hx * (cradius - 28) + ccenterx + 1;
  91. ohy = hy * (cradius - 28) + ccentery + 1;
  92. }
  93. void drawPrintTime(uint16_t x, uint16_t y, uint8_t h, uint8_t m, uint8_t s) {
  94. //tft.changeMode(TEXT);
  95. tft.setCursor (x, y);
  96. tft.print(__DATE__);
  97. tft.setCursor (x, y - 13);
  98. if (hh > 12) {
  99. if (hh < 22) tft.print('0');
  100. tft.print (hh - 12);
  101. }
  102. else {
  103. if (hh < 10) tft.print('0');
  104. tft.print (hh);
  105. }
  106. tft.print (':');
  107. if (mm < 10) tft.print('0');
  108. tft.print (mm);
  109. tft.print (':');
  110. if (ss < 10) tft.print('0');
  111. tft.print (ss);
  112. if (hh > 12) {
  113. tft.print(" pm");
  114. }
  115. else {
  116. tft.print (" am");
  117. }
  118. //tft.changeMode(GRAPHIC);
  119. }
  120. void loop() {
  121. if (targetTime < millis()) {
  122. targetTime = millis() + 1000;
  123. ss++;
  124. if (ss == 60) {
  125. ss = 0;
  126. mm++;
  127. if (mm > 59) {
  128. mm = 0;
  129. hh++;
  130. if (hh > 23) hh = 0;
  131. }
  132. }
  133. drawClockHands(hh, mm, ss);
  134. drawPrintTime(34, 151, hh, mm, ss);
  135. }
  136. }