// Joystick control for the Virtual Robot Presence
// Keyboard emulator
// Please be sure to follow the instructions online for flashing the Arduino Uno as a USB HID Keyboard
// When it is plugged in to the computer, check Device Manager, and it should show as another keyboard device


#define jY A1
#define jX A0
int jXval=0;
int jYval=0;
int lastDir = 5;
int dir = 0;
String dirName;
int buttonState = 0;
int buttonPin = 2;

int decKey = 34; // initialize at STOPPED

uint8_t buf[8] = { 
  0 };   /* Keyboard report buffer */


void setup() {
 //Keyboard.begin();
 // Code for keyboard
 Serial.begin(9600);
 randomSeed(analogRead(0));
 delay(200);

 pinMode(buttonPin, INPUT_PULLUP);
 // LED
 pinMode(4, OUTPUT);
 digitalWrite(4, LOW);
 
}

void loop() {
  // put your main code here, to run repeatedly:
  jXval = map(analogRead(jX), 0, 1023, -2, 2);
  delay(100);
  jYval = map(analogRead(jY), 0, 1023, -2, 2);

  // *************************
  // TESTING out the button function with LED
  buttonState = digitalRead(buttonPin);
  if (buttonState == 0){
    digitalWrite(4, HIGH);
    buf[2] = 21; // 'r'
    Serial.write(buf, 8);
    releaseKey();
  }
  else {
    digitalWrite(4, LOW);
  }
  //Serial.print("Button =  ");
  //Serial.println(buttonState);
  // ************************

 
  // JOYSTICK COMMANDS
  if (jXval > 0 && jYval == 0) { // FORWARD
    dir = 8;
    //dir = 'w';
    dirName = "WARP SPEED, ENGAGE!";
    decKey = 37;
    
    
  } else if (jXval <0 && jYval == 0) { // REVERSE
    dir = 2;
    //dir = 's';
    dirName = "REVERSE";
    decKey = 31;
    
    
  } else if (jYval < 0 && jXval == 0) { // ROTATE LEFT
    dir = 4;
    //dir = 'a';
    dirName = "ROTATE LEFT";
    decKey = 33;
    
    
  } else if (jYval > 0 && jXval == 0) { // ROTATE RIGHT
    dir = 6;
    //dir = 'd';
    dirName = "ROTATE RIGHT";
    decKey = 35;
    
    
  } else if (jYval == 0 && jXval == 0) { // STOP
    dir = 5;
    //dir = 'x';
    dirName = "STOP";
    decKey = 34;
    
    
  } else if (jYval >= 1 && jXval < 0) { // REVERSE RIGHT
    dir = 3;
    //dir = 'c';
    dirName = "REVERSE RIGHT";
    decKey = 32;
    
    
  } else if (jYval >= 1 && jXval > 0) {// FORWARD RIGHT
    dir = 9;
    //dir = 'e';
    dirName = "FORWARD RIGHT";
    decKey = 38;
    
    
  } else if (jYval <= -1 && jXval <= -1) {// REVERSE LEFT
    dir = 1;
    //dir = "z";
    dirName = "REVERSE LEFT";
    decKey = 30;
    
    
  } else if (jYval <= -1 && jXval >= 1) {// FORWARD LEFT
    dir = 7;
    //dir = 'q';
    dirName = "FORWARD LEFT";
    decKey = 36;
    
    
  }
  if (dir != lastDir) {// if the current direction is different from the last

    lastDir = dir;

    buf[2] = decKey;
    Serial.write(buf, 8);
    releaseKey();
        
  }
 delay(10);
}

// HEX (USAGE ID's) , DEC (USAGE ID's)
// 0 = 27, 39
// 1 = 1E, 30
// 2 = 1F, 31
// 3 = 20, 32
// 4 = 21, 33
// 5 = 22, 34
// 6 = 23, 35
// 7 = 24, 36
// 8 = 25, 37
// 9 = 26, 38
// r = 15, 21

void releaseKey() 
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key  
}
