From da6cd494c78fae6073756da49d421608d4a5bf12 Mon Sep 17 00:00:00 2001 From: Jorge Lopez Seijas Date: Sun, 19 Feb 2017 23:34:42 +0100 Subject: Add arduino source code compatible with X Y axis and 9 buttons --- arduino/arduino.ino | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 arduino/arduino.ino diff --git a/arduino/arduino.ino b/arduino/arduino.ino new file mode 100644 index 0000000..1dece85 --- /dev/null +++ b/arduino/arduino.ino @@ -0,0 +1,67 @@ +#include +#include + +#include + +const byte ROWS = 3; +const byte COLS = 3; + +char buttons[ROWS][COLS] = { + { '0' , '1', '2' }, + { '3' , '4', '5' }, + { '6' , '7', '8' } +}; + +byte rowPins[ROWS] = { 2, 3, 4 }; +byte colPins[COLS] = { 5, 6, 7 }; + +Keypad buttonsPad = Keypad( makeKeymap(buttons), rowPins, colPins, ROWS, COLS ); + +const int XYPadPins[2] = { A0, A1 }; + +void setup() { + Joystick.begin(); +} + +float convertAnalogRange(float x, float in_min, float in_max, float out_min, float out_max, bool isDPad) +{ + if(isDPad && x != in_min && x != in_max) + { + return 0; + } + + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +void gamepad(){ + if(buttonsPad.getKeys()) + { + for(int i = 0; i < LIST_MAX; i++) + { + if(buttonsPad.key[i].stateChanged) + { + switch (buttonsPad.key[i].kstate) + { + case PRESSED: + Joystick.setButton(i, 1); + break; + case RELEASED: + Joystick.setButton(i, 0); + break; + } + } + } + } + + float value = analogRead(XYPadPins[0]); + value = convertAnalogRange(value, 0, 1023, -127, 127, true); + Joystick.setXAxis(value); + + value = value = analogRead(XYPadPins[1]); + value = convertAnalogRange(analogRead(A1), 0, 1023, -127, 127, true); + Joystick.setYAxis(value); +} + +void loop() { + gamepad(); +} -- cgit v1.2.3-54-g00ecf