我正在尝试组合两个被识别为单个游戏手柄/控制器的输入源。为此,我使用了一个 Arduino Leonardo,它可以使用Joystick.h
库轻松转换为 HID 设备。作为输入,我使用 (1) 带有 PS2x 库的 PS2 控制器和 (2) 来自在主模式下工作的 HC-05 的蓝牙串行数据。
我已经构建了一个单独的草图来读取和解析蓝牙数据,甚至设法通过Joystick.h
库将其转发到 PC。
同时,为了使用 PS2 控制器,我使用相同的 Arduino Leonardo 将 PS2 连接到 USB。然而,当我试图结合这两个草图(结合来自 BT 和 PS2 的两个输入)时,有些东西不起作用。
我发现用于从 PS2 控制器读取值的 PS2_lib 会阻止 Arduino 读取 BTSerial 数据或反之亦然。
这是草图,当ReadOneByte()
被调用时,没有任何效果。
byte ReadOneByte() {
int ByteRead;
while(!BTSerial.available());
ByteRead = BTSerial.read();
return ByteRead;
}
如果我注释掉 while 循环,PS2 和游戏手柄就可以工作。
完整草图:
#include <PS2X_lib.h> //for v1.6
#include "Joystick.h"
#include<SoftwareSerial.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
12, 2, // Button Count, Hat Switch Count
true, true, true, // X and Y, but no Z Axis
false, false, true, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
#define PS2_DAT 3 //14
#define PS2_CMD 2 //15
#define PS2_SEL 12 //16
#define PS2_CLK 13 //17
//#define pressures true
#define pressures false
//#define rumble true
#define rumble false
const bool testAutoSendMode = false;
SoftwareSerial BTSerial(11, 10);
PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup() {
SoftwareSerial BTSerial(11, 10);
BTSerial.begin(57600);
Serial.begin(57600);
delay(300);
Joystick.begin();
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, true, false);
type = ps2x.readType();
Joystick.setXAxisRange(0, 255);
Joystick.setZAxisRange(0, 255);
Joystick.setYAxisRange(0, 255);
Joystick.setRzAxisRange(0, 255);
}
byte ReadOneByte() {
int ByteRead;
while (!BTSerial.available());
ByteRead = BTSerial.read();
return ByteRead;
}
void loop() {
ReadOneByte() ;
// Always be getting fresh data
if (error == 1) //skip loop if no controller found
return;
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
ps2x.read_gamepad(false, 0);
Joystick.setButton(0, ps2x.Button(PSB_TRIANGLE));
Joystick.setButton(1, ps2x.Button(PSB_CIRCLE));
Joystick.setButton(2, ps2x.Button(PSB_CROSS));
Joystick.setButton(3, ps2x.Button(PSB_SQUARE));
Joystick.setButton(4, ps2x.Button(PSB_L2));
Joystick.setButton(5, ps2x.Button(PSB_R2));
Joystick.setButton(6, ps2x.Button(PSB_L1));
Joystick.setButton(7, ps2x.Button(PSB_R1));
Joystick.setButton(8, ps2x.Button(PSB_SELECT));
Joystick.setButton(9, ps2x.Button(PSB_START));
Joystick.setButton(10, ps2x.Button(PSB_L3));
Joystick.setButton(11, ps2x.Button(PSB_R3));
Joystick.setXAxis(ps2x.Analog(PSS_LX));
Joystick.setYAxis(ps2x.Analog(PSS_LY));
Joystick.setZAxis(ps2x.Analog(PSS_RY));
Joystick.setRzAxis(ps2x.Analog(PSS_RX));
if (ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Joystick.setYAxis(0);
}
if (ps2x.Button(PSB_PAD_RIGHT)) {
Joystick.setXAxis(255);
}
if (ps2x.Button(PSB_PAD_LEFT)) {
Joystick.setXAxis(0);
}
if (ps2x.Button(PSB_PAD_DOWN)) {
Joystick.setYAxis(255);
}
}