output

Dependencies:   images mbed

Fork of display by madhu sudhana

Committer:
madhusudhana
Date:
Thu Oct 04 11:19:42 2018 +0000
Revision:
3:7d97b4f535c6
Parent:
2:e7a5b9bc75b4
second_version_display_code;

Who changed what in which revision?

UserRevisionLine numberNew 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
rs27 1:b64c81071d96 32 #include "ili9163lcd.h"
morita 0:c0be4e018a09 33 #include "mbed.h"
morita 0:c0be4e018a09 34
rs27 1:b64c81071d96 35 //--------------------------------------------------------------------------
rs27 1:b64c81071d96 36 // Construktor Objekt initialisieren
rs27 1:b64c81071d96 37 //
madhusudhana 3:7d97b4f535c6 38 ILI9163::ILI9163(PinName SCK, PinName SDA, PinName RESET, PinName CS)
madhusudhana 3:7d97b4f535c6 39 : SCK_(SCK), SDA_(SDA), RESET_(RESET), CS_(CS)
rs27 1:b64c81071d96 40 {
rs27 1:b64c81071d96 41 tm=10;
rs27 1:b64c81071d96 42 R=0,G=0,B=0;
rs27 1:b64c81071d96 43 RGB_state=0;
rs27 1:b64c81071d96 44
rs27 1:b64c81071d96 45 font_size = 2;
rs27 1:b64c81071d96 46
rs27 1:b64c81071d96 47 set_font((unsigned char*)font11x16);
rs27 1:b64c81071d96 48 }
morita 0:c0be4e018a09 49
rs27 1:b64c81071d96 50 //--------------------------------------------------------------------------
rs27 1:b64c81071d96 51 // Low-level LCD driving functions
morita 0:c0be4e018a09 52 // Reset the LCD hardware
rs27 1:b64c81071d96 53 void ILI9163::lcdReset(void)
morita 0:c0be4e018a09 54 {
morita 0:c0be4e018a09 55 // Reset pin is active low (0 = reset, 1 = ready)
morita 0:c0be4e018a09 56 RESET_ = 0;
morita 0:c0be4e018a09 57 wait_ms(50);
morita 0:c0be4e018a09 58
morita 0:c0be4e018a09 59 RESET_ = 1;
madhusudhana 3:7d97b4f535c6 60 wait_ms(150);
morita 0:c0be4e018a09 61 }
morita 0:c0be4e018a09 62
rs27 1:b64c81071d96 63 void ILI9163::lcdWriteCommand(uint8_t address)
morita 0:c0be4e018a09 64 {
morita 0:c0be4e018a09 65 uint8_t i;
morita 0:c0be4e018a09 66
morita 0:c0be4e018a09 67 CS_ = 0;
madhusudhana 3:7d97b4f535c6 68 SDA_ = 0;
madhusudhana 3:7d97b4f535c6 69
madhusudhana 3:7d97b4f535c6 70 SCK_ = 1;
madhusudhana 3:7d97b4f535c6 71 wait_us(1);
madhusudhana 3:7d97b4f535c6 72 SCK_ = 0;
madhusudhana 3:7d97b4f535c6 73 wait_us(0.5);
madhusudhana 3:7d97b4f535c6 74
madhusudhana 3:7d97b4f535c6 75
morita 0:c0be4e018a09 76 for(i=0;i<8;i++){
morita 0:c0be4e018a09 77 if(address & 128)SDA_= 1; else SDA_ = 0;
madhusudhana 3:7d97b4f535c6 78 SCK_ = 1;wait_us(1);
morita 0:c0be4e018a09 79 address <<= 1;
madhusudhana 3:7d97b4f535c6 80 SCK_ = 0;wait_us(1);
morita 0:c0be4e018a09 81 }
morita 0:c0be4e018a09 82 wait_us(1);
morita 0:c0be4e018a09 83 CS_ = 1;
morita 0:c0be4e018a09 84
madhusudhana 3:7d97b4f535c6 85 for(int i=0;i<10;i++)
madhusudhana 3:7d97b4f535c6 86 { SCK_ = 1;
madhusudhana 3:7d97b4f535c6 87 wait_us(1/1000);
madhusudhana 3:7d97b4f535c6 88 SCK_ = 0;
madhusudhana 3:7d97b4f535c6 89 wait_us(1/1000);
madhusudhana 3:7d97b4f535c6 90 }
madhusudhana 3:7d97b4f535c6 91 }
rs27 1:b64c81071d96 92 void ILI9163::lcdWriteParameter(uint8_t parameter)
morita 0:c0be4e018a09 93 {
morita 0:c0be4e018a09 94 uint8_t i;
morita 0:c0be4e018a09 95
morita 0:c0be4e018a09 96 CS_ = 0;
madhusudhana 3:7d97b4f535c6 97 SDA_ = 1;
madhusudhana 3:7d97b4f535c6 98 SCK_ = 1;
madhusudhana 3:7d97b4f535c6 99 wait_us(1);
madhusudhana 3:7d97b4f535c6 100 SCK_ = 0;
madhusudhana 3:7d97b4f535c6 101 wait_us(1);
madhusudhana 3:7d97b4f535c6 102
morita 0:c0be4e018a09 103 for(i=0;i<8;i++){
morita 0:c0be4e018a09 104 if(parameter & 128)SDA_= 1; else SDA_ = 0;
madhusudhana 3:7d97b4f535c6 105 SCK_ = 1; wait_us(1);
morita 0:c0be4e018a09 106 parameter <<= 1;
madhusudhana 3:7d97b4f535c6 107 SCK_ = 0; wait_us(1);
morita 0:c0be4e018a09 108 }
morita 0:c0be4e018a09 109 wait_us(1);
morita 0:c0be4e018a09 110 CS_ = 1;
madhusudhana 3:7d97b4f535c6 111 for(int i=0;i<10;i++)
madhusudhana 3:7d97b4f535c6 112 { SCK_ = 1; wait_us(1/1000);
madhusudhana 3:7d97b4f535c6 113 SCK_ = 0;
madhusudhana 3:7d97b4f535c6 114 wait_us(1/1000);
madhusudhana 3:7d97b4f535c6 115 }
madhusudhana 3:7d97b4f535c6 116 }
morita 0:c0be4e018a09 117
rs27 1:b64c81071d96 118 void ILI9163::lcdWriteData(uint8_t dataByte1, uint8_t dataByte2)
morita 0:c0be4e018a09 119 {
morita 0:c0be4e018a09 120 uint8_t i;
morita 0:c0be4e018a09 121
morita 0:c0be4e018a09 122 CS_ = 0;
madhusudhana 3:7d97b4f535c6 123 SDA_ = 1;
madhusudhana 3:7d97b4f535c6 124 SCK_ = 1;
madhusudhana 3:7d97b4f535c6 125 wait_us(1);
madhusudhana 3:7d97b4f535c6 126 SCK_ = 0;
madhusudhana 3:7d97b4f535c6 127 wait_us(1);
madhusudhana 3:7d97b4f535c6 128
morita 0:c0be4e018a09 129 for(i=0;i<8;i++){
morita 0:c0be4e018a09 130 if(dataByte1 & 128)SDA_= 1; else SDA_ = 0;
madhusudhana 3:7d97b4f535c6 131 SCK_ = 1; wait_us(1);
morita 0:c0be4e018a09 132 dataByte1 <<= 1;
madhusudhana 3:7d97b4f535c6 133 SCK_ = 0; wait_us(1);
morita 0:c0be4e018a09 134 }
madhusudhana 3:7d97b4f535c6 135
morita 0:c0be4e018a09 136 for(i=0;i<8;i++){
morita 0:c0be4e018a09 137 if(dataByte2 & 128)SDA_= 1; else SDA_ = 0;
madhusudhana 3:7d97b4f535c6 138 SCK_ = 1; wait_us(1);
morita 0:c0be4e018a09 139 dataByte2 <<= 1;
madhusudhana 3:7d97b4f535c6 140 SCK_ = 0; wait_us(1);
morita 0:c0be4e018a09 141 }
morita 0:c0be4e018a09 142 wait_us(1);
morita 0:c0be4e018a09 143 CS_ = 1;
madhusudhana 3:7d97b4f535c6 144 for(int i=0;i<10;i++)
madhusudhana 3:7d97b4f535c6 145 { SCK_ = 1; wait_us(1/1000);
madhusudhana 3:7d97b4f535c6 146 SCK_ = 0;
madhusudhana 3:7d97b4f535c6 147 wait_us(1/1000);
madhusudhana 3:7d97b4f535c6 148 }
madhusudhana 3:7d97b4f535c6 149 }
morita 0:c0be4e018a09 150
morita 0:c0be4e018a09 151 // Initialise the display with the require screen orientation
rs27 1:b64c81071d96 152 void ILI9163::lcdInitialise(uint8_t orientation)
morita 0:c0be4e018a09 153 {
morita 0:c0be4e018a09 154 CS_ = 1;
morita 0:c0be4e018a09 155 SCK_ = 0;
morita 0:c0be4e018a09 156 RESET_ = 1;
morita 0:c0be4e018a09 157
morita 0:c0be4e018a09 158 // Hardware reset the LCD
morita 0:c0be4e018a09 159 lcdReset();
morita 0:c0be4e018a09 160
morita 0:c0be4e018a09 161 lcdWriteCommand(EXIT_SLEEP_MODE);
morita 0:c0be4e018a09 162 wait_ms(5); // Wait for the screen to wake up
morita 0:c0be4e018a09 163
morita 0:c0be4e018a09 164 lcdWriteCommand(SET_PIXEL_FORMAT);
morita 0:c0be4e018a09 165 lcdWriteParameter(0x05); // 16 bits per pixel
morita 0:c0be4e018a09 166
morita 0:c0be4e018a09 167 lcdWriteCommand(SET_GAMMA_CURVE);
morita 0:c0be4e018a09 168 lcdWriteParameter(0x04); // Select gamma curve 3
morita 0:c0be4e018a09 169
morita 0:c0be4e018a09 170 lcdWriteCommand(GAM_R_SEL);
morita 0:c0be4e018a09 171 lcdWriteParameter(0x01); // Gamma adjustment enabled
morita 0:c0be4e018a09 172
morita 0:c0be4e018a09 173 lcdWriteCommand(POSITIVE_GAMMA_CORRECT);
morita 0:c0be4e018a09 174 lcdWriteParameter(0x3f); // 1st Parameter
morita 0:c0be4e018a09 175 lcdWriteParameter(0x25); // 2nd Parameter
morita 0:c0be4e018a09 176 lcdWriteParameter(0x1c); // 3rd Parameter
morita 0:c0be4e018a09 177 lcdWriteParameter(0x1e); // 4th Parameter
morita 0:c0be4e018a09 178 lcdWriteParameter(0x20); // 5th Parameter
morita 0:c0be4e018a09 179 lcdWriteParameter(0x12); // 6th Parameter
morita 0:c0be4e018a09 180 lcdWriteParameter(0x2a); // 7th Parameter
morita 0:c0be4e018a09 181 lcdWriteParameter(0x90); // 8th Parameter
morita 0:c0be4e018a09 182 lcdWriteParameter(0x24); // 9th Parameter
morita 0:c0be4e018a09 183 lcdWriteParameter(0x11); // 10th Parameter
morita 0:c0be4e018a09 184 lcdWriteParameter(0x00); // 11th Parameter
morita 0:c0be4e018a09 185 lcdWriteParameter(0x00); // 12th Parameter
morita 0:c0be4e018a09 186 lcdWriteParameter(0x00); // 13th Parameter
morita 0:c0be4e018a09 187 lcdWriteParameter(0x00); // 14th Parameter
morita 0:c0be4e018a09 188 lcdWriteParameter(0x00); // 15th Parameter
morita 0:c0be4e018a09 189
morita 0:c0be4e018a09 190 lcdWriteCommand(NEGATIVE_GAMMA_CORRECT);
morita 0:c0be4e018a09 191 lcdWriteParameter(0x20); // 1st Parameter
morita 0:c0be4e018a09 192 lcdWriteParameter(0x20); // 2nd Parameter
morita 0:c0be4e018a09 193 lcdWriteParameter(0x20); // 3rd Parameter
morita 0:c0be4e018a09 194 lcdWriteParameter(0x20); // 4th Parameter
morita 0:c0be4e018a09 195 lcdWriteParameter(0x05); // 5th Parameter
morita 0:c0be4e018a09 196 lcdWriteParameter(0x00); // 6th Parameter
morita 0:c0be4e018a09 197 lcdWriteParameter(0x15); // 7th Parameter
morita 0:c0be4e018a09 198 lcdWriteParameter(0xa7); // 8th Parameter
morita 0:c0be4e018a09 199 lcdWriteParameter(0x3d); // 9th Parameter
morita 0:c0be4e018a09 200 lcdWriteParameter(0x18); // 10th Parameter
morita 0:c0be4e018a09 201 lcdWriteParameter(0x25); // 11th Parameter
morita 0:c0be4e018a09 202 lcdWriteParameter(0x2a); // 12th Parameter
morita 0:c0be4e018a09 203 lcdWriteParameter(0x2b); // 13th Parameter
morita 0:c0be4e018a09 204 lcdWriteParameter(0x2b); // 14th Parameter
morita 0:c0be4e018a09 205 lcdWriteParameter(0x3a); // 15th Parameter
morita 0:c0be4e018a09 206
morita 0:c0be4e018a09 207 lcdWriteCommand(FRAME_RATE_CONTROL1);
morita 0:c0be4e018a09 208 lcdWriteParameter(0x08); // DIVA = 8
morita 0:c0be4e018a09 209 lcdWriteParameter(0x08); // VPA = 8
morita 0:c0be4e018a09 210
morita 0:c0be4e018a09 211 lcdWriteCommand(DISPLAY_INVERSION);
morita 0:c0be4e018a09 212 lcdWriteParameter(0x07); // NLA = 1, NLB = 1, NLC = 1 (all on Frame Inversion)
morita 0:c0be4e018a09 213
morita 0:c0be4e018a09 214 lcdWriteCommand(POWER_CONTROL1);
morita 0:c0be4e018a09 215 lcdWriteParameter(0x0a); // VRH = 10: GVDD = 4.30
morita 0:c0be4e018a09 216 lcdWriteParameter(0x02); // VC = 2: VCI1 = 2.65
morita 0:c0be4e018a09 217
morita 0:c0be4e018a09 218 lcdWriteCommand(POWER_CONTROL2);
morita 0:c0be4e018a09 219 lcdWriteParameter(0x02); // BT = 2: AVDD = 2xVCI1, VCL = -1xVCI1, VGH = 5xVCI1, VGL = -2xVCI1
morita 0:c0be4e018a09 220
morita 0:c0be4e018a09 221 lcdWriteCommand(VCOM_CONTROL1);
morita 0:c0be4e018a09 222 lcdWriteParameter(0x50); // VMH = 80: VCOMH voltage = 4.5
morita 0:c0be4e018a09 223 lcdWriteParameter(0x5b); // VML = 91: VCOML voltage = -0.225
morita 0:c0be4e018a09 224
morita 0:c0be4e018a09 225 lcdWriteCommand(VCOM_OFFSET_CONTROL);
morita 0:c0be4e018a09 226 lcdWriteParameter(0x40); // nVM = 0, VMF = 64: VCOMH output = VMH, VCOML output = VML
morita 0:c0be4e018a09 227
morita 0:c0be4e018a09 228 lcdWriteCommand(SET_COLUMN_ADDRESS);
morita 0:c0be4e018a09 229 lcdWriteParameter(0x00); // XSH
morita 0:c0be4e018a09 230 lcdWriteParameter(0x00); // XSL
morita 0:c0be4e018a09 231 lcdWriteParameter(0x00); // XEH
morita 0:c0be4e018a09 232 lcdWriteParameter(0x7f); // XEL (128 pixels x)
morita 0:c0be4e018a09 233
morita 0:c0be4e018a09 234 lcdWriteCommand(SET_PAGE_ADDRESS);
morita 0:c0be4e018a09 235 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 236 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 237 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 238 lcdWriteParameter(0x7f); // 128 pixels y
morita 0:c0be4e018a09 239
morita 0:c0be4e018a09 240 // Select display orientation
morita 0:c0be4e018a09 241 lcdWriteCommand(SET_ADDRESS_MODE);
morita 0:c0be4e018a09 242 lcdWriteParameter(orientation);
morita 0:c0be4e018a09 243
morita 0:c0be4e018a09 244 // Set the display to on
morita 0:c0be4e018a09 245 lcdWriteCommand(SET_DISPLAY_ON);
morita 0:c0be4e018a09 246 lcdWriteCommand(WRITE_MEMORY_START);
madhusudhana 3:7d97b4f535c6 247 // END
madhusudhana 3:7d97b4f535c6 248
morita 0:c0be4e018a09 249 }
morita 0:c0be4e018a09 250
morita 0:c0be4e018a09 251 // LCD graphics functions -----------------------------------------------------------------------------------
morita 0:c0be4e018a09 252
rs27 1:b64c81071d96 253 void ILI9163::lcdClearDisplay(uint16_t colour)
morita 0:c0be4e018a09 254 {
morita 0:c0be4e018a09 255 uint16_t pixel;
morita 0:c0be4e018a09 256
madhusudhana 3:7d97b4f535c6 257 // Set the column address to 0-160
morita 0:c0be4e018a09 258 lcdWriteCommand(SET_COLUMN_ADDRESS);
morita 0:c0be4e018a09 259 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 260 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 261 lcdWriteParameter(0x00);
madhusudhana 3:7d97b4f535c6 262 lcdWriteParameter(0xA0);
morita 0:c0be4e018a09 263
morita 0:c0be4e018a09 264 // Set the page address to 0-127
morita 0:c0be4e018a09 265 lcdWriteCommand(SET_PAGE_ADDRESS);
morita 0:c0be4e018a09 266 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 267 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 268 lcdWriteParameter(0x00);
madhusudhana 3:7d97b4f535c6 269 lcdWriteParameter(0xA0);
morita 0:c0be4e018a09 270
morita 0:c0be4e018a09 271 // Plot the pixels
morita 0:c0be4e018a09 272 lcdWriteCommand(WRITE_MEMORY_START);
madhusudhana 3:7d97b4f535c6 273 for(pixel = 0; pixel < 50000; pixel++) lcdWriteData(colour >> 8, colour);
morita 0:c0be4e018a09 274 }
morita 0:c0be4e018a09 275
rs27 1:b64c81071d96 276 void ILI9163::lcdPlot(uint8_t x, uint8_t y, uint16_t colour)
morita 0:c0be4e018a09 277 {
morita 0:c0be4e018a09 278 // Horizontal Address Start Position
morita 0:c0be4e018a09 279 lcdWriteCommand(SET_COLUMN_ADDRESS);
morita 0:c0be4e018a09 280 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 281 lcdWriteParameter(x);
morita 0:c0be4e018a09 282 lcdWriteParameter(0x00);
madhusudhana 3:7d97b4f535c6 283 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 284
morita 0:c0be4e018a09 285 // Vertical Address end Position
morita 0:c0be4e018a09 286 lcdWriteCommand(SET_PAGE_ADDRESS);
morita 0:c0be4e018a09 287 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 288 lcdWriteParameter(y);
madhusudhana 3:7d97b4f535c6 289 lcdWriteParameter(0xA0);
morita 0:c0be4e018a09 290 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 291
morita 0:c0be4e018a09 292 // Plot the point
morita 0:c0be4e018a09 293 lcdWriteCommand(WRITE_MEMORY_START);
morita 0:c0be4e018a09 294 lcdWriteData(colour >> 8, colour);
morita 0:c0be4e018a09 295 }
morita 0:c0be4e018a09 296
morita 0:c0be4e018a09 297 // Draw a line from x0, y0 to x1, y1
morita 0:c0be4e018a09 298 // Note: This is a version of Bresenham's line drawing algorithm
morita 0:c0be4e018a09 299 // It only draws lines from left to right!
rs27 1:b64c81071d96 300 void ILI9163::lcdLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour)
morita 0:c0be4e018a09 301 {
morita 0:c0be4e018a09 302 int16_t dy = y1 - y0;
morita 0:c0be4e018a09 303 int16_t dx = x1 - x0;
morita 0:c0be4e018a09 304 int16_t stepx, stepy;
morita 0:c0be4e018a09 305
morita 0:c0be4e018a09 306 if (dy < 0)
morita 0:c0be4e018a09 307 {
morita 0:c0be4e018a09 308 dy = -dy; stepy = -1;
morita 0:c0be4e018a09 309 }
morita 0:c0be4e018a09 310 else stepy = 1;
morita 0:c0be4e018a09 311
morita 0:c0be4e018a09 312 if (dx < 0)
morita 0:c0be4e018a09 313 {
morita 0:c0be4e018a09 314 dx = -dx; stepx = -1;
morita 0:c0be4e018a09 315 }
morita 0:c0be4e018a09 316 else stepx = 1;
morita 0:c0be4e018a09 317
morita 0:c0be4e018a09 318 dy <<= 1; // dy is now 2*dy
morita 0:c0be4e018a09 319 dx <<= 1; // dx is now 2*dx
morita 0:c0be4e018a09 320
morita 0:c0be4e018a09 321 lcdPlot(x0, y0, colour);
morita 0:c0be4e018a09 322
morita 0:c0be4e018a09 323 if (dx > dy) {
morita 0:c0be4e018a09 324 int fraction = dy - (dx >> 1); // same as 2*dy - dx
morita 0:c0be4e018a09 325 while (x0 != x1)
morita 0:c0be4e018a09 326 {
morita 0:c0be4e018a09 327 if (fraction >= 0)
morita 0:c0be4e018a09 328 {
morita 0:c0be4e018a09 329 y0 += stepy;
morita 0:c0be4e018a09 330 fraction -= dx; // same as fraction -= 2*dx
morita 0:c0be4e018a09 331 }
morita 0:c0be4e018a09 332
morita 0:c0be4e018a09 333 x0 += stepx;
morita 0:c0be4e018a09 334 fraction += dy; // same as fraction -= 2*dy
morita 0:c0be4e018a09 335 lcdPlot(x0, y0, colour);
morita 0:c0be4e018a09 336 }
morita 0:c0be4e018a09 337 }
morita 0:c0be4e018a09 338 else
morita 0:c0be4e018a09 339 {
morita 0:c0be4e018a09 340 int fraction = dx - (dy >> 1);
morita 0:c0be4e018a09 341 while (y0 != y1)
morita 0:c0be4e018a09 342 {
morita 0:c0be4e018a09 343 if (fraction >= 0)
morita 0:c0be4e018a09 344 {
morita 0:c0be4e018a09 345 x0 += stepx;
morita 0:c0be4e018a09 346 fraction -= dy;
morita 0:c0be4e018a09 347 }
morita 0:c0be4e018a09 348
morita 0:c0be4e018a09 349 y0 += stepy;
morita 0:c0be4e018a09 350 fraction += dx;
morita 0:c0be4e018a09 351 lcdPlot(x0, y0, colour);
morita 0:c0be4e018a09 352 }
morita 0:c0be4e018a09 353 }
morita 0:c0be4e018a09 354 }
morita 0:c0be4e018a09 355
morita 0:c0be4e018a09 356 // Draw a rectangle between x0, y0 and x1, y1
rs27 1:b64c81071d96 357 void ILI9163::lcdRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour)
morita 0:c0be4e018a09 358 {
morita 0:c0be4e018a09 359 lcdLine(x0, y0, x0, y1, colour);
morita 0:c0be4e018a09 360 lcdLine(x0, y1, x1, y1, colour);
morita 0:c0be4e018a09 361 lcdLine(x1, y0, x1, y1, colour);
morita 0:c0be4e018a09 362 lcdLine(x0, y0, x1, y0, colour);
morita 0:c0be4e018a09 363 }
morita 0:c0be4e018a09 364
morita 0:c0be4e018a09 365 // Draw a filled rectangle
morita 0:c0be4e018a09 366 // Note: y1 must be greater than y0 and x1 must be greater than x0
morita 0:c0be4e018a09 367 // for this to work
rs27 1:b64c81071d96 368 void ILI9163::lcdFilledRectangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour)
morita 0:c0be4e018a09 369 {
morita 0:c0be4e018a09 370 uint16_t pixels;
morita 0:c0be4e018a09 371
morita 0:c0be4e018a09 372 // To speed up plotting we define a x window with the width of the
morita 0:c0be4e018a09 373 // rectangle and then just output the required number of bytes to
morita 0:c0be4e018a09 374 // fill down to the end point
morita 0:c0be4e018a09 375
morita 0:c0be4e018a09 376 lcdWriteCommand(SET_COLUMN_ADDRESS); // Horizontal Address Start Position
morita 0:c0be4e018a09 377 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 378 lcdWriteParameter(x0);
morita 0:c0be4e018a09 379 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 380 lcdWriteParameter(x1);
morita 0:c0be4e018a09 381
morita 0:c0be4e018a09 382 lcdWriteCommand(SET_PAGE_ADDRESS); // Vertical Address end Position
morita 0:c0be4e018a09 383 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 384 lcdWriteParameter(y0);
morita 0:c0be4e018a09 385 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 386 lcdWriteParameter(0x7f);
morita 0:c0be4e018a09 387
morita 0:c0be4e018a09 388 lcdWriteCommand(WRITE_MEMORY_START);
morita 0:c0be4e018a09 389
morita 0:c0be4e018a09 390 for (pixels = 0; pixels < (((x1 - x0) + 1) * ((y1 - y0) + 1)); pixels++)
morita 0:c0be4e018a09 391 lcdWriteData(colour >> 8, colour);;
morita 0:c0be4e018a09 392 }
morita 0:c0be4e018a09 393
morita 0:c0be4e018a09 394 // Draw a circle
morita 0:c0be4e018a09 395 // Note: This is another version of Bresenham's line drawing algorithm.
morita 0:c0be4e018a09 396 // There's plenty of documentation on the web if you are curious
morita 0:c0be4e018a09 397 // how this works.
rs27 1:b64c81071d96 398 void ILI9163::lcdCircle(int16_t xCentre, int16_t yCentre, int16_t radius, uint16_t colour)
morita 0:c0be4e018a09 399 {
morita 0:c0be4e018a09 400 int16_t x = 0, y = radius;
morita 0:c0be4e018a09 401 int16_t d = 3 - (2 * radius);
morita 0:c0be4e018a09 402
morita 0:c0be4e018a09 403 while(x <= y)
morita 0:c0be4e018a09 404 {
morita 0:c0be4e018a09 405 lcdPlot(xCentre + x, yCentre + y, colour);
morita 0:c0be4e018a09 406 lcdPlot(xCentre + y, yCentre + x, colour);
morita 0:c0be4e018a09 407 lcdPlot(xCentre - x, yCentre + y, colour);
morita 0:c0be4e018a09 408 lcdPlot(xCentre + y, yCentre - x, colour);
morita 0:c0be4e018a09 409 lcdPlot(xCentre - x, yCentre - y, colour);
morita 0:c0be4e018a09 410 lcdPlot(xCentre - y, yCentre - x, colour);
morita 0:c0be4e018a09 411 lcdPlot(xCentre + x, yCentre - y, colour);
morita 0:c0be4e018a09 412 lcdPlot(xCentre - y, yCentre + x, colour);
morita 0:c0be4e018a09 413
morita 0:c0be4e018a09 414 if (d < 0) d += (4 * x) + 6;
morita 0:c0be4e018a09 415 else
morita 0:c0be4e018a09 416 {
morita 0:c0be4e018a09 417 d += (4 * (x - y)) + 10;
morita 0:c0be4e018a09 418 y -= 1;
morita 0:c0be4e018a09 419 }
morita 0:c0be4e018a09 420
morita 0:c0be4e018a09 421 x++;
morita 0:c0be4e018a09 422 }
morita 0:c0be4e018a09 423 }
morita 0:c0be4e018a09 424
morita 0:c0be4e018a09 425 // LCD text manipulation functions --------------------------------------------------------------------------
morita 0:c0be4e018a09 426
rs27 1:b64c81071d96 427 // die Schriftgröße definieren
rs27 1:b64c81071d96 428 void ILI9163::set_font(unsigned char* f) {
rs27 1:b64c81071d96 429 font = f;
rs27 1:b64c81071d96 430 font_bp_char = font[0]; // Anzal der Bytes pro Zeichen
rs27 1:b64c81071d96 431 font_hor = font[1]; // get hor size of font
rs27 1:b64c81071d96 432 font_vert = font[2]; // get vert size of font
rs27 1:b64c81071d96 433 font_bp_line = font[3]; // bytes per line
rs27 1:b64c81071d96 434 }
rs27 1:b64c81071d96 435
morita 0:c0be4e018a09 436 // Plot a character at the specified x, y co-ordinates (top left hand corner of character)
rs27 1:b64c81071d96 437 void ILI9163::lcdPutCh(unsigned char c, uint8_t x, uint8_t y, uint16_t fgColour, uint16_t bgColour)
morita 0:c0be4e018a09 438 {
morita 0:c0be4e018a09 439 uint8_t row, column;
rs27 1:b64c81071d96 440 uint16_t sign;
rs27 1:b64c81071d96 441 unsigned char z,w;
rs27 1:b64c81071d96 442 unsigned int j,i,b;
rs27 1:b64c81071d96 443
rs27 1:b64c81071d96 444 if ((c < 31) || (c > 127)) return; // auf druckbares Zeichen prüfen
morita 0:c0be4e018a09 445
morita 0:c0be4e018a09 446 // To speed up plotting we define a x window of 6 pixels and then
morita 0:c0be4e018a09 447 // write out one row at a time. This means the LCD will correctly
morita 0:c0be4e018a09 448 // update the memory pointer saving us a good few bytes
morita 0:c0be4e018a09 449
morita 0:c0be4e018a09 450 lcdWriteCommand(SET_COLUMN_ADDRESS); // Horizontal Address Start Position
morita 0:c0be4e018a09 451 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 452 lcdWriteParameter(x);
morita 0:c0be4e018a09 453 lcdWriteParameter(0x00);
rs27 1:b64c81071d96 454 lcdWriteParameter(x+font_hor-1); // x + w -1 >> XEnd
morita 0:c0be4e018a09 455
morita 0:c0be4e018a09 456 lcdWriteCommand(SET_PAGE_ADDRESS); // Vertical Address end Position
morita 0:c0be4e018a09 457 lcdWriteParameter(0x00);
morita 0:c0be4e018a09 458 lcdWriteParameter(y);
morita 0:c0be4e018a09 459 lcdWriteParameter(0x00);
rs27 1:b64c81071d96 460 lcdWriteParameter(y+font_vert-1); // y + h -1 >> YEnd 0x7F
morita 0:c0be4e018a09 461
morita 0:c0be4e018a09 462 lcdWriteCommand(WRITE_MEMORY_START);
morita 0:c0be4e018a09 463
rs27 1:b64c81071d96 464 sign = (((c -32) * font_bp_char) + 4); // start of char bitmap
rs27 1:b64c81071d96 465
morita 0:c0be4e018a09 466 // Plot the font data
rs27 1:b64c81071d96 467 for (j=0; j<font_vert; j++) { // vert line
rs27 1:b64c81071d96 468 for (i=0; i<font_hor; i++) { // horz line
rs27 1:b64c81071d96 469 z = font[sign + (font_bp_line * i) + ((j & 0xF8) >> 3)+1];
rs27 1:b64c81071d96 470 b = 1 << (j & 0x07);
rs27 1:b64c81071d96 471 if (( z & b ) == 0x00) lcdWriteData(fgColour >> 8, fgColour);
rs27 1:b64c81071d96 472 else lcdWriteData(bgColour >> 8, bgColour);
rs27 1:b64c81071d96 473 }
rs27 1:b64c81071d96 474 }
rs27 1:b64c81071d96 475
rs27 1:b64c81071d96 476 /*
rs27 1:b64c81071d96 477 // Plot the font data
rs27 1:b64c81071d96 478 for (row = 0; row < font_vert; row++)
morita 0:c0be4e018a09 479 {
rs27 1:b64c81071d96 480 for (column = 0; column < font_hor; column++)
morita 0:c0be4e018a09 481 {
rs27 1:b64c81071d96 482 if ((font[sign + column]) & (1 << row))
morita 0:c0be4e018a09 483 lcdWriteData(fgColour >> 8, fgColour);
morita 0:c0be4e018a09 484 else lcdWriteData(bgColour >> 8, bgColour);
morita 0:c0be4e018a09 485 }
morita 0:c0be4e018a09 486 }
rs27 1:b64c81071d96 487 */
morita 0:c0be4e018a09 488 }
morita 0:c0be4e018a09 489
morita 0:c0be4e018a09 490 // Plot a string of characters to the LCD
rs27 1:b64c81071d96 491 void ILI9163::lcdPutS(const char *string, uint8_t x, uint8_t y, uint16_t fgColour, uint16_t bgColour)
morita 0:c0be4e018a09 492 {
morita 0:c0be4e018a09 493 uint8_t origin = x;
morita 0:c0be4e018a09 494
morita 0:c0be4e018a09 495 for (uint8_t characterNumber = 0; characterNumber < strlen(string); characterNumber++)
morita 0:c0be4e018a09 496 {
morita 0:c0be4e018a09 497 // Check if we are out of bounds and move to
morita 0:c0be4e018a09 498 // the next line if we are
rs27 1:b64c81071d96 499 if (x > (127 - font_vert))
morita 0:c0be4e018a09 500 {
morita 0:c0be4e018a09 501 x = origin;
rs27 1:b64c81071d96 502 y += font_vert;
morita 0:c0be4e018a09 503 }
morita 0:c0be4e018a09 504
morita 0:c0be4e018a09 505 // If we move past the bottom of the screen just exit
madhusudhana 3:7d97b4f535c6 506 if (y > (159 - font_hor)) break;
morita 0:c0be4e018a09 507
morita 0:c0be4e018a09 508 // Plot the current character
morita 0:c0be4e018a09 509 lcdPutCh(string[characterNumber], x, y, fgColour, bgColour);
rs27 1:b64c81071d96 510 x += font_hor;
morita 0:c0be4e018a09 511 }
morita 0:c0be4e018a09 512 }