› Foros › Retro y descatalogado › Arcade y emulación
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
16, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
true, true, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
#include "UnoJoy.h"
#include "PS2X_lib.h"
#define PS2_DAT 11 //14
#define PS2_CMD 9 //15
#define PS2_SEL 8 //16
#define PS2_CLK 10 //17
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
setupPins();
setupUnoJoy();
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(0, 255);
Joystick.setYAxisRange(0,255);
Joystick.setRxAxisRange(0,255);
Joystick.setRyAxisRange(0,255);
}
void loop(){
// Always be getting fresh data
dataForController_t controllerData = getControllerData();
setControllerData(controllerData);
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
if (controllerData.triangleOn) {Joystick.setButton(0,1);} else {Joystick.setButton(0,0);}
if (controllerData.circleOn) {Joystick.setButton(1,1);} else {Joystick.setButton(1,0);}
if (controllerData.squareOn) {Joystick.setButton(2,1);} else {Joystick.setButton(2,0);}
if (controllerData.crossOn) {Joystick.setButton(3,1);} else {Joystick.setButton(3,0);}
if (controllerData.dpadUpOn) {Joystick.setButton(4,1);} else {Joystick.setButton(4,0);}
if (controllerData.dpadDownOn) {Joystick.setButton(5,1);} else {Joystick.setButton(5,0);}
if (controllerData.dpadLeftOn ) {Joystick.setButton(6,1);} else {Joystick.setButton(6,0);}
if (controllerData.dpadRightOn) {Joystick.setButton(7,1);} else {Joystick.setButton(7,0);}
if (controllerData.l1On) {Joystick.setButton(8,1);} else {Joystick.setButton(8,0);}
if (controllerData.r1On) {Joystick.setButton(9,1);} else {Joystick.setButton(9,0);}
//if (controllerData.selectOn) {Joystick.setButton(10,1);} else {Joystick.setButton(10,0);}
if (controllerData.startOn) {Joystick.setButton(11,1);} else {Joystick.setButton(11,0);}
if (controllerData.l2On) {Joystick.setButton(12,1);} else {Joystick.setButton(12,0);}
if (controllerData.l3On) {Joystick.setButton(13,1);} else {Joystick.setButton(13,0);}
if (controllerData.r2On) {Joystick.setButton(14,1);} else {Joystick.setButton(14,0);}
if (controllerData.r3On) {Joystick.setButton(15,1);} else {Joystick.setButton(15,0);}
Joystick.setXAxis(controllerData.leftStickX);
Joystick.setYAxis(controllerData.leftStickY);
Joystick.setRxAxis(controllerData.rightStickX);
Joystick.setRyAxis(controllerData.rightStickY);
}
void setupPins(void){
// Set all the digital pins as inputs
// with the pull-up enabled, except for the
// two serial line pins
for (int i = 2; i <= 12; i++){
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}
pinMode(A4, INPUT);
digitalWrite(A4, HIGH);
pinMode(A5, INPUT);
digitalWrite(A5, HIGH);
}
dataForController_t getControllerData(void){
// Set up a place for our controller data
// Use the getBlankDataForController() function, since
// just declaring a fresh dataForController_t tends
// to get you one filled with junk from other, random
// values that were in those memory locations before
dataForController_t controllerData = getBlankDataForController();
// Since our buttons are all held high and
// pulled low when pressed, we use the "!"
// operator to invert the readings from the pins
ps2x.read_gamepad(false, vibrate);
controllerData.triangleOn = ps2x.Button(PSB_TRIANGLE);
controllerData.circleOn = ps2x.Button(PSB_CIRCLE);
controllerData.squareOn = ps2x.Button(PSB_SQUARE);
controllerData.crossOn = ps2x.Button(PSB_CROSS);
controllerData.dpadUpOn = ps2x.Button(PSB_PAD_UP);
controllerData.dpadDownOn = ps2x.Button(PSB_PAD_DOWN);
controllerData.dpadLeftOn = ps2x.Button(PSB_PAD_LEFT);
controllerData.dpadRightOn = ps2x.Button(PSB_PAD_RIGHT);
controllerData.l1On = ps2x.Button(PSB_L1);
controllerData.r1On = ps2x.Button(PSB_R1);
controllerData.selectOn = ps2x.Button(PSB_SELECT);
controllerData.startOn = ps2x.Button(PSB_START);
controllerData.l2On = ps2x.Button(PSB_L2);
controllerData.l3On = ps2x.Button(PSB_L3);
controllerData.r2On = ps2x.Button(PSB_R2);
controllerData.r3On = ps2x.Button(PSB_R3);
// Set the analog sticks
// Since analogRead(pin) returns a 10 bit value,
// we need to perform a bit shift operation to
// lose the 2 least significant bits and get an
// 8 bit number that we can use
controllerData.leftStickX = ps2x.Analog(PSS_LX), DEC;
controllerData.leftStickY = ps2x.Analog(PSS_LY), DEC;
controllerData.rightStickX = ps2x.Analog(PSS_RX), DEC;
controllerData.rightStickY = ps2x.Analog(PSS_RY), DEC;
// And return the data!
return controllerData;
}