Paint for the FRDM-KL25Z

Dependencies:   mbed TFT_fonts SPI_TFT_ILI9341

Revision:
3:1396bac4ae71
Parent:
2:fe018965e46c
Child:
4:87b40e5986e7
--- a/main.cpp	Tue Jan 08 06:56:55 2019 +0000
+++ b/main.cpp	Thu Jan 10 01:14:40 2019 +0000
@@ -1,59 +1,35 @@
 #include "mbed.h"
-#include "ILI932x.h"
-
-//#include "SDFileSystem.h"
-//#include "SPI.h"
+#include "SPI_TFT_ILI9341.h"
+#include "Arial12x12.h"
 #include "BMP565.h"
-#include "Arial12x12.h"
 #include "stdio.h"
 #include "touch.h"
- 
-// For better pressure precision, we need to know the resistance
-// between X+ and X- Use any multimeter to read it
-// The 2.8" TFT Touch shield has 300 ohms across the X plate
-    
-//Measured ADC values for (0,0) and (210-1,320-1)
-//TS_MINX corresponds to ADC value when X = 0
-//TS_MINY corresponds to ADC value when Y = 0
-//TS_MAXX corresponds to ADC value when X = 240 -1
-//TS_MAXY corresponds to ADC value when Y = 320 -1
 
-#define PIN_YP PTB3  // must be an analog pin, use "An" notation!
-#define PIN_XM PTB2  // must be an analog pin, use "An" notation!
-#define PIN_YM PTD5   // can be a digital pin
-#define PIN_XP PTA13  // can be a digital pin
 
-#define PIN_CS_TFT      PTB3
-#define PIN_RST_TFT     PTC2
-#define PIN_DC_TFT      PTB2
-#define PIN_WR_TFT      PTB1
-#define PIN_RD_TFT      PTB0
+#define     PIN_XP          PTB3
+#define     PIN_XM          PTB2
+#define     PIN_YP          PTB1
+#define     PIN_YM          PTB0
+#define     PIN_MOSI        PTD2
+#define     PIN_MISO        PTD3
+#define     PIN_SCLK        PTD1
+#define     PIN_CS_TFT      PTA5
+#define     PIN_DC_TFT      PTC8
+#define     PIN_BL_TFT      PTC9
+#define     PIN_CS_SD       PTA4
 
-#if 0
-#define PIN_CS_SD       PTD0
-#define PIN_MOSI        PTD2
-#define PIN_MISO        PTD3
-#define PIN_SCLK        PTD1
-#endif
 
-#define PIN_CS_TSC      PTD0
-#define PIN_MOSI        PTD2
-#define PIN_MISO        PTD3
-#define PIN_SCLK        PTD1
+SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT, "TFT");
+TouchScreen tft(PIN_XP, PIN_YP, PIN_XM, PIN_YM); //init TouchScreen port pins
 
-PinName dataBus[] = {PTA13,PTD5,PTD4,PTA12,PTA4,PTA5,PTC8,PTC9};
-
-ILI932x TFT(BUS_8, dataBus, PIN_CS_TFT, PIN_RST_TFT, PIN_DC_TFT, PIN_WR_TFT, PIN_RD_TFT, "TFT", 240, 320);
-
-//SDFileSystem sd(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_SD, "sd");
 
 // Paint application - Demonstate both TFT and Touch Screen
-int ColorPaletteHigh = 30;
+int ColorPaletteHigh = 15;
 int color = White;  //Paint brush color
-unsigned int colors[8] = {Red, Green, Blue, Cyan, Yellow, Black, White, LightGrey};
+unsigned int colors[16] = {Red, Green, Blue, Cyan, Yellow, Purple, Pink, Black, Orange, Brown, BlueGreen, White, LightPink, DarkGrey, DarkGreen, Mustard};
 
