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.

157 lines
4.2KB

  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. #define RA8875_RESET 9//any pin or nothing!
  8. #if defined(NEEDS_SET_MODULE)//Energia, this case is for stellaris/tiva
  9. RA8875 tft = RA8875(3);//select SPI module 3
  10. /*
  11. for module 3 (stellaris)
  12. SCLK: PD_0
  13. MOSI: PD_3
  14. MISO: PD_2
  15. SS: PD_1
  16. */
  17. #endif
  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. tft.begin(RA8875_800x480);
  48. tft.setTextColor(RA8875_WHITE, RA8875_BLACK);
  49. ccenterx = tft.width() / 2;
  50. ccentery = tft.height() / 2;
  51. osx = ccenterx;
  52. osy = ccentery;
  53. omx = ccenterx;
  54. omy = ccentery;
  55. ohx = ccenterx;
  56. ohy = ccentery;
  57. drawClockFace();// Draw clock face
  58. //get current time from compiler
  59. hh = conv2d(__TIME__);
  60. mm = conv2d(__TIME__ + 3);
  61. ss = conv2d(__TIME__ + 6);
  62. targetTime = millis() + 1000;
  63. }
  64. void drawClockHands(uint8_t h, uint8_t m, uint8_t s) {
  65. // Pre-compute hand degrees, x & y coords for a fast screen update
  66. sdeg = s * 6; // 0-59 -> 0-354
  67. mdeg = m * 6 + sdeg * 0.01666667; // 0-59 -> 0-360 - includes seconds
  68. hdeg = h * 30 + mdeg * 0.0833333; // 0-11 -> 0-360 - includes minutes and seconds
  69. hx = cos((hdeg - 90) * scosConst);
  70. hy = sin((hdeg - 90) * scosConst);
  71. mx = cos((mdeg - 90) * scosConst);
  72. my = sin((mdeg - 90) * scosConst);
  73. sx = cos((sdeg - 90) * scosConst);
  74. sy = sin((sdeg - 90) * scosConst);
  75. // Erase just old hand positions
  76. tft.drawLine(ohx, ohy, ccenterx + 1, ccentery + 1, RA8875_BLACK);
  77. tft.drawLine(omx, omy, ccenterx + 1, ccentery + 1, RA8875_BLACK);
  78. tft.drawLine(osx, osy, ccenterx + 1, ccentery + 1, RA8875_BLACK);
  79. // Draw new hand positions
  80. tft.drawLine(hx * (cradius - 28) + ccenterx + 1, hy * (cradius - 28) + ccentery + 1, ccenterx + 1, ccentery + 1, RA8875_WHITE);
  81. tft.drawLine(mx * (cradius - 17) + ccenterx + 1, my * (cradius - 17) + ccentery + 1, ccenterx + 1, ccentery + 1, RA8875_WHITE);
  82. tft.drawLine(sx * (cradius - 14) + ccenterx + 1, sy * (cradius - 14) + ccentery + 1, ccenterx + 1, ccentery + 1, RA8875_RED);
  83. tft.fillCircle(ccenterx + 1, ccentery + 1, 3, RA8875_RED);
  84. // Update old x&y coords
  85. osx = sx * (cradius - 14) + ccenterx + 1;
  86. osy = sy * (cradius - 14) + ccentery + 1;
  87. omx = mx * (cradius - 17) + ccenterx + 1;
  88. omy = my * (cradius - 17) + ccentery + 1;
  89. ohx = hx * (cradius - 28) + ccenterx + 1;
  90. ohy = hy * (cradius - 28) + ccentery + 1;
  91. }
  92. void drawPrintTime(uint16_t x, uint16_t y, uint8_t h, uint8_t m, uint8_t s) {
  93. //tft.changeMode(TEXT);
  94. tft.setCursor (x, y);
  95. tft.print(__DATE__);
  96. tft.setCursor (x, y - 13);
  97. if (hh > 12) {
  98. if (hh < 22) tft.print('0');
  99. tft.print (hh - 12);
  100. }
  101. else {
  102. if (hh < 10) tft.print('0');
  103. tft.print (hh);
  104. }
  105. tft.print (':');
  106. if (mm < 10) tft.print('0');
  107. tft.print (mm);
  108. tft.print (':');
  109. if (ss < 10) tft.print('0');
  110. tft.print (ss);
  111. if (hh > 12) {
  112. tft.print(" pm");
  113. }
  114. else {
  115. tft.print (" am");
  116. }
  117. //tft.changeMode(GRAPHIC);
  118. }
  119. void loop() {
  120. if (targetTime < millis()) {
  121. targetTime = millis() + 1000;
  122. ss++;
  123. if (ss == 60) {
  124. ss = 0;
  125. mm++;
  126. if (mm > 59) {
  127. mm = 0;
  128. hh++;
  129. if (hh > 23) hh = 0;
  130. }
  131. }
  132. drawClockHands(hh, mm, ss);
  133. drawPrintTime(34, 151, hh, mm, ss);
  134. }
  135. }