Seeeduino Xiao HID code


#include "Adafruit_TinyUSB.h"

#define KEY_HOME        74
#define KEY_PAGEUP      75
#define KEY_DELETE      76
#define KEY_END         77
#define KEY_PAGEDOWN    78
#define KEY_RIGHT_ARROW 79
#define KEY_LEFT_ARROW  80
#define KEY_DOWN_ARROW  81
#define KEY_UP_ARROW    82
#define KEY_RETURN    	40 //important error correction


// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
  TUD_HID_REPORT_DESC_KEYBOARD(),
};

Adafruit_USBD_HID usb_hid;

// Output report callback for LED indicator such as Caplocks

void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
  // LED indicator is output report with only 1 byte length
  if ( report_type != HID_REPORT_TYPE_OUTPUT ) return;

  // The LED bit map is as follows: (also defined by KEYBOARD_LED_* )
  // Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
  uint8_t ledIndicator = buffer[0];
    // turn on LED if caplock is set
  digitalWrite(LED_BUILTIN, ledIndicator & KEYBOARD_LED_CAPSLOCK);

}

// the setup function runs once when you press reset or power the board
 void setup()
{
  pinMode(4, INPUT_PULLUP);

  usb_hid.setPollInterval(2);
  usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// this function shows capslock on or off, but inverted somehow.
  usb_hid.setReportCallback(NULL, hid_report_callback);

  usb_hid.begin();

  // wait until device mounted
  while( !USBDevice.mounted() ) delay(1);
}

void print_return(){
    uint8_t keycode[6] = {KEY_RETURN};
    usb_hid.keyboardReport(0, 0, keycode);
    delay(10);
    usb_hid.keyboardRelease(0);
    delay(100); 
}
void print_period(){
    uint8_t keycode[6] = {HID_KEY_PERIOD};
    usb_hid.keyboardReport(0, 0, keycode);
}

void print_home(){
     uint8_t keycode[6] = {  HID_KEY_HOME};
    usb_hid.keyboardReport(0, 0, keycode);
    delay(10);
    usb_hid.keyboardRelease(0);
    delay(100); 
}
void keyboard_hold_key(uint8_t key){
    uint8_t keycode[6] = {key};
    usb_hid.keyboardReport(0, 0, keycode);
  }

void print_tab(){
    uint8_t keycode[6] = { HID_KEY_TAB };
    usb_hid.keyboardReport(0, 0, keycode);
    delay(10);
    usb_hid.keyboardRelease(0);
    delay(100);   
}

void print_arrow_down(){
    uint8_t keycode[6] = { KEY_DOWN_ARROW };
    usb_hid.keyboardReport(0, 0, keycode);
    delay(10);
    usb_hid.keyboardRelease(0);
    delay(100);   
}
void print_backslash(){
    uint8_t keycode[6] = {HID_KEY_BACKSLASH};
    usb_hid.keyboardReport(0, 0, keycode);
    delay(10);
    usb_hid.keyboardRelease(0);
    delay(100);   
}



void print_ln(char *message,int len,char delays){
  char charactercecito=0;
  for(int i=0; i < len; i++){
      charactercecito=message[i];

      usb_hid.keyboardPress(0,charactercecito);

      delay(10);
      usb_hid.keyboardRelease(0);
      delay(delays+50);    
  }
  delay(100);
//  print_home();
}


char app_cmd[]="cmd";
void open_console(){
  uint8_t keycode[6] = {0};
  uint8_t modifier = 0;
  keycode[0] = HID_KEY_GUI_LEFT;
  keycode[2] = HID_KEY_R;
  usb_hid.keyboardReport(0, 0 , keycode);
  delay(100);
  usb_hid.keyboardRelease(0);
  delay(100);
  print_ln(app_cmd,sizeof(app_cmd),0);
}

char app_putty[]="c:\\users\\maren\\desktop\\putty.exe";
void open_putty(){
  uint8_t keycode[6] = {0};
  uint8_t modifier = 0;
  keycode[0] = HID_KEY_GUI_LEFT;
  keycode[2] = HID_KEY_R;
  usb_hid.keyboardReport(0, 0 , keycode);
  delay(100);
  usb_hid.keyboardRelease(0);
  delay(100);
  print_ln(app_putty,sizeof(app_putty),0);
}

char cmd_line[]="cd upload\nftp 192.168.1.9\n";
char ftp_line[]="maren\n,password\nbin\nprompt\nmput *\n";
char ftp_line2[]="bye\n";
char cmd_line2[]="exit\n";
char putty_line[]="192.168.1.9\n";
char login_line[]="root\npassword\ncd /home/www\n";
uint8_t state;

// the setup function runs once when you press reset or power the board
 void setup()
{
  pinMode(4, INPUT_PULLUP);

  usb_hid.setPollInterval(2);
  usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// this function shows capslock on or off, but inverted somehow.
  usb_hid.setReportCallback(NULL, hid_report_callback);

  usb_hid.begin();

  // wait until device mounted
  while( !USBDevice.mounted() ) delay(1);
}

void loop() {
  
  //  // Remote wakeup
  if ( USBDevice.suspended())
  {
    // Wake up host if we are in suspend mode
    // and REMOTE_WAKEUP feature is enabled by host
    USBDevice.remoteWakeup();
  }

  if ( !usb_hid.ready() ) return;
    
  delay(50);
  state = digitalRead(4); 
  delay(50); // maybe redundant

  if (state) {
    delay(2000); 
    open_console();
    delay(1000);
    print_return();
    delay(500);
    print_ln(cmd_line,sizeof(cmd_line),0);
    delay(500);
    print_ln(ftp_line,sizeof(ftp_line),0);  
    delay(5000);
    print_ln(ftp_line2,sizeof(ftp_line2),0);  
    delay(500);
    print_ln(cmd_line2,sizeof(cmd_line2),0);
  }  
  delay(2000);
  open_putty();
  delay(1000);
  print_return();
  delay(1000);
  print_ln(putty_line,sizeof(putty_line),0);
  delay(1000);
  print_ln(login_line,sizeof(login_line),0);
  delay(1000);
  
    
  while(1){
    delay(10000); // maybe redundant
  }
}