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