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.

clock.ino 3.8KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <SPI.h>
  2. #include <Adafruit_GFX.h>
  3. #include <TFT_ILI9163C.h>
  4. // Color definitions
  5. #define BLACK 0x0000
  6. #define BLUE 0x001F
  7. #define RED 0xF800
  8. #define GREEN 0x07E0
  9. #define CYAN 0x07FF
  10. #define MAGENTA 0xF81F
  11. #define YELLOW 0xFFE0
  12. #define WHITE 0xFFFF
  13. /*
  14. Teensy3.x and Arduino's
  15. You are using 4 wire SPI here, so:
  16. MOSI: 11//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  17. MISO: 12//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  18. SCK: 13//Teensy3.x/Arduino UNO (for MEGA/DUE refere to arduino site)
  19. the rest of pin below:
  20. */
  21. #define __CS 10
  22. #define __DC 9
  23. /*
  24. Teensy 3.x can use: 2,6,9,10,15,20,21,22,23
  25. Arduino's 8 bit: any
  26. DUE: check arduino site
  27. If you do not use reset, tie it to +3V3
  28. */
  29. TFT_ILI9163C tft = TFT_ILI9163C(__CS, __DC);
  30. uint16_t ccenterx,ccentery;//center x,y of the clock
  31. const uint16_t cradius = 63;//radius of the clock
  32. const float scosConst = 0.0174532925;
  33. float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;
  34. float sdeg=0, mdeg=0, hdeg=0;
  35. uint16_t osx,osy,omx,omy,ohx,ohy;
  36. uint16_t x0 = 0, x1 = 0, yy0 = 0, yy1 = 0;
  37. uint32_t targetTime = 0;// for next 1 second timeout
  38. uint8_t hh,mm,ss; //containers for current time
  39. void drawClockFace(){
  40. tft.fillCircle(ccenterx, ccentery, cradius, BLUE);
  41. tft.fillCircle(ccenterx, ccentery, cradius-4, BLACK);
  42. // Draw 12 lines
  43. for(int i = 0; i<360; i+= 30) {
  44. sx = cos((i-90)*scosConst);
  45. sy = sin((i-90)*scosConst);
  46. x0 = sx*(cradius-4)+ccenterx;
  47. yy0 = sy*(cradius-4)+ccentery;
  48. x1 = sx*(cradius-11)+ccenterx;
  49. yy1 = sy*(cradius-11)+ccentery;
  50. tft.drawLine(x0, yy0, x1, yy1, BLUE);
  51. }
  52. }
  53. static uint8_t conv2d(const char* p) {
  54. uint8_t v = 0;
  55. if ('0' <= *p && *p <= '9') v = *p - '0';
  56. return 10 * v + *++p - '0';
  57. }
  58. void setup(void) {
  59. tft.begin();
  60. tft.setTextColor(WHITE, BLACK);
  61. ccenterx = tft.width()/2;
  62. ccentery = tft.height()/2;
  63. osx = ccenterx;
  64. osy = ccentery;
  65. omx = ccenterx;
  66. omy = ccentery;
  67. ohx = ccenterx;
  68. ohy = ccentery;
  69. drawClockFace();// Draw clock face
  70. //get current time from compiler
  71. hh = conv2d(__TIME__);
  72. mm = conv2d(__TIME__+3);
  73. ss = conv2d(__TIME__+6);
  74. targetTime = millis() + 1000;
  75. }
  76. void drawClockHands(uint8_t h,uint8_t m,uint8_t s){
  77. // Pre-compute hand degrees, x & y coords for a fast screen update
  78. sdeg = s * 6; // 0-59 -> 0-354
  79. mdeg = m * 6 + sdeg * 0.01666667; // 0-59 -> 0-360 - includes seconds
  80. hdeg = h * 30 + mdeg * 0.0833333; // 0-11 -> 0-360 - includes minutes and seconds
  81. hx = cos((hdeg-90)*scosConst);
  82. hy = sin((hdeg-90)*scosConst);
  83. mx = cos((mdeg-90)*scosConst);
  84. my = sin((mdeg-90)*scosConst);
  85. sx = cos((sdeg-90)*scosConst);
  86. sy = sin((sdeg-90)*scosConst);
  87. // Erase just old hand positions
  88. tft.drawLine(ohx, ohy, ccenterx+1, ccentery+1, BLACK);
  89. tft.drawLine(omx, omy, ccenterx+1, ccentery+1, BLACK);
  90. tft.drawLine(osx, osy, ccenterx+1, ccentery+1, BLACK);
  91. // Draw new hand positions
  92. tft.drawLine(hx*(cradius-28)+ccenterx+1, hy*(cradius-28)+ccentery+1, ccenterx+1, ccentery+1, WHITE);
  93. tft.drawLine(mx*(cradius-17)+ccenterx+1, my*(cradius-17)+ccentery+1, ccenterx+1, ccentery+1, WHITE);
  94. tft.drawLine(sx*(cradius-14)+ccenterx+1, sy*(cradius-14)+ccentery+1, ccenterx+1, ccentery+1, RED);
  95. tft.fillCircle(ccenterx+1, ccentery+1, 3, RED);
  96. // Update old x&y coords
  97. osx = sx*(cradius-14)+ccenterx+1;
  98. osy = sy*(cradius-14)+ccentery+1;
  99. omx = mx*(cradius-17)+ccenterx+1;
  100. omy = my*(cradius-17)+ccentery+1;
  101. ohx = hx*(cradius-28)+ccenterx+1;
  102. ohy = hy*(cradius-28)+ccentery+1;
  103. }
  104. void loop() {
  105. if (targetTime < millis()) {
  106. targetTime = millis()+1000;
  107. ss++;
  108. if (ss == 60) {
  109. ss = 0;
  110. mm++;
  111. if(mm > 59) {
  112. mm = 0;
  113. hh++;
  114. if (hh > 23) hh = 0;
  115. }
  116. }
  117. drawClockHands(hh,mm,ss);
  118. }
  119. }