Как поменять местами координаты на touch screen ?

Тема в разделе "Arduino & Shields", создана пользователем An4ous, 11 ноя 2015.

  1. An4ous

    An4ous Нерд

    Всем привет. хочу сделать меню на TFT с поддержкой тача. Но не могу сообразить как перевернуть тач. при tft.setRotation 0 все работает как и положено, переворачиваю экран tft.setRotation 1 и параметры тача
    Код (C++):
      #elif LCDROTATION == 1
      if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x =  map(p.y, TS_MINX, TS_MAXX, 0, tft.height());
    p.y =  map(p.x, TS_MINY, TS_MAXY, tft.width(), 0);
    то p.y и p.x на одной оси находятся. помогите исправить пожалуйста


    Скетч
    Код (C++):

    #include <Adafruit_GFX.h>    // Core graphics library
    #include <MCUFRIEND_kbv.h>
    #include <TouchScreen.h>


    //тач скрин
    #define YP A1  // must be an analog pin, use "An" notation!
    #define XM A2  // must be an analog pin, use "An" notation!
    #define YM 7   // can be a digital pin
    #define XP 6   // can be a digital pin

    TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

    #define  BLACK   0x0000
    #define BLUE    0x001F
    #define RED     0xF800
    #define GREEN   0x07E0
    #define CYAN    0x07FF
    #define MAGENTA 0xF81F
    #define YELLOW  0xFFE0
    #define WHITE   0xFFFF

    /******************* UI details */
    #define BUTTON_X 60 //отступ от х
    #define BUTTON_Y 20 //отступ от у
    #define BUTTON_W 90 //ширина кнопки
    #define BUTTON_H 35 //высота кнопки

    #define BUTTON_SPACING_X 20 //отступ между кнопками по х
    #define BUTTON_SPACING_Y 10 //отступ между кнопками по у
    #define BUTTON_TEXTSIZE 2 //размер текста

    //настройка тача
    #define TS_MINX 150
    #define TS_MINY 130
    #define TS_MAXX 920
    #define TS_MAXY 900


    #define MINPRESSURE 10
    #define MAXPRESSURE 1000

    #define LCDROTATION 1

    int ledPin = 13;
    MCUFRIEND_kbv tft;
    Adafruit_GFX_Button buttons[7];
    char buttonlabels[7][7] =  {"On", "Off", "+", "-", "4", "5", "6"};
    uint16_t buttoncolors[7] = {GREEN, GREEN, GREEN,
                                 BLUE, BLUE, BLUE, BLUE };

    void setup(void) {

    Serial.begin(9600);
      Serial.println(F("TFT LCD test"));
      pinMode(ledPin, OUTPUT);
      digitalWrite(13, LOW);
      tft.reset();
      tft.begin(0x9488);
      tft.fillScreen(BLACK);
      tft.setRotation(LCDROTATION);

    for (uint8_t row=0; row<7; row++) {
        for (uint8_t col=0; col<1; col++) {
          buttons[col + row*1].initButton(&tft, BUTTON_X+col*(BUTTON_W),
                     BUTTON_Y+row*(BUTTON_H+BUTTON_SPACING_Y),    // x, y, w, h, outline, fill, text
                      BUTTON_W, BUTTON_H, WHITE, buttoncolors[col+row*1], WHITE,
                      buttonlabels[col + row*1], BUTTON_TEXTSIZE);
          buttons[col + row*1].drawButton();
        }
      }

      tft.drawRect(0, 0, 120, 320, RED);
    tft.drawRect(1, 1, 118, 318, RED);

    }


    void loop(void) {

      TSPoint p = ts.getPoint();

      //pinMode(XP, OUTPUT);
      pinMode(XM, OUTPUT);
      pinMode(YP, OUTPUT);
      //pinMode(YM, OUTPUT);


        #if LCDROTATION == 0
       if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
        // scale from 0->1023 to tft.width
      p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
      p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0);

        Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z);
       }

      #elif LCDROTATION == 1
      if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
    p.x =  map(p.y, TS_MINX, TS_MAXX, 0, tft.height());
    p.y =  map(p.x, TS_MINY, TS_MAXY, tft.width(), 0);

        Serial.print("X = "); Serial.print(p.x);
         Serial.print("\tY = "); Serial.print(p.y);
         Serial.print("\tPressure = "); Serial.println(p.z);
      }

        #elif LCDROTATION == 2

      #elif LCDROTATION == 3
    #endif

      // go thru all the buttons, checking if they were pressed
      for (uint8_t b=0; b<7; b++) {
        if (buttons[b].contains(p.x, p.y)) {
          buttons[b].press(true);  // tell the button it is pressed
        } else {
          buttons[b].press(false);  // tell the button it is NOT pressed
        }
      }

      // now we can ask the buttons if their state has changed
      for (uint8_t b=0; b<7; b++) {
        if (buttons[b].justReleased()) {
          buttons[b].drawButton();  // draw normal
        }

           if (buttons[b].justPressed()) {
            buttons[b].drawButton(true);  // draw invert!
         
            }

       
           if (b == 0) {
              Serial.print("led on");
              digitalWrite(ledPin, HIGH);    // выключаем LED
            }
         
            if (b == 1) {
              Serial.print("led off");
              digitalWrite(ledPin, LOW);
            }
         
            if (b == 2) {
            }
            if (b == 3) {
            }
            if (b == 4) {
      }

       }

                }
         
          delay(50); // UI debouncing
        }
      }

     
     
    Последнее редактирование: 14 ноя 2015
  2. An4ous

    An4ous Нерд

    helppp, подскажите за пивко
     
  3. id76453140

    id76453140 Нуб

    Я тоже хочу саздать что то похожое , но недавно начял .
    А ты можешь мне помочь ??????
     
  4. Kibit

    Kibit Нуб

    Добавь в код
    Код (C++):
     #define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}
           switch (LCDROTATION) {
           case 0:   SWAP(TS_MINX, TS_MAXX); break;
           case 1:   break;    
           case 2:   SWAP(TS_MINY, TS_MAXY); break;
           case 3:   SWAP(TS_MINX, TS_MAXX); SWAP(TS_MINY, TS_MAXY); break;
        }
    поменяй местами case если не попал сразу