An interface to the Sparkfun Serial Graphic LCD, LCD-09351; and Graphic LCD Serial Backpack, LCD-09352. Derived class from Serial so that you can conveniently send text to the display with printf(), putc(), etc.
SerialGraphicLCD.cpp@1:2f436b8aebf4, 2012-03-28 (annotated)
- Committer:
- shimniok
- Date:
- Wed Mar 28 16:33:27 2012 +0000
- Revision:
- 1:2f436b8aebf4
- Parent:
- 0:a3d518d2f36f
- Child:
- 2:84b78506add6
revised pos() to use text row, column for text position. Added posXY to specify x, y coordinates for text position.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 0:a3d518d2f36f | 1 | #include "SerialGraphicLCD.h" |
shimniok | 0:a3d518d2f36f | 2 | |
shimniok | 1:2f436b8aebf4 | 3 | #define XSIZE 6 |
shimniok | 1:2f436b8aebf4 | 4 | #define YSIZE 9 |
shimniok | 1:2f436b8aebf4 | 5 | |
shimniok | 0:a3d518d2f36f | 6 | SerialGraphicLCD::SerialGraphicLCD(PinName tx, PinName rx): |
shimniok | 0:a3d518d2f36f | 7 | Serial(tx, rx) |
shimniok | 0:a3d518d2f36f | 8 | { |
shimniok | 1:2f436b8aebf4 | 9 | baud(115200); // default baud rate |
shimniok | 1:2f436b8aebf4 | 10 | resolution(LCD_128x64); // default resolution |
shimniok | 0:a3d518d2f36f | 11 | } |
shimniok | 0:a3d518d2f36f | 12 | |
shimniok | 0:a3d518d2f36f | 13 | void SerialGraphicLCD::clear() { |
shimniok | 0:a3d518d2f36f | 14 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 15 | putc(0x00); |
shimniok | 0:a3d518d2f36f | 16 | } |
shimniok | 0:a3d518d2f36f | 17 | |
shimniok | 1:2f436b8aebf4 | 18 | void SerialGraphicLCD::pos(int row, int col) { |
shimniok | 1:2f436b8aebf4 | 19 | posXY(XSIZE*col, _yMax-(YSIZE*row)); |
shimniok | 1:2f436b8aebf4 | 20 | } |
shimniok | 1:2f436b8aebf4 | 21 | |
shimniok | 1:2f436b8aebf4 | 22 | void SerialGraphicLCD::posXY(int x, int y) { |
shimniok | 0:a3d518d2f36f | 23 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 24 | putc(0x18); |
shimniok | 0:a3d518d2f36f | 25 | putc(x); |
shimniok | 0:a3d518d2f36f | 26 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 27 | putc(0x19); |
shimniok | 0:a3d518d2f36f | 28 | putc(y); |
shimniok | 0:a3d518d2f36f | 29 | } |
shimniok | 0:a3d518d2f36f | 30 | |
shimniok | 0:a3d518d2f36f | 31 | void SerialGraphicLCD::pixel(int x, int y, bool set) { |
shimniok | 0:a3d518d2f36f | 32 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 33 | putc(0x10); |
shimniok | 0:a3d518d2f36f | 34 | putc(x); |
shimniok | 0:a3d518d2f36f | 35 | putc(y); |
shimniok | 0:a3d518d2f36f | 36 | putc((set) ? 0x01 : 0x00); |
shimniok | 0:a3d518d2f36f | 37 | } |
shimniok | 0:a3d518d2f36f | 38 | |
shimniok | 0:a3d518d2f36f | 39 | void SerialGraphicLCD::line(int x1, int y1, int x2, int y2, bool set) { |
shimniok | 0:a3d518d2f36f | 40 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 41 | putc(0x0c); |
shimniok | 0:a3d518d2f36f | 42 | putc(x1); |
shimniok | 0:a3d518d2f36f | 43 | putc(y1); |
shimniok | 0:a3d518d2f36f | 44 | putc(x2); |
shimniok | 0:a3d518d2f36f | 45 | putc(y2); |
shimniok | 0:a3d518d2f36f | 46 | putc((set) ? 0x01 : 0x00); |
shimniok | 0:a3d518d2f36f | 47 | } |
shimniok | 0:a3d518d2f36f | 48 | |
shimniok | 0:a3d518d2f36f | 49 | void SerialGraphicLCD::circle(int x, int y, int r, bool set) { |
shimniok | 0:a3d518d2f36f | 50 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 51 | putc(0x03); |
shimniok | 0:a3d518d2f36f | 52 | putc(x); |
shimniok | 0:a3d518d2f36f | 53 | putc(y); |
shimniok | 0:a3d518d2f36f | 54 | putc(r); |
shimniok | 0:a3d518d2f36f | 55 | putc((set) ? 0x01 : 0x00); |
shimniok | 0:a3d518d2f36f | 56 | } |
shimniok | 0:a3d518d2f36f | 57 | |
shimniok | 0:a3d518d2f36f | 58 | // Unfortunately, the datasheet is incorrect; the box command |
shimniok | 0:a3d518d2f36f | 59 | // does not take a 5th parameter for draw/erase like the others |
shimniok | 0:a3d518d2f36f | 60 | void SerialGraphicLCD::rect(int x1, int y1, int x2, int y2) { |
shimniok | 0:a3d518d2f36f | 61 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 62 | putc(0x0f); |
shimniok | 0:a3d518d2f36f | 63 | putc(x1); |
shimniok | 0:a3d518d2f36f | 64 | putc(y1); |
shimniok | 0:a3d518d2f36f | 65 | putc(x2); |
shimniok | 0:a3d518d2f36f | 66 | putc(y2); |
shimniok | 0:a3d518d2f36f | 67 | } |
shimniok | 0:a3d518d2f36f | 68 | |
shimniok | 0:a3d518d2f36f | 69 | void SerialGraphicLCD::erase(int x1, int y1, int x2, int y2) { |
shimniok | 0:a3d518d2f36f | 70 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 71 | putc(0x05); |
shimniok | 0:a3d518d2f36f | 72 | putc(x1); |
shimniok | 0:a3d518d2f36f | 73 | putc(y1); |
shimniok | 0:a3d518d2f36f | 74 | putc(x2); |
shimniok | 0:a3d518d2f36f | 75 | putc(y2); |
shimniok | 0:a3d518d2f36f | 76 | } |
shimniok | 0:a3d518d2f36f | 77 | |
shimniok | 0:a3d518d2f36f | 78 | void SerialGraphicLCD::backlight(int i) { |
shimniok | 0:a3d518d2f36f | 79 | if (i >= 0 && i <= 100) { |
shimniok | 0:a3d518d2f36f | 80 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 81 | putc(0x02); |
shimniok | 0:a3d518d2f36f | 82 | putc(i); |
shimniok | 0:a3d518d2f36f | 83 | } |
shimniok | 0:a3d518d2f36f | 84 | } |
shimniok | 0:a3d518d2f36f | 85 | |
shimniok | 0:a3d518d2f36f | 86 | void SerialGraphicLCD::reverseMode() { |
shimniok | 0:a3d518d2f36f | 87 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 88 | putc(0x12); |
shimniok | 0:a3d518d2f36f | 89 | } |
shimniok | 0:a3d518d2f36f | 90 | |
shimniok | 1:2f436b8aebf4 | 91 | void SerialGraphicLCD::resolution(int type) { |
shimniok | 1:2f436b8aebf4 | 92 | switch (type) { |
shimniok | 1:2f436b8aebf4 | 93 | case LCD_128x64 : |
shimniok | 1:2f436b8aebf4 | 94 | resolution(128, 64); |
shimniok | 1:2f436b8aebf4 | 95 | break; |
shimniok | 1:2f436b8aebf4 | 96 | case LCD_160x128 : |
shimniok | 1:2f436b8aebf4 | 97 | resolution(160, 128); |
shimniok | 1:2f436b8aebf4 | 98 | break; |
shimniok | 1:2f436b8aebf4 | 99 | } |
shimniok | 1:2f436b8aebf4 | 100 | } |
shimniok | 1:2f436b8aebf4 | 101 | |
shimniok | 1:2f436b8aebf4 | 102 | void SerialGraphicLCD::resolution(int x, int y) { |
shimniok | 1:2f436b8aebf4 | 103 | _xMax = x; |
shimniok | 1:2f436b8aebf4 | 104 | _yMax = y; |
shimniok | 1:2f436b8aebf4 | 105 | } |
shimniok | 1:2f436b8aebf4 | 106 | |
shimniok | 1:2f436b8aebf4 | 107 | |
shimniok | 0:a3d518d2f36f | 108 | void SerialGraphicLCD::lcdbaud(int b) { |
shimniok | 0:a3d518d2f36f | 109 | if (b > 0 && b < 7) { |
shimniok | 0:a3d518d2f36f | 110 | putc(0x7c); |
shimniok | 0:a3d518d2f36f | 111 | putc(0x07); |
shimniok | 0:a3d518d2f36f | 112 | putc(b+'0'); |
shimniok | 0:a3d518d2f36f | 113 | } |
shimniok | 0:a3d518d2f36f | 114 | } |