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

Slider_demo.ino (5939B)

      1 // Slider widget demo, requires display with touch screen
      2 
      3 // Requires widget library here:
      4 // https://github.com/Bodmer/TFT_eWidget
      5 
      6 #include "FS.h"
      7 
      8 #include "Free_Fonts.h" // Include the header file attached to this sketch
      9 
     10 #include <TFT_eSPI.h>
     11 #include <TFT_eWidget.h>           // Widget library
     12 
     13 TFT_eSPI tft = TFT_eSPI();
     14 TFT_eSprite knob = TFT_eSprite(&tft); // Sprite for the slide knob
     15 
     16 #define CALIBRATION_FILE "/TouchCalData1"
     17 #define REPEAT_CAL false
     18 
     19 SliderWidget s1 = SliderWidget(&tft, &knob);    // Slider 1 widget
     20 SliderWidget s2 = SliderWidget(&tft, &knob);    // Slider 2 widget
     21 
     22 
     23 void setup() {
     24   Serial.begin(115200);
     25   tft.begin();
     26   tft.setRotation(0);
     27   tft.fillScreen(TFT_BLACK);
     28   tft.setFreeFont(FF18);
     29 
     30   // Calibrate the touch screen and retrieve the scaling factors
     31   if (REPEAT_CAL) {
     32     touch_calibrate();
     33     tft.fillScreen(TFT_BLACK);
     34   }
     35 
     36   // Create a parameter set for the slider
     37   slider_t param;
     38 
     39   // Slider slot parameters
     40   param.slotWidth = 9;           // Note: ends of slot will be rounded and anti-aliased
     41   param.slotLength = 200;        // Length includes rounded ends
     42   param.slotColor = TFT_BLUE;    // Slot colour
     43   param.slotBgColor = TFT_BLACK; // Slot background colour for anti-aliasing
     44   param.orientation = H_SLIDER;  // sets it "true" for horizontal
     45 
     46   // Slider control knob parameters (smooth rounded rectangle)
     47   param.knobWidth = 15;          // Always along x axis
     48   param.knobHeight = 25;         // Always along y axis
     49   param.knobRadius = 5;          // Corner radius
     50   param.knobColor = TFT_WHITE;   // Anti-aliased with slot backgound colour
     51   param.knobLineColor = TFT_RED; // Colour of marker line (set to same as knobColor for no line)
     52 
     53   // Slider range and movement speed
     54   param.sliderLT = 0;            // Left side for horizontal, top for vertical slider
     55   param.sliderRB = 100;          // Right side for horizontal, bottom for vertical slider
     56   param.startPosition = 50;      // Start position for control knob
     57   param.sliderDelay = 0;         // Microseconds per pixel movement delay (0 = no delay)
     58 
     59   // Create slider using parameters and plot at 0,0
     60   s1.drawSlider(0, 0, param);
     61 
     62   // Show bounding box (1 pixel outside slider working area)
     63   int16_t x, y;    // x and y can be negative
     64   uint16_t w, h;   // Width and height
     65   s1.getBoundingRect(&x, &y, &w, &h);     // Update x,y,w,h with bounding box
     66   tft.drawRect(x, y, w, h, TFT_DARKGREY); // Draw rectangle outline
     67 /*
     68   // Alternative discrete fns to create/modify same slider - but fn sequence is important...
     69   s1.createSlider(9, 200, TFT_BLUE, TFT_BLACK, H_SLIDER);
     70   s1.createKnob(15, 25, 5, TFT_WHITE, TFT_RED);
     71   s1.setSliderScale(0, 100);
     72   s1.drawSlider(0, 0);
     73 */
     74   delay(1000);
     75   s1.setSliderPosition(50);
     76   delay(1000);
     77   s1.setSliderPosition(100);
     78 
     79   // Update any parameters that are different for slider 2
     80   param.slotWidth = 4;
     81   param.orientation = V_SLIDER; // sets it "false" for vertical
     82 
     83   param.knobWidth = 19;
     84   param.knobHeight = 19;
     85   param.knobRadius = 19/2; // Half w and h so creates a circle
     86 
     87   param.sliderLT = 200;     // Top for vertical slider
     88   param.sliderRB = 0;       // Bottom for vertical slider
     89   param.sliderDelay = 2000; // 2ms per pixel movement delay (movement is blocking until complete)
     90 
     91   s2.drawSlider(0, 50, param);
     92 
     93   s2.getBoundingRect(&x, &y, &w, &h);
     94   tft.drawRect(x, y, w, h, TFT_DARKGREY);
     95 /*
     96   // Alternative discrete fns to create/modify same slider - but fn sequence is important...
     97   s2.createSlider(4, 200, TFT_BLUE, TFT_BLACK, V_SLIDER);
     98   s2.createKnob(19, 19, 9, TFT_WHITE, TFT_RED);
     99   s2.setSliderScale(200, 0, 2000);
    100   s2.drawSlider(0, 50);
    101 */
    102   // Move slider under software control
    103   delay(1000);
    104   s2.setSliderPosition(50);
    105   delay(1000);
    106   s2.setSliderPosition(100);
    107 
    108 }
    109 
    110 void loop() {
    111   static uint32_t scanTime = millis();
    112   uint16_t t_x = 9999, t_y = 9999; // To store the touch coordinates
    113 
    114   // Scan for touch every 50ms
    115   if (millis() - scanTime >= 20) {
    116     // Pressed will be set true if there is a valid touch on the screen
    117     if( tft.getTouch(&t_x, &t_y, 250) ) {
    118       if (s1.checkTouch(t_x, t_y)) {
    119         Serial.print("Slider 1 = "); Serial.println(s1.getSliderPosition());
    120       }
    121       if (s2.checkTouch(t_x, t_y)) {
    122         Serial.print("Slider 2 = "); Serial.println(s2.getSliderPosition());
    123       }
    124     }
    125     scanTime = millis();
    126   }
    127 
    128   //s1.moveTo(random(101));
    129   //delay(250);
    130   //s2.moveTo(random(101));
    131   //delay(250);
    132 }
    133 
    134 
    135 
    136 
    137 
    138 void touch_calibrate()
    139 {
    140   uint16_t calData[5];
    141   uint8_t calDataOK = 0;
    142 
    143   // check file system exists
    144   if (!LittleFS.begin()) {
    145     Serial.println("Formating file system");
    146     LittleFS.format();
    147     LittleFS.begin();
    148   }
    149 
    150   // check if calibration file exists and size is correct
    151   if (LittleFS.exists(CALIBRATION_FILE)) {
    152     if (REPEAT_CAL)
    153     {
    154       // Delete if we want to re-calibrate
    155       LittleFS.remove(CALIBRATION_FILE);
    156     }
    157     else
    158     {
    159       File f = LittleFS.open(CALIBRATION_FILE, "r");
    160       if (f) {
    161         if (f.readBytes((char *)calData, 14) == 14)
    162           calDataOK = 1;
    163         f.close();
    164       }
    165     }
    166   }
    167 
    168   if (calDataOK && !REPEAT_CAL) {
    169     // calibration data valid
    170     tft.setTouch(calData);
    171   } else {
    172     // data not valid so recalibrate
    173     tft.fillScreen(TFT_BLACK);
    174     tft.setCursor(20, 0);
    175     tft.setTextFont(2);
    176     tft.setTextSize(1);
    177     tft.setTextColor(TFT_WHITE, TFT_BLACK);
    178 
    179     tft.println("Touch corners as indicated");
    180 
    181     tft.setTextFont(1);
    182     tft.println();
    183 
    184     if (REPEAT_CAL) {
    185       tft.setTextColor(TFT_RED, TFT_BLACK);
    186       tft.println("Set REPEAT_CAL to false to stop this running again!");
    187     }
    188 
    189     tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
    190 
    191     tft.setTextColor(TFT_GREEN, TFT_BLACK);
    192     tft.println("Calibration complete!");
    193 
    194     // store data
    195     File f = LittleFS.open(CALIBRATION_FILE, "w");
    196     if (f) {
    197       f.write((const unsigned char *)calData, 14);
    198       f.close();
    199     }
    200   }
    201 }