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

On_Off_Button.ino (5378B)

      1 // Example of drawing a graphical "switch" and using
      2 // the touch screen to change it's state.
      3 
      4 // This sketch does not use the libraries button drawing
      5 // and handling functions.
      6 
      7 // Based on Adafruit_GFX library onoffbutton example.
      8 
      9 // Touch handling for XPT2046 based screens is handled by
     10 // the TFT_eSPI library.
     11 
     12 // Calibration data is stored in SPIFFS so we need to include it
     13 #include "FS.h"
     14 
     15 #include <SPI.h>
     16 
     17 #include <TFT_eSPI.h> // Hardware-specific library
     18 
     19 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     20 
     21 // This is the file name used to store the touch coordinate
     22 // calibration data. Change the name to start a new calibration.
     23 #define CALIBRATION_FILE "/TouchCalData3"
     24 
     25 // Set REPEAT_CAL to true instead of false to run calibration
     26 // again, otherwise it will only be done once.
     27 // Repeat calibration if you change the screen rotation.
     28 #define REPEAT_CAL false
     29 
     30 bool SwitchOn = false;
     31 
     32 // Comment out to stop drawing black spots
     33 #define BLACK_SPOT
     34 
     35 // Switch position and size
     36 #define FRAME_X 100
     37 #define FRAME_Y 64
     38 #define FRAME_W 120
     39 #define FRAME_H 50
     40 
     41 // Red zone size
     42 #define REDBUTTON_X FRAME_X
     43 #define REDBUTTON_Y FRAME_Y
     44 #define REDBUTTON_W (FRAME_W/2)
     45 #define REDBUTTON_H FRAME_H
     46 
     47 // Green zone size
     48 #define GREENBUTTON_X (REDBUTTON_X + REDBUTTON_W)
     49 #define GREENBUTTON_Y FRAME_Y
     50 #define GREENBUTTON_W (FRAME_W/2)
     51 #define GREENBUTTON_H FRAME_H
     52 
     53 //------------------------------------------------------------------------------------------
     54 //------------------------------------------------------------------------------------------
     55 void setup(void)
     56 {
     57   Serial.begin(9600);
     58   tft.init();
     59 
     60   // Set the rotation before we calibrate
     61   tft.setRotation(1);
     62 
     63   // call screen calibration
     64   touch_calibrate();
     65 
     66   // clear screen
     67   tft.fillScreen(TFT_BLUE);
     68 
     69   // Draw button (this example does not use library Button class)
     70   redBtn();
     71 }
     72 //------------------------------------------------------------------------------------------
     73 //------------------------------------------------------------------------------------------
     74 void loop()
     75 {
     76   uint16_t x, y;
     77 
     78   // See if there's any touch data for us
     79   if (tft.getTouch(&x, &y))
     80   {
     81     // Draw a block spot to show where touch was calculated to be
     82     #ifdef BLACK_SPOT
     83       tft.fillCircle(x, y, 2, TFT_BLACK);
     84     #endif
     85     
     86     if (SwitchOn)
     87     {
     88       if ((x > REDBUTTON_X) && (x < (REDBUTTON_X + REDBUTTON_W))) {
     89         if ((y > REDBUTTON_Y) && (y <= (REDBUTTON_Y + REDBUTTON_H))) {
     90           Serial.println("Red btn hit");
     91           redBtn();
     92         }
     93       }
     94     }
     95     else //Record is off (SwitchOn == false)
     96     {
     97       if ((x > GREENBUTTON_X) && (x < (GREENBUTTON_X + GREENBUTTON_W))) {
     98         if ((y > GREENBUTTON_Y) && (y <= (GREENBUTTON_Y + GREENBUTTON_H))) {
     99           Serial.println("Green btn hit");
    100           greenBtn();
    101         }
    102       }
    103     }
    104 
    105     Serial.println(SwitchOn);
    106 
    107   }
    108 }
    109 //------------------------------------------------------------------------------------------
    110 
    111 void touch_calibrate()
    112 {
    113   uint16_t calData[5];
    114   uint8_t calDataOK = 0;
    115 
    116   // check file system exists
    117   if (!SPIFFS.begin()) {
    118     Serial.println("Formatting file system");
    119     SPIFFS.format();
    120     SPIFFS.begin();
    121   }
    122 
    123   // check if calibration file exists and size is correct
    124   if (SPIFFS.exists(CALIBRATION_FILE)) {
    125     if (REPEAT_CAL)
    126     {
    127       // Delete if we want to re-calibrate
    128       SPIFFS.remove(CALIBRATION_FILE);
    129     }
    130     else
    131     {
    132       File f = SPIFFS.open(CALIBRATION_FILE, "r");
    133       if (f) {
    134         if (f.readBytes((char *)calData, 14) == 14)
    135           calDataOK = 1;
    136         f.close();
    137       }
    138     }
    139   }
    140 
    141   if (calDataOK && !REPEAT_CAL) {
    142     // calibration data valid
    143     tft.setTouch(calData);
    144   } else {
    145     // data not valid so recalibrate
    146     tft.fillScreen(TFT_BLACK);
    147     tft.setCursor(20, 0);
    148     tft.setTextFont(2);
    149     tft.setTextSize(1);
    150     tft.setTextColor(TFT_WHITE, TFT_BLACK);
    151 
    152     tft.println("Touch corners as indicated");
    153 
    154     tft.setTextFont(1);
    155     tft.println();
    156 
    157     if (REPEAT_CAL) {
    158       tft.setTextColor(TFT_RED, TFT_BLACK);
    159       tft.println("Set REPEAT_CAL to false to stop this running again!");
    160     }
    161 
    162     tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
    163 
    164     tft.setTextColor(TFT_GREEN, TFT_BLACK);
    165     tft.println("Calibration complete!");
    166 
    167     // store data
    168     File f = SPIFFS.open(CALIBRATION_FILE, "w");
    169     if (f) {
    170       f.write((const unsigned char *)calData, 14);
    171       f.close();
    172     }
    173   }
    174 }
    175 
    176 void drawFrame()
    177 {
    178   tft.drawRect(FRAME_X, FRAME_Y, FRAME_W, FRAME_H, TFT_BLACK);
    179 }
    180 
    181 // Draw a red button
    182 void redBtn()
    183 {
    184   tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_RED);
    185   tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_DARKGREY);
    186   drawFrame();
    187   tft.setTextColor(TFT_WHITE);
    188   tft.setTextSize(2);
    189   tft.setTextDatum(MC_DATUM);
    190   tft.drawString("ON", GREENBUTTON_X + (GREENBUTTON_W / 2), GREENBUTTON_Y + (GREENBUTTON_H / 2));
    191   SwitchOn = false;
    192 }
    193 
    194 // Draw a green button
    195 void greenBtn()
    196 {
    197   tft.fillRect(GREENBUTTON_X, GREENBUTTON_Y, GREENBUTTON_W, GREENBUTTON_H, TFT_GREEN);
    198   tft.fillRect(REDBUTTON_X, REDBUTTON_Y, REDBUTTON_W, REDBUTTON_H, TFT_DARKGREY);
    199   drawFrame();
    200   tft.setTextColor(TFT_WHITE);
    201   tft.setTextSize(2);
    202   tft.setTextDatum(MC_DATUM);
    203   tft.drawString("OFF", REDBUTTON_X + (REDBUTTON_W / 2) + 1, REDBUTTON_Y + (REDBUTTON_H / 2));
    204   SwitchOn = true;
    205 }
    206