ESP32 BLE to P5.js

This code grabs the integrated gamepad API of your browser to use in context of P5. The connection is down through the integrated BLE interface of the ESP32.

Also have a look at a shader based example: https://editor.p5js.org/simon_oakey/sketches/5rAhgQXJM

CODE FOR ESP32

This sketch is based on the brilliant BLE lib written for Arduino and ESP32 by … . It grabs four Touchpins to send it via Bluetooth as a generic gamepad 🙂

// ----------------------------------
// -------MAIN LOOP -----------------
// ----------------------------------

// init BLE interface to communicate
#include <BleGamepad.h> 
 BleGamepad bleGamepad("HELLO_TWO", "Hello Studios",99);
 
// initalize the aTouch objects : aTouch namewahtevet(yourpinnumber);
aTouch touch1(2);
aTouch touch2(4);
aTouch touch3(12);
aTouch touch4(13);
 
void setup() {
   
 // Serial.begin(9600);
   
  bleGamepad.begin(4,2,2);
   bleGamepad.setAutoReport(true);
 
}
 
void loop() {
  
  // simply read input and do some calc
  touch1.readAndProcessInput();
  touch2.readAndProcessInput();
  touch3.readAndProcessInput();
  touch4.readAndProcessInput();
 
  // just call the touch object properties for further logic :)
  if(touch1.on_pressed){ Serial.println("T1 pressed");  bleGamepad.press(BUTTON_1); }
   if(touch2.on_pressed){ Serial.println("T2 pressed");  bleGamepad.press(BUTTON_2); }
   if(touch3.on_pressed){ Serial.println("T3 pressed");  bleGamepad.press(BUTTON_3); }
   if(touch4.on_pressed){ Serial.println("T4 pressed");  bleGamepad.press(BUTTON_4); }
  

   if(touch1.on_released){ Serial.println("T1 pressed");  bleGamepad.release(BUTTON_1); }
   if(touch2.on_released){ Serial.println("T2 pressed");  bleGamepad.release(BUTTON_2); }
   if(touch3.on_released){ Serial.println("T3 pressed");  bleGamepad.release(BUTTON_3); }
   if(touch4.on_released){ Serial.println("T4 pressed");  bleGamepad.release(BUTTON_4); }
  
  delay(10);
 
}
// ----------------------------------
// ------- TRXYS TOUCH HELPERS ------
// ----------------------------------
// ( put this code PRIOR to your mainloop code or you will get nice errors :) )
 
  // simple lerp helper function
 
float return_lerp(float _s, int _target,int _time){
  
   _s = _s + (( float(_target) - _s)/float(_time));
   return _s;
    
}
 
class aTouch {
  private:
    bool prev_touch_state = false;
    byte pin;
    int smooth_time = 4;
    int trigger_threshold = 15;
    long ts = 0;
  public:
    int current_val = 0;
    int smoothed_val = 0;
    int diff_val = 0;
    bool is_triggered = false;
    bool on_pressed = false;
    bool on_released = false;
     bool is_holded = false;
    aTouch(byte pin) {
     this->pin = pin;
    }
     
 
    void readAndProcessInput() {
 
        // reset interaction states
        on_pressed = false;
        on_released = false;
         
         // directly read out values TWICE = BUGFIX for debouncing 
         current_val = touchRead(pin);
         delayMicroseconds(10);
         current_val = touchRead(pin);
 
        //calculate smoothed input values 
         smoothed_val = return_lerp(smoothed_val,current_val,smooth_time);
 
         // calc current differential sum of button
          diff_val =   smoothed_val - current_val;
 
          // check if there is a noticable difference input values
          if(  diff_val  > trigger_threshold){
 
              if(prev_touch_state == false){
                is_triggered = true;
                prev_touch_state = is_triggered;
                on_pressed = true;
                ts = millis(); // set timestamp for hold routine
             }
              
          }else if( diff_val < trigger_threshold*-.4){
 
              if(prev_touch_state == true){
                is_triggered = false;
                 prev_touch_state = is_triggered;
                 on_released = true;
              }
                 
          }
 
 
          // calculate timed holding function
           
          if( ts + 2500 < millis() && is_triggered){
 
                
              is_holded = true;
          }else{
 
              is_holded = false;
           }
       
     }
 
 
     void printAllMetadata() {
 
           // only use this on one touchinput only - it will skrew up else
           Serial.print (on_pressed);
           Serial.print(","); 
           Serial.print (on_released);
           Serial.print(","); 
           Serial.print (is_triggered);
           Serial.print(","); 
           Serial.print (current_val);
           Serial.print(","); 
           Serial.println(smoothed_val);
            
 
       
       
     }
};