ILI9340_Driver_Lib
ILI9340_Driver.cpp@1:216d35e347b8, 2014-06-01 (annotated)
- Committer:
- dextorslabs
- Date:
- Sun Jun 01 16:52:16 2014 +0000
- Revision:
- 1:216d35e347b8
- Parent:
- 0:ea46340642a9
- Child:
- 2:6b2fd4ba0032
Added more graphics functions to give rectangles with rounded corners and filled circles.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dextorslabs | 0:ea46340642a9 | 1 | /*************************************************************** |
dextorslabs | 1:216d35e347b8 | 2 | ILI9340_Driver v1.1 01.06.14 Ian Weston |
dextorslabs | 0:ea46340642a9 | 3 | |
dextorslabs | 0:ea46340642a9 | 4 | Driver and integrated graphics library for displays that use the |
dextorslabs | 1:216d35e347b8 | 5 | ILI9340 controller in SPI mode. Such as the Adafruit 2.2" display. |
dextorslabs | 0:ea46340642a9 | 6 | |
dextorslabs | 1:216d35e347b8 | 7 | This code has been ported from several sources. The driver section |
dextorslabs | 0:ea46340642a9 | 8 | was completely ported from the Adafruits Arduino source code, and |
dextorslabs | 0:ea46340642a9 | 9 | the graphics functions were ported from the Adafruits GFX library |
dextorslabs | 0:ea46340642a9 | 10 | and some elements were ported from code by Elmicros seeduio port. |
dextorslabs | 0:ea46340642a9 | 11 | |
dextorslabs | 0:ea46340642a9 | 12 | Future revisions will include more advanced graphics functions. |
dextorslabs | 0:ea46340642a9 | 13 | |
dextorslabs | 0:ea46340642a9 | 14 | ***************************************************************/ |
dextorslabs | 0:ea46340642a9 | 15 | |
dextorslabs | 0:ea46340642a9 | 16 | |
dextorslabs | 0:ea46340642a9 | 17 | #include "mbed.h" |
dextorslabs | 0:ea46340642a9 | 18 | #include "ILI9340_Driver.h" |
dextorslabs | 0:ea46340642a9 | 19 | #include "SimpleFont.cpp" |
dextorslabs | 0:ea46340642a9 | 20 | |
dextorslabs | 0:ea46340642a9 | 21 | |
dextorslabs | 1:216d35e347b8 | 22 | |
dextorslabs | 1:216d35e347b8 | 23 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 24 | // Constructor, assigns the pins to the SPI object, set orientation, and sets screen dims. |
dextorslabs | 1:216d35e347b8 | 25 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 26 | ILI9340_Display::ILI9340_Display(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rst, PinName dc) |
dextorslabs | 0:ea46340642a9 | 27 | : spi(mosi, miso, sclk), cs(cs), rst(rst), dc(dc) { |
dextorslabs | 0:ea46340642a9 | 28 | _height = _TFTHEIGHT; |
dextorslabs | 0:ea46340642a9 | 29 | _width = _TFTWIDTH; |
dextorslabs | 0:ea46340642a9 | 30 | orientation = 0; |
dextorslabs | 0:ea46340642a9 | 31 | } |
dextorslabs | 0:ea46340642a9 | 32 | |
dextorslabs | 0:ea46340642a9 | 33 | |
dextorslabs | 1:216d35e347b8 | 34 | |
dextorslabs | 1:216d35e347b8 | 35 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 36 | // Command writing code |
dextorslabs | 1:216d35e347b8 | 37 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 38 | void ILI9340_Display::WriteCommand(uint8_t command) { |
dextorslabs | 0:ea46340642a9 | 39 | dc = 0; |
dextorslabs | 0:ea46340642a9 | 40 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 41 | spi.write(command); |
dextorslabs | 0:ea46340642a9 | 42 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 43 | } |
dextorslabs | 0:ea46340642a9 | 44 | |
dextorslabs | 1:216d35e347b8 | 45 | |
dextorslabs | 1:216d35e347b8 | 46 | |
dextorslabs | 1:216d35e347b8 | 47 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 48 | // Data writing code |
dextorslabs | 1:216d35e347b8 | 49 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 50 | void ILI9340_Display::WriteData(uint8_t data) { |
dextorslabs | 0:ea46340642a9 | 51 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 52 | dc = 1; |
dextorslabs | 0:ea46340642a9 | 53 | spi.write(data); |
dextorslabs | 0:ea46340642a9 | 54 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 55 | } |
dextorslabs | 0:ea46340642a9 | 56 | |
dextorslabs | 0:ea46340642a9 | 57 | |
dextorslabs | 1:216d35e347b8 | 58 | |
dextorslabs | 1:216d35e347b8 | 59 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 60 | // Initilise the display |
dextorslabs | 1:216d35e347b8 | 61 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 62 | void ILI9340_Display::DispInit(void) { |
dextorslabs | 0:ea46340642a9 | 63 | //CtrlOutput(); |
dextorslabs | 0:ea46340642a9 | 64 | |
dextorslabs | 0:ea46340642a9 | 65 | rst = 0; |
dextorslabs | 0:ea46340642a9 | 66 | |
dextorslabs | 0:ea46340642a9 | 67 | // Setup the spi for 8 bit data, high steady state clock, |
dextorslabs | 0:ea46340642a9 | 68 | // second edge capture, with a 1MHz clock rate |
dextorslabs | 0:ea46340642a9 | 69 | //spi.format(8,3); |
dextorslabs | 0:ea46340642a9 | 70 | spi.frequency(24000000); // actually seems to work up to about 20Mhz... way better than the 8mhz as std. |
dextorslabs | 0:ea46340642a9 | 71 | |
dextorslabs | 0:ea46340642a9 | 72 | // Toggle rst to reset |
dextorslabs | 0:ea46340642a9 | 73 | rst = 1; |
dextorslabs | 0:ea46340642a9 | 74 | wait(0.005); |
dextorslabs | 0:ea46340642a9 | 75 | rst = 0; |
dextorslabs | 0:ea46340642a9 | 76 | wait(0.020); |
dextorslabs | 0:ea46340642a9 | 77 | rst = 1; |
dextorslabs | 0:ea46340642a9 | 78 | wait(0.150); |
dextorslabs | 0:ea46340642a9 | 79 | |
dextorslabs | 0:ea46340642a9 | 80 | WriteCommand(0xEF); |
dextorslabs | 0:ea46340642a9 | 81 | WriteData(0x03); |
dextorslabs | 0:ea46340642a9 | 82 | WriteData(0x80); |
dextorslabs | 0:ea46340642a9 | 83 | WriteData(0x02); |
dextorslabs | 0:ea46340642a9 | 84 | |
dextorslabs | 0:ea46340642a9 | 85 | WriteCommand(0xCF); |
dextorslabs | 0:ea46340642a9 | 86 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 87 | WriteData(0xC1); |
dextorslabs | 0:ea46340642a9 | 88 | WriteData(0x30); |
dextorslabs | 0:ea46340642a9 | 89 | |
dextorslabs | 0:ea46340642a9 | 90 | WriteCommand(0xED); |
dextorslabs | 0:ea46340642a9 | 91 | WriteData(0x64); |
dextorslabs | 0:ea46340642a9 | 92 | WriteData(0x03); |
dextorslabs | 0:ea46340642a9 | 93 | WriteData(0x12); |
dextorslabs | 0:ea46340642a9 | 94 | WriteData(0x81); |
dextorslabs | 0:ea46340642a9 | 95 | |
dextorslabs | 0:ea46340642a9 | 96 | WriteCommand(0xE8); |
dextorslabs | 0:ea46340642a9 | 97 | WriteData(0x85); |
dextorslabs | 0:ea46340642a9 | 98 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 99 | WriteData(0x78); |
dextorslabs | 0:ea46340642a9 | 100 | |
dextorslabs | 0:ea46340642a9 | 101 | WriteCommand(0xCB); |
dextorslabs | 0:ea46340642a9 | 102 | WriteData(0x39); |
dextorslabs | 0:ea46340642a9 | 103 | WriteData(0x2C); |
dextorslabs | 0:ea46340642a9 | 104 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 105 | WriteData(0x34); |
dextorslabs | 0:ea46340642a9 | 106 | WriteData(0x02); |
dextorslabs | 0:ea46340642a9 | 107 | |
dextorslabs | 0:ea46340642a9 | 108 | WriteCommand(0xF7); |
dextorslabs | 0:ea46340642a9 | 109 | WriteData(0x20); |
dextorslabs | 0:ea46340642a9 | 110 | |
dextorslabs | 0:ea46340642a9 | 111 | WriteCommand(0xEA); |
dextorslabs | 0:ea46340642a9 | 112 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 113 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 114 | |
dextorslabs | 0:ea46340642a9 | 115 | WriteCommand(ILI9340_PWCTR1); //Power control |
dextorslabs | 0:ea46340642a9 | 116 | WriteData(0x23); //VRH[5:0] |
dextorslabs | 0:ea46340642a9 | 117 | |
dextorslabs | 0:ea46340642a9 | 118 | WriteCommand(ILI9340_PWCTR2); //Power control |
dextorslabs | 0:ea46340642a9 | 119 | WriteData(0x10); //SAP[2:0];BT[3:0] |
dextorslabs | 0:ea46340642a9 | 120 | |
dextorslabs | 0:ea46340642a9 | 121 | WriteCommand(ILI9340_VMCTR1); //VCM control |
dextorslabs | 0:ea46340642a9 | 122 | WriteData(0x3e); //�Աȶȵ��� |
dextorslabs | 0:ea46340642a9 | 123 | WriteData(0x28); |
dextorslabs | 0:ea46340642a9 | 124 | |
dextorslabs | 0:ea46340642a9 | 125 | WriteCommand(ILI9340_VMCTR2); //VCM control2 |
dextorslabs | 0:ea46340642a9 | 126 | WriteData(0x86); //-- |
dextorslabs | 0:ea46340642a9 | 127 | |
dextorslabs | 0:ea46340642a9 | 128 | WriteCommand(ILI9340_MADCTL); // Memory Access Control |
dextorslabs | 0:ea46340642a9 | 129 | WriteData(ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); |
dextorslabs | 0:ea46340642a9 | 130 | |
dextorslabs | 0:ea46340642a9 | 131 | WriteCommand(ILI9340_PIXFMT); |
dextorslabs | 0:ea46340642a9 | 132 | WriteData(0x55); |
dextorslabs | 0:ea46340642a9 | 133 | |
dextorslabs | 0:ea46340642a9 | 134 | WriteCommand(ILI9340_FRMCTR1); |
dextorslabs | 0:ea46340642a9 | 135 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 136 | WriteData(0x18); |
dextorslabs | 0:ea46340642a9 | 137 | |
dextorslabs | 0:ea46340642a9 | 138 | WriteCommand(ILI9340_DFUNCTR); // Display Function Control |
dextorslabs | 0:ea46340642a9 | 139 | WriteData(0x08); |
dextorslabs | 0:ea46340642a9 | 140 | WriteData(0x82); |
dextorslabs | 0:ea46340642a9 | 141 | WriteData(0x27); |
dextorslabs | 0:ea46340642a9 | 142 | |
dextorslabs | 0:ea46340642a9 | 143 | WriteCommand(0xF2); // 3Gamma Function Disable |
dextorslabs | 0:ea46340642a9 | 144 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 145 | |
dextorslabs | 0:ea46340642a9 | 146 | WriteCommand(ILI9340_GAMMASET); //Gamma curve selected |
dextorslabs | 0:ea46340642a9 | 147 | WriteData(0x01); |
dextorslabs | 0:ea46340642a9 | 148 | |
dextorslabs | 0:ea46340642a9 | 149 | WriteCommand(ILI9340_GMCTRP1); //Set Gamma |
dextorslabs | 0:ea46340642a9 | 150 | WriteData(0x0F); |
dextorslabs | 0:ea46340642a9 | 151 | WriteData(0x31); |
dextorslabs | 0:ea46340642a9 | 152 | WriteData(0x2B); |
dextorslabs | 0:ea46340642a9 | 153 | WriteData(0x0C); |
dextorslabs | 0:ea46340642a9 | 154 | WriteData(0x0E); |
dextorslabs | 0:ea46340642a9 | 155 | WriteData(0x08); |
dextorslabs | 0:ea46340642a9 | 156 | WriteData(0x4E); |
dextorslabs | 0:ea46340642a9 | 157 | WriteData(0xF1); |
dextorslabs | 0:ea46340642a9 | 158 | WriteData(0x37); |
dextorslabs | 0:ea46340642a9 | 159 | WriteData(0x07); |
dextorslabs | 0:ea46340642a9 | 160 | WriteData(0x10); |
dextorslabs | 0:ea46340642a9 | 161 | WriteData(0x03); |
dextorslabs | 0:ea46340642a9 | 162 | WriteData(0x0E); |
dextorslabs | 0:ea46340642a9 | 163 | WriteData(0x09); |
dextorslabs | 0:ea46340642a9 | 164 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 165 | |
dextorslabs | 0:ea46340642a9 | 166 | WriteCommand(ILI9340_GMCTRN1); //Set Gamma |
dextorslabs | 0:ea46340642a9 | 167 | WriteData(0x00); |
dextorslabs | 0:ea46340642a9 | 168 | WriteData(0x0E); |
dextorslabs | 0:ea46340642a9 | 169 | WriteData(0x14); |
dextorslabs | 0:ea46340642a9 | 170 | WriteData(0x03); |
dextorslabs | 0:ea46340642a9 | 171 | WriteData(0x11); |
dextorslabs | 0:ea46340642a9 | 172 | WriteData(0x07); |
dextorslabs | 0:ea46340642a9 | 173 | WriteData(0x31); |
dextorslabs | 0:ea46340642a9 | 174 | WriteData(0xC1); |
dextorslabs | 0:ea46340642a9 | 175 | WriteData(0x48); |
dextorslabs | 0:ea46340642a9 | 176 | WriteData(0x08); |
dextorslabs | 0:ea46340642a9 | 177 | WriteData(0x0F); |
dextorslabs | 0:ea46340642a9 | 178 | WriteData(0x0C); |
dextorslabs | 0:ea46340642a9 | 179 | WriteData(0x31); |
dextorslabs | 0:ea46340642a9 | 180 | WriteData(0x36); |
dextorslabs | 0:ea46340642a9 | 181 | WriteData(0x0F); |
dextorslabs | 0:ea46340642a9 | 182 | |
dextorslabs | 0:ea46340642a9 | 183 | WriteCommand(ILI9340_SLPOUT); //Exit Sleep |
dextorslabs | 0:ea46340642a9 | 184 | wait(0.120); |
dextorslabs | 0:ea46340642a9 | 185 | WriteCommand(ILI9340_DISPON); //Display on |
dextorslabs | 0:ea46340642a9 | 186 | |
dextorslabs | 0:ea46340642a9 | 187 | } |
dextorslabs | 0:ea46340642a9 | 188 | |
dextorslabs | 0:ea46340642a9 | 189 | |
dextorslabs | 1:216d35e347b8 | 190 | |
dextorslabs | 1:216d35e347b8 | 191 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 192 | // Sets the rotation of the display |
dextorslabs | 1:216d35e347b8 | 193 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 194 | void ILI9340_Display::SetRotation(uint8_t m) { |
dextorslabs | 0:ea46340642a9 | 195 | |
dextorslabs | 0:ea46340642a9 | 196 | WriteCommand(ILI9340_MADCTL); |
dextorslabs | 0:ea46340642a9 | 197 | orientation = m % 4; // can't be higher than 3 |
dextorslabs | 0:ea46340642a9 | 198 | |
dextorslabs | 0:ea46340642a9 | 199 | switch (orientation) { |
dextorslabs | 0:ea46340642a9 | 200 | case 0: |
dextorslabs | 0:ea46340642a9 | 201 | WriteData(ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); |
dextorslabs | 0:ea46340642a9 | 202 | _width = _TFTWIDTH; |
dextorslabs | 0:ea46340642a9 | 203 | _height = _TFTHEIGHT; |
dextorslabs | 0:ea46340642a9 | 204 | break; |
dextorslabs | 0:ea46340642a9 | 205 | case 1: |
dextorslabs | 0:ea46340642a9 | 206 | WriteData(ILI9340_MADCTL_MV | ILI9340_MADCTL_BGR); |
dextorslabs | 0:ea46340642a9 | 207 | _width = _TFTHEIGHT; |
dextorslabs | 0:ea46340642a9 | 208 | _height = _TFTWIDTH; |
dextorslabs | 0:ea46340642a9 | 209 | break; |
dextorslabs | 0:ea46340642a9 | 210 | case 2: |
dextorslabs | 0:ea46340642a9 | 211 | WriteData(ILI9340_MADCTL_MY | ILI9340_MADCTL_BGR); |
dextorslabs | 0:ea46340642a9 | 212 | _width = _TFTWIDTH; |
dextorslabs | 0:ea46340642a9 | 213 | _height = _TFTHEIGHT; |
dextorslabs | 0:ea46340642a9 | 214 | break; |
dextorslabs | 0:ea46340642a9 | 215 | case 3: |
dextorslabs | 0:ea46340642a9 | 216 | WriteData(ILI9340_MADCTL_MV | ILI9340_MADCTL_MY | ILI9340_MADCTL_MX | ILI9340_MADCTL_BGR); |
dextorslabs | 0:ea46340642a9 | 217 | _width = _TFTHEIGHT; |
dextorslabs | 0:ea46340642a9 | 218 | _height = _TFTWIDTH; |
dextorslabs | 0:ea46340642a9 | 219 | break; |
dextorslabs | 0:ea46340642a9 | 220 | } |
dextorslabs | 0:ea46340642a9 | 221 | } |
dextorslabs | 0:ea46340642a9 | 222 | |
dextorslabs | 0:ea46340642a9 | 223 | |
dextorslabs | 1:216d35e347b8 | 224 | |
dextorslabs | 1:216d35e347b8 | 225 | |
dextorslabs | 1:216d35e347b8 | 226 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 227 | // Invert the colours of the display in hardware |
dextorslabs | 1:216d35e347b8 | 228 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 229 | void ILI9340_Display::InvertDisplay(bool i) { |
dextorslabs | 0:ea46340642a9 | 230 | WriteCommand(i ? ILI9340_INVON : ILI9340_INVOFF); |
dextorslabs | 0:ea46340642a9 | 231 | } |
dextorslabs | 0:ea46340642a9 | 232 | |
dextorslabs | 0:ea46340642a9 | 233 | |
dextorslabs | 1:216d35e347b8 | 234 | |
dextorslabs | 1:216d35e347b8 | 235 | |
dextorslabs | 1:216d35e347b8 | 236 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 237 | // Set address window for writing data to. |
dextorslabs | 1:216d35e347b8 | 238 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 239 | void ILI9340_Display::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { |
dextorslabs | 0:ea46340642a9 | 240 | |
dextorslabs | 0:ea46340642a9 | 241 | WriteCommand(ILI9340_CASET); // Column addr set |
dextorslabs | 0:ea46340642a9 | 242 | WriteData(x0 >> 8); |
dextorslabs | 0:ea46340642a9 | 243 | WriteData(x0 & 0xFF); // XSTART |
dextorslabs | 0:ea46340642a9 | 244 | WriteData(x1 >> 8); |
dextorslabs | 0:ea46340642a9 | 245 | WriteData(x1 & 0xFF); // XEND |
dextorslabs | 0:ea46340642a9 | 246 | |
dextorslabs | 0:ea46340642a9 | 247 | WriteCommand(ILI9340_PASET); // Row addr set |
dextorslabs | 0:ea46340642a9 | 248 | WriteData(y0>>8); |
dextorslabs | 0:ea46340642a9 | 249 | WriteData(y0); // YSTART |
dextorslabs | 0:ea46340642a9 | 250 | WriteData(y1>>8); |
dextorslabs | 0:ea46340642a9 | 251 | WriteData(y1); // YEND |
dextorslabs | 0:ea46340642a9 | 252 | |
dextorslabs | 0:ea46340642a9 | 253 | WriteCommand(ILI9340_RAMWR); // write to RAM |
dextorslabs | 0:ea46340642a9 | 254 | } |
dextorslabs | 0:ea46340642a9 | 255 | |
dextorslabs | 0:ea46340642a9 | 256 | |
dextorslabs | 0:ea46340642a9 | 257 | |
dextorslabs | 1:216d35e347b8 | 258 | |
dextorslabs | 1:216d35e347b8 | 259 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 260 | // To draw the humble pixel |
dextorslabs | 1:216d35e347b8 | 261 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 262 | void ILI9340_Display::DrawPixel(uint16_t x, uint16_t y, uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 263 | if((x < 1) ||(x >= _width) || (y < 1) || (y >= _height)) return; |
dextorslabs | 0:ea46340642a9 | 264 | |
dextorslabs | 0:ea46340642a9 | 265 | SetAddrWindow(x,y,x+1,y+1); |
dextorslabs | 0:ea46340642a9 | 266 | |
dextorslabs | 0:ea46340642a9 | 267 | dc = 1; |
dextorslabs | 0:ea46340642a9 | 268 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 269 | |
dextorslabs | 0:ea46340642a9 | 270 | spi.write(colour >> 8); |
dextorslabs | 0:ea46340642a9 | 271 | spi.write(colour); |
dextorslabs | 0:ea46340642a9 | 272 | |
dextorslabs | 0:ea46340642a9 | 273 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 274 | } |
dextorslabs | 0:ea46340642a9 | 275 | |
dextorslabs | 0:ea46340642a9 | 276 | |
dextorslabs | 1:216d35e347b8 | 277 | |
dextorslabs | 1:216d35e347b8 | 278 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 279 | // Fill the screen with a colour |
dextorslabs | 1:216d35e347b8 | 280 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 281 | void ILI9340_Display::FillScreen(uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 282 | SetAddrWindow(0,0,_width,_height); |
dextorslabs | 0:ea46340642a9 | 283 | |
dextorslabs | 0:ea46340642a9 | 284 | dc = 1; |
dextorslabs | 0:ea46340642a9 | 285 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 286 | |
dextorslabs | 0:ea46340642a9 | 287 | unsigned int total = _width * _height; |
dextorslabs | 0:ea46340642a9 | 288 | unsigned int position = 0; |
dextorslabs | 0:ea46340642a9 | 289 | |
dextorslabs | 0:ea46340642a9 | 290 | while (position < total) { |
dextorslabs | 0:ea46340642a9 | 291 | spi.write(colour >> 8); |
dextorslabs | 0:ea46340642a9 | 292 | spi.write(colour); |
dextorslabs | 0:ea46340642a9 | 293 | position++; |
dextorslabs | 0:ea46340642a9 | 294 | } |
dextorslabs | 0:ea46340642a9 | 295 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 296 | } |
dextorslabs | 0:ea46340642a9 | 297 | |
dextorslabs | 0:ea46340642a9 | 298 | |
dextorslabs | 1:216d35e347b8 | 299 | |
dextorslabs | 1:216d35e347b8 | 300 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 301 | // Draws a vertical line fast |
dextorslabs | 1:216d35e347b8 | 302 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 303 | void ILI9340_Display::DrawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 304 | |
dextorslabs | 0:ea46340642a9 | 305 | // Rudimentary clipping |
dextorslabs | 0:ea46340642a9 | 306 | if((x >= _width) || (y >= _height)) return; |
dextorslabs | 0:ea46340642a9 | 307 | |
dextorslabs | 0:ea46340642a9 | 308 | if((y+h-1) >= _height) |
dextorslabs | 0:ea46340642a9 | 309 | h = _height-y; |
dextorslabs | 0:ea46340642a9 | 310 | |
dextorslabs | 0:ea46340642a9 | 311 | SetAddrWindow(x, y, x, y+h-1); |
dextorslabs | 0:ea46340642a9 | 312 | |
dextorslabs | 0:ea46340642a9 | 313 | uint8_t hi = colour >> 8, lo = colour; |
dextorslabs | 0:ea46340642a9 | 314 | |
dextorslabs | 0:ea46340642a9 | 315 | dc = 1; |
dextorslabs | 0:ea46340642a9 | 316 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 317 | |
dextorslabs | 0:ea46340642a9 | 318 | while (h--) { |
dextorslabs | 0:ea46340642a9 | 319 | spi.write(hi); |
dextorslabs | 0:ea46340642a9 | 320 | spi.write(lo); |
dextorslabs | 0:ea46340642a9 | 321 | } |
dextorslabs | 0:ea46340642a9 | 322 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 323 | } |
dextorslabs | 0:ea46340642a9 | 324 | |
dextorslabs | 0:ea46340642a9 | 325 | |
dextorslabs | 1:216d35e347b8 | 326 | |
dextorslabs | 1:216d35e347b8 | 327 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 328 | // Draws a horizontal line fast |
dextorslabs | 1:216d35e347b8 | 329 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 330 | void ILI9340_Display::DrawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 331 | |
dextorslabs | 0:ea46340642a9 | 332 | // Rudimentary clipping |
dextorslabs | 0:ea46340642a9 | 333 | if((x >= _width) || (y >= _height)) return; |
dextorslabs | 0:ea46340642a9 | 334 | if((x+w-1) >= _height) w = _width-x; |
dextorslabs | 0:ea46340642a9 | 335 | SetAddrWindow(x, y, x+w-1, y); |
dextorslabs | 0:ea46340642a9 | 336 | |
dextorslabs | 0:ea46340642a9 | 337 | uint8_t hi = colour >> 8, lo = colour; |
dextorslabs | 0:ea46340642a9 | 338 | dc = 1; |
dextorslabs | 0:ea46340642a9 | 339 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 340 | while (w--) { |
dextorslabs | 0:ea46340642a9 | 341 | spi.write(hi); |
dextorslabs | 0:ea46340642a9 | 342 | spi.write(lo); |
dextorslabs | 0:ea46340642a9 | 343 | } |
dextorslabs | 0:ea46340642a9 | 344 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 345 | } |
dextorslabs | 0:ea46340642a9 | 346 | |
dextorslabs | 0:ea46340642a9 | 347 | |
dextorslabs | 1:216d35e347b8 | 348 | |
dextorslabs | 1:216d35e347b8 | 349 | |
dextorslabs | 1:216d35e347b8 | 350 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 351 | // Draws a filled rectangle |
dextorslabs | 1:216d35e347b8 | 352 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 353 | void ILI9340_Display::FillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 354 | |
dextorslabs | 0:ea46340642a9 | 355 | // rudimentary clipping (drawChar w/big text requires this) |
dextorslabs | 0:ea46340642a9 | 356 | if((x >= _width) || (y >= _height)) return; |
dextorslabs | 0:ea46340642a9 | 357 | if((x + w - 1) >= _width) w = _width - x; |
dextorslabs | 0:ea46340642a9 | 358 | if((y + h - 1) >= _height) h = _height - y; |
dextorslabs | 0:ea46340642a9 | 359 | |
dextorslabs | 0:ea46340642a9 | 360 | SetAddrWindow(x, y, x+w-1, y+h-1); |
dextorslabs | 0:ea46340642a9 | 361 | |
dextorslabs | 0:ea46340642a9 | 362 | uint8_t hi = colour >> 8, lo = colour; |
dextorslabs | 0:ea46340642a9 | 363 | |
dextorslabs | 0:ea46340642a9 | 364 | dc = 1; |
dextorslabs | 0:ea46340642a9 | 365 | cs = 0; |
dextorslabs | 0:ea46340642a9 | 366 | |
dextorslabs | 0:ea46340642a9 | 367 | for(y=h; y>0; y--) { |
dextorslabs | 0:ea46340642a9 | 368 | for(x=w; x>0; x--) { |
dextorslabs | 0:ea46340642a9 | 369 | spi.write(hi); |
dextorslabs | 0:ea46340642a9 | 370 | spi.write(lo); |
dextorslabs | 0:ea46340642a9 | 371 | } |
dextorslabs | 0:ea46340642a9 | 372 | } |
dextorslabs | 0:ea46340642a9 | 373 | cs = 1; |
dextorslabs | 0:ea46340642a9 | 374 | } |
dextorslabs | 0:ea46340642a9 | 375 | |
dextorslabs | 0:ea46340642a9 | 376 | |
dextorslabs | 0:ea46340642a9 | 377 | |
dextorslabs | 1:216d35e347b8 | 378 | |
dextorslabs | 1:216d35e347b8 | 379 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 380 | // Draw an unfilled rectangle |
dextorslabs | 1:216d35e347b8 | 381 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 382 | void ILI9340_Display::DrawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color){ |
dextorslabs | 0:ea46340642a9 | 383 | DrawFastHLine(x, y, w, color); |
dextorslabs | 0:ea46340642a9 | 384 | DrawFastHLine(x, y+h-1, w, color); |
dextorslabs | 0:ea46340642a9 | 385 | DrawFastVLine(x, y, h, color); |
dextorslabs | 0:ea46340642a9 | 386 | DrawFastVLine(x+w-1, y, h, color); |
dextorslabs | 0:ea46340642a9 | 387 | } |
dextorslabs | 0:ea46340642a9 | 388 | |
dextorslabs | 0:ea46340642a9 | 389 | |
dextorslabs | 1:216d35e347b8 | 390 | |
dextorslabs | 1:216d35e347b8 | 391 | |
dextorslabs | 1:216d35e347b8 | 392 | |
dextorslabs | 1:216d35e347b8 | 393 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 394 | // draw an unfilled circle |
dextorslabs | 1:216d35e347b8 | 395 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 396 | void ILI9340_Display::DrawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t colour){ |
dextorslabs | 0:ea46340642a9 | 397 | int16_t f = 1 - r; |
dextorslabs | 0:ea46340642a9 | 398 | int16_t ddF_x = 1; |
dextorslabs | 0:ea46340642a9 | 399 | int16_t ddF_y = -2 * r; |
dextorslabs | 0:ea46340642a9 | 400 | int16_t x = 0; |
dextorslabs | 0:ea46340642a9 | 401 | int16_t y = r; |
dextorslabs | 0:ea46340642a9 | 402 | |
dextorslabs | 0:ea46340642a9 | 403 | DrawPixel(x0 , y0+r, colour); |
dextorslabs | 0:ea46340642a9 | 404 | DrawPixel(x0 , y0-r, colour); |
dextorslabs | 0:ea46340642a9 | 405 | DrawPixel(x0+r, y0 , colour); |
dextorslabs | 0:ea46340642a9 | 406 | DrawPixel(x0-r, y0 , colour); |
dextorslabs | 0:ea46340642a9 | 407 | |
dextorslabs | 0:ea46340642a9 | 408 | while (x<y) { |
dextorslabs | 0:ea46340642a9 | 409 | if (f >= 0) { |
dextorslabs | 0:ea46340642a9 | 410 | y--; |
dextorslabs | 0:ea46340642a9 | 411 | ddF_y += 2; |
dextorslabs | 0:ea46340642a9 | 412 | f += ddF_y; |
dextorslabs | 0:ea46340642a9 | 413 | } |
dextorslabs | 0:ea46340642a9 | 414 | x++; |
dextorslabs | 0:ea46340642a9 | 415 | ddF_x += 2; |
dextorslabs | 0:ea46340642a9 | 416 | f += ddF_x; |
dextorslabs | 0:ea46340642a9 | 417 | |
dextorslabs | 0:ea46340642a9 | 418 | DrawPixel(x0 + x, y0 + y, colour); |
dextorslabs | 0:ea46340642a9 | 419 | DrawPixel(x0 - x, y0 + y, colour); |
dextorslabs | 0:ea46340642a9 | 420 | DrawPixel(x0 + x, y0 - y, colour); |
dextorslabs | 0:ea46340642a9 | 421 | DrawPixel(x0 - x, y0 - y, colour); |
dextorslabs | 0:ea46340642a9 | 422 | DrawPixel(x0 + y, y0 + x, colour); |
dextorslabs | 0:ea46340642a9 | 423 | DrawPixel(x0 - y, y0 + x, colour); |
dextorslabs | 0:ea46340642a9 | 424 | DrawPixel(x0 + y, y0 - x, colour); |
dextorslabs | 0:ea46340642a9 | 425 | DrawPixel(x0 - y, y0 - x, colour); |
dextorslabs | 0:ea46340642a9 | 426 | } |
dextorslabs | 0:ea46340642a9 | 427 | } |
dextorslabs | 0:ea46340642a9 | 428 | |
dextorslabs | 0:ea46340642a9 | 429 | |
dextorslabs | 1:216d35e347b8 | 430 | |
dextorslabs | 1:216d35e347b8 | 431 | |
dextorslabs | 1:216d35e347b8 | 432 | |
dextorslabs | 1:216d35e347b8 | 433 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 434 | // Draw a filled circle |
dextorslabs | 1:216d35e347b8 | 435 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 436 | void ILI9340_Display::FillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t colour) { |
dextorslabs | 1:216d35e347b8 | 437 | DrawFastVLine(x0, y0-r, 2*r+1, colour); |
dextorslabs | 1:216d35e347b8 | 438 | FillCircleHelper(x0, y0, r, 3, 0, colour); |
dextorslabs | 1:216d35e347b8 | 439 | } |
dextorslabs | 1:216d35e347b8 | 440 | |
dextorslabs | 1:216d35e347b8 | 441 | |
dextorslabs | 1:216d35e347b8 | 442 | |
dextorslabs | 1:216d35e347b8 | 443 | |
dextorslabs | 1:216d35e347b8 | 444 | |
dextorslabs | 1:216d35e347b8 | 445 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 446 | // used to draw circles and roundrects! |
dextorslabs | 1:216d35e347b8 | 447 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 448 | void ILI9340_Display::FillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t colour) { |
dextorslabs | 1:216d35e347b8 | 449 | |
dextorslabs | 1:216d35e347b8 | 450 | int16_t f = 1 - r; |
dextorslabs | 1:216d35e347b8 | 451 | int16_t ddF_x = 1; |
dextorslabs | 1:216d35e347b8 | 452 | int16_t ddF_y = -2 * r; |
dextorslabs | 1:216d35e347b8 | 453 | int16_t x = 0; |
dextorslabs | 1:216d35e347b8 | 454 | int16_t y = r; |
dextorslabs | 1:216d35e347b8 | 455 | |
dextorslabs | 1:216d35e347b8 | 456 | while (x<y) { |
dextorslabs | 1:216d35e347b8 | 457 | if (f >= 0) { |
dextorslabs | 1:216d35e347b8 | 458 | y--; |
dextorslabs | 1:216d35e347b8 | 459 | ddF_y += 2; |
dextorslabs | 1:216d35e347b8 | 460 | f += ddF_y; |
dextorslabs | 1:216d35e347b8 | 461 | } |
dextorslabs | 1:216d35e347b8 | 462 | x++; |
dextorslabs | 1:216d35e347b8 | 463 | ddF_x += 2; |
dextorslabs | 1:216d35e347b8 | 464 | f += ddF_x; |
dextorslabs | 1:216d35e347b8 | 465 | |
dextorslabs | 1:216d35e347b8 | 466 | if (cornername & 0x1) { |
dextorslabs | 1:216d35e347b8 | 467 | DrawFastVLine(x0+x, y0-y, 2*y+1+delta, colour); |
dextorslabs | 1:216d35e347b8 | 468 | DrawFastVLine(x0+y, y0-x, 2*x+1+delta, colour); |
dextorslabs | 1:216d35e347b8 | 469 | } |
dextorslabs | 1:216d35e347b8 | 470 | if (cornername & 0x2) { |
dextorslabs | 1:216d35e347b8 | 471 | DrawFastVLine(x0-x, y0-y, 2*y+1+delta, colour); |
dextorslabs | 1:216d35e347b8 | 472 | DrawFastVLine(x0-y, y0-x, 2*x+1+delta, colour); |
dextorslabs | 1:216d35e347b8 | 473 | } |
dextorslabs | 1:216d35e347b8 | 474 | } |
dextorslabs | 1:216d35e347b8 | 475 | } |
dextorslabs | 1:216d35e347b8 | 476 | |
dextorslabs | 1:216d35e347b8 | 477 | |
dextorslabs | 1:216d35e347b8 | 478 | |
dextorslabs | 1:216d35e347b8 | 479 | |
dextorslabs | 1:216d35e347b8 | 480 | |
dextorslabs | 1:216d35e347b8 | 481 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 482 | // used for drawing rounded corner radii |
dextorslabs | 1:216d35e347b8 | 483 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 484 | void ILI9340_Display::DrawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t colour) { |
dextorslabs | 1:216d35e347b8 | 485 | int16_t f = 1 - r; |
dextorslabs | 1:216d35e347b8 | 486 | int16_t ddF_x = 1; |
dextorslabs | 1:216d35e347b8 | 487 | int16_t ddF_y = -2 * r; |
dextorslabs | 1:216d35e347b8 | 488 | int16_t x = 0; |
dextorslabs | 1:216d35e347b8 | 489 | int16_t y = r; |
dextorslabs | 1:216d35e347b8 | 490 | |
dextorslabs | 1:216d35e347b8 | 491 | while (x<y) { |
dextorslabs | 1:216d35e347b8 | 492 | if (f >= 0) { |
dextorslabs | 1:216d35e347b8 | 493 | y--; |
dextorslabs | 1:216d35e347b8 | 494 | ddF_y += 2; |
dextorslabs | 1:216d35e347b8 | 495 | f += ddF_y; |
dextorslabs | 1:216d35e347b8 | 496 | } |
dextorslabs | 1:216d35e347b8 | 497 | x++; |
dextorslabs | 1:216d35e347b8 | 498 | ddF_x += 2; |
dextorslabs | 1:216d35e347b8 | 499 | f += ddF_x; |
dextorslabs | 1:216d35e347b8 | 500 | if (cornername & 0x4) { |
dextorslabs | 1:216d35e347b8 | 501 | DrawPixel(x0 + x, y0 + y, colour); |
dextorslabs | 1:216d35e347b8 | 502 | DrawPixel(x0 + y, y0 + x, colour); |
dextorslabs | 1:216d35e347b8 | 503 | } |
dextorslabs | 1:216d35e347b8 | 504 | if (cornername & 0x2) { |
dextorslabs | 1:216d35e347b8 | 505 | DrawPixel(x0 + x, y0 - y, colour); |
dextorslabs | 1:216d35e347b8 | 506 | DrawPixel(x0 + y, y0 - x, colour); |
dextorslabs | 1:216d35e347b8 | 507 | } |
dextorslabs | 1:216d35e347b8 | 508 | if (cornername & 0x8) { |
dextorslabs | 1:216d35e347b8 | 509 | DrawPixel(x0 - y, y0 + x, colour); |
dextorslabs | 1:216d35e347b8 | 510 | DrawPixel(x0 - x, y0 + y, colour); |
dextorslabs | 1:216d35e347b8 | 511 | } |
dextorslabs | 1:216d35e347b8 | 512 | if (cornername & 0x1) { |
dextorslabs | 1:216d35e347b8 | 513 | DrawPixel(x0 - y, y0 - x, colour); |
dextorslabs | 1:216d35e347b8 | 514 | DrawPixel(x0 - x, y0 - y, colour); |
dextorslabs | 1:216d35e347b8 | 515 | } |
dextorslabs | 1:216d35e347b8 | 516 | } |
dextorslabs | 1:216d35e347b8 | 517 | } |
dextorslabs | 1:216d35e347b8 | 518 | |
dextorslabs | 1:216d35e347b8 | 519 | |
dextorslabs | 1:216d35e347b8 | 520 | |
dextorslabs | 1:216d35e347b8 | 521 | |
dextorslabs | 1:216d35e347b8 | 522 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 523 | // draw a rounded rectangle! |
dextorslabs | 1:216d35e347b8 | 524 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 525 | void ILI9340_Display::DrawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t colour) { |
dextorslabs | 1:216d35e347b8 | 526 | // smarter version |
dextorslabs | 1:216d35e347b8 | 527 | DrawFastHLine(x+r , y , w-2*r, colour); // Top |
dextorslabs | 1:216d35e347b8 | 528 | DrawFastHLine(x+r , y+h-1, w-2*r, colour); // Bottom |
dextorslabs | 1:216d35e347b8 | 529 | DrawFastVLine( x , y+r , h-2*r, colour); // Left |
dextorslabs | 1:216d35e347b8 | 530 | DrawFastVLine( x+w-1, y+r , h-2*r, colour); // Right |
dextorslabs | 1:216d35e347b8 | 531 | // draw four corners |
dextorslabs | 1:216d35e347b8 | 532 | DrawCircleHelper(x+r , y+r , r, 1, colour); |
dextorslabs | 1:216d35e347b8 | 533 | DrawCircleHelper(x+w-r-1, y+r , r, 2, colour); |
dextorslabs | 1:216d35e347b8 | 534 | DrawCircleHelper(x+w-r-1, y+h-r-1, r, 4, colour); |
dextorslabs | 1:216d35e347b8 | 535 | DrawCircleHelper(x+r , y+h-r-1, r, 8, colour); |
dextorslabs | 1:216d35e347b8 | 536 | } |
dextorslabs | 1:216d35e347b8 | 537 | |
dextorslabs | 1:216d35e347b8 | 538 | |
dextorslabs | 1:216d35e347b8 | 539 | |
dextorslabs | 1:216d35e347b8 | 540 | |
dextorslabs | 1:216d35e347b8 | 541 | |
dextorslabs | 1:216d35e347b8 | 542 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 543 | // fill a rounded rectangle! |
dextorslabs | 1:216d35e347b8 | 544 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 1:216d35e347b8 | 545 | void ILI9340_Display::FillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t colour) { |
dextorslabs | 1:216d35e347b8 | 546 | // smarter version |
dextorslabs | 1:216d35e347b8 | 547 | FillRect(x+r, y, w-2*r, h, colour); |
dextorslabs | 1:216d35e347b8 | 548 | |
dextorslabs | 1:216d35e347b8 | 549 | // draw four corners |
dextorslabs | 1:216d35e347b8 | 550 | FillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, colour); |
dextorslabs | 1:216d35e347b8 | 551 | FillCircleHelper(x+r , y+r, r, 2, h-2*r-1, colour); |
dextorslabs | 1:216d35e347b8 | 552 | } |
dextorslabs | 1:216d35e347b8 | 553 | |
dextorslabs | 1:216d35e347b8 | 554 | |
dextorslabs | 1:216d35e347b8 | 555 | |
dextorslabs | 1:216d35e347b8 | 556 | |
dextorslabs | 1:216d35e347b8 | 557 | |
dextorslabs | 1:216d35e347b8 | 558 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 559 | // Pass 8-bit (each) R,G,B, get back 16-bit packed color |
dextorslabs | 1:216d35e347b8 | 560 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 561 | uint16_t ILI9340_Display::Colour565(uint8_t r, uint8_t g, uint8_t b) { |
dextorslabs | 0:ea46340642a9 | 562 | return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3); |
dextorslabs | 0:ea46340642a9 | 563 | } |
dextorslabs | 0:ea46340642a9 | 564 | |
dextorslabs | 0:ea46340642a9 | 565 | |
dextorslabs | 1:216d35e347b8 | 566 | |
dextorslabs | 1:216d35e347b8 | 567 | |
dextorslabs | 1:216d35e347b8 | 568 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 569 | // Writes an ascii character to the display |
dextorslabs | 1:216d35e347b8 | 570 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 571 | void ILI9340_Display::DrawAscii(unsigned char ascii, uint16_t x, uint16_t y, uint16_t size, uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 572 | SetAddrWindow(x, y, x+size, y+size); |
dextorslabs | 0:ea46340642a9 | 573 | |
dextorslabs | 0:ea46340642a9 | 574 | if( (ascii < 0x20) || (ascii > 0x7e) ) //check for valid ASCII char |
dextorslabs | 0:ea46340642a9 | 575 | { |
dextorslabs | 0:ea46340642a9 | 576 | ascii = '?'; //char not supported |
dextorslabs | 0:ea46340642a9 | 577 | } |
dextorslabs | 0:ea46340642a9 | 578 | for(unsigned char i=0; i<8; i++) |
dextorslabs | 0:ea46340642a9 | 579 | { |
dextorslabs | 0:ea46340642a9 | 580 | unsigned char temp = simpleFont[ascii - 0x20][i]; |
dextorslabs | 0:ea46340642a9 | 581 | for(unsigned char f=0; f<8; f++) |
dextorslabs | 0:ea46340642a9 | 582 | { |
dextorslabs | 0:ea46340642a9 | 583 | if( (temp>>f) & 0x01 ) |
dextorslabs | 0:ea46340642a9 | 584 | { |
dextorslabs | 0:ea46340642a9 | 585 | switch(orientation) |
dextorslabs | 0:ea46340642a9 | 586 | { |
dextorslabs | 0:ea46340642a9 | 587 | case '0': |
dextorslabs | 0:ea46340642a9 | 588 | FillRect(x+f*size, y-i*size, size, size, colour); |
dextorslabs | 0:ea46340642a9 | 589 | break; |
dextorslabs | 0:ea46340642a9 | 590 | case '1': |
dextorslabs | 0:ea46340642a9 | 591 | FillRect(x-i*size, x-f*size, size, size, colour); |
dextorslabs | 0:ea46340642a9 | 592 | break; |
dextorslabs | 0:ea46340642a9 | 593 | case '2': |
dextorslabs | 0:ea46340642a9 | 594 | FillRect(x-f*size, y+i*size, size, size, colour); |
dextorslabs | 0:ea46340642a9 | 595 | break; |
dextorslabs | 0:ea46340642a9 | 596 | case '3': |
dextorslabs | 0:ea46340642a9 | 597 | default: |
dextorslabs | 0:ea46340642a9 | 598 | FillRect(x+i*size, y+f*size, size, size, colour); |
dextorslabs | 0:ea46340642a9 | 599 | } |
dextorslabs | 0:ea46340642a9 | 600 | } |
dextorslabs | 0:ea46340642a9 | 601 | } |
dextorslabs | 0:ea46340642a9 | 602 | } |
dextorslabs | 0:ea46340642a9 | 603 | } |
dextorslabs | 0:ea46340642a9 | 604 | |
dextorslabs | 0:ea46340642a9 | 605 | |
dextorslabs | 1:216d35e347b8 | 606 | |
dextorslabs | 1:216d35e347b8 | 607 | |
dextorslabs | 1:216d35e347b8 | 608 | |
dextorslabs | 1:216d35e347b8 | 609 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 610 | // Writes a character array to the display |
dextorslabs | 1:216d35e347b8 | 611 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 612 | void ILI9340_Display::DrawString(char *string, uint16_t x, uint16_t y, uint8_t size, uint16_t colour) |
dextorslabs | 0:ea46340642a9 | 613 | { |
dextorslabs | 0:ea46340642a9 | 614 | while(*string) |
dextorslabs | 0:ea46340642a9 | 615 | { |
dextorslabs | 0:ea46340642a9 | 616 | DrawAscii(*string, x, y, size, colour); |
dextorslabs | 0:ea46340642a9 | 617 | *string++; |
dextorslabs | 0:ea46340642a9 | 618 | switch(orientation) |
dextorslabs | 0:ea46340642a9 | 619 | { |
dextorslabs | 0:ea46340642a9 | 620 | case '0': |
dextorslabs | 0:ea46340642a9 | 621 | if(y > 0) y-=8*size; //Change position to next char |
dextorslabs | 0:ea46340642a9 | 622 | break; |
dextorslabs | 0:ea46340642a9 | 623 | case '1': |
dextorslabs | 0:ea46340642a9 | 624 | if(x > 0) x-=8*size; |
dextorslabs | 0:ea46340642a9 | 625 | break; |
dextorslabs | 0:ea46340642a9 | 626 | case '2': |
dextorslabs | 0:ea46340642a9 | 627 | if(y < _height) y+=8*size; |
dextorslabs | 0:ea46340642a9 | 628 | break; |
dextorslabs | 0:ea46340642a9 | 629 | case '3': |
dextorslabs | 0:ea46340642a9 | 630 | default: |
dextorslabs | 0:ea46340642a9 | 631 | if(x < _width) x+=8*size; |
dextorslabs | 0:ea46340642a9 | 632 | } |
dextorslabs | 0:ea46340642a9 | 633 | } |
dextorslabs | 0:ea46340642a9 | 634 | } |
dextorslabs | 0:ea46340642a9 | 635 | |
dextorslabs | 1:216d35e347b8 | 636 | |
dextorslabs | 1:216d35e347b8 | 637 | |
dextorslabs | 1:216d35e347b8 | 638 | |
dextorslabs | 1:216d35e347b8 | 639 | |
dextorslabs | 1:216d35e347b8 | 640 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 641 | // Converts integers into a character array |
dextorslabs | 1:216d35e347b8 | 642 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 643 | void ILI9340_Display::IntToChars (char* buffer, int value, uint8_t spaceonbuffer, uint8_t countbase, uint16_t x, uint16_t y, uint8_t size, uint16_t colour) { |
dextorslabs | 0:ea46340642a9 | 644 | int workvalue = value; |
dextorslabs | 0:ea46340642a9 | 645 | int i; |
dextorslabs | 0:ea46340642a9 | 646 | int valuetowrite; |
dextorslabs | 0:ea46340642a9 | 647 | int end_i = 0; |
dextorslabs | 0:ea46340642a9 | 648 | |
dextorslabs | 0:ea46340642a9 | 649 | if (value < 0) |
dextorslabs | 0:ea46340642a9 | 650 | { |
dextorslabs | 0:ea46340642a9 | 651 | workvalue = -value; |
dextorslabs | 0:ea46340642a9 | 652 | end_i = 1; |
dextorslabs | 0:ea46340642a9 | 653 | buffer[0] = '-'; |
dextorslabs | 0:ea46340642a9 | 654 | } |
dextorslabs | 0:ea46340642a9 | 655 | |
dextorslabs | 0:ea46340642a9 | 656 | for (i = spaceonbuffer - 1; i >= end_i; i--) |
dextorslabs | 0:ea46340642a9 | 657 | { |
dextorslabs | 0:ea46340642a9 | 658 | valuetowrite = (workvalue % countbase); |
dextorslabs | 0:ea46340642a9 | 659 | if (workvalue == 0) |
dextorslabs | 0:ea46340642a9 | 660 | { |
dextorslabs | 0:ea46340642a9 | 661 | if (i == (spaceonbuffer - 1)) |
dextorslabs | 0:ea46340642a9 | 662 | { |
dextorslabs | 0:ea46340642a9 | 663 | buffer[i] = 48; // ASCII 0 |
dextorslabs | 0:ea46340642a9 | 664 | } else { |
dextorslabs | 0:ea46340642a9 | 665 | buffer[i] = 32; // ASCII SPACE |
dextorslabs | 0:ea46340642a9 | 666 | } |
dextorslabs | 0:ea46340642a9 | 667 | } else { |
dextorslabs | 0:ea46340642a9 | 668 | if (valuetowrite > 9) |
dextorslabs | 0:ea46340642a9 | 669 | { |
dextorslabs | 0:ea46340642a9 | 670 | buffer[i] = valuetowrite + 55; // ASCII A-Z |
dextorslabs | 0:ea46340642a9 | 671 | } else { |
dextorslabs | 0:ea46340642a9 | 672 | buffer[i] = valuetowrite + 48; // ASCII of that character |
dextorslabs | 0:ea46340642a9 | 673 | } |
dextorslabs | 0:ea46340642a9 | 674 | }; |
dextorslabs | 0:ea46340642a9 | 675 | workvalue = (workvalue - valuetowrite) / countbase; |
dextorslabs | 0:ea46340642a9 | 676 | } |
dextorslabs | 0:ea46340642a9 | 677 | |
dextorslabs | 0:ea46340642a9 | 678 | DrawString(buffer, x, y, size, colour); |
dextorslabs | 0:ea46340642a9 | 679 | } |
dextorslabs | 0:ea46340642a9 | 680 | |
dextorslabs | 0:ea46340642a9 | 681 | |
dextorslabs | 0:ea46340642a9 | 682 | |
dextorslabs | 1:216d35e347b8 | 683 | |
dextorslabs | 1:216d35e347b8 | 684 | |
dextorslabs | 1:216d35e347b8 | 685 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 686 | // Functional code to swap data contents of 16bit registers |
dextorslabs | 1:216d35e347b8 | 687 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 688 | void ILI9340_Display::Swap(int16_t *a, int16_t *b) { |
dextorslabs | 0:ea46340642a9 | 689 | |
dextorslabs | 0:ea46340642a9 | 690 | int16_t x = *a; |
dextorslabs | 0:ea46340642a9 | 691 | *a = *b; |
dextorslabs | 0:ea46340642a9 | 692 | *b = x; |
dextorslabs | 0:ea46340642a9 | 693 | } |
dextorslabs | 0:ea46340642a9 | 694 | |
dextorslabs | 0:ea46340642a9 | 695 | |
dextorslabs | 1:216d35e347b8 | 696 | |
dextorslabs | 1:216d35e347b8 | 697 | |
dextorslabs | 1:216d35e347b8 | 698 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 699 | // Draws a line with any length and orientation |
dextorslabs | 1:216d35e347b8 | 700 | //////////////////////////////////////////////////////////////////////////////////////////////// |
dextorslabs | 0:ea46340642a9 | 701 | void ILI9340_Display::DrawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t colour){ |
dextorslabs | 0:ea46340642a9 | 702 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
dextorslabs | 0:ea46340642a9 | 703 | if (steep) { |
dextorslabs | 0:ea46340642a9 | 704 | Swap(&x0, &y0); |
dextorslabs | 0:ea46340642a9 | 705 | Swap(&x1, &y1); |
dextorslabs | 0:ea46340642a9 | 706 | } |
dextorslabs | 0:ea46340642a9 | 707 | |
dextorslabs | 0:ea46340642a9 | 708 | if (x0 > x1) { |
dextorslabs | 0:ea46340642a9 | 709 | Swap(&x0, &x1); |
dextorslabs | 0:ea46340642a9 | 710 | Swap(&y0, &y1); |
dextorslabs | 0:ea46340642a9 | 711 | } |
dextorslabs | 0:ea46340642a9 | 712 | |
dextorslabs | 0:ea46340642a9 | 713 | int16_t dx, dy; |
dextorslabs | 0:ea46340642a9 | 714 | dx = x1 - x0; |
dextorslabs | 0:ea46340642a9 | 715 | dy = abs(y1 - y0); |
dextorslabs | 0:ea46340642a9 | 716 | |
dextorslabs | 0:ea46340642a9 | 717 | int16_t err = dx / 2; |
dextorslabs | 0:ea46340642a9 | 718 | int16_t ystep; |
dextorslabs | 0:ea46340642a9 | 719 | |
dextorslabs | 0:ea46340642a9 | 720 | if (y0 < y1) { |
dextorslabs | 0:ea46340642a9 | 721 | ystep = 1; |
dextorslabs | 0:ea46340642a9 | 722 | } else { |
dextorslabs | 0:ea46340642a9 | 723 | ystep = -1; |
dextorslabs | 0:ea46340642a9 | 724 | } |
dextorslabs | 0:ea46340642a9 | 725 | |
dextorslabs | 0:ea46340642a9 | 726 | for (; x0<=x1; x0++) { |
dextorslabs | 0:ea46340642a9 | 727 | if (steep) { |
dextorslabs | 0:ea46340642a9 | 728 | DrawPixel(y0, x0, colour); |
dextorslabs | 0:ea46340642a9 | 729 | } else { |
dextorslabs | 0:ea46340642a9 | 730 | DrawPixel(x0, y0, colour); |
dextorslabs | 0:ea46340642a9 | 731 | } |
dextorslabs | 0:ea46340642a9 | 732 | err -= dy; |
dextorslabs | 0:ea46340642a9 | 733 | if (err < 0) { |
dextorslabs | 0:ea46340642a9 | 734 | y0 += ystep; |
dextorslabs | 0:ea46340642a9 | 735 | err += dx; |
dextorslabs | 0:ea46340642a9 | 736 | } |
dextorslabs | 0:ea46340642a9 | 737 | } |
dextorslabs | 0:ea46340642a9 | 738 | } |
dextorslabs | 0:ea46340642a9 | 739 | |
dextorslabs | 0:ea46340642a9 | 740 | |
dextorslabs | 0:ea46340642a9 | 741 | |
dextorslabs | 0:ea46340642a9 | 742 | |
dextorslabs | 0:ea46340642a9 | 743 | |
dextorslabs | 0:ea46340642a9 | 744 | |
dextorslabs | 0:ea46340642a9 | 745 | |
dextorslabs | 0:ea46340642a9 | 746 |