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

Button_demo.ino (5073B)

      1 // Button widget demo, requires SPI display with touch screen
      2 
      3 // Requires widget library here:
      4 // https://github.com/Bodmer/TFT_eWidget
      5 
      6 #include <FS.h>
      7 #include "Free_Fonts.h" // Include the header file attached to this sketch
      8 
      9 #include <TFT_eSPI.h>              // Hardware-specific library
     10 #include <TFT_eWidget.h>           // Widget library
     11 
     12 TFT_eSPI tft = TFT_eSPI();         // Invoke custom library
     13 
     14 #define CALIBRATION_FILE "/TouchCalData1"
     15 #define REPEAT_CAL false
     16 
     17 ButtonWidget btnL = ButtonWidget(&tft);
     18 ButtonWidget btnR = ButtonWidget(&tft);
     19 
     20 #define BUTTON_W 100
     21 #define BUTTON_H 50
     22 
     23 // Create an array of button instances to use in for() loops
     24 // This is more useful where large numbers of buttons are employed
     25 ButtonWidget* btn[] = {&btnL , &btnR};;
     26 uint8_t buttonCount = sizeof(btn) / sizeof(btn[0]);
     27 
     28 void btnL_pressAction(void)
     29 {
     30   if (btnL.justPressed()) {
     31     Serial.println("Left button just pressed");
     32     btnL.drawSmoothButton(true);
     33   }
     34 }
     35 
     36 void btnL_releaseAction(void)
     37 {
     38   static uint32_t waitTime = 1000;
     39   if (btnL.justReleased()) {
     40     Serial.println("Left button just released");
     41     btnL.drawSmoothButton(false);
     42     btnL.setReleaseTime(millis());
     43     waitTime = 10000;
     44   }
     45   else {
     46     if (millis() - btnL.getReleaseTime() >= waitTime) {
     47       waitTime = 1000;
     48       btnL.setReleaseTime(millis());
     49       btnL.drawSmoothButton(!btnL.getState());
     50     }
     51   }
     52 }
     53 
     54 void btnR_pressAction(void)
     55 {
     56   if (btnR.justPressed()) {
     57     btnR.drawSmoothButton(!btnR.getState(), 3, TFT_BLACK, btnR.getState() ? "OFF" : "ON");
     58     Serial.print("Button toggled: ");
     59     if (btnR.getState()) Serial.println("ON");
     60     else  Serial.println("OFF");
     61     btnR.setPressTime(millis());
     62   }
     63 
     64   // if button pressed for more than 1 sec...
     65   if (millis() - btnR.getPressTime() >= 1000) {
     66     Serial.println("Stop pressing my buttton.......");
     67   }
     68   else Serial.println("Right button is being pressed");
     69 }
     70 
     71 void btnR_releaseAction(void)
     72 {
     73   // Not action
     74 }
     75 
     76 void initButtons() {
     77   uint16_t x = (tft.width() - BUTTON_W) / 2;
     78   uint16_t y = tft.height() / 2 - BUTTON_H - 10;
     79   btnL.initButtonUL(x, y, BUTTON_W, BUTTON_H, TFT_WHITE, TFT_RED, TFT_BLACK, "Button", 1);
     80   btnL.setPressAction(btnL_pressAction);
     81   btnL.setReleaseAction(btnL_releaseAction);
     82   btnL.drawSmoothButton(false, 3, TFT_BLACK); // 3 is outline width, TFT_BLACK is the surrounding background colour for anti-aliasing
     83 
     84   y = tft.height() / 2 + 10;
     85   btnR.initButtonUL(x, y, BUTTON_W, BUTTON_H, TFT_WHITE, TFT_BLACK, TFT_GREEN, "OFF", 1);
     86   btnR.setPressAction(btnR_pressAction);
     87   //btnR.setReleaseAction(btnR_releaseAction);
     88   btnR.drawSmoothButton(false, 3, TFT_BLACK); // 3 is outline width, TFT_BLACK is the surrounding background colour for anti-aliasing
     89 }
     90 
     91 void setup() {
     92   Serial.begin(115200);
     93   tft.begin();
     94   tft.setRotation(0);
     95   tft.fillScreen(TFT_BLACK);
     96   tft.setFreeFont(FF18);
     97 
     98   // Calibrate the touch screen and retrieve the scaling factors
     99   touch_calibrate();
    100   initButtons();
    101 }
    102 
    103 void loop() {
    104   static uint32_t scanTime = millis();
    105   uint16_t t_x = 9999, t_y = 9999; // To store the touch coordinates
    106 
    107   // Scan keys every 50ms at most
    108   if (millis() - scanTime >= 50) {
    109     // Pressed will be set true if there is a valid touch on the screen
    110     bool pressed = tft.getTouch(&t_x, &t_y);
    111     scanTime = millis();
    112     for (uint8_t b = 0; b < buttonCount; b++) {
    113       if (pressed) {
    114         if (btn[b]->contains(t_x, t_y)) {
    115           btn[b]->press(true);
    116           btn[b]->pressAction();
    117         }
    118       }
    119       else {
    120         btn[b]->press(false);
    121         btn[b]->releaseAction();
    122       }
    123     }
    124   }
    125 
    126 }
    127 
    128 void touch_calibrate()
    129 {
    130   uint16_t calData[5];
    131   uint8_t calDataOK = 0;
    132 
    133   // check file system exists
    134   if (!LittleFS.begin()) {
    135     Serial.println("Formating file system");
    136     LittleFS.format();
    137     LittleFS.begin();
    138   }
    139 
    140   // check if calibration file exists and size is correct
    141   if (LittleFS.exists(CALIBRATION_FILE)) {
    142     if (REPEAT_CAL)
    143     {
    144       // Delete if we want to re-calibrate
    145       LittleFS.remove(CALIBRATION_FILE);
    146     }
    147     else
    148     {
    149       File f = LittleFS.open(CALIBRATION_FILE, "r");
    150       if (f) {
    151         if (f.readBytes((char *)calData, 14) == 14)
    152           calDataOK = 1;
    153         f.close();
    154       }
    155     }
    156   }
    157 
    158   if (calDataOK && !REPEAT_CAL) {
    159     // calibration data valid
    160     tft.setTouch(calData);
    161   } else {
    162     // data not valid so recalibrate
    163     tft.fillScreen(TFT_BLACK);
    164     tft.setCursor(20, 0);
    165     tft.setTextFont(2);
    166     tft.setTextSize(1);
    167     tft.setTextColor(TFT_WHITE, TFT_BLACK);
    168 
    169     tft.println("Touch corners as indicated");
    170 
    171     tft.setTextFont(1);
    172     tft.println();
    173 
    174     if (REPEAT_CAL) {
    175       tft.setTextColor(TFT_RED, TFT_BLACK);
    176       tft.println("Set REPEAT_CAL to false to stop this running again!");
    177     }
    178 
    179     tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
    180 
    181     tft.setTextColor(TFT_GREEN, TFT_BLACK);
    182     tft.println("Calibration complete!");
    183 
    184     // store data
    185     File f = LittleFS.open(CALIBRATION_FILE, "w");
    186     if (f) {
    187       f.write((const unsigned char *)calData, 14);
    188       f.close();
    189     }
    190   }
    191 }