acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Button_Label_Datum.ino (5035B)

      1 /*
      2   The TFT_eSPI library incorporates an Adafruit_GFX compatible
      3   button handling class.
      4 
      5   This example displays a column of buttons with varying label
      6   alignments.
      7 
      8   The sketch has been tested on the ESP32 (which supports SPIFFS)
      9 
     10   Adjust the definitions below according to your screen size
     11 */
     12 
     13 #include "FS.h"
     14 
     15 #include <SPI.h>
     16 
     17 #include <TFT_eSPI.h>
     18 
     19 TFT_eSPI tft = TFT_eSPI();
     20 
     21 // This is the file name used to store the calibration data
     22 // You can change this to create new calibration files.
     23 // The SPIFFS file name must start with "/".
     24 #define CALIBRATION_FILE "/TouchCalData1"
     25 
     26 // Set REPEAT_CAL to true instead of false to run calibration
     27 // again, otherwise it will only be done once.
     28 // Repeat calibration if you change the screen rotation.
     29 #define REPEAT_CAL false
     30 
     31 // Keypad start position, key sizes and spacing
     32 #define KEY_X 160 // Centre of key
     33 #define KEY_Y 50
     34 #define KEY_W 320 // Width and height
     35 #define KEY_H 22
     36 #define KEY_SPACING_X 0 // X and Y gap
     37 #define KEY_SPACING_Y 1
     38 #define KEY_TEXTSIZE 1   // Font size multiplier
     39 #define BUTTON_X_DELTA 22
     40 #define NUM_KEYS 6
     41 
     42 TFT_eSPI_Button key[NUM_KEYS];
     43 
     44 void setup() {
     45   
     46   Serial.begin(115200);
     47   
     48   tft.init();
     49 
     50   // Set the rotation before we calibrate
     51   tft.setRotation(1);
     52 
     53   // Check for backlight pin if not connected to VCC
     54   #ifndef TFT_BL
     55     Serial.println("No TFT backlight pin defined");
     56   #else
     57     pinMode(TFT_BL, OUTPUT);
     58     digitalWrite(TFT_BL, HIGH);
     59   #endif
     60 
     61   // call screen calibration
     62   touch_calibrate();
     63 
     64   // Clear screen
     65   tft.fillScreen(TFT_BLACK);
     66 
     67   tft.setFreeFont(&FreeMono9pt7b);
     68   
     69   drawButtons();
     70 }
     71 
     72 void loop() {
     73   uint16_t t_x = 0, t_y = 0; // To store the touch coordinates
     74 
     75   // Get current touch state and coordinates
     76   bool pressed = tft.getTouch(&t_x, &t_y);
     77 
     78   // Adjust press state of each key appropriately
     79   for (uint8_t b = 0; b < NUM_KEYS; b++) {
     80     if (pressed && key[b].contains(t_x, t_y)) 
     81       key[b].press(true);  // tell the button it is pressed
     82     else
     83       key[b].press(false);  // tell the button it is NOT pressed
     84   }
     85 
     86   // Check if any key has changed state
     87   for (uint8_t b = 0; b < NUM_KEYS; b++) {
     88     // If button was just pressed, redraw inverted button
     89     if (key[b].justPressed()) {
     90       Serial.println("Button " + (String)b + " pressed");
     91       key[b].drawButton(true, "ML_DATUM + " + (String)(b * 10) + "px");
     92     }
     93 
     94     // If button was just released, redraw normal color button
     95     if (key[b].justReleased()) {
     96       Serial.println("Button " + (String)b + " released");
     97       key[b].drawButton(false, "ML_DATUM + " + (String)(b * 10) + "px");
     98     }
     99   }
    100 
    101 }
    102 
    103 void drawButtons()
    104 {
    105   // Generate buttons with different size X deltas
    106   for (int i = 0; i < NUM_KEYS; i++)
    107   {
    108     key[i].initButton(&tft,
    109                       KEY_X + 0 * (KEY_W + KEY_SPACING_X),
    110                       KEY_Y + i * (KEY_H + KEY_SPACING_Y), // x, y, w, h, outline, fill, text
    111                       KEY_W,
    112                       KEY_H,
    113                       TFT_BLACK, // Outline
    114                       TFT_CYAN, // Fill
    115                       TFT_BLACK, // Text
    116                       "", // 10 Byte Label
    117                       KEY_TEXTSIZE);
    118 
    119     // Adjust button label X delta according to array position
    120     // setLabelDatum(uint16_t x_delta, uint16_t y_delta, uint8_t datum)
    121     key[i].setLabelDatum(i * 10 - (KEY_W/2), 0, ML_DATUM);
    122 
    123     // Draw button and specify label string
    124     // Specifying label string here will allow more than the default 10 byte label
    125     key[i].drawButton(false, "ML_DATUM + " + (String)(i * 10) + "px");
    126   }
    127 }
    128 
    129 void touch_calibrate()
    130 {
    131   uint16_t calData[5];
    132   uint8_t calDataOK = 0;
    133 
    134   // check file system exists
    135   if (!SPIFFS.begin()) {
    136     Serial.println("Formatting file system");
    137     SPIFFS.format();
    138     SPIFFS.begin();
    139   }
    140 
    141   // check if calibration file exists and size is correct
    142   if (SPIFFS.exists(CALIBRATION_FILE)) {
    143     if (REPEAT_CAL)
    144     {
    145       // Delete if we want to re-calibrate
    146       SPIFFS.remove(CALIBRATION_FILE);
    147     }
    148     else
    149     {
    150       File f = SPIFFS.open(CALIBRATION_FILE, "r");
    151       if (f) {
    152         if (f.readBytes((char *)calData, 14) == 14)
    153           calDataOK = 1;
    154         f.close();
    155       }
    156     }
    157   }
    158 
    159   if (calDataOK && !REPEAT_CAL) {
    160     // calibration data valid
    161     tft.setTouch(calData);
    162   } else {
    163     // data not valid so recalibrate
    164     tft.fillScreen(TFT_BLACK);
    165     tft.setCursor(20, 0);
    166     tft.setTextFont(2);
    167     tft.setTextSize(1);
    168     tft.setTextColor(TFT_WHITE, TFT_BLACK);
    169 
    170     tft.println("Touch corners as indicated");
    171 
    172     tft.setTextFont(1);
    173     tft.println();
    174 
    175     if (REPEAT_CAL) {
    176       tft.setTextColor(TFT_RED, TFT_BLACK);
    177       tft.println("Set REPEAT_CAL to false to stop this running again!");
    178     }
    179 
    180     tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
    181 
    182     tft.setTextColor(TFT_GREEN, TFT_BLACK);
    183     tft.println("Calibration complete!");
    184 
    185     // store data
    186     File f = SPIFFS.open(CALIBRATION_FILE, "w");
    187     if (f) {
    188       f.write((const unsigned char *)calData, 14);
    189       f.close();
    190     }
    191   }
    192 }