Sample TFT to KL25Z program.
Dependencies: SPI_STMPE610 TFT_fonts UniGraphic mbed
Fork of TFT_test_frdm-kl25z by
main.cpp@5:449dc35a29bd, 2018-02-13 (annotated)
- Committer:
- Sustainable
- Date:
- Tue Feb 13 20:21:30 2018 +0000
- Revision:
- 5:449dc35a29bd
- Parent:
- 3:34d8706e1614
Basic TFT to KL25Z board. Shows how to use colors and circles to create buttons. Also displays the x,y data after each touch.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Sustainable | 5:449dc35a29bd | 1 | /******************************************************************* |
| Sustainable | 5:449dc35a29bd | 2 | * TFT Sample program for STEM project |
| Sustainable | 5:449dc35a29bd | 3 | * By Henry Foley, Feb 2017 |
| Rhyme | 0:6b8a2d4c88b3 | 4 | * |
| Sustainable | 5:449dc35a29bd | 5 | * Hardware: Freedom KL25Z uC development board with a TFT 2.8" |
| Sustainable | 5:449dc35a29bd | 6 | * resistive touch display shield from Adafruit (1651). |
| Sustainable | 5:449dc35a29bd | 7 | * |
| Rhyme | 0:6b8a2d4c88b3 | 8 | */ |
| Rhyme | 0:6b8a2d4c88b3 | 9 | |
| Rhyme | 0:6b8a2d4c88b3 | 10 | #include "mbed.h" |
| Rhyme | 3:34d8706e1614 | 11 | #include "ILI9341.h" |
| Rhyme | 0:6b8a2d4c88b3 | 12 | #include "SPI_STMPE610.h" |
| Rhyme | 0:6b8a2d4c88b3 | 13 | #include "Arial12x12.h" |
| Rhyme | 0:6b8a2d4c88b3 | 14 | #include "Arial24x23.h" |
| Rhyme | 0:6b8a2d4c88b3 | 15 | #include "Arial28x28.h" |
| Sustainable | 5:449dc35a29bd | 16 | #include "Verdana22x21-16.h" |
| Rhyme | 0:6b8a2d4c88b3 | 17 | |
| Rhyme | 0:6b8a2d4c88b3 | 18 | #define PIN_XP PTB3 |
| Rhyme | 0:6b8a2d4c88b3 | 19 | #define PIN_XM PTB1 |
| Rhyme | 0:6b8a2d4c88b3 | 20 | #define PIN_YP PTB2 |
| Rhyme | 0:6b8a2d4c88b3 | 21 | #define PIN_YM PTB0 |
| Rhyme | 0:6b8a2d4c88b3 | 22 | #define PIN_MOSI PTD2 |
| Sustainable | 5:449dc35a29bd | 23 | #define PIN_MISO PTD3 |
| Sustainable | 5:449dc35a29bd | 24 | #define PIN_SCLK PTD1 |
| Sustainable | 5:449dc35a29bd | 25 | #define PIN_CS_TFT PTD0 |
| Sustainable | 5:449dc35a29bd | 26 | #define PIN_DC_TFT PTD5 |
| Sustainable | 5:449dc35a29bd | 27 | #define PIN_BL_TFT PTC9 |
| Sustainable | 5:449dc35a29bd | 28 | #define PIN_CS_SD PTA4 |
| Rhyme | 0:6b8a2d4c88b3 | 29 | #define PIN_CS_TSC PTA13 |
| Sustainable | 5:449dc35a29bd | 30 | #define PIN_RESET_TFT PTB10 |
| Rhyme | 0:6b8a2d4c88b3 | 31 | |
| Sustainable | 5:449dc35a29bd | 32 | |
| Rhyme | 0:6b8a2d4c88b3 | 33 | |
| Rhyme | 3:34d8706e1614 | 34 | DigitalOut backlight(PTA12) ; |
| Sustainable | 5:449dc35a29bd | 35 | |
| Rhyme | 0:6b8a2d4c88b3 | 36 | SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ; |
| Rhyme | 0:6b8a2d4c88b3 | 37 | |
| Sustainable | 5:449dc35a29bd | 38 | ILI9341 TFT(SPI_8, 10000000, |
| Sustainable | 5:449dc35a29bd | 39 | PIN_MOSI, PIN_MISO, PIN_SCLK, |
| Sustainable | 5:449dc35a29bd | 40 | PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ; |
| Sustainable | 5:449dc35a29bd | 41 | |
| Sustainable | 5:449dc35a29bd | 42 | void initTFT(void); |
| Sustainable | 5:449dc35a29bd | 43 | void drawScreen(void); |
| Sustainable | 5:449dc35a29bd | 44 | uint16_t map(uint16_t, uint16_t, uint16_t, uint16_t, uint16_t); |
| Sustainable | 5:449dc35a29bd | 45 | void display_xy(void); |
| Sustainable | 5:449dc35a29bd | 46 | |
| Sustainable | 5:449dc35a29bd | 47 | int main() |
| Sustainable | 5:449dc35a29bd | 48 | { |
| Sustainable | 5:449dc35a29bd | 49 | static uint16_t x, y, z ; |
| Sustainable | 5:449dc35a29bd | 50 | uint16_t temp_x; |
| Sustainable | 5:449dc35a29bd | 51 | int touched = 0; |
| Sustainable | 5:449dc35a29bd | 52 | |
| Sustainable | 5:449dc35a29bd | 53 | initTFT() ; |
| Rhyme | 0:6b8a2d4c88b3 | 54 | |
| Sustainable | 5:449dc35a29bd | 55 | drawScreen(); |
| Sustainable | 5:449dc35a29bd | 56 | |
| Sustainable | 5:449dc35a29bd | 57 | while(true) |
| Sustainable | 5:449dc35a29bd | 58 | { |
| Sustainable | 5:449dc35a29bd | 59 | touched = TSC.getRAWPoint(&x, &y, &z); |
| Sustainable | 5:449dc35a29bd | 60 | |
| Sustainable | 5:449dc35a29bd | 61 | if(touched) // any touch |
| Sustainable | 5:449dc35a29bd | 62 | { |
| Sustainable | 5:449dc35a29bd | 63 | |
| Sustainable | 5:449dc35a29bd | 64 | // scale the raw values to match the screen orentation and 320(x = horiz) 240(y = vert) size, 0,0 = upper left corner |
| Sustainable | 5:449dc35a29bd | 65 | temp_x = x; |
| Sustainable | 5:449dc35a29bd | 66 | x = map(y,180, 3800,0,320); |
| Sustainable | 5:449dc35a29bd | 67 | y = map(temp_x,3800,280,0,240); |
| Sustainable | 5:449dc35a29bd | 68 | if( (x > 0 && x < 320) && ( y > 0 && y < 240 )) // if valid values |
| Sustainable | 5:449dc35a29bd | 69 | { |
| Sustainable | 5:449dc35a29bd | 70 | drawScreen(); |
| Sustainable | 5:449dc35a29bd | 71 | |
| Sustainable | 5:449dc35a29bd | 72 | TFT.BusEnable(true) ; |
| Sustainable | 5:449dc35a29bd | 73 | TFT.background(Blue); |
| Sustainable | 5:449dc35a29bd | 74 | wait(0.2); |
| Sustainable | 5:449dc35a29bd | 75 | TFT.foreground(White); |
| Sustainable | 5:449dc35a29bd | 76 | TFT.locate(120, 160); |
| Sustainable | 5:449dc35a29bd | 77 | TFT.printf("x%3d", x); |
| Sustainable | 5:449dc35a29bd | 78 | TFT.locate(120, 190); |
| Sustainable | 5:449dc35a29bd | 79 | TFT.printf("y%3d", y); |
| Sustainable | 5:449dc35a29bd | 80 | TFT.BusEnable(false) ; |
| Sustainable | 5:449dc35a29bd | 81 | wait(0.5); // to reduce mutiple hits from one touch |
| Sustainable | 5:449dc35a29bd | 82 | } |
| Sustainable | 5:449dc35a29bd | 83 | |
| Sustainable | 5:449dc35a29bd | 84 | } |
| Sustainable | 5:449dc35a29bd | 85 | } |
| Sustainable | 5:449dc35a29bd | 86 | } // end of main() |
| Rhyme | 0:6b8a2d4c88b3 | 87 | |
| Rhyme | 0:6b8a2d4c88b3 | 88 | void initTFT(void) |
| Rhyme | 0:6b8a2d4c88b3 | 89 | { |
| Rhyme | 0:6b8a2d4c88b3 | 90 | //Configure the display driver |
| Rhyme | 3:34d8706e1614 | 91 | TFT.BusEnable(true) ; |
| Sustainable | 5:449dc35a29bd | 92 | TFT.set_orientation(1); |
| Sustainable | 5:449dc35a29bd | 93 | backlight = 1 ; |
| Sustainable | 5:449dc35a29bd | 94 | TFT.background(Blue); |
| Sustainable | 5:449dc35a29bd | 95 | wait(0.2); |
| Rhyme | 0:6b8a2d4c88b3 | 96 | TFT.foreground(White); |
| Sustainable | 5:449dc35a29bd | 97 | wait(0.2); |
| Rhyme | 0:6b8a2d4c88b3 | 98 | TFT.cls(); |
| Sustainable | 5:449dc35a29bd | 99 | wait(0.2); |
| Sustainable | 5:449dc35a29bd | 100 | TFT.set_font((unsigned char*) Arial28x28); |
| Sustainable | 5:449dc35a29bd | 101 | wait(0.2); |
| Sustainable | 5:449dc35a29bd | 102 | TFT.locate(15, 10); |
| Sustainable | 5:449dc35a29bd | 103 | TFT.printf(" TFT Test "); |
| Sustainable | 5:449dc35a29bd | 104 | TFT.locate(15, 100); |
| Sustainable | 5:449dc35a29bd | 105 | TFT.BusEnable(false); |
| Sustainable | 5:449dc35a29bd | 106 | wait(5); |
| Sustainable | 5:449dc35a29bd | 107 | } |
| Sustainable | 5:449dc35a29bd | 108 | |
| Sustainable | 5:449dc35a29bd | 109 | |
| Sustainable | 5:449dc35a29bd | 110 | void drawScreen(void) |
| Sustainable | 5:449dc35a29bd | 111 | { |
| Sustainable | 5:449dc35a29bd | 112 | TFT.BusEnable(true); |
| Sustainable | 5:449dc35a29bd | 113 | TFT.foreground(White); // Set default text colors |
| Sustainable | 5:449dc35a29bd | 114 | TFT.background(Blue); |
| Sustainable | 5:449dc35a29bd | 115 | TFT.cls(); // wipe the screen clean and set bg colors |
| Sustainable | 5:449dc35a29bd | 116 | wait(0.1); |
| Sustainable | 5:449dc35a29bd | 117 | |
| Sustainable | 5:449dc35a29bd | 118 | TFT.foreground(Black); |
| Sustainable | 5:449dc35a29bd | 119 | TFT.set_font((unsigned char*) Arial24x23); |
| Sustainable | 5:449dc35a29bd | 120 | TFT.fillcircle( 60, 175, 50, Yellow); |
| Sustainable | 5:449dc35a29bd | 121 | TFT.background(Yellow) ; |
| Sustainable | 5:449dc35a29bd | 122 | TFT.locate(25, 160); |
| Sustainable | 5:449dc35a29bd | 123 | TFT.printf(" A "); |
| Sustainable | 5:449dc35a29bd | 124 | |
| Sustainable | 5:449dc35a29bd | 125 | TFT.fillcircle( 160, 105, 50, Cyan); |
| Sustainable | 5:449dc35a29bd | 126 | TFT.background(Cyan) ; |
| Sustainable | 5:449dc35a29bd | 127 | TFT.locate(127, 92); |
| Sustainable | 5:449dc35a29bd | 128 | TFT.printf(" B "); |
| Sustainable | 5:449dc35a29bd | 129 | |
| Sustainable | 5:449dc35a29bd | 130 | TFT.fillcircle( 260, 175, 50, Green); |
| Sustainable | 5:449dc35a29bd | 131 | TFT.background(Green) ; |
| Sustainable | 5:449dc35a29bd | 132 | TFT.locate(222, 160); |
| Sustainable | 5:449dc35a29bd | 133 | TFT.printf(" C "); |
| Sustainable | 5:449dc35a29bd | 134 | |
| Sustainable | 5:449dc35a29bd | 135 | TFT.fillcircle( 60, 60, 50, Orange); |
| Sustainable | 5:449dc35a29bd | 136 | TFT.background(Orange) ; |
| Sustainable | 5:449dc35a29bd | 137 | TFT.locate(15, 50); |
| Sustainable | 5:449dc35a29bd | 138 | TFT.printf(" D "); |
| Sustainable | 5:449dc35a29bd | 139 | |
| Sustainable | 5:449dc35a29bd | 140 | TFT.fillcircle( 260, 60, 50, GreenYellow); |
| Sustainable | 5:449dc35a29bd | 141 | TFT.background(GreenYellow) ; |
| Sustainable | 5:449dc35a29bd | 142 | TFT.locate(225, 50); |
| Sustainable | 5:449dc35a29bd | 143 | TFT.printf(" E "); |
| Sustainable | 5:449dc35a29bd | 144 | |
| Rhyme | 3:34d8706e1614 | 145 | TFT.BusEnable(false) ; |
| Rhyme | 0:6b8a2d4c88b3 | 146 | } |
| Rhyme | 0:6b8a2d4c88b3 | 147 | |
| Sustainable | 5:449dc35a29bd | 148 | // mapping function to change the orentation of the screen to horizontal |
| Sustainable | 5:449dc35a29bd | 149 | uint16_t map(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) |
| Rhyme | 0:6b8a2d4c88b3 | 150 | { |
| Sustainable | 5:449dc35a29bd | 151 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
| Rhyme | 0:6b8a2d4c88b3 | 152 | } |
| Rhyme | 0:6b8a2d4c88b3 | 153 | |
| Sustainable | 5:449dc35a29bd | 154 | // Code used to debug screen orentation and touch pad |
| Sustainable | 5:449dc35a29bd | 155 | void display_xy(void) |
| Rhyme | 0:6b8a2d4c88b3 | 156 | { |
| Sustainable | 5:449dc35a29bd | 157 | static uint16_t x, y, z ; |
| Sustainable | 5:449dc35a29bd | 158 | uint16_t temp_x; |
| Sustainable | 5:449dc35a29bd | 159 | int touched = 0; |
| Sustainable | 5:449dc35a29bd | 160 | |
| Sustainable | 5:449dc35a29bd | 161 | while(true) { |
| Sustainable | 5:449dc35a29bd | 162 | touched = TSC.getRAWPoint(&x, &y, &z); |
| Rhyme | 0:6b8a2d4c88b3 | 163 | |
| Sustainable | 5:449dc35a29bd | 164 | x = x & 0x7f; |
| Sustainable | 5:449dc35a29bd | 165 | y = y & 0x7f; |
| Sustainable | 5:449dc35a29bd | 166 | |
| Sustainable | 5:449dc35a29bd | 167 | if(x) { // any touch |
| Sustainable | 5:449dc35a29bd | 168 | wait(0.5); |
| Sustainable | 5:449dc35a29bd | 169 | TFT.BusEnable(true); |
| Sustainable | 5:449dc35a29bd | 170 | TFT.locate(5, 40); |
| Sustainable | 5:449dc35a29bd | 171 | TFT.printf("x= %5d x=%4X", x, x); |
| Sustainable | 5:449dc35a29bd | 172 | TFT.locate(5, 80); |
| Sustainable | 5:449dc35a29bd | 173 | TFT.printf("y= %5d y=%4X", y, y); |
| Sustainable | 5:449dc35a29bd | 174 | // TFT.locate(5, 120); |
| Sustainable | 5:449dc35a29bd | 175 | // TFT.printf("raw z%6d z%4X", z, z); |
| Sustainable | 5:449dc35a29bd | 176 | |
| Sustainable | 5:449dc35a29bd | 177 | // scale the raw values to match the screen orentation and 320(x = horiz) 240(y = vert) size |
| Sustainable | 5:449dc35a29bd | 178 | // 0,0 = upper left corner |
| Sustainable | 5:449dc35a29bd | 179 | temp_x = x; |
| Sustainable | 5:449dc35a29bd | 180 | x = map(y,180, 3800,0,320); // TFT.set_orientation(1); |
| Sustainable | 5:449dc35a29bd | 181 | y = map(temp_x,3800,280,0,240); |
| Sustainable | 5:449dc35a29bd | 182 | |
| Sustainable | 5:449dc35a29bd | 183 | // x = map(y,3800, 180,0,320); // TFT.set_orientation(3); |
| Sustainable | 5:449dc35a29bd | 184 | // y = map(temp_x,280,3800,0,240); |
| Sustainable | 5:449dc35a29bd | 185 | |
| Sustainable | 5:449dc35a29bd | 186 | TFT.locate(5, 120); |
| Sustainable | 5:449dc35a29bd | 187 | TFT.printf("map x%5d y%5d", x, y); |
| Rhyme | 0:6b8a2d4c88b3 | 188 | |
| Sustainable | 5:449dc35a29bd | 189 | TFT.BusEnable(false); |
| Sustainable | 5:449dc35a29bd | 190 | wait(0.5); |
| Sustainable | 5:449dc35a29bd | 191 | } |
| Rhyme | 0:6b8a2d4c88b3 | 192 | } |
| Rhyme | 0:6b8a2d4c88b3 | 193 | } |
| Sustainable | 5:449dc35a29bd | 194 |
