3 * Graphics-based LCD driver.
4 * This is designed to drive the parallel interface LCD drivers
5 * based on the generic 12864H chips
7 * There are many variations on these chips, however they all mostly
9 * This implementation has the Pins from the Pi hard-wired into it,
10 * in particular wiringPi pins 0-7 so that we can use
11 * digitalWriteByete() to speed things up somewhat.
13 * Copyright (c) 2013 Gordon Henderson.
14 ***********************************************************************
15 * This file is part of wiringPi:
16 * https://projects.drogon.net/raspberry-pi/wiringpi/
18 * wiringPi is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * wiringPi is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
30 ***********************************************************************
39 #include "lcd128x64.h"
47 // Note pins 0-7 are the 8-bit data port
54 // Software copy of the framebuffer
55 // it's 8-bit deep although the display itself is only 1-bit deep.
57 static unsigned char frameBuffer [LCD_WIDTH * LCD_HEIGHT] ;
59 static int maxX, maxY ;
60 static int lastX, lastY ;
61 static int xOrigin, yOrigin ;
62 static int lcdOrientation = 0 ;
66 * Toggle the strobe (Really the "E") pin to the device.
67 * According to the docs, data is latched on the falling edge.
68 *********************************************************************************
71 static void strobe (void)
73 digitalWrite (STROBE, 1) ; delayMicroseconds (1) ;
74 digitalWrite (STROBE, 0) ; delayMicroseconds (5) ;
80 * Send an data or command byte to the display.
81 *********************************************************************************
84 static void sendData (const int data, const int chip)
86 digitalWrite (chip, 0) ;
87 digitalWriteByte (data) ;
89 digitalWrite (chip, 1) ;
95 * Send a command byte to the display
96 *********************************************************************************
99 static void sendCommand (const int command, const int chip)
101 digitalWrite (RS, 0) ;
102 sendData (command, chip) ;
103 digitalWrite (RS, 1) ;
109 * Set the column and line addresses
110 *********************************************************************************
113 static void setCol (int col, const int chip)
114 { sendCommand (0x40 | (col & 0x3F), chip) ; }
116 static void setLine (int line, const int chip)
117 { sendCommand (0xB8 | (line & 0x07), chip) ; }
122 * Copy our software version to the real display
123 *********************************************************************************
126 void lcd128x64update (void)
128 int line, x, y, fbLoc ;
133 for (line = 0 ; line < 8 ; ++line)
136 setLine (line, CS1) ;
138 for (x = 63 ; x >= 0 ; --x)
141 for (y = 0 ; y < 8 ; ++y)
143 fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ;
144 if (frameBuffer [fbLoc] != 0)
147 sendData (byte, CS1) ;
153 for (line = 0 ; line < 8 ; ++line)
156 setLine (line, CS2) ;
158 for (x = 127 ; x >= 64 ; --x)
161 for (y = 0 ; y < 8 ; ++y)
163 fbLoc = x + (((7 - line) * 8) + (7 - y)) * LCD_WIDTH ;
164 if (frameBuffer [fbLoc] != 0)
167 sendData (byte, CS2) ;
174 * lcd128x64setOrigin:
175 * Set the display offset origin
176 *********************************************************************************
179 void lcd128x64setOrigin (int x, int y)
187 * lcd128x64setOrientation:
188 * Set the display orientation:
189 * 0: Normal, the display is portrait mode, 0,0 is top left
191 * 2: Portrait, flipped
192 * 3: Landscape, flipped
193 *********************************************************************************
196 void lcd128x64setOrientation (int orientation)
198 lcdOrientation = orientation & 3 ;
200 lcd128x64setOrigin (0,0) ;
202 switch (lcdOrientation)
228 * lcd128x64orientCoordinates:
229 * Adjust the coordinates given to the display orientation
230 *********************************************************************************
233 void lcd128x64orientCoordinates (int *x, int *y)
241 switch (lcdOrientation)
247 tmp = maxY - *y - 1 ;
268 * lcd128x64getScreenSize:
269 * Return the max X & Y screen sizes. Needs to be called again, if you
270 * change screen orientation.
271 *********************************************************************************
274 void lcd128x64getScreenSize (int *x, int *y)
282 *********************************************************************************
283 * Standard Graphical Functions
284 *********************************************************************************
291 *********************************************************************************
294 void lcd128x64point (int x, int y, int colour)
299 lcd128x64orientCoordinates (&x, &y) ;
301 if ((x < 0) || (x >= LCD_WIDTH) || (y < 0) || (y >= LCD_HEIGHT))
304 frameBuffer [x + y * LCD_WIDTH] = colour ;
309 * lcd128x64line: lcd128x64lineTo:
310 * Classic Bressenham Line code
311 *********************************************************************************
314 void lcd128x64line (int x0, int y0, int x1, int y1, int colour)
326 sx = (x0 < x1) ? 1 : -1 ;
327 sy = (y0 < y1) ? 1 : -1 ;
333 lcd128x64point (x0, y0, colour) ;
335 if ((x0 == x1) && (y0 == y1))
355 void lcd128x64lineTo (int x, int y, int colour)
357 lcd128x64line (lastX, lastY, x, y, colour) ;
362 * lcd128x64rectangle:
363 * A rectangle is a spoilt days fishing
364 *********************************************************************************
367 void lcd128x64rectangle (int x1, int y1, int x2, int y2, int colour, int filled)
374 lcd128x64line (x1, y1, x2, y2, colour) ;
376 for (x = x1 ; x <= x2 ; ++x)
377 lcd128x64line (x, y1, x, y2, colour) ;
379 for (x = x2 ; x <= x1 ; ++x)
380 lcd128x64line (x, y1, x, y2, colour) ;
384 lcd128x64line (x1, y1, x2, y1, colour) ;
385 lcd128x64lineTo (x2, y2, colour) ;
386 lcd128x64lineTo (x1, y2, colour) ;
387 lcd128x64lineTo (x1, y1, colour) ;
394 * This is the midpoint circle algorithm.
395 *********************************************************************************
398 void lcd128x64circle (int x, int y, int r, int colour, int filled)
409 lcd128x64line (x, y + r, x, y - r, colour) ;
410 lcd128x64line (x + r, y, x - r, y, colour) ;
414 lcd128x64point (x, y + r, colour) ;
415 lcd128x64point (x, y - r, colour) ;
416 lcd128x64point (x + r, y, colour) ;
417 lcd128x64point (x - r, y, colour) ;
433 lcd128x64line (x + x1, y + y1, x - x1, y + y1, colour) ;
434 lcd128x64line (x + x1, y - y1, x - x1, y - y1, colour) ;
435 lcd128x64line (x + y1, y + x1, x - y1, y + x1, colour) ;
436 lcd128x64line (x + y1, y - x1, x - y1, y - x1, colour) ;
440 lcd128x64point (x + x1, y + y1, colour) ; lcd128x64point (x - x1, y + y1, colour) ;
441 lcd128x64point (x + x1, y - y1, colour) ; lcd128x64point (x - x1, y - y1, colour) ;
442 lcd128x64point (x + y1, y + x1, colour) ; lcd128x64point (x - y1, y + x1, colour) ;
443 lcd128x64point (x + y1, y - x1, colour) ; lcd128x64point (x - y1, y - x1, colour) ;
451 * Fast ellipse drawing algorithm by
453 * Mathematics Department
454 * Santa Monica College
456 * Santa Monica, CA 90405
457 * jrkennedy6@gmail.com
458 * -Confirned in email this algorithm is in the public domain -GH-
459 *********************************************************************************
462 static void plot4ellipsePoints (int cx, int cy, int x, int y, int colour, int filled)
466 lcd128x64line (cx + x, cy + y, cx - x, cy + y, colour) ;
467 lcd128x64line (cx - x, cy - y, cx + x, cy - y, colour) ;
471 lcd128x64point (cx + x, cy + y, colour) ;
472 lcd128x64point (cx - x, cy + y, colour) ;
473 lcd128x64point (cx - x, cy - y, colour) ;
474 lcd128x64point (cx + x, cy - y, colour) ;
478 void lcd128x64ellipse (int cx, int cy, int xRadius, int yRadius, int colour, int filled)
481 int xChange, yChange, ellipseError ;
482 int twoAsquare, twoBsquare ;
483 int stoppingX, stoppingY ;
485 twoAsquare = 2 * xRadius * xRadius ;
486 twoBsquare = 2 * yRadius * yRadius ;
491 xChange = yRadius * yRadius * (1 - 2 * xRadius) ;
492 yChange = xRadius * xRadius ;
495 stoppingX = twoBsquare * xRadius ;
498 while (stoppingX >= stoppingY) // 1st set of points
500 plot4ellipsePoints (cx, cy, x, y, colour, filled) ;
502 stoppingY += twoAsquare ;
503 ellipseError += yChange ;
504 yChange += twoAsquare ;
506 if ((2 * ellipseError + xChange) > 0 )
509 stoppingX -= twoBsquare ;
510 ellipseError += xChange ;
511 xChange += twoBsquare ;
518 xChange = yRadius * yRadius ;
519 yChange = xRadius * xRadius * (1 - 2 * yRadius) ;
523 stoppingY = twoAsquare * yRadius ;
525 while (stoppingX <= stoppingY) //2nd set of points
527 plot4ellipsePoints (cx, cy, x, y, colour, filled) ;
529 stoppingX += twoBsquare ;
530 ellipseError += xChange ;
531 xChange += twoBsquare ;
533 if ((2 * ellipseError + yChange) > 0 )
536 stoppingY -= twoAsquare ;
537 ellipseError += yChange ;
538 yChange += twoAsquare ;
546 * Print a single character to the screen
547 *********************************************************************************
550 void lcd128x64putchar (int x, int y, int c, int bgCol, int fgCol)
555 unsigned char *fontPtr ;
557 // Can't print if we're offscreen
559 //if ((x < 0) || (x >= (maxX - fontWidth)) || (y < 0) || (y >= (maxY - fontHeight)))
562 fontPtr = font + c * fontHeight ;
564 for (y1 = fontHeight - 1 ; y1 >= 0 ; --y1)
568 lcd128x64point (x + 0, y2, (line & 0x80) == 0 ? bgCol : fgCol) ;
569 lcd128x64point (x + 1, y2, (line & 0x40) == 0 ? bgCol : fgCol) ;
570 lcd128x64point (x + 2, y2, (line & 0x20) == 0 ? bgCol : fgCol) ;
571 lcd128x64point (x + 3, y2, (line & 0x10) == 0 ? bgCol : fgCol) ;
572 lcd128x64point (x + 4, y2, (line & 0x08) == 0 ? bgCol : fgCol) ;
573 lcd128x64point (x + 5, y2, (line & 0x04) == 0 ? bgCol : fgCol) ;
574 lcd128x64point (x + 6, y2, (line & 0x02) == 0 ? bgCol : fgCol) ;
575 lcd128x64point (x + 7, y2, (line & 0x01) == 0 ? bgCol : fgCol) ;
582 * Send a string to the display. Obeys \n and \r formatting
583 *********************************************************************************
586 void lcd128x64puts (int x, int y, const char *str, int bgCol, int fgCol)
609 lcd128x64putchar (mx, my, c, bgCol, fgCol) ;
612 if (mx >= (maxX - fontWidth))
623 * Clear the display to the given colour.
624 *********************************************************************************
627 void lcd128x64clear (int colour)
630 register unsigned char *ptr = frameBuffer ;
632 for (i = 0 ; i < (maxX * maxY) ; ++i)
641 * Initialise the display and GPIO.
642 *********************************************************************************
645 int lcd128x64setup (void)
649 for (i = 0 ; i < 8 ; ++i)
650 pinMode (i, OUTPUT) ;
652 digitalWrite (CS1, 1) ;
653 digitalWrite (CS2, 1) ;
654 digitalWrite (STROBE, 0) ;
655 digitalWrite (RS, 1) ;
657 pinMode (CS1, OUTPUT) ;
658 pinMode (CS2, OUTPUT) ;
659 pinMode (STROBE, OUTPUT) ;
660 pinMode (RS, OUTPUT) ;
662 sendCommand (0x3F, CS1) ; // Display ON
663 sendCommand (0xC0, CS1) ; // Set display start line to 0
665 sendCommand (0x3F, CS2) ; // Display ON
666 sendCommand (0xC0, CS2) ; // Set display start line to 0
669 lcd128x64setOrientation (0) ;