![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Paint for the FRDM-KL25Z
Dependencies: mbed TFT_fonts SPI_TFT_ILI9341
main.cpp@4:87b40e5986e7, 2019-01-10 (annotated)
- Committer:
- ecowboy
- Date:
- Thu Jan 10 03:48:37 2019 +0000
- Revision:
- 4:87b40e5986e7
- Parent:
- 3:1396bac4ae71
- Child:
- 5:80791250acb2
touch class enabled and working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ecowboy | 0:42ec9a44bc24 | 1 | #include "mbed.h" |
ecowboy | 3:1396bac4ae71 | 2 | #include "SPI_TFT_ILI9341.h" |
ecowboy | 3:1396bac4ae71 | 3 | #include "Arial12x12.h" |
ecowboy | 0:42ec9a44bc24 | 4 | #include "BMP565.h" |
ecowboy | 0:42ec9a44bc24 | 5 | #include "stdio.h" |
ecowboy | 0:42ec9a44bc24 | 6 | #include "touch.h" |
ecowboy | 0:42ec9a44bc24 | 7 | |
ecowboy | 0:42ec9a44bc24 | 8 | |
ecowboy | 3:1396bac4ae71 | 9 | #define PIN_XP PTB3 |
ecowboy | 3:1396bac4ae71 | 10 | #define PIN_XM PTB2 |
ecowboy | 3:1396bac4ae71 | 11 | #define PIN_YP PTB1 |
ecowboy | 3:1396bac4ae71 | 12 | #define PIN_YM PTB0 |
ecowboy | 3:1396bac4ae71 | 13 | #define PIN_MOSI PTD2 |
ecowboy | 3:1396bac4ae71 | 14 | #define PIN_MISO PTD3 |
ecowboy | 3:1396bac4ae71 | 15 | #define PIN_SCLK PTD1 |
ecowboy | 3:1396bac4ae71 | 16 | #define PIN_CS_TFT PTA5 |
ecowboy | 3:1396bac4ae71 | 17 | #define PIN_DC_TFT PTC8 |
ecowboy | 3:1396bac4ae71 | 18 | #define PIN_BL_TFT PTC9 |
ecowboy | 3:1396bac4ae71 | 19 | #define PIN_CS_SD PTA4 |
ecowboy | 0:42ec9a44bc24 | 20 | |
ecowboy | 0:42ec9a44bc24 | 21 | |
ecowboy | 3:1396bac4ae71 | 22 | SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT, "TFT"); |
ecowboy | 3:1396bac4ae71 | 23 | TouchScreen tft(PIN_XP, PIN_YP, PIN_XM, PIN_YM); //init TouchScreen port pins |
ecowboy | 0:42ec9a44bc24 | 24 | |
ecowboy | 0:42ec9a44bc24 | 25 | |
ecowboy | 0:42ec9a44bc24 | 26 | // Paint application - Demonstate both TFT and Touch Screen |
ecowboy | 3:1396bac4ae71 | 27 | int ColorPaletteHigh = 15; |
ecowboy | 0:42ec9a44bc24 | 28 | int color = White; //Paint brush color |
ecowboy | 3:1396bac4ae71 | 29 | unsigned int colors[16] = {Red, Green, Blue, Cyan, Yellow, Purple, Pink, Black, Orange, Brown, BlueGreen, White, LightPink, DarkGrey, DarkGreen, Mustard}; |
ecowboy | 0:42ec9a44bc24 | 30 | |
ecowboy | 3:1396bac4ae71 | 31 | int RXPLATE = 350; |
ecowboy | 3:1396bac4ae71 | 32 | int Q = 1024; //10 bit TSC Resolution |
ecowboy | 0:42ec9a44bc24 | 33 | |
ecowboy | 1:33506fcfdd95 | 34 | struct point |
ecowboy | 1:33506fcfdd95 | 35 | { |
ecowboy | 1:33506fcfdd95 | 36 | int x; |
ecowboy | 1:33506fcfdd95 | 37 | int y; |
ecowboy | 1:33506fcfdd95 | 38 | int z; |
ecowboy | 1:33506fcfdd95 | 39 | }; |
ecowboy | 1:33506fcfdd95 | 40 | |
ecowboy | 1:33506fcfdd95 | 41 | typedef struct point Point; |
ecowboy | 1:33506fcfdd95 | 42 | |
ecowboy | 1:33506fcfdd95 | 43 | |
ecowboy | 1:33506fcfdd95 | 44 | Point point(int xx, int yy, int zz){ |
ecowboy | 1:33506fcfdd95 | 45 | Point p; |
ecowboy | 1:33506fcfdd95 | 46 | p.x = xx; |
ecowboy | 1:33506fcfdd95 | 47 | p.y = yy; |
ecowboy | 1:33506fcfdd95 | 48 | p.z = zz; |
ecowboy | 1:33506fcfdd95 | 49 | return p; |
ecowboy | 3:1396bac4ae71 | 50 | } |
ecowboy | 3:1396bac4ae71 | 51 | |
ecowboy | 3:1396bac4ae71 | 52 | int Read_X(){ |
ecowboy | 3:1396bac4ae71 | 53 | AnalogIn _yp(PTB2); |
ecowboy | 3:1396bac4ae71 | 54 | AnalogIn _ym(PTB0); |
ecowboy | 3:1396bac4ae71 | 55 | DigitalOut _xp(PTB3); |
ecowboy | 3:1396bac4ae71 | 56 | DigitalOut _xm(PTB1); |
ecowboy | 3:1396bac4ae71 | 57 | //uint16_t x1=1; |
ecowboy | 3:1396bac4ae71 | 58 | //uint16_t x2=0; |
ecowboy | 3:1396bac4ae71 | 59 | int x1=1; |
ecowboy | 3:1396bac4ae71 | 60 | int x2=0; |
ecowboy | 3:1396bac4ae71 | 61 | _xp = 1; |
ecowboy | 3:1396bac4ae71 | 62 | _xm = 0; |
ecowboy | 3:1396bac4ae71 | 63 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 64 | //while(x1 != x2){ |
ecowboy | 3:1396bac4ae71 | 65 | x1 = _yp.read_u16(); |
ecowboy | 3:1396bac4ae71 | 66 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 67 | x2 = _yp.read_u16(); |
ecowboy | 3:1396bac4ae71 | 68 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 69 | //} |
ecowboy | 3:1396bac4ae71 | 70 | _xp=0; |
ecowboy | 3:1396bac4ae71 | 71 | return (x1+x2)/2; |
ecowboy | 3:1396bac4ae71 | 72 | } |
ecowboy | 3:1396bac4ae71 | 73 | |
ecowboy | 3:1396bac4ae71 | 74 | int Read_Y(){ |
ecowboy | 3:1396bac4ae71 | 75 | AnalogIn _xp(PTB3); |
ecowboy | 3:1396bac4ae71 | 76 | AnalogIn _xm(PTB1); |
ecowboy | 3:1396bac4ae71 | 77 | DigitalOut _yp(PTB2); |
ecowboy | 3:1396bac4ae71 | 78 | DigitalOut _ym(PTB0); |
ecowboy | 3:1396bac4ae71 | 79 | //uint16_t y1=1; |
ecowboy | 3:1396bac4ae71 | 80 | //uint16_t y2=0; |
ecowboy | 3:1396bac4ae71 | 81 | int y1=1; |
ecowboy | 3:1396bac4ae71 | 82 | int y2=0; |
ecowboy | 3:1396bac4ae71 | 83 | _yp = 1; |
ecowboy | 3:1396bac4ae71 | 84 | _ym = 0; |
ecowboy | 3:1396bac4ae71 | 85 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 86 | //while(y1 != y2){ |
ecowboy | 3:1396bac4ae71 | 87 | y1 = _xp.read_u16(); |
ecowboy | 3:1396bac4ae71 | 88 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 89 | y2 = _xp.read_u16(); |
ecowboy | 3:1396bac4ae71 | 90 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 91 | //} |
ecowboy | 3:1396bac4ae71 | 92 | _yp=0; |
ecowboy | 3:1396bac4ae71 | 93 | return (y1+y2)/2; |
ecowboy | 3:1396bac4ae71 | 94 | } |
ecowboy | 3:1396bac4ae71 | 95 | |
ecowboy | 3:1396bac4ae71 | 96 | int Read_Z1(){ |
ecowboy | 3:1396bac4ae71 | 97 | AnalogIn _ym(PTB0); |
ecowboy | 3:1396bac4ae71 | 98 | AnalogIn _xp(PTB3); |
ecowboy | 3:1396bac4ae71 | 99 | DigitalOut _yp(PTB2); |
ecowboy | 3:1396bac4ae71 | 100 | DigitalOut _xm(PTB1); |
ecowboy | 3:1396bac4ae71 | 101 | //uint16_t y1=1; |
ecowboy | 3:1396bac4ae71 | 102 | //uint16_t y2=0; |
ecowboy | 3:1396bac4ae71 | 103 | int y1=1; |
ecowboy | 3:1396bac4ae71 | 104 | int y2=0; |
ecowboy | 3:1396bac4ae71 | 105 | _yp = 1; |
ecowboy | 3:1396bac4ae71 | 106 | _xm = 0; |
ecowboy | 3:1396bac4ae71 | 107 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 108 | //while(y1 != y2){ |
ecowboy | 3:1396bac4ae71 | 109 | int z1 = _xp.read_u16(); |
ecowboy | 3:1396bac4ae71 | 110 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 111 | int z2 = _xp.read_u16(); |
ecowboy | 3:1396bac4ae71 | 112 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 113 | //} |
ecowboy | 3:1396bac4ae71 | 114 | _yp=0; |
ecowboy | 3:1396bac4ae71 | 115 | return (z1+z2)/2; |
ecowboy | 3:1396bac4ae71 | 116 | } |
ecowboy | 3:1396bac4ae71 | 117 | |
ecowboy | 3:1396bac4ae71 | 118 | int Read_Z2(){ |
ecowboy | 3:1396bac4ae71 | 119 | AnalogIn _ym(PTB0); |
ecowboy | 3:1396bac4ae71 | 120 | AnalogIn _xp(PTB3); |
ecowboy | 3:1396bac4ae71 | 121 | DigitalOut _yp(PTB2); |
ecowboy | 3:1396bac4ae71 | 122 | DigitalOut _xm(PTB1); |
ecowboy | 3:1396bac4ae71 | 123 | //uint16_t y1=1; |
ecowboy | 3:1396bac4ae71 | 124 | //uint16_t y2=0; |
ecowboy | 3:1396bac4ae71 | 125 | int y1=1; |
ecowboy | 3:1396bac4ae71 | 126 | int y2=0; |
ecowboy | 3:1396bac4ae71 | 127 | _yp = 1; |
ecowboy | 3:1396bac4ae71 | 128 | _xm = 0; |
ecowboy | 3:1396bac4ae71 | 129 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 130 | //while(y1 != y2){ |
ecowboy | 3:1396bac4ae71 | 131 | int z1 = _ym.read_u16(); |
ecowboy | 3:1396bac4ae71 | 132 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 133 | int z2 = _ym.read_u16(); |
ecowboy | 3:1396bac4ae71 | 134 | wait_us(50); |
ecowboy | 3:1396bac4ae71 | 135 | //} |
ecowboy | 3:1396bac4ae71 | 136 | _yp=0; |
ecowboy | 3:1396bac4ae71 | 137 | return (z1+z2)/2; |
ecowboy | 3:1396bac4ae71 | 138 | } |
ecowboy | 3:1396bac4ae71 | 139 | |
ecowboy | 3:1396bac4ae71 | 140 | |
ecowboy | 1:33506fcfdd95 | 141 | Point getPoint(){ |
ecowboy | 3:1396bac4ae71 | 142 | |
ecowboy | 3:1396bac4ae71 | 143 | //PinName _yp, _ym, _xm, _xp; |
ecowboy | 3:1396bac4ae71 | 144 | float z; |
ecowboy | 3:1396bac4ae71 | 145 | |
ecowboy | 4:87b40e5986e7 | 146 | //int y2 = Read_Y(); |
ecowboy | 3:1396bac4ae71 | 147 | int x2 = Read_X(); |
ecowboy | 3:1396bac4ae71 | 148 | int z1 = Read_Z1(); |
ecowboy | 3:1396bac4ae71 | 149 | int z2 = Read_Z2(); |
ecowboy | 2:fe018965e46c | 150 | |
ecowboy | 4:87b40e5986e7 | 151 | //int y2 = tft.readTouch(); |
ecowboy | 4:87b40e5986e7 | 152 | int y2 = tft.readTouch(PTB3, PTB1, PTB2, PTB0); |
ecowboy | 3:1396bac4ae71 | 153 | //int y2 = tft.readTouch(_yp,_ym,_xp,_xm); |
ecowboy | 3:1396bac4ae71 | 154 | /*int x2 = tft.readTouch(_xp,_xm,_yp,_ym); |
ecowboy | 3:1396bac4ae71 | 155 | int y1 = tft.readTouch(_yp,_ym,_xm,_xp); |
ecowboy | 3:1396bac4ae71 | 156 | int x1 = tft.readTouch(_xp,_xm,_ym,_yp); |
ecowboy | 3:1396bac4ae71 | 157 | int z2 = tft.readTouch(_yp,_xm,_ym,_xp); |
ecowboy | 3:1396bac4ae71 | 158 | int z1 = tft.readTouch(_yp,_xm,_xp,_ym);*/ |
ecowboy | 2:fe018965e46c | 159 | |
ecowboy | 3:1396bac4ae71 | 160 | //int y2 = 700; |
ecowboy | 3:1396bac4ae71 | 161 | //int x2 = 600; |
ecowboy | 3:1396bac4ae71 | 162 | //int z2 = 110; |
ecowboy | 3:1396bac4ae71 | 163 | //int z1 = 100; |
ecowboy | 3:1396bac4ae71 | 164 | |
ecowboy | 1:33506fcfdd95 | 165 | if (z1!=0){ |
ecowboy | 3:1396bac4ae71 | 166 | z = (float)z2/(float)z1; |
ecowboy | 1:33506fcfdd95 | 167 | }else{ |
ecowboy | 1:33506fcfdd95 | 168 | z = 0; |
ecowboy | 1:33506fcfdd95 | 169 | } |
ecowboy | 1:33506fcfdd95 | 170 | int x = x2; |
ecowboy | 1:33506fcfdd95 | 171 | int y = y2; |
ecowboy | 1:33506fcfdd95 | 172 | return point(x,y,z); |
ecowboy | 1:33506fcfdd95 | 173 | } |
ecowboy | 3:1396bac4ae71 | 174 | |
ecowboy | 0:42ec9a44bc24 | 175 | int main(){ |
ecowboy | 0:42ec9a44bc24 | 176 | TFT.claim(stdout); // send stdout to the TFT display |
ecowboy | 0:42ec9a44bc24 | 177 | TFT.set_orientation(0); |
ecowboy | 0:42ec9a44bc24 | 178 | //TFT.Bitmap(28,90,265,65,p1); |
ecowboy | 0:42ec9a44bc24 | 179 | //wait(1); |
ecowboy | 0:42ec9a44bc24 | 180 | //TFT.cls(); |
ecowboy | 0:42ec9a44bc24 | 181 | TFT.set_font((unsigned char*) Arial12x12); |
ecowboy | 0:42ec9a44bc24 | 182 | TFT.foreground(Cyan); // set chars to Cyan |
ecowboy | 3:1396bac4ae71 | 183 | TFT.background(Purple); // set background to Purple |
ecowboy | 0:42ec9a44bc24 | 184 | TFT.locate(80,90); |
ecowboy | 0:42ec9a44bc24 | 185 | //printf("Initializing z...\r\n"); |
ecowboy | 3:1396bac4ae71 | 186 | for(int i = 0; i<16; i++){ |
ecowboy | 3:1396bac4ae71 | 187 | TFT.fillrect(i*15, 0, 15*(i+1), ColorPaletteHigh, colors[i]); |
ecowboy | 0:42ec9a44bc24 | 188 | } |
ecowboy | 3:1396bac4ae71 | 189 | float TS_MINX = 5500; |
ecowboy | 3:1396bac4ae71 | 190 | float TS_MAXX = 58100; |
ecowboy | 3:1396bac4ae71 | 191 | float TS_MINY = 4700; |
ecowboy | 3:1396bac4ae71 | 192 | float TS_MAXY = 57600; |
ecowboy | 3:1396bac4ae71 | 193 | int PRESSURE = 200; |
ecowboy | 0:42ec9a44bc24 | 194 | while(1){ |
ecowboy | 0:42ec9a44bc24 | 195 | // a point object holds x y and z coordinates. |
ecowboy | 3:1396bac4ae71 | 196 | //TFT.BusEnable(false); |
ecowboy | 2:fe018965e46c | 197 | Point p = getPoint(); |
ecowboy | 3:1396bac4ae71 | 198 | //TFT.BusEnable(true); |
ecowboy | 0:42ec9a44bc24 | 199 | //map the ADC value read to into pixel co-ordinates |
ecowboy | 2:fe018965e46c | 200 | p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); |
ecowboy | 2:fe018965e46c | 201 | p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); |
ecowboy | 3:1396bac4ae71 | 202 | p.z = p.x * RXPLATE / Q * (p.z -1); |
ecowboy | 0:42ec9a44bc24 | 203 | // we have some minimum pressure we consider 'valid' |
ecowboy | 0:42ec9a44bc24 | 204 | // pressure of 0 means no pressing! |
ecowboy | 3:1396bac4ae71 | 205 | /*TFT.locate(60,90); |
ecowboy | 2:fe018965e46c | 206 | printf("x = %d \r\n", p.x); |
ecowboy | 2:fe018965e46c | 207 | TFT.locate(60,110); |
ecowboy | 2:fe018965e46c | 208 | printf("y = %d \r\n", p.y); |
ecowboy | 2:fe018965e46c | 209 | TFT.locate(60,130); |
ecowboy | 2:fe018965e46c | 210 | printf("z = %d \r\n", p.z); |
ecowboy | 3:1396bac4ae71 | 211 | //TFT.locate(60,170);*/ |
ecowboy | 3:1396bac4ae71 | 212 | //printf("Initializing z...%d \r\n",zx); |
ecowboy | 3:1396bac4ae71 | 213 | if (p.z > 40 && p.z < PRESSURE) { |
ecowboy | 0:42ec9a44bc24 | 214 | // Detect paint brush color change |
ecowboy | 3:1396bac4ae71 | 215 | if(int(p.y) < ColorPaletteHigh+2){ |
ecowboy | 3:1396bac4ae71 | 216 | color = colors[int(p.x)/15]; |
ecowboy | 0:42ec9a44bc24 | 217 | }else{ |
ecowboy | 3:1396bac4ae71 | 218 | TFT.fillcircle(int(p.x),int(p.y),2,color); |
ecowboy | 0:42ec9a44bc24 | 219 | } |
ecowboy | 2:fe018965e46c | 220 | } |
ecowboy | 0:42ec9a44bc24 | 221 | } |
ecowboy | 0:42ec9a44bc24 | 222 | } |