Library for handling ILI9163 - based LCD displays.
ILI9163 displays are typically 128*128 pixels with 15-bit color and controlled over 8-bit SPI.
This library is adapted from https://developer.mbed.org/teams/Temp/code/DL144128_LCD_b/, with some changes.
ili9163lcd.h@0:06faf770a0c7, 2017-01-25 (annotated)
- Committer:
- jackmax
- Date:
- Wed Jan 25 23:46:47 2017 +0000
- Revision:
- 0:06faf770a0c7
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jackmax | 0:06faf770a0c7 | 1 | /** |
jackmax | 0:06faf770a0c7 | 2 | * @file ili9163lcd.h |
jackmax | 0:06faf770a0c7 | 3 | * @brief ILI9163 128x128 LCD Driver (Header file) |
jackmax | 0:06faf770a0c7 | 4 | * |
jackmax | 0:06faf770a0c7 | 5 | * This code has been ported from the ili9163lcd library for mbed |
jackmax | 0:06faf770a0c7 | 6 | * made by Jun Morita. |
jackmax | 0:06faf770a0c7 | 7 | * Source form <http://files.noccylabs.info/lib430/liblcd/ili9163lcd_8c.html> |
jackmax | 0:06faf770a0c7 | 8 | * |
jackmax | 0:06faf770a0c7 | 9 | * This code has been ported from the ili9163lcd library for avr made |
jackmax | 0:06faf770a0c7 | 10 | * by Simon Inns, to run on a msp430. |
jackmax | 0:06faf770a0c7 | 11 | * |
jackmax | 0:06faf770a0c7 | 12 | * This program is free software: you can redistribute it and/or modify |
jackmax | 0:06faf770a0c7 | 13 | * it under the terms of the GNU General Public License as published by |
jackmax | 0:06faf770a0c7 | 14 | * the Free Software Foundation, either version 3 of the License, or |
jackmax | 0:06faf770a0c7 | 15 | * (at your option) any later version. |
jackmax | 0:06faf770a0c7 | 16 | * |
jackmax | 0:06faf770a0c7 | 17 | * This program is distributed in the hope that it will be useful, |
jackmax | 0:06faf770a0c7 | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
jackmax | 0:06faf770a0c7 | 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jackmax | 0:06faf770a0c7 | 20 | * GNU General Public License for more details. |
jackmax | 0:06faf770a0c7 | 21 | * |
jackmax | 0:06faf770a0c7 | 22 | * You should have received a copy of the GNU General Public License |
jackmax | 0:06faf770a0c7 | 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
jackmax | 0:06faf770a0c7 | 24 | * |
jackmax | 0:06faf770a0c7 | 25 | * @author Jun Morita (iccraft) |
jackmax | 0:06faf770a0c7 | 26 | * @author Simon Inns <simon.inns@gmail.com> |
jackmax | 0:06faf770a0c7 | 27 | * @author Christopher Vagnetoft (NoccyLabs) |
jackmax | 0:06faf770a0c7 | 28 | * @copyright (C) 2012 Simon Inns |
jackmax | 0:06faf770a0c7 | 29 | * @copyright parts (C) 2012 NoccyLabs |
jackmax | 0:06faf770a0c7 | 30 | */ |
jackmax | 0:06faf770a0c7 | 31 | |
jackmax | 0:06faf770a0c7 | 32 | #ifndef ILI9163LCD_H_ |
jackmax | 0:06faf770a0c7 | 33 | #define ILI9163LCD_H_ |
jackmax | 0:06faf770a0c7 | 34 | |
jackmax | 0:06faf770a0c7 | 35 | #include "mbed.h" |
jackmax | 0:06faf770a0c7 | 36 | #include "font5x8.h" |
jackmax | 0:06faf770a0c7 | 37 | |
jackmax | 0:06faf770a0c7 | 38 | // Screen orientation defines: |
jackmax | 0:06faf770a0c7 | 39 | // 0 = Ribbon at top |
jackmax | 0:06faf770a0c7 | 40 | // 1 = Ribbon at left |
jackmax | 0:06faf770a0c7 | 41 | // 2 = Ribbon at right |
jackmax | 0:06faf770a0c7 | 42 | // 3 = Ribbon at bottom |
jackmax | 0:06faf770a0c7 | 43 | #define LCD_ORIENTATION0 0 |
jackmax | 0:06faf770a0c7 | 44 | #define LCD_ORIENTATION1 96 |
jackmax | 0:06faf770a0c7 | 45 | #define LCD_ORIENTATION2 160 |
jackmax | 0:06faf770a0c7 | 46 | #define LCD_ORIENTATION3 192 |
jackmax | 0:06faf770a0c7 | 47 | |
jackmax | 0:06faf770a0c7 | 48 | // Each colour takes up 5 bits |
jackmax | 0:06faf770a0c7 | 49 | // Green is shifted 6 bits to the left |
jackmax | 0:06faf770a0c7 | 50 | // Blue is shifted 11 bits to the left |
jackmax | 0:06faf770a0c7 | 51 | // some RGB color definitions BLUE GREEN RED |
jackmax | 0:06faf770a0c7 | 52 | #define Black 0x0000 /* 0, 0, 0 */ |
jackmax | 0:06faf770a0c7 | 53 | #define Maroon 0x000F /* 0, 0,15 */ |
jackmax | 0:06faf770a0c7 | 54 | #define DarkGreen 0x03C0 /* 0,15, 0 */ |
jackmax | 0:06faf770a0c7 | 55 | #define Navy 0x7800 /* 15, 0, 0 */ |
jackmax | 0:06faf770a0c7 | 56 | #define Red 0x001F /* 0, 0,31 */ |
jackmax | 0:06faf770a0c7 | 57 | #define Green 0x07C0 /* 0,31, 0 */ |
jackmax | 0:06faf770a0c7 | 58 | #define Blue 0xF800 /* 31, 0, 0 */ |
jackmax | 0:06faf770a0c7 | 59 | #define Yellow 0x07DF /* 0,31,31 */ |
jackmax | 0:06faf770a0c7 | 60 | #define Magenta 0xF81F /* 31, 0,31 */ |
jackmax | 0:06faf770a0c7 | 61 | #define Cyan 0xFFC0 /* 31,31, 0 */ |
jackmax | 0:06faf770a0c7 | 62 | #define White 0xFFFF /* 31,31,31 */ |
jackmax | 0:06faf770a0c7 | 63 | |
jackmax | 0:06faf770a0c7 | 64 | class ILI9163 { |
jackmax | 0:06faf770a0c7 | 65 | public: |
jackmax | 0:06faf770a0c7 | 66 | // Definitions for control lines (port C) |
jackmax | 0:06faf770a0c7 | 67 | static const uint8_t LCD_WR =(1 << 2); |
jackmax | 0:06faf770a0c7 | 68 | static const uint8_t LCD_RS =(1 << 4); |
jackmax | 0:06faf770a0c7 | 69 | static const uint8_t LCD_RD =(1 << 5); |
jackmax | 0:06faf770a0c7 | 70 | static const uint8_t LCD_CS =(1 << 6); |
jackmax | 0:06faf770a0c7 | 71 | static const uint8_t LCD_RESET =(1 << 7); |
jackmax | 0:06faf770a0c7 | 72 | // ILI9163 LCD Controller Commands |
jackmax | 0:06faf770a0c7 | 73 | static const uint8_t NOP = 0x00; |
jackmax | 0:06faf770a0c7 | 74 | static const uint8_t SOFT_RESET = 0x01; |
jackmax | 0:06faf770a0c7 | 75 | static const uint8_t GET_RED_CHANNEL = 0x06; |
jackmax | 0:06faf770a0c7 | 76 | static const uint8_t GET_GREEN_CHANNEL = 0x07; |
jackmax | 0:06faf770a0c7 | 77 | static const uint8_t GET_BLUE_CHANNEL = 0x08; |
jackmax | 0:06faf770a0c7 | 78 | static const uint8_t GET_PIXEL_FORMAT = 0x0C; |
jackmax | 0:06faf770a0c7 | 79 | static const uint8_t GET_POWER_MODE = 0x0A; |
jackmax | 0:06faf770a0c7 | 80 | static const uint8_t GET_ADDRESS_MODE = 0x0B; |
jackmax | 0:06faf770a0c7 | 81 | static const uint8_t GET_DISPLAY_MODE = 0x0D; |
jackmax | 0:06faf770a0c7 | 82 | static const uint8_t GET_SIGNAL_MODE = 0x0E; |
jackmax | 0:06faf770a0c7 | 83 | static const uint8_t GET_DIAGNOSTIC_RESULT = 0x0F; |
jackmax | 0:06faf770a0c7 | 84 | static const uint8_t ENTER_SLEEP_MODE = 0x10; |
jackmax | 0:06faf770a0c7 | 85 | static const uint8_t EXIT_SLEEP_MODE = 0x11; |
jackmax | 0:06faf770a0c7 | 86 | static const uint8_t ENTER_PARTIAL_MODE = 0x12; |
jackmax | 0:06faf770a0c7 | 87 | static const uint8_t ENTER_NORMAL_MODE = 0x13; |
jackmax | 0:06faf770a0c7 | 88 | static const uint8_t EXIT_INVERT_MODE = 0x20; |
jackmax | 0:06faf770a0c7 | 89 | static const uint8_t ENTER_INVERT_MODE = 0x21; |
jackmax | 0:06faf770a0c7 | 90 | static const uint8_t SET_GAMMA_CURVE = 0x26; |
jackmax | 0:06faf770a0c7 | 91 | static const uint8_t SET_DISPLAY_OFF = 0x28; |
jackmax | 0:06faf770a0c7 | 92 | static const uint8_t SET_DISPLAY_ON = 0x29; |
jackmax | 0:06faf770a0c7 | 93 | static const uint8_t SET_COLUMN_ADDRESS = 0x2A; |
jackmax | 0:06faf770a0c7 | 94 | static const uint8_t SET_PAGE_ADDRESS = 0x2B; |
jackmax | 0:06faf770a0c7 | 95 | static const uint8_t WRITE_MEMORY_START = 0x2C; |
jackmax | 0:06faf770a0c7 | 96 | static const uint8_t WRITE_LUT = 0x2D; |
jackmax | 0:06faf770a0c7 | 97 | static const uint8_t READ_MEMORY_START = 0x2E; |
jackmax | 0:06faf770a0c7 | 98 | static const uint8_t SET_PARTIAL_AREA = 0x30; |
jackmax | 0:06faf770a0c7 | 99 | static const uint8_t SET_SCROLL_AREA = 0x33; |
jackmax | 0:06faf770a0c7 | 100 | static const uint8_t SET_TEAR_OFF = 0x34; |
jackmax | 0:06faf770a0c7 | 101 | static const uint8_t SET_TEAR_ON = 0x35; |
jackmax | 0:06faf770a0c7 | 102 | static const uint8_t SET_ADDRESS_MODE = 0x36; |
jackmax | 0:06faf770a0c7 | 103 | static const uint8_t SET_SCROLL_START = 0x37; |
jackmax | 0:06faf770a0c7 | 104 | static const uint8_t EXIT_IDLE_MODE = 0x38; |
jackmax | 0:06faf770a0c7 | 105 | static const uint8_t ENTER_IDLE_MODE = 0x39; |
jackmax | 0:06faf770a0c7 | 106 | static const uint8_t SET_PIXEL_FORMAT = 0x3A; |
jackmax | 0:06faf770a0c7 | 107 | static const uint8_t WRITE_MEMORY_CONTINUE = 0x3C; |
jackmax | 0:06faf770a0c7 | 108 | static const uint8_t READ_MEMORY_CONTINUE = 0x3E; |
jackmax | 0:06faf770a0c7 | 109 | static const uint8_t SET_TEAR_SCANLINE = 0x44; |
jackmax | 0:06faf770a0c7 | 110 | static const uint8_t GET_SCANLINE = 0x45; |
jackmax | 0:06faf770a0c7 | 111 | static const uint8_t READ_ID1 = 0xDA; |
jackmax | 0:06faf770a0c7 | 112 | static const uint8_t READ_ID2 = 0xDB; |
jackmax | 0:06faf770a0c7 | 113 | static const uint8_t READ_ID3 = 0xDC; |
jackmax | 0:06faf770a0c7 | 114 | static const uint8_t FRAME_RATE_CONTROL1 = 0xB1; |
jackmax | 0:06faf770a0c7 | 115 | static const uint8_t FRAME_RATE_CONTROL2 = 0xB2; |
jackmax | 0:06faf770a0c7 | 116 | static const uint8_t FRAME_RATE_CONTROL3 = 0xB3; |
jackmax | 0:06faf770a0c7 | 117 | static const uint8_t DISPLAY_INVERSION = 0xB4; |
jackmax | 0:06faf770a0c7 | 118 | static const uint8_t SOURCE_DRIVER_DIRECTION = 0xB7; |
jackmax | 0:06faf770a0c7 | 119 | static const uint8_t GATE_DRIVER_DIRECTION = 0xB8; |
jackmax | 0:06faf770a0c7 | 120 | static const uint8_t POWER_CONTROL1 = 0xC0; |
jackmax | 0:06faf770a0c7 | 121 | static const uint8_t POWER_CONTROL2 = 0xC1; |
jackmax | 0:06faf770a0c7 | 122 | static const uint8_t POWER_CONTROL3 = 0xC2; |
jackmax | 0:06faf770a0c7 | 123 | static const uint8_t POWER_CONTROL4 = 0xC3; |
jackmax | 0:06faf770a0c7 | 124 | static const uint8_t POWER_CONTROL5 = 0xC4; |
jackmax | 0:06faf770a0c7 | 125 | static const uint8_t VCOM_CONTROL1 = 0xC5; |
jackmax | 0:06faf770a0c7 | 126 | static const uint8_t VCOM_CONTROL2 = 0xC6; |
jackmax | 0:06faf770a0c7 | 127 | static const uint8_t VCOM_OFFSET_CONTROL = 0xC7; |
jackmax | 0:06faf770a0c7 | 128 | static const uint8_t WRITE_ID4_VALUE = 0xD3; |
jackmax | 0:06faf770a0c7 | 129 | static const uint8_t NV_MEMORY_FUNCTION1 = 0xD7; |
jackmax | 0:06faf770a0c7 | 130 | static const uint8_t NV_MEMORY_FUNCTION2 = 0xDE; |
jackmax | 0:06faf770a0c7 | 131 | static const uint8_t POSITIVE_GAMMA_CORRECT = 0xE0; |
jackmax | 0:06faf770a0c7 | 132 | static const uint8_t NEGATIVE_GAMMA_CORRECT = 0xE1; |
jackmax | 0:06faf770a0c7 | 133 | static const uint8_t GAM_R_SEL = 0xF2; |
jackmax | 0:06faf770a0c7 | 134 | |
jackmax | 0:06faf770a0c7 | 135 | // Create the ILI9163 object |
jackmax | 0:06faf770a0c7 | 136 | // |
jackmax | 0:06faf770a0c7 | 137 | // @param D13 SCK |
jackmax | 0:06faf770a0c7 | 138 | // @param D11 SDA |
jackmax | 0:06faf770a0c7 | 139 | // @param D10 A0 |
jackmax | 0:06faf770a0c7 | 140 | // @param D9 RESET pin connected to RESET of display |
jackmax | 0:06faf770a0c7 | 141 | // @param D8 CS pin connected to CS of display |
jackmax | 0:06faf770a0c7 | 142 | // @param D12 LED (optional) connected to LED pin (for controlling backlight) |
jackmax | 0:06faf770a0c7 | 143 | // |
jackmax | 0:06faf770a0c7 | 144 | // ILI9163 lcd(D13,D11,D10,D9,D8,D12); |
jackmax | 0:06faf770a0c7 | 145 | // |
jackmax | 0:06faf770a0c7 | 146 | ILI9163(PinName SCK, PinName SDA, PinName A0, PinName RESET, PinName CS, PinName LED = NC); |
jackmax | 0:06faf770a0c7 | 147 | |
jackmax | 0:06faf770a0c7 | 148 | SPI SPI_; |
jackmax | 0:06faf770a0c7 | 149 | DigitalOut A0_; |
jackmax | 0:06faf770a0c7 | 150 | DigitalOut RESET_; |
jackmax | 0:06faf770a0c7 | 151 | DigitalOut CS_; |
jackmax | 0:06faf770a0c7 | 152 | DigitalOut LED_; |
jackmax | 0:06faf770a0c7 | 153 | |
jackmax | 0:06faf770a0c7 | 154 | // font array |
jackmax | 0:06faf770a0c7 | 155 | unsigned char* font; |
jackmax | 0:06faf770a0c7 | 156 | uint8_t font_bp_char; // Bytes per character |
jackmax | 0:06faf770a0c7 | 157 | uint8_t font_hor; // Horizontal size |
jackmax | 0:06faf770a0c7 | 158 | uint8_t font_vert; // Vertical size |
jackmax | 0:06faf770a0c7 | 159 | uint8_t font_bp_line; // Bytes per line |
jackmax | 0:06faf770a0c7 | 160 | |
jackmax | 0:06faf770a0c7 | 161 | // LCD function prototypes |
jackmax | 0:06faf770a0c7 | 162 | void reset(void); |
jackmax | 0:06faf770a0c7 | 163 | void writeCommand(uint8_t address); |
jackmax | 0:06faf770a0c7 | 164 | void writeParameter(uint8_t parameter); |
jackmax | 0:06faf770a0c7 | 165 | void writeData(uint16_t data); |
jackmax | 0:06faf770a0c7 | 166 | void init(uint8_t orientation); |
jackmax | 0:06faf770a0c7 | 167 | |
jackmax | 0:06faf770a0c7 | 168 | //Theoretically, it is possible to use PWM to change the display brightness |
jackmax | 0:06faf770a0c7 | 169 | //However, I found that it can interfere with data transfers, so I just use backlight on/off |
jackmax | 0:06faf770a0c7 | 170 | inline void backlightOn() { LED_ = 1; }; |
jackmax | 0:06faf770a0c7 | 171 | inline void backlightOff() { LED_ = 0; }; |
jackmax | 0:06faf770a0c7 | 172 | inline void backlightToggle() { LED_ = !LED_; }; |
jackmax | 0:06faf770a0c7 | 173 | |
jackmax | 0:06faf770a0c7 | 174 | // Translates a 3 byte RGB value into a 2 byte value for the LCD (values should be 0-31) |
jackmax | 0:06faf770a0c7 | 175 | static inline uint16_t colourFromRGB5(uint8_t r, uint8_t g, uint8_t b) |
jackmax | 0:06faf770a0c7 | 176 | { return (b << 11) | (g << 6) | (r); } |
jackmax | 0:06faf770a0c7 | 177 | |
jackmax | 0:06faf770a0c7 | 178 | static inline uint16_t colourFromRGB8(uint8_t r, uint8_t g, uint8_t b) |
jackmax | 0:06faf770a0c7 | 179 | { return colourFromRGB5(r >> 3, g >> 3, b >> 3); } |
jackmax | 0:06faf770a0c7 | 180 | |
jackmax | 0:06faf770a0c7 | 181 | void clearDisplay(uint16_t colour); |
jackmax | 0:06faf770a0c7 | 182 | void plot(uint8_t x, uint8_t y, uint16_t colour); |
jackmax | 0:06faf770a0c7 | 183 | void line(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour); |
jackmax | 0:06faf770a0c7 | 184 | void rectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour); |
jackmax | 0:06faf770a0c7 | 185 | void filledRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour); |
jackmax | 0:06faf770a0c7 | 186 | void circle(int16_t xCentre, int16_t yCentre, int16_t radius, uint16_t colour); |
jackmax | 0:06faf770a0c7 | 187 | |
jackmax | 0:06faf770a0c7 | 188 | // select the font to use |
jackmax | 0:06faf770a0c7 | 189 | // |
jackmax | 0:06faf770a0c7 | 190 | // @param f pointer to font array |
jackmax | 0:06faf770a0c7 | 191 | // |
jackmax | 0:06faf770a0c7 | 192 | // font array can be created with GLCD Font Creator from http://www.mikroe.com |
jackmax | 0:06faf770a0c7 | 193 | // you have to add 4 parameter at the beginning of the font array to use it: |
jackmax | 0:06faf770a0c7 | 194 | // - the number of bytes per char |
jackmax | 0:06faf770a0c7 | 195 | // - the vertical size in pixel |
jackmax | 0:06faf770a0c7 | 196 | // - the horizontal size in pixel |
jackmax | 0:06faf770a0c7 | 197 | // - the number of bytes per vertical line |
jackmax | 0:06faf770a0c7 | 198 | // you also have to change the array to char[] |
jackmax | 0:06faf770a0c7 | 199 | // |
jackmax | 0:06faf770a0c7 | 200 | void setFont(unsigned char* f); |
jackmax | 0:06faf770a0c7 | 201 | inline uint8_t lcdTextX(uint8_t x) { return x*font_hor; } |
jackmax | 0:06faf770a0c7 | 202 | inline uint8_t lcdTextY(uint8_t y) { return y*font_vert; } |
jackmax | 0:06faf770a0c7 | 203 | |
jackmax | 0:06faf770a0c7 | 204 | void putCh(unsigned char character, uint8_t x, uint8_t y, uint16_t fgColour, uint16_t bgColour); |
jackmax | 0:06faf770a0c7 | 205 | void putS(const char *string, uint8_t x, uint8_t y, uint16_t fgColour, uint16_t bgColour); |
jackmax | 0:06faf770a0c7 | 206 | |
jackmax | 0:06faf770a0c7 | 207 | }; // end class |
jackmax | 0:06faf770a0c7 | 208 | |
jackmax | 0:06faf770a0c7 | 209 | #endif /* ILI9163LCD_H_ */ |