![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
KL25Z driving an ILI9320 LCD board with touch panel (HY28A-LCDB SPI)
Dependencies: SPI_TFT_ILI9320 mbed
main.cpp@3:cb2172c95bf1, 2014-11-04 (annotated)
- Committer:
- frankvnk
- Date:
- Tue Nov 04 15:18:17 2014 +0000
- Revision:
- 3:cb2172c95bf1
- Parent:
- 1:76f92f6b3737
Updated SPI_TFT_ILI9320 library to the latest version (auto-detect ILI9320, 9325 and 9328)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frankvnk | 0:ff976ad09b2f | 1 | /* |
frankvnk | 0:ff976ad09b2f | 2 | KL25Z - ILI9320 SPI demo: |
frankvnk | 0:ff976ad09b2f | 3 | Display some text and graphics, followed by a simple paint demo. |
frankvnk | 0:ff976ad09b2f | 4 | */ |
frankvnk | 0:ff976ad09b2f | 5 | |
frankvnk | 0:ff976ad09b2f | 6 | #include "stdio.h" |
frankvnk | 0:ff976ad09b2f | 7 | #include "mbed.h" |
frankvnk | 0:ff976ad09b2f | 8 | #include "SPI_TFT_ILI9320.h" |
frankvnk | 0:ff976ad09b2f | 9 | #include "Touch.h" |
frankvnk | 0:ff976ad09b2f | 10 | #include "Arial12x12.h" |
frankvnk | 0:ff976ad09b2f | 11 | #include "Arial24x23.h" |
frankvnk | 0:ff976ad09b2f | 12 | #include "Arial28x28.h" |
frankvnk | 0:ff976ad09b2f | 13 | #include "font_big.h" |
frankvnk | 0:ff976ad09b2f | 14 | |
frankvnk | 0:ff976ad09b2f | 15 | extern unsigned char p1[]; // the mbed logo |
frankvnk | 0:ff976ad09b2f | 16 | #define RGB565CONVERT(red, green, blue) (uint16_t)( (( red >> 3 ) << 11 ) | (( green >> 2 ) << 5 ) | ( blue >> 3 )) |
frankvnk | 0:ff976ad09b2f | 17 | |
frankvnk | 0:ff976ad09b2f | 18 | // TFT -> mosi, miso, sclk, cs |
frankvnk | 0:ff976ad09b2f | 19 | SPI_TFT TFT(PTD3, PTD2, PTD1, PTD0,"TFT"); |
frankvnk | 0:ff976ad09b2f | 20 | |
frankvnk | 0:ff976ad09b2f | 21 | // ADS7843 -> mosi, miso, sclk, cs, irq, SPI_TFT |
frankvnk | 0:ff976ad09b2f | 22 | TouchScreenADS7843 TP(PTD6 ,PTD7 ,PTD5 ,PTD4 ,PTA13, &TFT); |
frankvnk | 0:ff976ad09b2f | 23 | |
frankvnk | 0:ff976ad09b2f | 24 | int main() |
frankvnk | 0:ff976ad09b2f | 25 | { |
frankvnk | 1:76f92f6b3737 | 26 | Matrix matrix; |
frankvnk | 1:76f92f6b3737 | 27 | Coordinate ScreenSample[3]; |
frankvnk | 1:76f92f6b3737 | 28 | |
frankvnk | 0:ff976ad09b2f | 29 | unsigned short LCD_id; |
frankvnk | 0:ff976ad09b2f | 30 | TFT.claim(stdout); // send stdout to the TFT display |
frankvnk | 0:ff976ad09b2f | 31 | // Disable stdout buffering, allows us to omit \n with printf. |
frankvnk | 0:ff976ad09b2f | 32 | // More info at http://www.cplusplus.com/reference/cstdio/setvbuf/ |
frankvnk | 0:ff976ad09b2f | 33 | setvbuf ( stdout , NULL , _IONBF , NULL ); |
frankvnk | 0:ff976ad09b2f | 34 | |
frankvnk | 0:ff976ad09b2f | 35 | TFT.background(Black); // set background to black |
frankvnk | 0:ff976ad09b2f | 36 | TFT.foreground(White); // set chars to white |
frankvnk | 0:ff976ad09b2f | 37 | |
frankvnk | 0:ff976ad09b2f | 38 | // LCD demo |
frankvnk | 0:ff976ad09b2f | 39 | // first show the 4 directions |
frankvnk | 0:ff976ad09b2f | 40 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 41 | TFT.set_font((unsigned char*) Arial12x12); |
frankvnk | 0:ff976ad09b2f | 42 | TFT.set_orientation(0); |
frankvnk | 0:ff976ad09b2f | 43 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 44 | printf(" Hello Mbed 0"); |
frankvnk | 0:ff976ad09b2f | 45 | TFT.set_orientation(1); |
frankvnk | 0:ff976ad09b2f | 46 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 47 | printf(" Hello Mbed 1"); |
frankvnk | 0:ff976ad09b2f | 48 | TFT.set_orientation(2); |
frankvnk | 0:ff976ad09b2f | 49 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 50 | printf(" Hello Mbed 2"); |
frankvnk | 0:ff976ad09b2f | 51 | TFT.set_orientation(3); |
frankvnk | 0:ff976ad09b2f | 52 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 53 | printf(" Hello Mbed 3"); |
frankvnk | 0:ff976ad09b2f | 54 | TFT.set_orientation(1); |
frankvnk | 0:ff976ad09b2f | 55 | TFT.set_font((unsigned char*) Arial24x23); |
frankvnk | 0:ff976ad09b2f | 56 | TFT.locate(50,100); |
frankvnk | 0:ff976ad09b2f | 57 | printf("TFT orientation"); |
frankvnk | 0:ff976ad09b2f | 58 | wait(2); |
frankvnk | 0:ff976ad09b2f | 59 | |
frankvnk | 0:ff976ad09b2f | 60 | // draw some graphics |
frankvnk | 0:ff976ad09b2f | 61 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 62 | TFT.set_orientation(1); |
frankvnk | 0:ff976ad09b2f | 63 | TFT.set_font((unsigned char*) Arial24x23); |
frankvnk | 0:ff976ad09b2f | 64 | TFT.locate(120,115); |
frankvnk | 0:ff976ad09b2f | 65 | printf("Graphic"); |
frankvnk | 0:ff976ad09b2f | 66 | TFT.line(0,0,100,200,Green); |
frankvnk | 0:ff976ad09b2f | 67 | TFT.rect(100,50,50,50,Red); |
frankvnk | 0:ff976ad09b2f | 68 | TFT.fillrect(180,25,40,45,Blue); |
frankvnk | 0:ff976ad09b2f | 69 | TFT.draw_ellipse(80, 150, 33, 33, White); |
frankvnk | 0:ff976ad09b2f | 70 | TFT.fill_ellipse(80, 50, 33, 33, White); |
frankvnk | 0:ff976ad09b2f | 71 | wait(2); |
frankvnk | 0:ff976ad09b2f | 72 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 73 | TFT.draw_ellipse(160, 120, 100, 50, Yellow); |
frankvnk | 0:ff976ad09b2f | 74 | TFT.draw_ellipse(160, 120, 100, 100, Blue); |
frankvnk | 0:ff976ad09b2f | 75 | TFT.fill_ellipse(160, 120, 80, 40, Green); |
frankvnk | 0:ff976ad09b2f | 76 | wait(2); |
frankvnk | 0:ff976ad09b2f | 77 | |
frankvnk | 0:ff976ad09b2f | 78 | // bigger text |
frankvnk | 0:ff976ad09b2f | 79 | TFT.foreground(White); |
frankvnk | 0:ff976ad09b2f | 80 | TFT.background(Blue); |
frankvnk | 0:ff976ad09b2f | 81 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 82 | TFT.set_font((unsigned char*) Arial24x23); |
frankvnk | 0:ff976ad09b2f | 83 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 84 | printf("Different Fonts :"); |
frankvnk | 0:ff976ad09b2f | 85 | |
frankvnk | 0:ff976ad09b2f | 86 | TFT.set_font((unsigned char*) Neu42x35); |
frankvnk | 0:ff976ad09b2f | 87 | TFT.locate(0,50); |
frankvnk | 0:ff976ad09b2f | 88 | printf("Hello"); |
frankvnk | 0:ff976ad09b2f | 89 | TFT.set_font((unsigned char*) Arial24x23); |
frankvnk | 0:ff976ad09b2f | 90 | TFT.locate(50,100); |
frankvnk | 0:ff976ad09b2f | 91 | printf("Hello"); |
frankvnk | 0:ff976ad09b2f | 92 | TFT.set_font((unsigned char*) Arial12x12); |
frankvnk | 0:ff976ad09b2f | 93 | TFT.locate(55,150); |
frankvnk | 0:ff976ad09b2f | 94 | printf("Hello"); |
frankvnk | 0:ff976ad09b2f | 95 | |
frankvnk | 0:ff976ad09b2f | 96 | TFT.set_orientation(2); |
frankvnk | 0:ff976ad09b2f | 97 | TFT.set_font((unsigned char*) Arial24x23); |
frankvnk | 0:ff976ad09b2f | 98 | TFT.locate(10,10); |
frankvnk | 0:ff976ad09b2f | 99 | printf("Hi mbed"); |
frankvnk | 0:ff976ad09b2f | 100 | wait(2); |
frankvnk | 0:ff976ad09b2f | 101 | // mbed logo |
frankvnk | 0:ff976ad09b2f | 102 | TFT.set_orientation(1); |
frankvnk | 0:ff976ad09b2f | 103 | TFT.background(Black); |
frankvnk | 0:ff976ad09b2f | 104 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 105 | TFT.Bitmap(90,90,172,55,p1); |
frankvnk | 0:ff976ad09b2f | 106 | |
frankvnk | 0:ff976ad09b2f | 107 | // Read LCD ID |
frankvnk | 0:ff976ad09b2f | 108 | TFT.set_orientation(0); |
frankvnk | 0:ff976ad09b2f | 109 | LCD_id = TFT.Read_ID(); |
frankvnk | 0:ff976ad09b2f | 110 | TFT.locate(10,10); |
frankvnk | 0:ff976ad09b2f | 111 | printf("LCD: ILI%04X", LCD_id); |
frankvnk | 0:ff976ad09b2f | 112 | wait(2); |
frankvnk | 0:ff976ad09b2f | 113 | |
frankvnk | 0:ff976ad09b2f | 114 | // RGB color wheel demo (cycle through all colors) |
frankvnk | 0:ff976ad09b2f | 115 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 116 | TFT.foreground(Yellow); // set chars to yellow |
frankvnk | 0:ff976ad09b2f | 117 | TFT.set_font((unsigned char*) Arial12x12); |
frankvnk | 0:ff976ad09b2f | 118 | TFT.locate(10,10); |
frankvnk | 0:ff976ad09b2f | 119 | printf("RGB color wheel (2x)"); |
frankvnk | 0:ff976ad09b2f | 120 | |
frankvnk | 0:ff976ad09b2f | 121 | uint8_t r = 255, g = 0, b = 0, step = 5, i; |
frankvnk | 0:ff976ad09b2f | 122 | for (i=0;i<2;i++) |
frankvnk | 0:ff976ad09b2f | 123 | { |
frankvnk | 0:ff976ad09b2f | 124 | for(;g<255;g+=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));} // Cycle from FF0000 to FFFF00 : red to yellow |
frankvnk | 0:ff976ad09b2f | 125 | for(;r>0;r-=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));} // Cycle from FFFF00 to 00FF00 : yellow to green |
frankvnk | 0:ff976ad09b2f | 126 | for(;b<255;b+=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));} // Cycle from 00FF00 to 00FFFF : green to cyan |
frankvnk | 0:ff976ad09b2f | 127 | for(;g>0;g-=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));} // Cycle from 00FFFF to 0000FF : cyan to blue |
frankvnk | 0:ff976ad09b2f | 128 | for(;r<255;r+=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));} // Cycle from 0000FF to FF00FF : blue to purple |
frankvnk | 0:ff976ad09b2f | 129 | for(;b>0;b-=step) {TFT.fillrect(70,110,100,100,RGB565CONVERT(r, g, b));} // Cycle from FF00FF to FF0000 : purple to red |
frankvnk | 0:ff976ad09b2f | 130 | } |
frankvnk | 0:ff976ad09b2f | 131 | wait(2); |
frankvnk | 0:ff976ad09b2f | 132 | |
frankvnk | 0:ff976ad09b2f | 133 | // Touchpanel demo |
frankvnk | 0:ff976ad09b2f | 134 | TP.TouchPanel_Calibrate(); |
frankvnk | 0:ff976ad09b2f | 135 | TFT.set_font((unsigned char*) Arial12x12); |
frankvnk | 0:ff976ad09b2f | 136 | TFT.set_orientation(0); |
frankvnk | 1:76f92f6b3737 | 137 | // Display calibration values |
frankvnk | 1:76f92f6b3737 | 138 | TP.GetCalibration(&matrix, &ScreenSample[0]); |
frankvnk | 1:76f92f6b3737 | 139 | TFT.cls(); |
frankvnk | 1:76f92f6b3737 | 140 | TFT.locate(0,20); |
frankvnk | 1:76f92f6b3737 | 141 | // read calibration results |
frankvnk | 1:76f92f6b3737 | 142 | printf("Read calibration results.\n"); |
frankvnk | 1:76f92f6b3737 | 143 | printf("matrix.An = %d\n", matrix.An); |
frankvnk | 1:76f92f6b3737 | 144 | printf("matrix.Bn = %d\n", matrix.Bn); |
frankvnk | 1:76f92f6b3737 | 145 | printf("matrix.Cn = %d\n", matrix.Cn); |
frankvnk | 1:76f92f6b3737 | 146 | printf("matrix.Dn = %d\n", matrix.Dn); |
frankvnk | 1:76f92f6b3737 | 147 | printf("matrix.En = %d\n", matrix.En); |
frankvnk | 1:76f92f6b3737 | 148 | printf("matrix.Fn = %d\n", matrix.Fn); |
frankvnk | 1:76f92f6b3737 | 149 | printf("matrix.Di = %d\n", matrix.Divider); |
frankvnk | 1:76f92f6b3737 | 150 | for (i=0;i<3;i++) |
frankvnk | 1:76f92f6b3737 | 151 | printf("sample x[%d] = %d\nsample y[%d] = %d\n", i, ScreenSample[i].x, i, ScreenSample[i].y); |
frankvnk | 1:76f92f6b3737 | 152 | // Write calibration results |
frankvnk | 1:76f92f6b3737 | 153 | printf("\nWrite calibration results...\n"); |
frankvnk | 1:76f92f6b3737 | 154 | TP.SetCalibration(&matrix, &ScreenSample[0]); |
frankvnk | 1:76f92f6b3737 | 155 | printf("Done.\nTouch panel to read again.\n"); |
frankvnk | 1:76f92f6b3737 | 156 | while(TP._tp_irq); |
frankvnk | 1:76f92f6b3737 | 157 | // read calibration results |
frankvnk | 1:76f92f6b3737 | 158 | TFT.cls(); |
frankvnk | 1:76f92f6b3737 | 159 | TFT.locate(0,20); |
frankvnk | 1:76f92f6b3737 | 160 | printf("Calibration results.\n\n"); |
frankvnk | 1:76f92f6b3737 | 161 | printf("matrix.An = %d\n", matrix.An); |
frankvnk | 1:76f92f6b3737 | 162 | printf("matrix.Bn = %d\n", matrix.Bn); |
frankvnk | 1:76f92f6b3737 | 163 | printf("matrix.Cn = %d\n", matrix.Cn); |
frankvnk | 1:76f92f6b3737 | 164 | printf("matrix.Dn = %d\n", matrix.Dn); |
frankvnk | 1:76f92f6b3737 | 165 | printf("matrix.En = %d\n", matrix.En); |
frankvnk | 1:76f92f6b3737 | 166 | printf("matrix.Fn = %d\n", matrix.Fn); |
frankvnk | 1:76f92f6b3737 | 167 | printf("matrix.Di = %d\n", matrix.Divider); |
frankvnk | 1:76f92f6b3737 | 168 | for (i=0;i<3;i++) |
frankvnk | 1:76f92f6b3737 | 169 | printf("sample x[%d] = %d\nsample y[%d] = %d\n", i, ScreenSample[i].x, i, ScreenSample[i].y); |
frankvnk | 1:76f92f6b3737 | 170 | |
frankvnk | 0:ff976ad09b2f | 171 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 172 | printf(" X:"); |
frankvnk | 0:ff976ad09b2f | 173 | TFT.locate(70,0); |
frankvnk | 0:ff976ad09b2f | 174 | printf(" Y:"); |
frankvnk | 0:ff976ad09b2f | 175 | while (1) |
frankvnk | 0:ff976ad09b2f | 176 | { |
frankvnk | 0:ff976ad09b2f | 177 | if (!TP._tp_irq) |
frankvnk | 0:ff976ad09b2f | 178 | { |
frankvnk | 0:ff976ad09b2f | 179 | if (TP.Read_Ads7843()) |
frankvnk | 0:ff976ad09b2f | 180 | { |
frankvnk | 0:ff976ad09b2f | 181 | TP.getDisplayPoint() ; |
frankvnk | 0:ff976ad09b2f | 182 | TP.TP_DrawPoint(TP.display.x,TP.display.y, Blue); |
frankvnk | 0:ff976ad09b2f | 183 | TFT.locate(25,0); |
frankvnk | 0:ff976ad09b2f | 184 | printf("%03d",TP.display.x); |
frankvnk | 0:ff976ad09b2f | 185 | TFT.locate(95,0); |
frankvnk | 0:ff976ad09b2f | 186 | printf("%03d",TP.display.y); |
frankvnk | 0:ff976ad09b2f | 187 | // Touchscreen area is larger than LCD area. |
frankvnk | 0:ff976ad09b2f | 188 | // We use the bottom area outside the LCD area to clear the screen (y value > 320). |
frankvnk | 0:ff976ad09b2f | 189 | if (TP.display.y > 320) |
frankvnk | 0:ff976ad09b2f | 190 | { |
frankvnk | 0:ff976ad09b2f | 191 | TFT.cls(); |
frankvnk | 0:ff976ad09b2f | 192 | TFT.locate(0,0); |
frankvnk | 0:ff976ad09b2f | 193 | printf(" X:"); |
frankvnk | 0:ff976ad09b2f | 194 | TFT.locate(70,0); |
frankvnk | 0:ff976ad09b2f | 195 | printf(" Y:"); |
frankvnk | 0:ff976ad09b2f | 196 | } |
frankvnk | 0:ff976ad09b2f | 197 | } |
frankvnk | 0:ff976ad09b2f | 198 | } |
frankvnk | 0:ff976ad09b2f | 199 | } |
frankvnk | 0:ff976ad09b2f | 200 | } |
frankvnk | 0:ff976ad09b2f | 201 |