Advertisement
nikolas77

bouton simple

Sep 4th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #if 1
  2.  
  3. #include <Adafruit_GFX.h>
  4. #include <MCUFRIEND_kbv.h>
  5. MCUFRIEND_kbv tft;
  6. #include <TouchScreen.h>
  7. #define MINPRESSURE 200
  8. #define MAXPRESSURE 1000
  9.  
  10. // ALL Touch panels and wiring is DIFFERENT
  11. // copy-paste results from TouchScreen_Calibr_native.ino
  12. const int XP = 8, XM = A2, YP = A3, YM = 9; //ID=0x9341
  13. const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;
  14.  
  15. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  16.  
  17. Adafruit_GFX_Button on_btn, off_btn;
  18.  
  19. int pixel_x, pixel_y;     //Touch_getXY() updates global vars
  20. bool Touch_getXY(void)
  21. {
  22.     TSPoint p = ts.getPoint();
  23.     pinMode(YP, OUTPUT);      //restore shared pins
  24.     pinMode(XM, OUTPUT);
  25.     digitalWrite(YP, HIGH);   //because TFT control pins
  26.     digitalWrite(XM, HIGH);
  27.     bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  28.     if (pressed) {
  29.         pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
  30.         pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  31.     }
  32.     return pressed;
  33. }
  34.  
  35. #define BLACK   0x0000
  36. #define BLUE    0x001F
  37. #define RED     0xF800
  38. #define GREEN   0x07E0
  39. #define CYAN    0x07FF
  40. #define MAGENTA 0xF81F
  41. #define YELLOW  0xFFE0
  42. #define WHITE   0xFFFF
  43.  
  44. void setup(void)
  45. {
  46.     Serial.begin(9600);
  47.     uint16_t ID = tft.readID();
  48.     Serial.print("TFT ID = 0x");
  49.     Serial.println(ID, HEX);
  50.     Serial.println("Calibrate for your Touch Panel");
  51.     if (ID == 0xD3D3) ID = 0x9486; // write-only shield
  52.     tft.begin(ID);
  53.     tft.setRotation(0);            //PORTRAIT
  54.     tft.fillScreen(BLACK);
  55.     on_btn.initButton(&tft,  60, 200, 100, 40, WHITE, CYAN, BLACK, "ON", 2);
  56.     off_btn.initButton(&tft, 180, 200, 100, 40, WHITE, CYAN, BLACK, "OFF", 2);
  57.     on_btn.drawButton(false);
  58.     off_btn.drawButton(false);
  59.     tft.fillRect(40, 80, 160, 80, RED);
  60. }
  61.  
  62. /* two buttons are quite simple
  63.  */
  64. void loop(void)
  65. {
  66.     bool down = Touch_getXY();
  67.     on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  68.     off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
  69.     if (on_btn.justReleased())
  70.         on_btn.drawButton();
  71.     if (off_btn.justReleased())
  72.         off_btn.drawButton();
  73.     if (on_btn.justPressed()) {
  74.         on_btn.drawButton(true);
  75.         tft.fillRect(40, 80, 160, 80, GREEN);
  76.     }
  77.     if (off_btn.justPressed()) {
  78.         off_btn.drawButton(true);
  79.         tft.fillRect(40, 80, 160, 80, RED);
  80.     }
  81. }
  82. #endif
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement