#include <FastLED.h>
#include <BleKeyboard.h>
#include <EEPROM.h>
#include "keyboard.h"
#include "save.h"

void changeID(int DevNum)
{
  save_config();
  clean_led();     
  esp_sleep_enable_timer_wakeup(1);
  esp_deep_sleep_start();
}

void selector_channel_led(int channel)
{
  switch (channel)
  {
    case 0:
      Wire.beginTransmission(0x2D); // Address of the STG8G
      Wire.write(0x03); // Value to write
      Wire.endTransmission();
      break;
    case 1:
      Wire.beginTransmission(0x2D); // Address of the STG8G
      Wire.write(0x04); // Value to write
      Wire.endTransmission();
      break;
    case 2:
      Wire.beginTransmission(0x2D); // Address of the STG8G
      Wire.write(0x05); // Value to write
      Wire.endTransmission();
      break;
  }
}


void usb_mode_led()
{
  Wire.beginTransmission(0x2D); // Address of the STG8G
  Wire.write(0x06); // Value to write
  Wire.endTransmission();
}

/*
  Method to print the reason by which ESP32
  has been awaken from sleep
*/
void print_wakeup_reason() {
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch (wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break;
  }
}

/*
  Method to print the GPIO that triggered the wakeup
*/
void print_GPIO_wake_up() {
  uint64_t GPIO_reason = esp_sleep_get_ext1_wakeup_status();
  Serial.print("GPIO that triggered the wake up: GPIO ");
  Serial.println((log(GPIO_reason)) / log(2), 0);
}
