/*
 * Dr. Tõnis
 * September 2022
 * Pfäffikon
 * 
 * Boss Detectr with XIAO BLE module
 * 
 * This is the simplest solution by just adding a sensor to the 
 * XIAO and connecting it directly to the PC. If something 
 * triggers the sensor (in this case it's the mechanical switch) 
 * the Alt-Tab button combination is sent and the screen switched, 
 * the same would happen if one presses these buttons on a 
 * keyboard.
 */

//Include this module whether using Arduino stack or TinyUSB stack
#include <TinyUSB_Mouse_and_Keyboard.h>

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 10;     // the number of the pushbutton pin
const int ledPin =  LED_BUILTIN;      // the number of the LED pin
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Keyboard.begin();   //Unlike Arduino Keyboard.h, you must use begin.
  Serial.begin(115200);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  
  Serial.println("USB keyboard test");
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If not, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED off:
    digitalWrite(ledPin, HIGH); // negative logic!
    
    Keyboard.press(KEY_LEFT_ALT); //hold down the shift
    Keyboard.press(KEY_TAB);//release the shift
    Keyboard.releaseAll();          //release all
  
    delay(1000);     
  } else {
    // turn LED on:
    digitalWrite(ledPin, LOW); // negative logic!
    // do nothing
  }
}
