diff options
author | Jorge Lopez Seijas <jorginho@riseup.net> | 2017-02-19 23:34:42 +0100 |
---|---|---|
committer | Jorge Lopez Seijas <jorginho@riseup.net> | 2017-02-19 23:34:42 +0100 |
commit | da6cd494c78fae6073756da49d421608d4a5bf12 (patch) | |
tree | c43c05916258f30f1e4a88936195b1832eff607b /arduino | |
parent | 75a1e754e0dfc7d5baddec5035cb2ad87e216a72 (diff) |
Diffstat (limited to 'arduino')
-rw-r--r-- | arduino/arduino.ino | 67 |
1 files changed, 67 insertions, 0 deletions
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 <Key.h> +#include <Keypad.h> + +#include <Joystick.h> + +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(); +} |