void setup() 
{
  Serial.begin(115200);
  delay(1000);  //Take some time to open up the Serial Monitor

  // Increment boot number and print it every reboot
  ++bootCount;
  Serial.println();
  Serial.println("Safe lock system v2, 7 nov 2024");
  Serial.println("===============================");
  Serial.println("Boot number: " + String(bootCount));

  // Print the wakeup reason for ESP32
  print_wakeup_reason();

  if(wakeup_reason==ESP_SLEEP_WAKEUP_TIMER)
  {
    if (getBatteryVoltage() < BATT_VOLTAGE_LOW)
      sendBatteryLowMessage();
  }
  else   // Keyboard interrupt
  {

  uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();

      Serial.print("KEY that triggered the wake up: ");
        
      switch(GPIO_reason)
      {
        case GPIO_SEL_13 | GPIO_SEL_27: Serial.println("1"); break;
        case GPIO_SEL_12 | GPIO_SEL_27: Serial.println("2"); break;
        case GPIO_SEL_14 | GPIO_SEL_27: Serial.println("3"); break;

        case GPIO_SEL_13 | GPIO_SEL_26: Serial.println("4"); break;
        case GPIO_SEL_12 | GPIO_SEL_26: Serial.println("5"); break;
        case GPIO_SEL_14 | GPIO_SEL_26: Serial.println("6"); break;

        case GPIO_SEL_13 | GPIO_SEL_25: Serial.println("7"); break;
        case GPIO_SEL_12 | GPIO_SEL_25: Serial.println("8"); break;
        case GPIO_SEL_14 | GPIO_SEL_25: Serial.println("9"); break;

        case GPIO_SEL_13 | GPIO_SEL_33: Serial.println("*"); break;
        case GPIO_SEL_12 | GPIO_SEL_33: Serial.println("0"); break;
        case GPIO_SEL_14 | GPIO_SEL_33: Serial.println("#"); break;

        default: Serial.println((log(GPIO_reason))/log(2)); break;
      }
  }


// Define bitmask for multiple GPIOs
uint64_t bitmask = GPIO_SEL_13 | GPIO_SEL_12 | GPIO_SEL_14 | GPIO_SEL_27 | GPIO_SEL_26 | GPIO_SEL_25 | GPIO_SEL_33;


esp_sleep_enable_ext1_wakeup(bitmask, ESP_EXT1_WAKEUP_ANY_HIGH);

  rtc_gpio_pulldown_en(GPIO_NUM_13);  rtc_gpio_pullup_dis(GPIO_NUM_13); 
  rtc_gpio_pulldown_en(GPIO_NUM_12);  rtc_gpio_pullup_dis(GPIO_NUM_12); 
  rtc_gpio_pulldown_en(GPIO_NUM_14);  rtc_gpio_pullup_dis(GPIO_NUM_14); 

  rtc_gpio_pulldown_en(GPIO_NUM_27);  rtc_gpio_pullup_dis(GPIO_NUM_27); 
  rtc_gpio_pulldown_en(GPIO_NUM_26);  rtc_gpio_pullup_dis(GPIO_NUM_26);  
  rtc_gpio_pulldown_en(GPIO_NUM_25);  rtc_gpio_pullup_dis(GPIO_NUM_25); 
  rtc_gpio_pulldown_en(GPIO_NUM_33);  rtc_gpio_pullup_dis(GPIO_NUM_33);



/*
  Second we configure the wake up source
  We set our ESP32 to wake up every N seconds
  */
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup system to sleep for " + String(TIME_TO_SLEEP) + " Seconds");

  //Go to sleep now
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}