projets de S3 s4 2021

Dependencies:   mbed TFT_fonts SPI_TFT_ILI9341

main.cpp

Committer:
ecowboy
Date:
2019-01-10
Revision:
5:80791250acb2
Parent:
4:87b40e5986e7
Child:
6:13d0de9e679c

File content as of revision 5:80791250acb2:

#include "mbed.h"
#include "SPI_TFT_ILI9341.h"
#include "Arial12x12.h"
#include "BMP565.h"
#include "stdio.h"
#include "touch.h"


#define     PIN_XP          PTB3
#define     PIN_XM          PTB1
#define     PIN_YP          PTB2
#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


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


// Paint application - Demonstate both TFT and Touch Screen
int ColorPaletteHigh = 15;
int color = White;  //Paint brush color
unsigned int colors[16] = {Red, Green, Blue, Cyan, Yellow, Purple, Pink, Black, Orange, Brown, BlueGreen, White, LightPink, DarkGrey, DarkGreen, Mustard};

int RXPLATE = 350;
int Q = 1024;  //10 bit TSC Resolution

struct point
{
    int x;
    int y;
    int z;
};

typedef struct point Point;


Point point(int xx, int yy, int zz){
    Point p;
    p.x = xx;
    p.y = yy;
    p.z = zz;
    return p;
}


Point getPoint(){

    float z;
    int y2 = tft.readTouch(PTB3,PTB1,PTB2,PTB0);  //a(analog),i(analog),n(digital),m(digital)
    int x2 = tft.readTouch(PTB2,PTB0,PTB3,PTB1);
    int z2 = tft.readTouch(PTB0,PTB3,PTB2,PTB1);
    int z1 = tft.readTouch(PTB3,PTB0,PTB2,PTB1);
    
    if (z1!=0){
        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);
    //wait(1);
    //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<16; i++){
        TFT.fillrect(i*15, 0, 15*(i+1), ColorPaletteHigh, colors[i]);
    }
    float TS_MINX = 5500;
    float TS_MAXX = 58100;
    float TS_MINY = 4700;
    float TS_MAXY = 57600;
    int PRESSURE = 200;
    while(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);
        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 > 40 && p.z < PRESSURE) {
            // Detect  paint brush color change
            if(int(p.y) < ColorPaletteHigh+2){
                color = colors[int(p.x)/15];
            }else{
                TFT.fillcircle(int(p.x),int(p.y),2,color);
            }
        }
    }
}