-
-TouchScreen tft(PIN_XP, PIN_YP, PIN_XM, PIN_YM); //init TouchScreen port pins
+int RXPLATE = 350;
+int Q = 1024;  //10 bit TSC Resolution
 
 struct point
 {
@@ -71,39 +47,131 @@
     p.y = yy;
     p.z = zz;
     return p;
-}    
-    
+}
+
+int Read_X(){
+    AnalogIn   _yp(PTB2);
+    AnalogIn   _ym(PTB0);
+    DigitalOut _xp(PTB3);
+    DigitalOut _xm(PTB1);
+    //uint16_t x1=1;
+    //uint16_t x2=0;
+    int x1=1;
+    int x2=0;
+    _xp = 1;
+    _xm = 0;
+    wait_us(50);
+    //while(x1 != x2){
+        x1 = _yp.read_u16();
+        wait_us(50);
+        x2 = _yp.read_u16();
+        wait_us(50);
+    //}
+    _xp=0;
+    return (x1+x2)/2;
+}
+
+int Read_Y(){
+    //AnalogIn   _xp(xp);
+    AnalogIn   _xp(PTB3);
+    AnalogIn   _xm(PTB1);
+    DigitalOut _yp(PTB2);
+    DigitalOut _ym(PTB0);
+    //uint16_t y1=1;
+    //uint16_t y2=0;
+    int y1=1;
+    int y2=0;
+    _yp = 1;
+    _ym = 0;
+    wait_us(50);
+    //while(y1 != y2){
+        y1 = _xp.read_u16();
+        wait_us(50);
+        y2 = _xp.read_u16();
+        wait_us(50);
+    //}
+    _yp=0;
+    return (y1+y2)/2;
+}
+
+int Read_Z1(){
+    AnalogIn   _ym(PTB0);
+    AnalogIn   _xp(PTB3);
+    DigitalOut _yp(PTB2);
+    DigitalOut _xm(PTB1);
+    //uint16_t y1=1;
+    //uint16_t y2=0;
+    int y1=1;
+    int y2=0;
+    _yp = 1;
+    _xm = 0;
+    wait_us(50);
+    //while(y1 != y2){
+        int z1 = _xp.read_u16();
+        wait_us(50);
+        int z2 = _xp.read_u16();
+        wait_us(50);
+    //}
+    _yp=0;
+    return (z1+z2)/2;
+}
+
+int Read_Z2(){
+    AnalogIn   _ym(PTB0);
+    AnalogIn   _xp(PTB3);
+    DigitalOut _yp(PTB2);
+    DigitalOut _xm(PTB1);
+    //uint16_t y1=1;
+    //uint16_t y2=0;
+    int y1=1;
+    int y2=0;
+    _yp = 1;
+    _xm = 0;
+    wait_us(50);
+    //while(y1 != y2){
+        int z1 = _ym.read_u16();
+        wait_us(50);
+        int z2 = _ym.read_u16();
+        wait_us(50);
+    //}
+    _yp=0;
+    return (z1+z2)/2;
+}
+
+
 Point getPoint(){
-    PinName _yp, _ym, _xm, _xp;
-    int z;
-    int RXPLATE = 350;
-    int Q = 1024;  //10 bit TSC Resolution
-    /*int y2 = tft.readTouch(PIN_XP, PIN_YP, PIN_XM, PIN_YM);
-    /*int x2 = tft.readTouch(_yp,_ym,_xm,_xp);
-    int y1 = tft.readTouch(_xp,_xm,_ym,_yp);
-    int x1 = tft.readTouch(_yp,_ym,_xp,_xm);
-    int z2 = tft.readTouch(_ym,_xp,_yp,_xm);
-    int z1 = tft.readTouch(_ym,_xp,_xm,_yp);*/
+
+    //PinName _yp, _ym, _xm, _xp;
+    float z;
+    
+    int y2 = Read_Y();
+    int x2 = Read_X();
+    int z1 = Read_Z1();
+    int z2 = Read_Z2();
     
-    int y2 = 700;
-    int x2 = 600;
-    int z2 = 110;
-    int z1 = 100;
+    //int y2 = tft.readTouch(_yp,_ym,_xp,_xm);
+    /*int x2 = tft.readTouch(_xp,_xm,_yp,_ym);
+    int y1 = tft.readTouch(_yp,_ym,_xm,_xp);
+    int x1 = tft.readTouch(_xp,_xm,_ym,_yp);
+    int z2 = tft.readTouch(_yp,_xm,_ym,_xp);
+    int z1 = tft.readTouch(_yp,_xm,_xp,_ym);*/
     
-
+    //int y2 = 700;
+    //int x2 = 600;
+    //int z2 = 110;
+    //int z1 = 100;
+    
     if (z1!=0){
-        z = (float)RXPLATE/(float)Q*x2*((float)z2/(float)z1-1);
+        z = (float)z2/(float)z1;
     }else{
         z = 0;
     }
     int x = x2;
     int y = y2;
-
     return point(x,y,z);
 }
-
+    
 int main(){
-    
     TFT.claim(stdout);          // send stdout to the TFT display
     TFT.set_orientation(0);
     //TFT.Bitmap(28,90,265,65,p1);
@@ -111,41 +179,42 @@
     //TFT.cls();
     TFT.set_font((unsigned char*) Arial12x12);
     TFT.foreground(Cyan);      // set chars to Cyan
+    TFT.background(Purple);      // set background to Purple
     TFT.locate(80,90);
     //printf("Initializing z...\r\n");
-    for(int i = 0; i<8; i++){
-        TFT.fillrect(i*30, 0, 30*(i+1), ColorPaletteHigh, colors[i]);
+    for(int i = 0; i<16; i++){
+        TFT.fillrect(i*15, 0, 15*(i+1), ColorPaletteHigh, colors[i]);
     }
-    int zx=0;
-    float TS_MINX = 116;
-    float TS_MAXX = 890;
-    float TS_MINY = 83;
-    float TS_MAXY = 913;
-    int PRESSURE = 10;
+    float TS_MINX = 5500;
+    float TS_MAXX = 58100;
+    float TS_MINY = 4700;
+    float TS_MAXY = 57600;
+    int PRESSURE = 200;
     while(1){
-        zx = zx + 1;
         // a point object holds x y and z coordinates.
+        //TFT.BusEnable(false);
         Point p = getPoint();
+        //TFT.BusEnable(true);
         //map the ADC value read to into pixel co-ordinates
         p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
         p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
-
+        p.z = p.x * RXPLATE / Q * (p.z -1);
         // we have some minimum pressure we consider 'valid'
         // pressure of 0 means no pressing!
-        TFT.locate(60,90);
+        /*TFT.locate(60,90);
         printf("x = %d      \r\n", p.x);
         TFT.locate(60,110);
         printf("y = %d      \r\n", p.y);
         TFT.locate(60,130);
         printf("z = %d      \r\n", p.z);
-        TFT.locate(60,170);
-        printf("Initializing z...%d      \r\n",zx);
-        if (p.z > PRESSURE) {
+        //TFT.locate(60,170);*/
+        //printf("Initializing z...%d      \r\n",zx);
+        if (p.z > 40 && p.z < PRESSURE) {
             // Detect  paint brush color change
-            if(p.y < ColorPaletteHigh+2){
-                color = colors[p.x/30];
+            if(int(p.y) < ColorPaletteHigh+2){
+                color = colors[int(p.x)/15];
             }else{
-                TFT.fillcircle(p.x,p.y,2,color);
+                TFT.fillcircle(int(p.x),int(p.y),2,color);
             }
         }
     }