Paint for the FRDM-KL25Z

Dependencies:   mbed TFT_fonts SPI_TFT_ILI9341

Committer:
ecowboy
Date:
Sat Jan 12 03:55:06 2019 +0000
Revision:
7:a72b2f80ae04
Parent:
6:13d0de9e679c
Paint for the FRDm-KL25Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ecowboy 6:13d0de9e679c 1 /* KL25Z mbed library for ILI9341 touch devices
ecowboy 6:13d0de9e679c 2 * SPI Interface
ecowboy 6:13d0de9e679c 3
ecowboy 6:13d0de9e679c 4 * Uses Peter Drescher ILI9341 Library
ecowboy 6:13d0de9e679c 5 *
ecowboy 6:13d0de9e679c 6 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ecowboy 6:13d0de9e679c 7 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ecowboy 6:13d0de9e679c 8 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ecowboy 6:13d0de9e679c 9 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ecowboy 6:13d0de9e679c 10 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ecowboy 6:13d0de9e679c 11 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ecowboy 6:13d0de9e679c 12 * THE SOFTWARE.
ecowboy 6:13d0de9e679c 13 */
ecowboy 6:13d0de9e679c 14
ecowboy 6:13d0de9e679c 15
ecowboy 0:42ec9a44bc24 16 #include "mbed.h"
ecowboy 3:1396bac4ae71 17 #include "SPI_TFT_ILI9341.h"
ecowboy 3:1396bac4ae71 18 #include "Arial12x12.h"
ecowboy 0:42ec9a44bc24 19 #include "touch.h"
ecowboy 0:42ec9a44bc24 20
ecowboy 0:42ec9a44bc24 21
ecowboy 3:1396bac4ae71 22 #define PIN_XP PTB3
ecowboy 5:80791250acb2 23 #define PIN_XM PTB1
ecowboy 5:80791250acb2 24 #define PIN_YP PTB2
ecowboy 3:1396bac4ae71 25 #define PIN_YM PTB0
ecowboy 3:1396bac4ae71 26 #define PIN_MOSI PTD2
ecowboy 3:1396bac4ae71 27 #define PIN_MISO PTD3
ecowboy 3:1396bac4ae71 28 #define PIN_SCLK PTD1
ecowboy 3:1396bac4ae71 29 #define PIN_CS_TFT PTA5
ecowboy 3:1396bac4ae71 30 #define PIN_DC_TFT PTC8
ecowboy 3:1396bac4ae71 31 #define PIN_BL_TFT PTC9
ecowboy 3:1396bac4ae71 32 #define PIN_CS_SD PTA4
ecowboy 0:42ec9a44bc24 33
ecowboy 0:42ec9a44bc24 34
ecowboy 6:13d0de9e679c 35 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT, "TFT"); //PIN_BL_TFT does not have to be connected
ecowboy 3:1396bac4ae71 36 TouchScreen tft(PIN_XP, PIN_YP, PIN_XM, PIN_YM); //init TouchScreen port pins
ecowboy 0:42ec9a44bc24 37
ecowboy 0:42ec9a44bc24 38
ecowboy 0:42ec9a44bc24 39 // Paint application - Demonstate both TFT and Touch Screen
ecowboy 6:13d0de9e679c 40 int ColorPaletteHigh = 15; //Height of color palette
ecowboy 6:13d0de9e679c 41 int color = White; //Paint brush initial color
ecowboy 3:1396bac4ae71 42 unsigned int colors[16] = {Red, Green, Blue, Cyan, Yellow, Purple, Pink, Black, Orange, Brown, BlueGreen, White, LightPink, DarkGrey, DarkGreen, Mustard};
ecowboy 0:42ec9a44bc24 43
ecowboy 6:13d0de9e679c 44 int RXPLATE = 350; //Set this to the resistance measured across XP and XM
ecowboy 3:1396bac4ae71 45 int Q = 1024; //10 bit TSC Resolution
ecowboy 6:13d0de9e679c 46 float TS_MINX = 5500; //Set this to screen x min
ecowboy 6:13d0de9e679c 47 float TS_MAXX = 58100; //Set this to screen x max
ecowboy 6:13d0de9e679c 48 float TS_MINY = 4700; //Set this to screen y min
ecowboy 6:13d0de9e679c 49 float TS_MAXY = 57600; //Set this to screen y max
ecowboy 6:13d0de9e679c 50 int PRESSURE = 200; //Set this to max pressure (z) that will register as a touch event
ecowboy 6:13d0de9e679c 51 int v[3] ={0,0,0}; //array to roll touches for comparison and validation, initialized at zero briefly.
ecowboy 0:42ec9a44bc24 52
ecowboy 1:33506fcfdd95 53 struct point
ecowboy 1:33506fcfdd95 54 {
ecowboy 1:33506fcfdd95 55 int x;
ecowboy 1:33506fcfdd95 56 int y;
ecowboy 1:33506fcfdd95 57 int z;
ecowboy 1:33506fcfdd95 58 };
ecowboy 1:33506fcfdd95 59
ecowboy 1:33506fcfdd95 60 typedef struct point Point;
ecowboy 1:33506fcfdd95 61
ecowboy 1:33506fcfdd95 62
ecowboy 1:33506fcfdd95 63 Point point(int xx, int yy, int zz){
ecowboy 1:33506fcfdd95 64 Point p;
ecowboy 1:33506fcfdd95 65 p.x = xx;
ecowboy 1:33506fcfdd95 66 p.y = yy;
ecowboy 1:33506fcfdd95 67 p.z = zz;
ecowboy 1:33506fcfdd95 68 return p;
ecowboy 3:1396bac4ae71 69 }
ecowboy 3:1396bac4ae71 70
ecowboy 3:1396bac4ae71 71
ecowboy 1:33506fcfdd95 72 Point getPoint(){
ecowboy 3:1396bac4ae71 73
ecowboy 3:1396bac4ae71 74 float z;
ecowboy 6:13d0de9e679c 75 int y2 = tft.readTouch(PTB3,PTB1,PTB2,PTB0); //a(analog),i(analog),n(digital),m(digital)
ecowboy 6:13d0de9e679c 76 int x2 = tft.readTouch(PTB2,PTB0,PTB3,PTB1); //Resources to help set these up
ecowboy 6:13d0de9e679c 77 int z2 = tft.readTouch(PTB0,PTB3,PTB2,PTB1); //http://electronics.stackexchange.com/questions/113223/4-wire-resisitive-touch-screen-not-working
ecowboy 6:13d0de9e679c 78 int z1 = tft.readTouch(PTB3,PTB0,PTB2,PTB1); //http://www.ti.com/lit/an/sbaa155a/sbaa155a.pdf
ecowboy 3:1396bac4ae71 79
ecowboy 1:33506fcfdd95 80 if (z1!=0){
ecowboy 3:1396bac4ae71 81 z = (float)z2/(float)z1;
ecowboy 1:33506fcfdd95 82 }else{
ecowboy 1:33506fcfdd95 83 z = 0;
ecowboy 1:33506fcfdd95 84 }
ecowboy 1:33506fcfdd95 85 int x = x2;
ecowboy 1:33506fcfdd95 86 int y = y2;
ecowboy 1:33506fcfdd95 87 return point(x,y,z);
ecowboy 1:33506fcfdd95 88 }
ecowboy 6:13d0de9e679c 89
ecowboy 3:1396bac4ae71 90
ecowboy 5:80791250acb2 91
ecowboy 0:42ec9a44bc24 92 int main(){
ecowboy 0:42ec9a44bc24 93 TFT.claim(stdout); // send stdout to the TFT display
ecowboy 0:42ec9a44bc24 94 TFT.set_orientation(0);
ecowboy 6:13d0de9e679c 95 TFT.set_font((unsigned char*) Arial12x12); //set font to arial 12x12
ecowboy 6:13d0de9e679c 96 TFT.foreground(Cyan); // set text chars to Cyan
ecowboy 6:13d0de9e679c 97 TFT.background(Purple); // set text background to Purple
ecowboy 6:13d0de9e679c 98
ecowboy 3:1396bac4ae71 99 for(int i = 0; i<16; i++){
ecowboy 6:13d0de9e679c 100 TFT.fillrect(i*15, 0, 15*(i+1), ColorPaletteHigh, colors[i]); //Fill color palete with 16 colors defined in SPI_TFT_ILI9341.h
ecowboy 0:42ec9a44bc24 101 }
ecowboy 0:42ec9a44bc24 102 while(1){
ecowboy 0:42ec9a44bc24 103 // a point object holds x y and z coordinates.
ecowboy 2:fe018965e46c 104 Point p = getPoint();
ecowboy 0:42ec9a44bc24 105 //map the ADC value read to into pixel co-ordinates
ecowboy 6:13d0de9e679c 106 p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240); //Use map functionm to size p.x to screen size
ecowboy 6:13d0de9e679c 107 p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320); //Use map functionm to size p.y to screen size
ecowboy 6:13d0de9e679c 108 p.z = 20+p.x * RXPLATE / Q * (p.z -1); //Calc p.z based on above reference links
ecowboy 6:13d0de9e679c 109
ecowboy 6:13d0de9e679c 110 v[2]=v[1]; //Rotate p.z touch values in array
ecowboy 6:13d0de9e679c 111 v[1]=v[0];
ecowboy 6:13d0de9e679c 112 v[0]=p.z;
ecowboy 6:13d0de9e679c 113
ecowboy 3:1396bac4ae71 114 /*TFT.locate(60,90);
ecowboy 6:13d0de9e679c 115 printf("x = %d \r\n", p.x); //Uncomment these to see point data on screen and to obtain calibration values
ecowboy 2:fe018965e46c 116 TFT.locate(60,110);
ecowboy 2:fe018965e46c 117 printf("y = %d \r\n", p.y);
ecowboy 2:fe018965e46c 118 TFT.locate(60,130);
ecowboy 6:13d0de9e679c 119 printf("z = %d \r\n", p.z);*/
ecowboy 6:13d0de9e679c 120
ecowboy 6:13d0de9e679c 121 // we have some minimum pressure we consider 'valid'
ecowboy 6:13d0de9e679c 122 // pressure of 0 means no pressing!
ecowboy 6:13d0de9e679c 123 if (p.z > 40 && p.z < PRESSURE & p.z == (v[1]+v[2])/2) { //Validate that p.z senses a touch event
ecowboy 0:42ec9a44bc24 124 // Detect paint brush color change
ecowboy 3:1396bac4ae71 125 if(int(p.y) < ColorPaletteHigh+2){
ecowboy 6:13d0de9e679c 126 color = colors[int(p.x)/15]; //Touch event occured in color palette, change color.
ecowboy 0:42ec9a44bc24 127 }else{
ecowboy 6:13d0de9e679c 128 TFT.fillcircle(int(p.x),int(p.y),2,color); //Touch event caused paint event with 2 pixel brush
ecowboy 0:42ec9a44bc24 129 }
ecowboy 2:fe018965e46c 130 }
ecowboy 0:42ec9a44bc24 131 }
ecowboy 0:42ec9a44bc24 132 }