![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Paint for the FRDM-KL25Z
Dependencies: mbed TFT_fonts SPI_TFT_ILI9341
main.cpp@1:33506fcfdd95, 2019-01-07 (annotated)
- Committer:
- ecowboy
- Date:
- Mon Jan 07 16:15:52 2019 +0000
- Revision:
- 1:33506fcfdd95
- Parent:
- 0:42ec9a44bc24
- Child:
- 2:fe018965e46c
Struct Point Change
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 TS_MINX 116*2 |
ecowboy | 0:42ec9a44bc24 | 22 | #define TS_MAXX 890*2 |
ecowboy | 0:42ec9a44bc24 | 23 | #define TS_MINY 83*2 |
ecowboy | 0:42ec9a44bc24 | 24 | #define TS_MAXY 913*2 |
ecowboy | 0:42ec9a44bc24 | 25 | |
ecowboy | 0:42ec9a44bc24 | 26 | #define PIN_YP PTB3 // must be an analog pin, use "An" notation! |
ecowboy | 0:42ec9a44bc24 | 27 | #define PIN_XM PTB2 // must be an analog pin, use "An" notation! |
ecowboy | 0:42ec9a44bc24 | 28 | #define PIN_YM PTD5 // can be a digital pin |
ecowboy | 0:42ec9a44bc24 | 29 | #define PIN_XP PTA13 // can be a digital pin |
ecowboy | 0:42ec9a44bc24 | 30 | |
ecowboy | 0:42ec9a44bc24 | 31 | #define PIN_CS_TFT PTB3 |
ecowboy | 0:42ec9a44bc24 | 32 | #define PIN_RST_TFT PTC2 |
ecowboy | 0:42ec9a44bc24 | 33 | #define PIN_DC_TFT PTB2 |
ecowboy | 0:42ec9a44bc24 | 34 | #define PIN_WR_TFT PTB1 |
ecowboy | 0:42ec9a44bc24 | 35 | #define PIN_RD_TFT PTB0 |
ecowboy | 0:42ec9a44bc24 | 36 | |
ecowboy | 0:42ec9a44bc24 | 37 | #if 0 |
ecowboy | 0:42ec9a44bc24 | 38 | #define PIN_CS_SD PTD0 |
ecowboy | 0:42ec9a44bc24 | 39 | #define PIN_MOSI PTD2 |
ecowboy | 0:42ec9a44bc24 | 40 | #define PIN_MISO PTD3 |
ecowboy | 0:42ec9a44bc24 | 41 | #define PIN_SCLK PTD1 |
ecowboy | 0:42ec9a44bc24 | 42 | #endif |
ecowboy | 0:42ec9a44bc24 | 43 | |
ecowboy | 0:42ec9a44bc24 | 44 | #define PIN_CS_TSC PTD0 |
ecowboy | 0:42ec9a44bc24 | 45 | #define PIN_MOSI PTD2 |
ecowboy | 0:42ec9a44bc24 | 46 | #define PIN_MISO PTD3 |
ecowboy | 0:42ec9a44bc24 | 47 | #define PIN_SCLK PTD1 |
ecowboy | 0:42ec9a44bc24 | 48 | |
ecowboy | 0:42ec9a44bc24 | 49 | PinName dataBus[] = {PTA13,PTD5,PTD4,PTA12,PTA4,PTA5,PTC8,PTC9}; |
ecowboy | 0:42ec9a44bc24 | 50 | |
ecowboy | 0:42ec9a44bc24 | 51 | 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 | 52 | |
ecowboy | 0:42ec9a44bc24 | 53 | //SDFileSystem sd(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_SD, "sd"); |
ecowboy | 0:42ec9a44bc24 | 54 | |
ecowboy | 0:42ec9a44bc24 | 55 | // Paint application - Demonstate both TFT and Touch Screen |
ecowboy | 0:42ec9a44bc24 | 56 | int ColorPaletteHigh = 30; |
ecowboy | 0:42ec9a44bc24 | 57 | int color = White; //Paint brush color |
ecowboy | 0:42ec9a44bc24 | 58 | unsigned int colors[8] = {Red, Green, Blue, Cyan, Yellow, Black, White, LightGrey}; |
ecowboy | 0:42ec9a44bc24 | 59 | |
ecowboy | 0:42ec9a44bc24 | 60 | |
ecowboy | 0:42ec9a44bc24 | 61 | TouchScreen tft(PIN_XP, PIN_YP, PIN_XM, PIN_YM); //init TouchScreen port pins |
ecowboy | 0:42ec9a44bc24 | 62 | |
ecowboy | 1:33506fcfdd95 | 63 | struct point |
ecowboy | 1:33506fcfdd95 | 64 | { |
ecowboy | 1:33506fcfdd95 | 65 | int x; |
ecowboy | 1:33506fcfdd95 | 66 | int y; |
ecowboy | 1:33506fcfdd95 | 67 | int z; |
ecowboy | 1:33506fcfdd95 | 68 | }; |
ecowboy | 1:33506fcfdd95 | 69 | |
ecowboy | 1:33506fcfdd95 | 70 | typedef struct point Point; |
ecowboy | 1:33506fcfdd95 | 71 | |
ecowboy | 1:33506fcfdd95 | 72 | |
ecowboy | 1:33506fcfdd95 | 73 | Point point(int xx, int yy, int zz){ |
ecowboy | 1:33506fcfdd95 | 74 | Point p; |
ecowboy | 1:33506fcfdd95 | 75 | p.x = xx; |
ecowboy | 1:33506fcfdd95 | 76 | p.y = yy; |
ecowboy | 1:33506fcfdd95 | 77 | p.z = zz; |
ecowboy | 1:33506fcfdd95 | 78 | return p; |
ecowboy | 1:33506fcfdd95 | 79 | } |
ecowboy | 1:33506fcfdd95 | 80 | |
ecowboy | 1:33506fcfdd95 | 81 | Point getPoint(){ |
ecowboy | 1:33506fcfdd95 | 82 | PinName _yp, _ym, _xm, _xp; |
ecowboy | 1:33506fcfdd95 | 83 | int z = 0; |
ecowboy | 1:33506fcfdd95 | 84 | int y2 = tft.readTouch(_xp,_xm,_yp,_ym); |
ecowboy | 1:33506fcfdd95 | 85 | int x2 = tft.readTouch(_yp,_ym,_xm,_xp); |
ecowboy | 1:33506fcfdd95 | 86 | int y1 = tft.readTouch(_xp,_xm,_ym,_yp); |
ecowboy | 1:33506fcfdd95 | 87 | int x1 = tft.readTouch(_yp,_ym,_xp,_xm); |
ecowboy | 1:33506fcfdd95 | 88 | int z2 = tft.readTouch(_ym,_xp,_yp,_xm); |
ecowboy | 1:33506fcfdd95 | 89 | int z1 = tft.readTouch(_ym,_xp,_xm,_yp); |
ecowboy | 1:33506fcfdd95 | 90 | |
ecowboy | 1:33506fcfdd95 | 91 | if (z1!=0){ |
ecowboy | 1:33506fcfdd95 | 92 | z = RXPLATE*x2/Q*(z2/z1-1); |
ecowboy | 1:33506fcfdd95 | 93 | }else{ |
ecowboy | 1:33506fcfdd95 | 94 | z = 0; |
ecowboy | 1:33506fcfdd95 | 95 | } |
ecowboy | 1:33506fcfdd95 | 96 | int x = x2; |
ecowboy | 1:33506fcfdd95 | 97 | int y = y2; |
ecowboy | 1:33506fcfdd95 | 98 | |
ecowboy | 1:33506fcfdd95 | 99 | return point(x,y,z); |
ecowboy | 1:33506fcfdd95 | 100 | } |
ecowboy | 0:42ec9a44bc24 | 101 | |
ecowboy | 0:42ec9a44bc24 | 102 | int main(){ |
ecowboy | 0:42ec9a44bc24 | 103 | |
ecowboy | 0:42ec9a44bc24 | 104 | TFT.claim(stdout); // send stdout to the TFT display |
ecowboy | 0:42ec9a44bc24 | 105 | TFT.set_orientation(0); |
ecowboy | 0:42ec9a44bc24 | 106 | //TFT.Bitmap(28,90,265,65,p1); |
ecowboy | 0:42ec9a44bc24 | 107 | //wait(1); |
ecowboy | 0:42ec9a44bc24 | 108 | //TFT.cls(); |
ecowboy | 0:42ec9a44bc24 | 109 | TFT.set_font((unsigned char*) Arial12x12); |
ecowboy | 0:42ec9a44bc24 | 110 | TFT.foreground(Cyan); // set chars to Cyan |
ecowboy | 0:42ec9a44bc24 | 111 | TFT.locate(80,90); |
ecowboy | 0:42ec9a44bc24 | 112 | //printf("Initializing z...\r\n"); |
ecowboy | 0:42ec9a44bc24 | 113 | for(int i = 0; i<8; i++){ |
ecowboy | 0:42ec9a44bc24 | 114 | TFT.fillrect(i*30, 0, 30*(i+1), ColorPaletteHigh, colors[i]); |
ecowboy | 0:42ec9a44bc24 | 115 | } |
ecowboy | 0:42ec9a44bc24 | 116 | int zz=0; |
ecowboy | 0:42ec9a44bc24 | 117 | while(1){ |
ecowboy | 0:42ec9a44bc24 | 118 | zz = zz + 1; |
ecowboy | 0:42ec9a44bc24 | 119 | // a point object holds x y and z coordinates. |
ecowboy | 1:33506fcfdd95 | 120 | //Point p = getPoint(); |
ecowboy | 0:42ec9a44bc24 | 121 | //map the ADC value read to into pixel co-ordinates |
ecowboy | 0:42ec9a44bc24 | 122 | //p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); |
ecowboy | 0:42ec9a44bc24 | 123 | //p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); |
ecowboy | 0:42ec9a44bc24 | 124 | |
ecowboy | 0:42ec9a44bc24 | 125 | // we have some minimum pressure we consider 'valid' |
ecowboy | 0:42ec9a44bc24 | 126 | // pressure of 0 means no pressing! |
ecowboy | 1:33506fcfdd95 | 127 | //TFT.locate(60,90); |
ecowboy | 1:33506fcfdd95 | 128 | //printf("z = %d \r\n", p.x); |
ecowboy | 0:42ec9a44bc24 | 129 | TFT.locate(60,120); |
ecowboy | 0:42ec9a44bc24 | 130 | printf("Initializing z...%d \r\n",zz); |
ecowboy | 0:42ec9a44bc24 | 131 | /*if (p.z > PRESSURE) { |
ecowboy | 0:42ec9a44bc24 | 132 | // Detect paint brush color change |
ecowboy | 0:42ec9a44bc24 | 133 | if(p.y < ColorPaletteHigh+2){ |
ecowboy | 0:42ec9a44bc24 | 134 | color = colors[p.x/30]; |
ecowboy | 0:42ec9a44bc24 | 135 | }else{ |
ecowboy | 0:42ec9a44bc24 | 136 | TFT.fillcircle(p.x,p.y,2,color); |
ecowboy | 0:42ec9a44bc24 | 137 | } |
ecowboy | 0:42ec9a44bc24 | 138 | }*/ |
ecowboy | 0:42ec9a44bc24 | 139 | } |
ecowboy | 0:42ec9a44bc24 | 140 | } |