nikthefix / Lilygo_Support_T_Display_S3_Long_TFT_eSPI_Volos-nikthefix

16 stars 2 forks source link

Debounce/Timeout suggestion #8

Open aviost opened 4 weeks ago

aviost commented 4 weeks ago

Hi Nick, Thank you for your excellent work. I would abandon this piece of hardware without your code. With the Ruler (and my old fat fingers), I had many "double-touch" accidents. This code works much better for me; you can consider including it. Avi Ostfeld

In the global scope, add:

// Touch timeout
#define CHECK_TOUCH_INTERVAL 100 // Check touch only every X ms.
unsigned long checkTouchTime=0;  // When will be the next time to check for touch
bool touchHeld, savedTouchHeld=false; // Touch is being held now?

Replace the loop code with:

void loop()
{
  if (millis() >= checkTouchTime)         // Time to check for touch?
  {
    touchHeld = !digitalRead(TOUCH_INT);   // get the status. The NOT sign is because I believe it it LOW when touching, So better inverse to keep it readable
    if (touchHeld != savedTouchHeld)      // New status? 
    {
      // Uncomment for debug, don't forget to add Serial.begin in Setup... // Serial.printf("New touch status:%d\n",touchHeld);
      if (touchHeld) // New status AND holding?
      {
        getTouch();
        draw();
      }
      // else do nothing - if it is a new status AND released - just save it as savedTouchHeld as usual for new status
      savedTouchHeld = touchHeld;
      checkTouchTime = millis() + CHECK_TOUCH_INTERVAL; // Set earliest next time to check
    }
  }
} 
nikthefix commented 4 weeks ago

@aviost Thank you for your code. I will try it and compare it to the existing implementation. Looks good. I also have fat fingers!

nik

aviost commented 4 weeks ago

The logic is: Anyway - Check only if enough time has passed from the last status change Then... do the following only if the status is new (held/not held) Then... If the new status is "HELD" do your work with this touch Reset "earliest time to check" and update the current (saved) status

You can uncomment the debug line and see it at work