madhu sudhana
/
DL144128_LCD_b
output
Fork of display by
ili9163lcd.cpp@0:c0be4e018a09, 2014-06-14 (annotated)
- Committer:
- morita
- Date:
- Sat Jun 14 02:27:06 2014 +0000
- Revision:
- 0:c0be4e018a09
- Child:
- 1:b64c81071d96
DL144128TF driving Test.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
morita | 0:c0be4e018a09 | 1 | /** |
morita | 0:c0be4e018a09 | 2 | * @file ili9163lcd.c |
morita | 0:c0be4e018a09 | 3 | * @brief ILI9163 128x128 LCD Driver |
morita | 0:c0be4e018a09 | 4 | * |
morita | 0:c0be4e018a09 | 5 | * This code has been ported from the ili9163lcd library for mbed |
morita | 0:c0be4e018a09 | 6 | * made by Jun Morita. |
morita | 0:c0be4e018a09 | 7 | * Source form <http://files.noccylabs.info/lib430/liblcd/ili9163lcd_8c.html> |
morita | 0:c0be4e018a09 | 8 | * |
morita | 0:c0be4e018a09 | 9 | * This code has been ported from the ili9163lcd library for avr made |
morita | 0:c0be4e018a09 | 10 | * by Simon Inns, to run on a msp430. |
morita | 0:c0be4e018a09 | 11 | * |
morita | 0:c0be4e018a09 | 12 | * This program is free software: you can redistribute it and/or modify |
morita | 0:c0be4e018a09 | 13 | * it under the terms of the GNU General Public License as published by |
morita | 0:c0be4e018a09 | 14 | * the Free Software Foundation, either version 3 of the License, or |
morita | 0:c0be4e018a09 | 15 | * (at your option) any later version. |
morita | 0:c0be4e018a09 | 16 | * |
morita | 0:c0be4e018a09 | 17 | * This program is distributed in the hope that it will be useful, |
morita | 0:c0be4e018a09 | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
morita | 0:c0be4e018a09 | 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
morita | 0:c0be4e018a09 | 20 | * GNU General Public License for more details. |
morita | 0:c0be4e018a09 | 21 | * |
morita | 0:c0be4e018a09 | 22 | * You should have received a copy of the GNU General Public License |
morita | 0:c0be4e018a09 | 23 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
morita | 0:c0be4e018a09 | 24 | * |
morita | 0:c0be4e018a09 | 25 | * @author Jun Morita (iccraft) |
morita | 0:c0be4e018a09 | 26 | * @author Simon Inns <simon.inns@gmail.com> |
morita | 0:c0be4e018a09 | 27 | * @author Christopher Vagnetoft (NoccyLabs) |
morita | 0:c0be4e018a09 | 28 | * @copyright (C) 2012 Simon Inns |
morita | 0:c0be4e018a09 | 29 | * @copyright parts (C) 2012 NoccyLabs |
morita | 0:c0be4e018a09 | 30 | */ |
morita | 0:c0be4e018a09 | 31 | |
morita | 0:c0be4e018a09 | 32 | #include "mbed.h" |
morita | 0:c0be4e018a09 | 33 | #include "font5x8.h" |
morita | 0:c0be4e018a09 | 34 | #include "ili9163lcd.h" |
morita | 0:c0be4e018a09 | 35 | |
morita | 0:c0be4e018a09 | 36 | // Connection of mbed pins. |
morita | 0:c0be4e018a09 | 37 | DigitalOut SCK_(D2); |
morita | 0:c0be4e018a09 | 38 | DigitalOut SDA_(D3); |
morita | 0:c0be4e018a09 | 39 | DigitalOut A0_(D4); |
morita | 0:c0be4e018a09 | 40 | DigitalOut RESET_(D5); |
morita | 0:c0be4e018a09 | 41 | DigitalOut CS_(D6); |
morita | 0:c0be4e018a09 | 42 | |
morita | 0:c0be4e018a09 | 43 | // Low-level LCD driving functions -------------------------------------------------------------------------- |
morita | 0:c0be4e018a09 | 44 | |
morita | 0:c0be4e018a09 | 45 | // Reset the LCD hardware |
morita | 0:c0be4e018a09 | 46 | void lcdReset(void) |
morita | 0:c0be4e018a09 | 47 | { |
morita | 0:c0be4e018a09 | 48 | // Reset pin is active low (0 = reset, 1 = ready) |
morita | 0:c0be4e018a09 | 49 | RESET_ = 0; |
morita | 0:c0be4e018a09 | 50 | wait_ms(50); |
morita | 0:c0be4e018a09 | 51 | |
morita | 0:c0be4e018a09 | 52 | RESET_ = 1; |
morita | 0:c0be4e018a09 | 53 | wait_ms(120); |
morita | 0:c0be4e018a09 | 54 | } |
morita | 0:c0be4e018a09 | 55 | |
morita | 0:c0be4e018a09 | 56 | void lcdWriteCommand(uint8_t address) |
morita | 0:c0be4e018a09 | 57 | { |
morita | 0:c0be4e018a09 | 58 | uint8_t i; |
morita | 0:c0be4e018a09 | 59 | |
morita | 0:c0be4e018a09 | 60 | CS_ = 0; |
morita | 0:c0be4e018a09 | 61 | A0_ = 0; |
morita | 0:c0be4e018a09 | 62 | for(i=0;i<8;i++){ |
morita | 0:c0be4e018a09 | 63 | if(address & 128)SDA_= 1; else SDA_ = 0; |
morita | 0:c0be4e018a09 | 64 | SCK_ = 1; |
morita | 0:c0be4e018a09 | 65 | address <<= 1; |
morita | 0:c0be4e018a09 | 66 | SCK_ = 0; |
morita | 0:c0be4e018a09 | 67 | } |
morita | 0:c0be4e018a09 | 68 | wait_us(1); |
morita | 0:c0be4e018a09 | 69 | CS_ = 1; |
morita | 0:c0be4e018a09 | 70 | } |
morita | 0:c0be4e018a09 | 71 | |
morita | 0:c0be4e018a09 | 72 | void lcdWriteParameter(uint8_t parameter) |
morita | 0:c0be4e018a09 | 73 | { |
morita | 0:c0be4e018a09 | 74 | uint8_t i; |
morita | 0:c0be4e018a09 | 75 | |
morita | 0:c0be4e018a09 | 76 | CS_ = 0; |
morita | 0:c0be4e018a09 | 77 | A0_ = 1; |
morita | 0:c0be4e018a09 | 78 | for(i=0;i<8;i++){ |
morita | 0:c0be4e018a09 | 79 | if(parameter & 128)SDA_= 1; else SDA_ = 0; |
morita | 0:c0be4e018a09 | 80 | SCK_ = 1; |
morita | 0:c0be4e018a09 | 81 | parameter <<= 1; |
morita | 0:c0be4e018a09 | 82 | SCK_ = 0; |
morita | 0:c0be4e018a09 | 83 | } |
morita | 0:c0be4e018a09 | 84 | wait_us(1); |
morita | 0:c0be4e018a09 | 85 | CS_ = 1; |
morita | 0:c0be4e018a09 | 86 | } |
morita | 0:c0be4e018a09 | 87 | |
morita | 0:c0be4e018a09 | 88 | void lcdWriteData(uint8_t dataByte1, uint8_t dataByte2) |
morita | 0:c0be4e018a09 | 89 | { |
morita | 0:c0be4e018a09 | 90 | uint8_t i; |
morita | 0:c0be4e018a09 | 91 | |
morita | 0:c0be4e018a09 | 92 | CS_ = 0; |
morita | 0:c0be4e018a09 | 93 | A0_ = 1; |
morita | 0:c0be4e018a09 | 94 | for(i=0;i<8;i++){ |
morita | 0:c0be4e018a09 | 95 | if(dataByte1 & 128)SDA_= 1; else SDA_ = 0; |
morita | 0:c0be4e018a09 | 96 | SCK_ = 1; |
morita | 0:c0be4e018a09 | 97 | dataByte1 <<= 1; |
morita | 0:c0be4e018a09 | 98 | SCK_ = 0; |
morita | 0:c0be4e018a09 | 99 | } |
morita | 0:c0be4e018a09 | 100 | wait_us(1); |
morita | 0:c0be4e018a09 | 101 | for(i=0;i<8;i++){ |
morita | 0:c0be4e018a09 | 102 | if(dataByte2 & 128)SDA_= 1; else SDA_ = 0; |
morita | 0:c0be4e018a09 | 103 | SCK_ = 1; |
morita | 0:c0be4e018a09 | 104 | dataByte2 <<= 1; |
morita | 0:c0be4e018a09 | 105 | SCK_ = 0; |
morita | 0:c0be4e018a09 | 106 | } |
morita | 0:c0be4e018a09 | 107 | wait_us(1); |
morita | 0:c0be4e018a09 | 108 | CS_ = 1; |
morita | 0:c0be4e018a09 | 109 | } |
morita | 0:c0be4e018a09 | 110 | |
morita | 0:c0be4e018a09 | 111 | // Initialise the display with the require screen orientation |
morita | 0:c0be4e018a09 | 112 | void lcdInitialise(uint8_t orientation) |
morita | 0:c0be4e018a09 | 113 | { |
morita | 0:c0be4e018a09 | 114 | CS_ = 1; |
morita | 0:c0be4e018a09 | 115 | SCK_ = 0; |
morita | 0:c0be4e018a09 | 116 | RESET_ = 1; |
morita | 0:c0be4e018a09 | 117 | |
morita | 0:c0be4e018a09 | 118 | // Hardware reset the LCD |
morita | 0:c0be4e018a09 | 119 | lcdReset(); |
morita | 0:c0be4e018a09 | 120 | |
morita | 0:c0be4e018a09 | 121 | lcdWriteCommand(EXIT_SLEEP_MODE); |
morita | 0:c0be4e018a09 | 122 | wait_ms(5); // Wait for the screen to wake up |
morita | 0:c0be4e018a09 | 123 | |
morita | 0:c0be4e018a09 | 124 | lcdWriteCommand(SET_PIXEL_FORMAT); |
morita | 0:c0be4e018a09 | 125 | lcdWriteParameter(0x05); // 16 bits per pixel |
morita | 0:c0be4e018a09 | 126 | |
morita | 0:c0be4e018a09 | 127 | lcdWriteCommand(SET_GAMMA_CURVE); |
morita | 0:c0be4e018a09 | 128 | lcdWriteParameter(0x04); // Select gamma curve 3 |
morita | 0:c0be4e018a09 | 129 | |
morita | 0:c0be4e018a09 | 130 | lcdWriteCommand(GAM_R_SEL); |
morita | 0:c0be4e018a09 | 131 | lcdWriteParameter(0x01); // Gamma adjustment enabled |
morita | 0:c0be4e018a09 | 132 | |
morita | 0:c0be4e018a09 | 133 | lcdWriteCommand(POSITIVE_GAMMA_CORRECT); |
morita | 0:c0be4e018a09 | 134 | lcdWriteParameter(0x3f); // 1st Parameter |
morita | 0:c0be4e018a09 | 135 | lcdWriteParameter(0x25); // 2nd Parameter |
morita | 0:c0be4e018a09 | 136 | lcdWriteParameter(0x1c); // 3rd Parameter |
morita | 0:c0be4e018a09 | 137 | lcdWriteParameter(0x1e); // 4th Parameter |
morita | 0:c0be4e018a09 | 138 | lcdWriteParameter(0x20); // 5th Parameter |
morita | 0:c0be4e018a09 | 139 | lcdWriteParameter(0x12); // 6th Parameter |
morita | 0:c0be4e018a09 | 140 | lcdWriteParameter(0x2a); // 7th Parameter |
morita | 0:c0be4e018a09 | 141 | lcdWriteParameter(0x90); // 8th Parameter |
morita | 0:c0be4e018a09 | 142 | lcdWriteParameter(0x24); // 9th Parameter |
morita | 0:c0be4e018a09 | 143 | lcdWriteParameter(0x11); // 10th Parameter |
morita | 0:c0be4e018a09 | 144 | lcdWriteParameter(0x00); // 11th Parameter |
morita | 0:c0be4e018a09 | 145 | lcdWriteParameter(0x00); // 12th Parameter |
morita | 0:c0be4e018a09 | 146 | lcdWriteParameter(0x00); // 13th Parameter |
morita | 0:c0be4e018a09 | 147 | lcdWriteParameter(0x00); // 14th Parameter |
morita | 0:c0be4e018a09 | 148 | lcdWriteParameter(0x00); // 15th Parameter |
morita | 0:c0be4e018a09 | 149 | |
morita | 0:c0be4e018a09 | 150 | lcdWriteCommand(NEGATIVE_GAMMA_CORRECT); |
morita | 0:c0be4e018a09 | 151 | lcdWriteParameter(0x20); // 1st Parameter |
morita | 0:c0be4e018a09 | 152 | lcdWriteParameter(0x20); // 2nd Parameter |
morita | 0:c0be4e018a09 | 153 | lcdWriteParameter(0x20); // 3rd Parameter |
morita | 0:c0be4e018a09 | 154 | lcdWriteParameter(0x20); // 4th Parameter |
morita | 0:c0be4e018a09 | 155 | lcdWriteParameter(0x05); // 5th Parameter |
morita | 0:c0be4e018a09 | 156 | lcdWriteParameter(0x00); // 6th Parameter |
morita | 0:c0be4e018a09 | 157 | lcdWriteParameter(0x15); // 7th Parameter |
morita | 0:c0be4e018a09 | 158 | lcdWriteParameter(0xa7); // 8th Parameter |
morita | 0:c0be4e018a09 | 159 | lcdWriteParameter(0x3d); // 9th Parameter |
morita | 0:c0be4e018a09 | 160 | lcdWriteParameter(0x18); // 10th Parameter |
morita | 0:c0be4e018a09 | 161 | lcdWriteParameter(0x25); // 11th Parameter |
morita | 0:c0be4e018a09 | 162 | lcdWriteParameter(0x2a); // 12th Parameter |
morita | 0:c0be4e018a09 | 163 | lcdWriteParameter(0x2b); // 13th Parameter |
morita | 0:c0be4e018a09 | 164 | lcdWriteParameter(0x2b); // 14th Parameter |
morita | 0:c0be4e018a09 | 165 | lcdWriteParameter(0x3a); // 15th Parameter |
morita | 0:c0be4e018a09 | 166 | |
morita | 0:c0be4e018a09 | 167 | lcdWriteCommand(FRAME_RATE_CONTROL1); |
morita | 0:c0be4e018a09 | 168 | lcdWriteParameter(0x08); // DIVA = 8 |
morita | 0:c0be4e018a09 | 169 | lcdWriteParameter(0x08); // VPA = 8 |
morita | 0:c0be4e018a09 | 170 | |
morita | 0:c0be4e018a09 | 171 | lcdWriteCommand(DISPLAY_INVERSION); |
morita | 0:c0be4e018a09 | 172 | lcdWriteParameter(0x07); // NLA = 1, NLB = 1, NLC = 1 (all on Frame Inversion) |
morita | 0:c0be4e018a09 | 173 | |
morita | 0:c0be4e018a09 | 174 | lcdWriteCommand(POWER_CONTROL1); |
morita | 0:c0be4e018a09 | 175 | lcdWriteParameter(0x0a); // VRH = 10: GVDD = 4.30 |
morita | 0:c0be4e018a09 | 176 | lcdWriteParameter(0x02); // VC = 2: VCI1 = 2.65 |
morita | 0:c0be4e018a09 | 177 | |
morita | 0:c0be4e018a09 | 178 | lcdWriteCommand(POWER_CONTROL2); |
morita | 0:c0be4e018a09 | 179 | lcdWriteParameter(0x02); // BT = 2: AVDD = 2xVCI1, VCL = -1xVCI1, VGH = 5xVCI1, VGL = -2xVCI1 |
morita | 0:c0be4e018a09 | 180 | |
morita | 0:c0be4e018a09 | 181 | lcdWriteCommand(VCOM_CONTROL1); |
morita | 0:c0be4e018a09 | 182 | lcdWriteParameter(0x50); // VMH = 80: VCOMH voltage = 4.5 |
morita | 0:c0be4e018a09 | 183 | lcdWriteParameter(0x5b); // VML = 91: VCOML voltage = -0.225 |
morita | 0:c0be4e018a09 | 184 | |
morita | 0:c0be4e018a09 | 185 | lcdWriteCommand(VCOM_OFFSET_CONTROL); |
morita | 0:c0be4e018a09 | 186 | lcdWriteParameter(0x40); // nVM = 0, VMF = 64: VCOMH output = VMH, VCOML output = VML |
morita | 0:c0be4e018a09 | 187 | |
morita | 0:c0be4e018a09 | 188 | lcdWriteCommand(SET_COLUMN_ADDRESS); |
morita | 0:c0be4e018a09 | 189 | lcdWriteParameter(0x00); // XSH |
morita | 0:c0be4e018a09 | 190 | lcdWriteParameter(0x00); // XSL |
morita | 0:c0be4e018a09 | 191 | lcdWriteParameter(0x00); // XEH |
morita | 0:c0be4e018a09 | 192 | lcdWriteParameter(0x7f); // XEL (128 pixels x) |
morita | 0:c0be4e018a09 | 193 | |
morita | 0:c0be4e018a09 | 194 | lcdWriteCommand(SET_PAGE_ADDRESS); |
morita | 0:c0be4e018a09 | 195 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 196 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 197 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 198 | lcdWriteParameter(0x7f); // 128 pixels y |
morita | 0:c0be4e018a09 | 199 | |
morita | 0:c0be4e018a09 | 200 | // Select display orientation |
morita | 0:c0be4e018a09 | 201 | lcdWriteCommand(SET_ADDRESS_MODE); |
morita | 0:c0be4e018a09 | 202 | lcdWriteParameter(orientation); |
morita | 0:c0be4e018a09 | 203 | |
morita | 0:c0be4e018a09 | 204 | // Set the display to on |
morita | 0:c0be4e018a09 | 205 | lcdWriteCommand(SET_DISPLAY_ON); |
morita | 0:c0be4e018a09 | 206 | lcdWriteCommand(WRITE_MEMORY_START); |
morita | 0:c0be4e018a09 | 207 | } |
morita | 0:c0be4e018a09 | 208 | |
morita | 0:c0be4e018a09 | 209 | // LCD graphics functions ----------------------------------------------------------------------------------- |
morita | 0:c0be4e018a09 | 210 | |
morita | 0:c0be4e018a09 | 211 | void lcdClearDisplay(uint16_t colour) |
morita | 0:c0be4e018a09 | 212 | { |
morita | 0:c0be4e018a09 | 213 | uint16_t pixel; |
morita | 0:c0be4e018a09 | 214 | |
morita | 0:c0be4e018a09 | 215 | // Set the column address to 0-127 |
morita | 0:c0be4e018a09 | 216 | lcdWriteCommand(SET_COLUMN_ADDRESS); |
morita | 0:c0be4e018a09 | 217 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 218 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 219 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 220 | lcdWriteParameter(0x7f); |
morita | 0:c0be4e018a09 | 221 | |
morita | 0:c0be4e018a09 | 222 | // Set the page address to 0-127 |
morita | 0:c0be4e018a09 | 223 | lcdWriteCommand(SET_PAGE_ADDRESS); |
morita | 0:c0be4e018a09 | 224 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 225 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 226 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 227 | lcdWriteParameter(0x7f); |
morita | 0:c0be4e018a09 | 228 | |
morita | 0:c0be4e018a09 | 229 | // Plot the pixels |
morita | 0:c0be4e018a09 | 230 | lcdWriteCommand(WRITE_MEMORY_START); |
morita | 0:c0be4e018a09 | 231 | for(pixel = 0; pixel < 16385; pixel++) lcdWriteData(colour >> 8, colour); |
morita | 0:c0be4e018a09 | 232 | } |
morita | 0:c0be4e018a09 | 233 | |
morita | 0:c0be4e018a09 | 234 | void lcdPlot(uint8_t x, uint8_t y, uint16_t colour) |
morita | 0:c0be4e018a09 | 235 | { |
morita | 0:c0be4e018a09 | 236 | // Horizontal Address Start Position |
morita | 0:c0be4e018a09 | 237 | lcdWriteCommand(SET_COLUMN_ADDRESS); |
morita | 0:c0be4e018a09 | 238 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 239 | lcdWriteParameter(x); |
morita | 0:c0be4e018a09 | 240 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 241 | lcdWriteParameter(0x7f); |
morita | 0:c0be4e018a09 | 242 | |
morita | 0:c0be4e018a09 | 243 | // Vertical Address end Position |
morita | 0:c0be4e018a09 | 244 | lcdWriteCommand(SET_PAGE_ADDRESS); |
morita | 0:c0be4e018a09 | 245 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 246 | lcdWriteParameter(y); |
morita | 0:c0be4e018a09 | 247 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 248 | lcdWriteParameter(0x7f); |
morita | 0:c0be4e018a09 | 249 | |
morita | 0:c0be4e018a09 | 250 | // Plot the point |
morita | 0:c0be4e018a09 | 251 | lcdWriteCommand(WRITE_MEMORY_START); |
morita | 0:c0be4e018a09 | 252 | lcdWriteData(colour >> 8, colour); |
morita | 0:c0be4e018a09 | 253 | } |
morita | 0:c0be4e018a09 | 254 | |
morita | 0:c0be4e018a09 | 255 | // Draw a line from x0, y0 to x1, y1 |
morita | 0:c0be4e018a09 | 256 | // Note: This is a version of Bresenham's line drawing algorithm |
morita | 0:c0be4e018a09 | 257 | // It only draws lines from left to right! |
morita | 0:c0be4e018a09 | 258 | void lcdLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour) |
morita | 0:c0be4e018a09 | 259 | { |
morita | 0:c0be4e018a09 | 260 | int16_t dy = y1 - y0; |
morita | 0:c0be4e018a09 | 261 | int16_t dx = x1 - x0; |
morita | 0:c0be4e018a09 | 262 | int16_t stepx, stepy; |
morita | 0:c0be4e018a09 | 263 | |
morita | 0:c0be4e018a09 | 264 | if (dy < 0) |
morita | 0:c0be4e018a09 | 265 | { |
morita | 0:c0be4e018a09 | 266 | dy = -dy; stepy = -1; |
morita | 0:c0be4e018a09 | 267 | } |
morita | 0:c0be4e018a09 | 268 | else stepy = 1; |
morita | 0:c0be4e018a09 | 269 | |
morita | 0:c0be4e018a09 | 270 | if (dx < 0) |
morita | 0:c0be4e018a09 | 271 | { |
morita | 0:c0be4e018a09 | 272 | dx = -dx; stepx = -1; |
morita | 0:c0be4e018a09 | 273 | } |
morita | 0:c0be4e018a09 | 274 | else stepx = 1; |
morita | 0:c0be4e018a09 | 275 | |
morita | 0:c0be4e018a09 | 276 | dy <<= 1; // dy is now 2*dy |
morita | 0:c0be4e018a09 | 277 | dx <<= 1; // dx is now 2*dx |
morita | 0:c0be4e018a09 | 278 | |
morita | 0:c0be4e018a09 | 279 | lcdPlot(x0, y0, colour); |
morita | 0:c0be4e018a09 | 280 | |
morita | 0:c0be4e018a09 | 281 | if (dx > dy) { |
morita | 0:c0be4e018a09 | 282 | int fraction = dy - (dx >> 1); // same as 2*dy - dx |
morita | 0:c0be4e018a09 | 283 | while (x0 != x1) |
morita | 0:c0be4e018a09 | 284 | { |
morita | 0:c0be4e018a09 | 285 | if (fraction >= 0) |
morita | 0:c0be4e018a09 | 286 | { |
morita | 0:c0be4e018a09 | 287 | y0 += stepy; |
morita | 0:c0be4e018a09 | 288 | fraction -= dx; // same as fraction -= 2*dx |
morita | 0:c0be4e018a09 | 289 | } |
morita | 0:c0be4e018a09 | 290 | |
morita | 0:c0be4e018a09 | 291 | x0 += stepx; |
morita | 0:c0be4e018a09 | 292 | fraction += dy; // same as fraction -= 2*dy |
morita | 0:c0be4e018a09 | 293 | lcdPlot(x0, y0, colour); |
morita | 0:c0be4e018a09 | 294 | } |
morita | 0:c0be4e018a09 | 295 | } |
morita | 0:c0be4e018a09 | 296 | else |
morita | 0:c0be4e018a09 | 297 | { |
morita | 0:c0be4e018a09 | 298 | int fraction = dx - (dy >> 1); |
morita | 0:c0be4e018a09 | 299 | while (y0 != y1) |
morita | 0:c0be4e018a09 | 300 | { |
morita | 0:c0be4e018a09 | 301 | if (fraction >= 0) |
morita | 0:c0be4e018a09 | 302 | { |
morita | 0:c0be4e018a09 | 303 | x0 += stepx; |
morita | 0:c0be4e018a09 | 304 | fraction -= dy; |
morita | 0:c0be4e018a09 | 305 | } |
morita | 0:c0be4e018a09 | 306 | |
morita | 0:c0be4e018a09 | 307 | y0 += stepy; |
morita | 0:c0be4e018a09 | 308 | fraction += dx; |
morita | 0:c0be4e018a09 | 309 | lcdPlot(x0, y0, colour); |
morita | 0:c0be4e018a09 | 310 | } |
morita | 0:c0be4e018a09 | 311 | } |
morita | 0:c0be4e018a09 | 312 | } |
morita | 0:c0be4e018a09 | 313 | |
morita | 0:c0be4e018a09 | 314 | // Draw a rectangle between x0, y0 and x1, y1 |
morita | 0:c0be4e018a09 | 315 | void lcdRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour) |
morita | 0:c0be4e018a09 | 316 | { |
morita | 0:c0be4e018a09 | 317 | lcdLine(x0, y0, x0, y1, colour); |
morita | 0:c0be4e018a09 | 318 | lcdLine(x0, y1, x1, y1, colour); |
morita | 0:c0be4e018a09 | 319 | lcdLine(x1, y0, x1, y1, colour); |
morita | 0:c0be4e018a09 | 320 | lcdLine(x0, y0, x1, y0, colour); |
morita | 0:c0be4e018a09 | 321 | } |
morita | 0:c0be4e018a09 | 322 | |
morita | 0:c0be4e018a09 | 323 | // Draw a filled rectangle |
morita | 0:c0be4e018a09 | 324 | // Note: y1 must be greater than y0 and x1 must be greater than x0 |
morita | 0:c0be4e018a09 | 325 | // for this to work |
morita | 0:c0be4e018a09 | 326 | void lcdFilledRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour) |
morita | 0:c0be4e018a09 | 327 | { |
morita | 0:c0be4e018a09 | 328 | uint16_t pixels; |
morita | 0:c0be4e018a09 | 329 | |
morita | 0:c0be4e018a09 | 330 | // To speed up plotting we define a x window with the width of the |
morita | 0:c0be4e018a09 | 331 | // rectangle and then just output the required number of bytes to |
morita | 0:c0be4e018a09 | 332 | // fill down to the end point |
morita | 0:c0be4e018a09 | 333 | |
morita | 0:c0be4e018a09 | 334 | lcdWriteCommand(SET_COLUMN_ADDRESS); // Horizontal Address Start Position |
morita | 0:c0be4e018a09 | 335 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 336 | lcdWriteParameter(x0); |
morita | 0:c0be4e018a09 | 337 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 338 | lcdWriteParameter(x1); |
morita | 0:c0be4e018a09 | 339 | |
morita | 0:c0be4e018a09 | 340 | lcdWriteCommand(SET_PAGE_ADDRESS); // Vertical Address end Position |
morita | 0:c0be4e018a09 | 341 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 342 | lcdWriteParameter(y0); |
morita | 0:c0be4e018a09 | 343 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 344 | lcdWriteParameter(0x7f); |
morita | 0:c0be4e018a09 | 345 | |
morita | 0:c0be4e018a09 | 346 | lcdWriteCommand(WRITE_MEMORY_START); |
morita | 0:c0be4e018a09 | 347 | |
morita | 0:c0be4e018a09 | 348 | for (pixels = 0; pixels < (((x1 - x0) + 1) * ((y1 - y0) + 1)); pixels++) |
morita | 0:c0be4e018a09 | 349 | lcdWriteData(colour >> 8, colour);; |
morita | 0:c0be4e018a09 | 350 | } |
morita | 0:c0be4e018a09 | 351 | |
morita | 0:c0be4e018a09 | 352 | // Draw a circle |
morita | 0:c0be4e018a09 | 353 | // Note: This is another version of Bresenham's line drawing algorithm. |
morita | 0:c0be4e018a09 | 354 | // There's plenty of documentation on the web if you are curious |
morita | 0:c0be4e018a09 | 355 | // how this works. |
morita | 0:c0be4e018a09 | 356 | void lcdCircle(int16_t xCentre, int16_t yCentre, int16_t radius, uint16_t colour) |
morita | 0:c0be4e018a09 | 357 | { |
morita | 0:c0be4e018a09 | 358 | int16_t x = 0, y = radius; |
morita | 0:c0be4e018a09 | 359 | int16_t d = 3 - (2 * radius); |
morita | 0:c0be4e018a09 | 360 | |
morita | 0:c0be4e018a09 | 361 | while(x <= y) |
morita | 0:c0be4e018a09 | 362 | { |
morita | 0:c0be4e018a09 | 363 | lcdPlot(xCentre + x, yCentre + y, colour); |
morita | 0:c0be4e018a09 | 364 | lcdPlot(xCentre + y, yCentre + x, colour); |
morita | 0:c0be4e018a09 | 365 | lcdPlot(xCentre - x, yCentre + y, colour); |
morita | 0:c0be4e018a09 | 366 | lcdPlot(xCentre + y, yCentre - x, colour); |
morita | 0:c0be4e018a09 | 367 | lcdPlot(xCentre - x, yCentre - y, colour); |
morita | 0:c0be4e018a09 | 368 | lcdPlot(xCentre - y, yCentre - x, colour); |
morita | 0:c0be4e018a09 | 369 | lcdPlot(xCentre + x, yCentre - y, colour); |
morita | 0:c0be4e018a09 | 370 | lcdPlot(xCentre - y, yCentre + x, colour); |
morita | 0:c0be4e018a09 | 371 | |
morita | 0:c0be4e018a09 | 372 | if (d < 0) d += (4 * x) + 6; |
morita | 0:c0be4e018a09 | 373 | else |
morita | 0:c0be4e018a09 | 374 | { |
morita | 0:c0be4e018a09 | 375 | d += (4 * (x - y)) + 10; |
morita | 0:c0be4e018a09 | 376 | y -= 1; |
morita | 0:c0be4e018a09 | 377 | } |
morita | 0:c0be4e018a09 | 378 | |
morita | 0:c0be4e018a09 | 379 | x++; |
morita | 0:c0be4e018a09 | 380 | } |
morita | 0:c0be4e018a09 | 381 | } |
morita | 0:c0be4e018a09 | 382 | |
morita | 0:c0be4e018a09 | 383 | // LCD text manipulation functions -------------------------------------------------------------------------- |
morita | 0:c0be4e018a09 | 384 | |
morita | 0:c0be4e018a09 | 385 | // Plot a character at the specified x, y co-ordinates (top left hand corner of character) |
morita | 0:c0be4e018a09 | 386 | void lcdPutCh(unsigned char character, uint8_t x, uint8_t y, uint16_t fgColour, uint16_t bgColour) |
morita | 0:c0be4e018a09 | 387 | { |
morita | 0:c0be4e018a09 | 388 | uint8_t row, column; |
morita | 0:c0be4e018a09 | 389 | |
morita | 0:c0be4e018a09 | 390 | // To speed up plotting we define a x window of 6 pixels and then |
morita | 0:c0be4e018a09 | 391 | // write out one row at a time. This means the LCD will correctly |
morita | 0:c0be4e018a09 | 392 | // update the memory pointer saving us a good few bytes |
morita | 0:c0be4e018a09 | 393 | |
morita | 0:c0be4e018a09 | 394 | lcdWriteCommand(SET_COLUMN_ADDRESS); // Horizontal Address Start Position |
morita | 0:c0be4e018a09 | 395 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 396 | lcdWriteParameter(x); |
morita | 0:c0be4e018a09 | 397 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 398 | lcdWriteParameter(x+5); |
morita | 0:c0be4e018a09 | 399 | |
morita | 0:c0be4e018a09 | 400 | lcdWriteCommand(SET_PAGE_ADDRESS); // Vertical Address end Position |
morita | 0:c0be4e018a09 | 401 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 402 | lcdWriteParameter(y); |
morita | 0:c0be4e018a09 | 403 | lcdWriteParameter(0x00); |
morita | 0:c0be4e018a09 | 404 | lcdWriteParameter(0x7f); |
morita | 0:c0be4e018a09 | 405 | |
morita | 0:c0be4e018a09 | 406 | lcdWriteCommand(WRITE_MEMORY_START); |
morita | 0:c0be4e018a09 | 407 | |
morita | 0:c0be4e018a09 | 408 | // Plot the font data |
morita | 0:c0be4e018a09 | 409 | for (row = 0; row < 8; row++) |
morita | 0:c0be4e018a09 | 410 | { |
morita | 0:c0be4e018a09 | 411 | for (column = 0; column < 6; column++) |
morita | 0:c0be4e018a09 | 412 | { |
morita | 0:c0be4e018a09 | 413 | if ((font5x8[character][column]) & (1 << row)) |
morita | 0:c0be4e018a09 | 414 | lcdWriteData(fgColour >> 8, fgColour); |
morita | 0:c0be4e018a09 | 415 | else lcdWriteData(bgColour >> 8, bgColour); |
morita | 0:c0be4e018a09 | 416 | } |
morita | 0:c0be4e018a09 | 417 | } |
morita | 0:c0be4e018a09 | 418 | } |
morita | 0:c0be4e018a09 | 419 | |
morita | 0:c0be4e018a09 | 420 | // Plot a string of characters to the LCD |
morita | 0:c0be4e018a09 | 421 | void lcdPutS(const char *string, uint8_t x, uint8_t y, uint16_t fgColour, uint16_t bgColour) |
morita | 0:c0be4e018a09 | 422 | { |
morita | 0:c0be4e018a09 | 423 | uint8_t origin = x; |
morita | 0:c0be4e018a09 | 424 | |
morita | 0:c0be4e018a09 | 425 | for (uint8_t characterNumber = 0; characterNumber < strlen(string); characterNumber++) |
morita | 0:c0be4e018a09 | 426 | { |
morita | 0:c0be4e018a09 | 427 | // Check if we are out of bounds and move to |
morita | 0:c0be4e018a09 | 428 | // the next line if we are |
morita | 0:c0be4e018a09 | 429 | if (x > 121) |
morita | 0:c0be4e018a09 | 430 | { |
morita | 0:c0be4e018a09 | 431 | x = origin; |
morita | 0:c0be4e018a09 | 432 | y += 8; |
morita | 0:c0be4e018a09 | 433 | } |
morita | 0:c0be4e018a09 | 434 | |
morita | 0:c0be4e018a09 | 435 | // If we move past the bottom of the screen just exit |
morita | 0:c0be4e018a09 | 436 | if (y > 120) break; |
morita | 0:c0be4e018a09 | 437 | |
morita | 0:c0be4e018a09 | 438 | // Plot the current character |
morita | 0:c0be4e018a09 | 439 | lcdPutCh(string[characterNumber], x, y, fgColour, bgColour); |
morita | 0:c0be4e018a09 | 440 | x += 6; |
morita | 0:c0be4e018a09 | 441 | } |
morita | 0:c0be4e018a09 | 442 | } |