clock.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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://projects.drogon.net/raspberry-pi/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", now->tm_mday, now->tm_mon + 1, 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. delay (10) ;
  161. }
  162. return 0 ;
  163. }