![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Paint for the FRDM-KL25Z
Dependencies: mbed TFT_fonts SPI_TFT_ILI9341
main.cpp@2:fe018965e46c, 2019-01-08 (annotated)
- Committer:
- ecowboy
- Date:
- Tue Jan 08 06:56:55 2019 +0000
- Revision:
- 2:fe018965e46c
- Parent:
- 1:33506fcfdd95
- Child:
- 3:1396bac4ae71
Point error resolved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ecowboy | 0:42ec9a44bc24 | 1 | #include "mbed.h" |
ecowboy | 0:42ec9a44bc24 | 2 | #include "ILI932x.h" |
ecowboy | 0:42ec9a44bc24 | 3 | |
ecowboy | 0:42ec9a44bc24 | 4 | //#include "SDFileSystem.h" |
ecowboy | 0:42ec9a44bc24 | 5 | //#include "SPI.h" |
ecowboy | 0:42ec9a44bc24 | 6 | #include "BMP565.h" |
ecowboy | 0:42ec9a44bc24 | 7 | #include "Arial12x12.h" |
ecowboy | 0:42ec9a44bc24 | 8 | #include "stdio.h" |
ecowboy | 0:42ec9a44bc24 | 9 | #include "touch.h" |
ecowboy | 0:42ec9a44bc24 | 10 | |
ecowboy | 0:42ec9a44bc24 | 11 | // For better pressure precision, we need to know the resistance |
ecowboy | 0:42ec9a44bc24 | 12 | // between X+ and X- Use any multimeter to read it |
ecowboy | 0:42ec9a44bc24 | 13 | // The 2.8" TFT Touch shield has 300 ohms across the X plate |
ecowboy | 0:42ec9a44bc24 | 14 | |
ecowboy | 0:42ec9a44bc24 | 15 | //Measured ADC values for (0,0) and (210-1,320-1) |
ecowboy | 0:42ec9a44bc24 | 16 | //TS_MINX corresponds to ADC value when X = 0 |
ecowboy | 0:42ec9a44bc24 | 17 | //TS_MINY corresponds to ADC value when Y = 0 |
ecowboy | 0:42ec9a44bc24 | 18 | //TS_MAXX corresponds to ADC value when X = 240 -1 |
ecowboy | 0:42ec9a44bc24 | 19 | //TS_MAXY corresponds to ADC value when Y = 320 -1 |
ecowboy | 0:42ec9a44bc24 | 20 | |
ecowboy | 0:42ec9a44bc24 | 21 | #define PIN_YP PTB3 // must be an analog pin, use "An" notation! |
ecowboy | 0:42ec9a44bc24 | 22 | #define PIN_XM PTB2 // must be an analog pin, use "An" notation! |
ecowboy | 0:42ec9a44bc24 | 23 | #define PIN_YM PTD5 // can be a digital pin |
ecowboy | 0:42ec9a44bc24 | 24 | #define PIN_XP PTA13 // can be a digital pin |
ecowboy | 0:42ec9a44bc24 | 25 | |
ecowboy | 0:42ec9a44bc24 | 26 | #define PIN_CS_TFT PTB3 |
ecowboy | 0:42ec9a44bc24 | 27 | #define PIN_RST_TFT PTC2 |
ecowboy | 0:42ec9a44bc24 | 28 | #define PIN_DC_TFT PTB2 |
ecowboy | 0:42ec9a44bc24 | 29 | #define PIN_WR_TFT PTB1 |
ecowboy | 0:42ec9a44bc24 | 30 | #define PIN_RD_TFT PTB0 |
ecowboy | 0:42ec9a44bc24 | 31 | |
ecowboy | 0:42ec9a44bc24 | 32 | #if 0 |
ecowboy | 0:42ec9a44bc24 | 33 | #define PIN_CS_SD PTD0 |
ecowboy | 0:42ec9a44bc24 | 34 | #define PIN_MOSI PTD2 |
ecowboy | 0:42ec9a44bc24 | 35 | #define PIN_MISO PTD3 |
ecowboy | 0:42ec9a44bc24 | 36 | #define PIN_SCLK PTD1 |
ecowboy | 0:42ec9a44bc24 | 37 | #endif |
ecowboy | 0:42ec9a44bc24 | 38 | |
ecowboy | 0:42ec9a44bc24 | 39 | #define PIN_CS_TSC PTD0 |
ecowboy | 0:42ec9a44bc24 | 40 | #define PIN_MOSI PTD2 |
ecowboy | 0:42ec9a44bc24 | 41 | #define PIN_MISO PTD3 |
ecowboy | 0:42ec9a44bc24 | 42 | #define PIN_SCLK PTD1 |
ecowboy | 0:42ec9a44bc24 | 43 | |
ecowboy | 0:42ec9a44bc24 | 44 | PinName dataBus[] = {PTA13,PTD5,PTD4,PTA12,PTA4,PTA5,PTC8,PTC9}; |
ecowboy | 0:42ec9a44bc24 | 45 | |
ecowboy | 0:42ec9a44bc24 | 46 | ILI932x TFT(BUS_8, dataBus, PIN_CS_TFT, PIN_RST_TFT, PIN_DC_TFT, PIN_WR_TFT, PIN_RD_TFT, "TFT", 240, 320); |
ecowboy | 0:42ec9a44bc24 | 47 | |
ecowboy | 0:42ec9a44bc24 | 48 | //SDFileSystem sd(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_SD, "sd"); |
ecowboy | 0:42ec9a44bc24 | 49 | |
ecowboy | 0:42ec9a44bc24 | 50 | // Paint application - Demonstate both TFT and Touch Screen |
ecowboy | 0:42ec9a44bc24 | 51 | int ColorPaletteHigh = 30; |
ecowboy | 0:42ec9a44bc24 | 52 | int color = White; //Paint brush color |
ecowboy | 0:42ec9a44bc24 | 53 | unsigned int colors[8] = {Red, Green, Blue, Cyan, Yellow, Black, White, LightGrey}; |
ecowboy | 0:42ec9a44bc24 | 54 | |
ecowboy | 0:42ec9a44bc24 | 55 | |
ecowboy | 0:42ec9a44bc24 | 56 | TouchScreen tft(PIN_XP, PIN_YP, PIN_XM, PIN_YM); //init TouchScreen port pins |
ecowboy | 0:42ec9a44bc24 | 57 | |
ecowboy | 1:33506fcfdd95 | 58 | struct point |
ecowboy | 1:33506fcfdd95 | 59 | { |
ecowboy | 1:33506fcfdd95 | 60 | int x; |
ecowboy | 1:33506fcfdd95 | 61 | int y; |
ecowboy | 1:33506fcfdd95 | 62 | int z; |
ecowboy | 1:33506fcfdd95 | 63 | }; |
ecowboy | 1:33506fcfdd95 | 64 | |
ecowboy | 1:33506fcfdd95 | 65 | typedef struct point Point; |
ecowboy | 1:33506fcfdd95 | 66 | |
ecowboy | 1:33506fcfdd95 | 67 | |
ecowboy | 1:33506fcfdd95 | 68 | Point point(int xx, int yy, int zz){ |
ecowboy | 1:33506fcfdd95 | 69 | Point p; |
ecowboy | 1:33506fcfdd95 | 70 | p.x = xx; |
ecowboy | 1:33506fcfdd95 | 71 | p.y = yy; |
ecowboy | 1:33506fcfdd95 | 72 | p.z = zz; |
ecowboy | 1:33506fcfdd95 | 73 | return p; |
ecowboy | 1:33506fcfdd95 | 74 | } |
ecowboy | 1:33506fcfdd95 | 75 | |
ecowboy | 1:33506fcfdd95 | 76 | Point getPoint(){ |
ecowboy | 1:33506fcfdd95 | 77 | PinName _yp, _ym, _xm, _xp; |
ecowboy | 2:fe018965e46c | 78 | int z; |
ecowboy | 2:fe018965e46c | 79 | int RXPLATE = 350; |
ecowboy | 2:fe018965e46c | 80 | int Q = 1024; //10 bit TSC Resolution |
ecowboy | 2:fe018965e46c | 81 | /*int y2 = tft.readTouch(PIN_XP, PIN_YP, PIN_XM, PIN_YM); |
ecowboy | 2:fe018965e46c | 82 | /*int x2 = tft.readTouch(_yp,_ym,_xm,_xp); |
ecowboy | 1:33506fcfdd95 | 83 | int y1 = tft.readTouch(_xp,_xm,_ym,_yp); |
ecowboy | 1:33506fcfdd95 | 84 | int x1 = tft.readTouch(_yp,_ym,_xp,_xm); |
ecowboy | 1:33506fcfdd95 | 85 | int z2 = tft.readTouch(_ym,_xp,_yp,_xm); |
ecowboy | 2:fe018965e46c | 86 | int z1 = tft.readTouch(_ym,_xp,_xm,_yp);*/ |
ecowboy | 2:fe018965e46c | 87 | |
ecowboy | 2:fe018965e46c | 88 | int y2 = 700; |
ecowboy | 2:fe018965e46c | 89 | int x2 = 600; |
ecowboy | 2:fe018965e46c | 90 | int z2 = 110; |
ecowboy | 2:fe018965e46c | 91 | int z1 = 100; |
ecowboy | 2:fe018965e46c | 92 | |
ecowboy | 1:33506fcfdd95 | 93 | |
ecowboy | 1:33506fcfdd95 | 94 | if (z1!=0){ |
ecowboy | 2:fe018965e46c | 95 | z = (float)RXPLATE/(float)Q*x2*((float)z2/(float)z1-1); |
ecowboy | 1:33506fcfdd95 | 96 | }else{ |
ecowboy | 1:33506fcfdd95 | 97 | z = 0; |
ecowboy | 1:33506fcfdd95 | 98 | } |
ecowboy | 1:33506fcfdd95 | 99 | int x = x2; |
ecowboy | 1:33506fcfdd95 | 100 | int y = y2; |
ecowboy | 1:33506fcfdd95 | 101 | |
ecowboy | 1:33506fcfdd95 | 102 | return point(x,y,z); |
ecowboy | 1:33506fcfdd95 | 103 | } |
ecowboy | 0:42ec9a44bc24 | 104 | |
ecowboy | 0:42ec9a44bc24 | 105 | int main(){ |
ecowboy | 0:42ec9a44bc24 | 106 | |
ecowboy | 0:42ec9a44bc24 | 107 | TFT.claim(stdout); // send stdout to the TFT display |
ecowboy | 0:42ec9a44bc24 | 108 | TFT.set_orientation(0); |
ecowboy | 0:42ec9a44bc24 | 109 | //TFT.Bitmap(28,90,265,65,p1); |
ecowboy | 0:42ec9a44bc24 | 110 | //wait(1); |
ecowboy | 0:42ec9a44bc24 | 111 | //TFT.cls(); |
ecowboy | 0:42ec9a44bc24 | 112 | TFT.set_font((unsigned char*) Arial12x12); |
ecowboy | 0:42ec9a44bc24 | 113 | TFT.foreground(Cyan); // set chars to Cyan |
ecowboy | 0:42ec9a44bc24 | 114 | TFT.locate(80,90); |
ecowboy | 0:42ec9a44bc24 | 115 | //printf("Initializing z...\r\n"); |
ecowboy | 0:42ec9a44bc24 | 116 | for(int i = 0; i<8; i++){ |
ecowboy | 0:42ec9a44bc24 | 117 | TFT.fillrect(i*30, 0, 30*(i+1), ColorPaletteHigh, colors[i]); |
ecowboy | 0:42ec9a44bc24 | 118 | } |
ecowboy | 2:fe018965e46c | 119 | int zx=0; |
ecowboy | 2:fe018965e46c | 120 | float TS_MINX = 116; |
ecowboy | 2:fe018965e46c | 121 | float TS_MAXX = 890; |
ecowboy | 2:fe018965e46c | 122 | float TS_MINY = 83; |
ecowboy | 2:fe018965e46c | 123 | float TS_MAXY = 913; |
ecowboy | 2:fe018965e46c | 124 | int PRESSURE = 10; |
ecowboy | 0:42ec9a44bc24 | 125 | while(1){ |
ecowboy | 2:fe018965e46c | 126 | zx = zx + 1; |
ecowboy | 0:42ec9a44bc24 | 127 | // a point object holds x y and z coordinates. |
ecowboy | 2:fe018965e46c | 128 | Point p = getPoint(); |
ecowboy | 0:42ec9a44bc24 | 129 | //map the ADC value read to into pixel co-ordinates |
ecowboy | 2:fe018965e46c | 130 | p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); |
ecowboy | 2:fe018965e46c | 131 | p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); |
ecowboy | 0:42ec9a44bc24 | 132 | |
ecowboy | 0:42ec9a44bc24 | 133 | // we have some minimum pressure we consider 'valid' |
ecowboy | 0:42ec9a44bc24 | 134 | // pressure of 0 means no pressing! |
ecowboy | 2:fe018965e46c | 135 | TFT.locate(60,90); |
ecowboy | 2:fe018965e46c | 136 | printf("x = %d \r\n", p.x); |
ecowboy | 2:fe018965e46c | 137 | TFT.locate(60,110); |
ecowboy | 2:fe018965e46c | 138 | printf("y = %d \r\n", p.y); |
ecowboy | 2:fe018965e46c | 139 | TFT.locate(60,130); |
ecowboy | 2:fe018965e46c | 140 | printf("z = %d \r\n", p.z); |
ecowboy | 2:fe018965e46c | 141 | TFT.locate(60,170); |
ecowboy | 2:fe018965e46c | 142 | printf("Initializing z...%d \r\n",zx); |
ecowboy | 2:fe018965e46c | 143 | if (p.z > PRESSURE) { |
ecowboy | 0:42ec9a44bc24 | 144 | // Detect paint brush color change |
ecowboy | 0:42ec9a44bc24 | 145 | if(p.y < ColorPaletteHigh+2){ |
ecowboy | 0:42ec9a44bc24 | 146 | color = colors[p.x/30]; |
ecowboy | 0:42ec9a44bc24 | 147 | }else{ |
ecowboy | 0:42ec9a44bc24 | 148 | TFT.fillcircle(p.x,p.y,2,color); |
ecowboy | 0:42ec9a44bc24 | 149 | } |
ecowboy | 2:fe018965e46c | 150 | } |
ecowboy | 0:42ec9a44bc24 | 151 | } |
ecowboy | 0:42ec9a44bc24 | 152 | } |