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.
 
 
 
 
 

191 line
4.9 KiB

  1. /*
  2. * clock.c:
  3. * Demo of the 128x64 graphics based LCD driver.
  4. * This is designed to drive the parallel interface LCD drivers
  5. * based on the popular 12864H controller chip.
  6. *
  7. * This test program assumes the following:
  8. * (Which is currently hard-wired into the driver)
  9. *
  10. * GPIO 0-7 is connected to display data pins 0-7.
  11. * GPIO 10 is CS1
  12. * GPIO 11 is CS2
  13. * GPIO 12 is STROBE
  14. * GPIO 10 is RS
  15. *
  16. * Copyright (c) 2012-2013 Gordon Henderson.
  17. ***********************************************************************
  18. * This file is part of wiringPi:
  19. * https://github.com/WiringPi/WiringPi/
  20. *
  21. * wiringPi is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Lesser General Public License as published by
  23. * the Free Software Foundation, either version 3 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * wiringPi is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public License
  32. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  33. ***********************************************************************
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <stdint.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <time.h>
  42. #include <math.h>
  43. #include <wiringPi.h>
  44. #include <lcd128x64.h>
  45. #ifndef TRUE
  46. # define TRUE (1==1)
  47. # define FALSE (1==2)
  48. #endif
  49. double clockRadius ;
  50. double thickness, barLen ;
  51. int maxX, maxY ;
  52. double rads (double degs)
  53. {
  54. return degs * M_PI / 180.0 ;
  55. }
  56. void drawClockHands (void)
  57. {
  58. time_t t ;
  59. struct tm *now ;
  60. double angle, p, x0, y0, x1, y1 ;
  61. int h24, h, m, s ;
  62. char text [20] ;
  63. time (&t) ;
  64. now = localtime (&t) ;
  65. h24 = now->tm_hour ;
  66. m = now->tm_min ;
  67. s = now->tm_sec ;
  68. h = h24 ;
  69. if (h > 12)
  70. h -= 12 ;
  71. // Hour hand
  72. angle = h * 30 + m * 0.5 ;
  73. x0 = sin (rads (angle)) * (clockRadius * 0.75) ;
  74. y0 = cos (rads (angle)) * (clockRadius * 0.75) ;
  75. for (p = -3.0 ; p <= 3.0 ; p += 0.2)
  76. {
  77. x1 = sin (rads (angle + p)) * (clockRadius * 0.7) ;
  78. y1 = cos (rads (angle + p)) * (clockRadius * 0.7) ;
  79. lcd128x64line (0, 0, x1, y1, 1) ;
  80. lcd128x64lineTo (x0, y0, 1) ;
  81. }
  82. // Minute hand
  83. angle = m * 6 ;
  84. x0 = sin (rads (angle)) * (clockRadius * 0.9) ;
  85. y0 = cos (rads (angle)) * (clockRadius * 0.9) ;
  86. for (p = -1.0 ; p <= 1.0 ; p += 0.2)
  87. {
  88. x1 = sin (rads (angle + p)) * (clockRadius * 0.85) ;
  89. y1 = cos (rads (angle + p)) * (clockRadius * 0.85) ;
  90. lcd128x64line (0, 0, x1, y1, 1) ;
  91. lcd128x64lineTo (x0, y0, 1) ;
  92. }
  93. // Second hand
  94. angle = s * 6 ;
  95. x0 = sin (rads (angle)) * (clockRadius * 0.2) ;
  96. y0 = cos (rads (angle)) * (clockRadius * 0.2) ;
  97. x1 = sin (rads (angle)) * (clockRadius * 0.95) ;
  98. y1 = cos (rads (angle)) * (clockRadius * 0.95) ;
  99. lcd128x64line (0 - x0, 0 - y0, x1, y1, 1) ;
  100. lcd128x64circle (0, 0, clockRadius * 0.1, 0, 1) ;
  101. lcd128x64circle (0, 0, clockRadius * 0.05, 1, 1) ;
  102. // Text:
  103. sprintf (text, "%02d:%02d:%02d", h24, m, s) ;
  104. lcd128x64puts (32, 24, text, 0, 1) ;
  105. sprintf (text, "%2d/%2d/%2d", (uint8_t)now->tm_mday, (uint8_t)now->tm_mon + 1, (uint8_t)(now->tm_year - 100)) ;
  106. lcd128x64puts (32, -23, text, 0, 1) ;
  107. }
  108. void drawClockFace (void)
  109. {
  110. int m ;
  111. double d, px1, py1, px2, py2 ;
  112. lcd128x64clear (0) ;
  113. lcd128x64circle (0,0, clockRadius, 1, TRUE) ;
  114. lcd128x64circle (0,0, clockRadius - thickness, 0, TRUE) ;
  115. // The four big indicators for 12,15,30 and 45
  116. lcd128x64rectangle (- 3, clockRadius - barLen, 3, clockRadius, 1, TRUE) ; // 12
  117. lcd128x64rectangle (clockRadius - barLen, 3, clockRadius, -3, 1, TRUE) ; // 3
  118. lcd128x64rectangle (- 3, -clockRadius + barLen, 3, -clockRadius, 1, TRUE) ; // 6
  119. lcd128x64rectangle (-clockRadius + barLen, 3, -clockRadius, -3, 1, TRUE) ; // 9
  120. // Smaller 5 and 1 minute ticks
  121. for (m = 0 ; m < 60 ; ++m)
  122. {
  123. px1 = sin (rads (m * 6)) * clockRadius ;
  124. py1 = cos (rads (m * 6)) * clockRadius ;
  125. if ((m % 5) == 0)
  126. d = barLen ;
  127. else
  128. d = barLen / 2.0 ;
  129. px2 = sin (rads (m * 6)) * (clockRadius - d) ;
  130. py2 = cos (rads (m * 6)) * (clockRadius - d) ;
  131. lcd128x64line (px1, py1, px2, py2, 1) ;
  132. }
  133. }
  134. void setup (void)
  135. {
  136. lcd128x64getScreenSize (&maxX, &maxY) ;
  137. clockRadius = maxY / 2 - 1 ;
  138. thickness = maxX / 48 ;
  139. barLen = thickness * 4 ;
  140. lcd128x64setOrigin (32, 32) ;
  141. }
  142. /*
  143. ***********************************************************************
  144. * The main program
  145. ***********************************************************************
  146. */
  147. int main (int argc, char *argv [])
  148. {
  149. time_t now ;
  150. wiringPiSetup () ;
  151. lcd128x64setup () ;
  152. setup () ;
  153. for (;;)
  154. {
  155. drawClockFace () ;
  156. drawClockHands () ;
  157. lcd128x64update () ;
  158. now = time (NULL) ;
  159. while (time (NULL) == now)
  160. delayMs(10) ;
  161. }
  162. return 0 ;
  163. }