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.

156 lines
4.4KB

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