#include "common.h"

#define JOY_UPDOWN_PIN          18
#define JOY_LEFTRIGHT_PIN       19
#define JOY_BUTTON_PIN          20
#define JOYSTICK_RESOLUTION     4096  //12 bit


static uint8_t prev_stt = HIGH;
static bool btn_pressed = false;
static uint8_t pin_press = 0;
static unsigned long long btn_time = 0;

void IRAM_ATTR btnClicked() {
  btn_pressed = true;
  btn_time = millis();
}

void btnRisingIsrHandler(uint8_t btn_dir)
{
  btn_pressed = true;
  pin_press = btn_dir;
  btn_time = millis();
}

void btnFallingIsrHandler(uint8_t btn_dir)
{

}

void JOYSTICK_Init()
{
  pinMode(JOY_UPDOWN_PIN,     INPUT);
  pinMode(JOY_LEFTRIGHT_PIN,  INPUT);
  pinMode(JOY_BUTTON_PIN,     INPUT_PULLUP);
}

void handleInterrupt(int i) {
    // your stuff
}

void LocalRegisterButton(uint8_t pin)
{
  pinMode(pin, INPUT_PULLUP);
  
  switch ( pin )
  {
    case 4:
      attachInterrupt(4, []() IRAM_ATTR {
          if (digitalRead(4)) {
            btnRisingIsrHandler(4);
          } else {
            btnFallingIsrHandler(4);
          }
        },
        CHANGE);
    break;

    case 11:
      attachInterrupt(11, []() IRAM_ATTR {
          if (digitalRead(11)) {
            btnRisingIsrHandler(11);
          } else {
            btnFallingIsrHandler(11);
          }
        },
        CHANGE);
    break;
  }

}

template<int pin> void RegisterLambdaPin()
{
      pinMode(pin, INPUT_PULLUP);
      attachInterrupt(pin, []() IRAM_ATTR {
          if (digitalRead(pin)) {
            btnRisingIsrHandler(pin);
          } else {
            btnFallingIsrHandler(pin);
          }
        },
        CHANGE);
}

void setup() {
  int btn_id = 0;

  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32-S2!");
  pinMode(10, INPUT_PULLUP);
  pinMode(SPEAKER_PIN, OUTPUT);
  int i = 10;

  // LocalRegisterButton(4);
  RegisterLambdaPin<4>();

  MELODY_Init();
}

void beep(int bCount,int bDelay){
  for (int i = 0; i<=bCount; i++)
  {
    digitalWrite(SPEAKER_PIN,HIGH);
    for(int i2=0; i2<bDelay; i2++) {
      __asm__("nop\n\t");
    }
  
    digitalWrite(SPEAKER_PIN,LOW);
    for(int i2=0; i2<bDelay; i2++) {
      __asm__("nop\n\t");
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(10); // this speeds up the simulation

  if (pin_press > 0 ) {
    Serial.printf("Pin pressed: %d\n", pin_press);
    Serial.printf("Button Pressed at %d\n", btn_time);
    pin_press = 0;
  }

  return;
  if (btn_pressed) {
    btn_pressed = false;
    Serial.printf("Button Pressed at %d\n", btn_time);

    // tone(SPEAKER_PIN, 4000, 30);
    // delay(30);
    // tone(SPEAKER_PIN, 3000, 30);
    // delay(30);
    // tone(SPEAKER_PIN, 2000, 30);
    // delay(30);

    // beep(20, 420);
    // delay(100);
    // beep(20, 420);
    // delay(100);
    // beep(20, 420);
    // delay(100);
    // beep(20, 420);
    // delay(1000);

    // int powerUp = 0;
    // beep(20, 400 + (powerUp * 100));
    // delay(100);
    // beep(20, 400 + (powerUp * 100));
    // delay(100);
    // beep(20, 400 + (powerUp * 100));
    // delay(100);
    // beep(20, 400 + (powerUp * 100));
    // delay(1000);

    // powerUp = 1;
    // beep(20, 400 + (powerUp * 100));
    // delay(100);
    // beep(20, 400 + (powerUp * 100));
    // delay(100);
    // beep(20, 400 + (powerUp * 100));
    // delay(100);
    // beep(20, 400 + (powerUp * 100));
    // delay(1000);

    MELODY_Play(0);
    // GAMES_Beep(100, 500);
  }

  // Serial.print("LR: "); Serial.print(analogRead(JOY_LEFTRIGHT_PIN));
  // Serial.print(" - UD: "); Serial.println(analogRead(JOY_UPDOWN_PIN));
}
Loading
esp32-s2-devkitm-1