output

Dependencies:   images mbed

Fork of display by madhu sudhana

Committer:
madhusudhana
Date:
Wed Jun 13 11:34:37 2018 +0000
Revision:
2:e7a5b9bc75b4
Parent:
1:b64c81071d96
Child:
3:7d97b4f535c6
1.44 tft display;

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