How can we help you today?
【通訊】PS2X搖桿基礎連線
簡介
本單元介紹DUAL22開發板如何連接PS2搖桿裝置,接線方式請參考下圖說明。此外,PS2搖桿接收端燈號閃爍具有那些意義呢,請參照後續DEMO影片。
PS2接收器端-GPIO接線對應關係,如下。
- DI (data) :G34
- DO (command):G17
- SEL (select):G16
- CLK (clock):G21
- GND:接地端
- VCC:5V
成果展示
函數庫
#include <PS2X_lib.h>
韌體範例說明
本程式範例,微調自<PS2X_lib.h>內建範例程式。
#include <PS2X_lib.h>
//PS2X Pins & Setting-----------------
#define PS2_DAT 34 //input pin, DI
#define PS2_CMD 17 //output pin, DO
#define PS2_SEL 16 //output pin
#define PS2_CLK 21 //output pin
#define pressures false
#define rumble false
PS2X ps2x; // create PS2 Controller Class
int error = -1;
byte PS2X_type = 0;
byte vibrate = 0;
int tryNum = 1;
//-----------------
void setup() {
Serial.begin(115200);
PS2X_INIT();
}
void loop() {
PS2X_TESTING();
}
/*====================================
// PS2X 搖桿設定
=====================================*/
void PS2X_INIT(){
while (error != 0) {
delay(1000);// 1 second wait
//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, pressures, rumble);
Serial.print("#try config ");
Serial.println(tryNum);
tryNum ++;
}
Serial.println(ps2x.Analog(1), HEX);
PS2X_type = ps2x.readType();
switch(PS2X_type) {
case 0:
Serial.printf(" Unknown Controller type found, type is %d\n", PS2X_type);
break;
case 1:
Serial.printf(" DualShock Controller found, type is %d\n", PS2X_type);
break;
case 2:
Serial.printf(" GuitarHero Controller found, type is %d\n", PS2X_type);
break;
case 3:
Serial.printf(" Wireless Sony DualShock Controller found, type is %d\n", PS2X_type);
break;
}
}
/*====================================
// PS2X 搖桿各種按鍵測試,
// 串列監控視窗輸出按鍵訊息
=====================================*/
void PS2X_TESTING(){
if(PS2X_type != 2){ //DualShock Controller
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}
vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
if(ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if(ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if(ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if(ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if(ps2x.Button(PSB_TRIANGLE))
Serial.println("Triangle pressed");
}
if(ps2x.ButtonPressed(PSB_CIRCLE)) //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");
if(ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR released
Serial.println("X just changed");
if(ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST released
Serial.println("Square just released");
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);
}
}
delay(50);
}