ESP32 keypad matrix deep sleep and wake up procedure

172 Views Asked by At

I'm a complete noob. so I have an ESP32 dev kit and built a gamepad/keypad for sim racing (with matrix button diagram). 5 ROWS connected to the PINS: 13, 14, 15, 16, 33 4 columns connected to the PINS: 17, 18, 19, 21

here's my gamepad/keypad code:

#include <ESP32Encoder.h>     // https://github.com/madhephaestus/ESP32Encoder/
#include   <Keypad.h>           // https://github.com/Chris--A/Keypad
#include <BleGamepad.h>       // https://github.com/MagnusThome/ESP32-BLE-Gamepad


BleGamepad bleGamepad("WirelessGameHub", "breeze", 100);


////////////////////// BUTTON MATRIX //////////////////////
#define ROWS 5
#define COLS 4
uint8_t rowPins[ROWS] = {13, 14, 15, 16, 33};
uint8_t colPins[COLS] = {17, 18, 19, 21};
uint8_t keymap[ROWS][COLS] = {
  {0,1,2,3},
  {4,5,6,7},
  {8,9,10,11},
  {12,13,14,15},
  {24,25,26,27}
};
Keypad customKeypad = Keypad( makeKeymap(keymap), rowPins, colPins, ROWS, COLS); 


//////////// ROTARY ENCODERS (WITH PUSH SWITCHES) ////////////
#define MAXENC 4
uint8_t uppPin[MAXENC] = {22, 25, 27, 04};
uint8_t dwnPin[MAXENC] = {23, 26, 32, 05};
uint8_t encoderUpp[MAXENC] = {16,18,20,22};
uint8_t encoderDwn[MAXENC] = {17,19,21,23};
ESP32Encoder encoder[MAXENC];
unsigned long holdoff[MAXENC] = {0,0,0,0};
int32_t prevenccntr[MAXENC] = {0,0,0,0};
bool prevprs[MAXENC] = {0,0,0,0};
#define HOLDOFFTIME 150   






////////////////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(115200);

  for (uint8_t i=0; i<MAXENC; i++) {
    encoder[i].clearCount();
    encoder[i].attachSingleEdge(dwnPin[i], uppPin[i]);
  }
  customKeypad.addEventListener(keypadEvent);
  customKeypad.setHoldTime(1);
  bleGamepad.begin();
  Serial.println("Booted!");
}



////////////////////////////////////////////////////////////////////////////////////////

void loop() {

  unsigned long now = millis();


  // -- ROTARY ENCODERS : ROTATION -- //

  for (uint8_t i=0; i<MAXENC; i++) {
    int32_t cntr = encoder[i].getCount();
    if (cntr!=prevenccntr[i]) {
      if (!holdoff[i]) {
        if (cntr>prevenccntr[i]) { sendKey(encoderUpp[i]); }
        if (cntr<prevenccntr[i]) { sendKey(encoderDwn[i]); }
        holdoff[i] = now;
        if (holdoff[i]==0) holdoff[i] = 1;  // SAFEGUARD WRAP AROUND OF millis() (WHICH IS TO 0) SINCE holdoff[i]==0 HAS A SPECIAL MEANING ABOVE
      }
      else if (now-holdoff[i] > HOLDOFFTIME) {
        prevenccntr[i] = encoder[i].getCount();
        holdoff[i] = 0;
      }
    }
    
  // -- ROTARY ENCODERS : PUSH SWITCH -- //

  }

customKeypad.getKey();    // READ BUTTON MATRIX (EVENT CALLBACK SETUP)

  //delay(10);
 
}




////////////////////////////////////////////////////////////////////////////////////////

void keypadEvent(KeypadEvent key){
  uint8_t keystate = customKeypad.getState();
  if (keystate==PRESSED)  { pressKey(key); }
  if (keystate==RELEASED) { releaseKey(key); }
}


////////////////////////////////////////////////////////////////////////////////////////

void sendKey(uint8_t key) {
    uint32_t gamepadbutton = pow(2,key);      // CONVERT TO THE BINARY MAPPING GAMEPAD KEYS USE
    Serial.print("pulse\t");
    Serial.println(key);
    if(bleGamepad.isConnected()) {
      bleGamepad.press(gamepadbutton);
      delay(150);
      bleGamepad.release(gamepadbutton);
    }
}

void pressKey(uint8_t key) {
    uint32_t gamepadbutton = pow(2,key);      // CONVERT TO THE BINARY MAPPING GAMEPAD KEYS USE
    Serial.print("hold\t");
    Serial.println(key);
    if(bleGamepad.isConnected()) {
      bleGamepad.press(gamepadbutton);
    }
}

void releaseKey(uint8_t key) {
    uint32_t gamepadbutton = pow(2,key);      // CONVERT TO THE BINARY MAPPING GAMEPAD KEYS USE
    Serial.print("release\t");
    Serial.println(key);
    if(bleGamepad.isConnected()) {
      bleGamepad.release(gamepadbutton);
    }
}



////////////////////////////////////////////////////////////////////////////////////////

my intention /my expected behavior : to make this gamepad/keypad works with a feature of deep sleep after 1 minute of inactivity (to save more power) and wake up with ANY button that I press. Do you have an idea how I could proceed? Unfortunately, I lack a technical understanding. sorry for my english, I'm not a native. hope someone can help me to solve this. TIA

0

There are 0 best solutions below