Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 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 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stdint.h> |
| 39 | #include <unistd.h> |
| 40 | #include <string.h> |
| 41 | #include <errno.h> |
| 42 | #include <time.h> |
| 43 | #include <math.h> |
| 44 | |
| 45 | #include <wiringPi.h> |
| 46 | #include <lcd128x64.h> |
| 47 | |
| 48 | #ifndef TRUE |
| 49 | # define TRUE (1==1) |
| 50 | # define FALSE (1==2) |
| 51 | #endif |
| 52 | |
| 53 | double clockRadius ; |
| 54 | double thickness, barLen ; |
| 55 | int maxX, maxY ; |
| 56 | |
| 57 | double rads (double degs) |
| 58 | { |
| 59 | return degs * M_PI / 180.0 ; |
| 60 | } |
| 61 | |
| 62 | void drawClockHands (void) |
| 63 | { |
| 64 | time_t t ; |
| 65 | struct tm *now ; |
| 66 | double angle, p, x0, y0, x1, y1 ; |
| 67 | int h24, h, m, s ; |
| 68 | char text [20] ; |
| 69 | |
| 70 | time (&t) ; |
| 71 | now = localtime (&t) ; |
| 72 | |
| 73 | h24 = now->tm_hour ; |
| 74 | m = now->tm_min ; |
| 75 | s = now->tm_sec ; |
| 76 | |
| 77 | h = h24 ; |
| 78 | if (h > 12) |
| 79 | h -= 12 ; |
| 80 | |
| 81 | // Hour hand |
| 82 | |
| 83 | angle = h * 30 + m * 0.5 ; |
| 84 | x0 = sin (rads (angle)) * (clockRadius * 0.75) ; |
| 85 | y0 = cos (rads (angle)) * (clockRadius * 0.75) ; |
| 86 | for (p = -3.0 ; p <= 3.0 ; p += 0.2) |
| 87 | { |
| 88 | x1 = sin (rads (angle + p)) * (clockRadius * 0.7) ; |
| 89 | y1 = cos (rads (angle + p)) * (clockRadius * 0.7) ; |
| 90 | lcd128x64line (0, 0, x1, y1, 1) ; |
| 91 | lcd128x64lineTo (x0, y0, 1) ; |
| 92 | } |
| 93 | |
| 94 | // Minute hand |
| 95 | |
| 96 | angle = m * 6 ; |
| 97 | x0 = sin (rads (angle)) * (clockRadius * 0.9) ; |
| 98 | y0 = cos (rads (angle)) * (clockRadius * 0.9) ; |
| 99 | for (p = -1.0 ; p <= 1.0 ; p += 0.2) |
| 100 | { |
| 101 | x1 = sin (rads (angle + p)) * (clockRadius * 0.85) ; |
| 102 | y1 = cos (rads (angle + p)) * (clockRadius * 0.85) ; |
| 103 | lcd128x64line (0, 0, x1, y1, 1) ; |
| 104 | lcd128x64lineTo (x0, y0, 1) ; |
| 105 | } |
| 106 | |
| 107 | // Second hand |
| 108 | |
| 109 | angle = s * 6 ; |
| 110 | x0 = sin (rads (angle)) * (clockRadius * 0.2) ; |
| 111 | y0 = cos (rads (angle)) * (clockRadius * 0.2) ; |
| 112 | x1 = sin (rads (angle)) * (clockRadius * 0.95) ; |
| 113 | y1 = cos (rads (angle)) * (clockRadius * 0.95) ; |
| 114 | lcd128x64line (0 - x0, 0 - y0, x1, y1, 1) ; |
| 115 | lcd128x64circle (0, 0, clockRadius * 0.1, 0, 1) ; |
| 116 | lcd128x64circle (0, 0, clockRadius * 0.05, 1, 1) ; |
| 117 | |
| 118 | // Text: |
| 119 | |
| 120 | sprintf (text, "%02d:%02d:%02d", h24, m, s) ; |
| 121 | lcd128x64puts (32, 24, text, 0, 1) ; |
| 122 | |
| 123 | sprintf (text, "%2d/%2d/%2d", now->tm_mday, now->tm_mon + 1, now->tm_year - 100) ; |
| 124 | lcd128x64puts (32, -23, text, 0, 1) ; |
| 125 | } |
| 126 | |
| 127 | void drawClockFace (void) |
| 128 | { |
| 129 | int m ; |
| 130 | double d, px1, py1, px2, py2 ; |
| 131 | |
| 132 | lcd128x64clear (0) ; |
| 133 | lcd128x64circle (0,0, clockRadius, 1, TRUE) ; |
| 134 | lcd128x64circle (0,0, clockRadius - thickness, 0, TRUE) ; |
| 135 | |
| 136 | // The four big indicators for 12,15,30 and 45 |
| 137 | |
| 138 | lcd128x64rectangle (- 3, clockRadius - barLen, 3, clockRadius, 1, TRUE) ; // 12 |
| 139 | lcd128x64rectangle (clockRadius - barLen, 3, clockRadius, -3, 1, TRUE) ; // 3 |
| 140 | lcd128x64rectangle (- 3, -clockRadius + barLen, 3, -clockRadius, 1, TRUE) ; // 6 |
| 141 | lcd128x64rectangle (-clockRadius + barLen, 3, -clockRadius, -3, 1, TRUE) ; // 9 |
| 142 | |
| 143 | |
| 144 | // Smaller 5 and 1 minute ticks |
| 145 | |
| 146 | for (m = 0 ; m < 60 ; ++m) |
| 147 | { |
| 148 | px1 = sin (rads (m * 6)) * clockRadius ; |
| 149 | py1 = cos (rads (m * 6)) * clockRadius ; |
| 150 | if ((m % 5) == 0) |
| 151 | d = barLen ; |
| 152 | else |
| 153 | d = barLen / 2.0 ; |
| 154 | |
| 155 | px2 = sin (rads (m * 6)) * (clockRadius - d) ; |
| 156 | py2 = cos (rads (m * 6)) * (clockRadius - d) ; |
| 157 | lcd128x64line (px1, py1, px2, py2, 1) ; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void setup (void) |
| 162 | { |
| 163 | lcd128x64getScreenSize (&maxX, &maxY) ; |
| 164 | clockRadius = maxY / 2 - 1 ; |
| 165 | thickness = maxX / 48 ; |
| 166 | barLen = thickness * 4 ; |
| 167 | lcd128x64setOrigin (32, 32) ; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | /* |
| 174 | *********************************************************************** |
| 175 | * The main program |
| 176 | *********************************************************************** |
| 177 | */ |
| 178 | |
| 179 | int main (int argc, char *argv []) |
| 180 | { |
| 181 | time_t now ; |
| 182 | |
| 183 | wiringPiSetup () ; |
| 184 | |
| 185 | lcd128x64setup () ; |
| 186 | |
| 187 | setup () ; |
| 188 | for (;;) |
| 189 | { |
| 190 | drawClockFace () ; |
| 191 | drawClockHands () ; |
| 192 | lcd128x64update () ; |
| 193 | |
| 194 | now = time (NULL) ; |
| 195 | while (time (NULL) == now) |
| 196 | delay (10) ; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | return 0 ; |
| 201 | } |