Last active
December 13, 2017 01:58
-
-
Save numpad0/ccb267cb571b8bed719270589b5421b2 to your computer and use it in GitHub Desktop.
SerialGamepad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SerialGamepad: Takes serial input to set Joystick status | |
// for easy gamepad input on various platforms | |
// Command syntax: | |
// b0000000000000000 set buttons 1-16 to on or off | |
// a0128 0128 0128 set axes 0-2 to respective values, 0-1023 | |
// Target: | |
// Arduino Leonardo, Micro, and compatible boards | |
//-Adapted-from:------------------------------------------------------ | |
// Simple gamepad example that demonstraits how to read five Arduino | |
// digital pins and map them to the Arduino Joystick library. | |
// by Matthew Heironimus | |
// 2016-11-24 | |
//-------------------------------------------------------------------- | |
#include <Joystick.h> | |
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, | |
16, 0, // Buttons, Hat switches | |
false, false, false, // X, Y, Z axes | |
false, false, false, // Rx, Ry, Rz axes | |
false, false, // Rudder, Throttle axes | |
true, true, true); // Accelerator, Brake, Steering axes | |
char letter; | |
char str[32]; | |
int strptr = 0; | |
void setup() { | |
// Initialize Joystick Library | |
Serial.begin(115200); | |
Joystick.begin(false); // false: don't update state immediately | |
for (int i = 0; i < 32; i++) { | |
str[i] = NULL; | |
} | |
Serial.println("Ready"); | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
letter = Serial.read(); | |
str[strptr++] = letter; | |
} | |
if (letter == '\n' || letter == '\r') { | |
str[strptr] = NULL; | |
Serial.print("Received command: " + String(str)); | |
if (str[0] == 'B' || str[0] == 'b') { | |
for (int i = 1; i < 17; i++) { | |
if (str[i] == '1') Joystick.setButton(i - 1, true); | |
if (str[i] != '1') Joystick.setButton(i - 1, false); | |
} | |
Serial.println("Button states updated"); | |
} | |
if (str[0] == 'A' || str[0] == 'a') { | |
int acc = 0; | |
int brake = 0; | |
int steer = 0; | |
acc = ((int)str[ 1] - 48) * 1000 + ((int)str[ 2] - 48) * 100 + ((int)str[ 3] - 48) * 10 + ((int)str[ 4] - 48); | |
brake = ((int)str[ 6] - 48) * 1000 + ((int)str[ 7] - 48) * 100 + ((int)str[ 8] - 48) * 10 + ((int)str[ 9] - 48); | |
steer = ((int)str[11] - 48) * 1000 + ((int)str[12] - 48) * 100 + ((int)str[13] - 48) * 10 + ((int)str[14] - 48); | |
Joystick.setAccelerator(acc); | |
Joystick.setBrake(brake); | |
Joystick.setSteering(steer); | |
Serial.println("Acc: " + String(acc) + " Brake: " + String(brake) + " Steering: " + String(steer)); | |
Serial.println("Analog axes updated"); | |
} | |
strptr = 0; | |
letter = NULL; | |
for (int i = 0; i < 32; i++) str[i] = NULL; | |
} | |
Joystick.sendState(); | |
delay(8); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment