索尼游戏手柄SP2的开发体会
索尼游戲手柄SP2的開發體會
- 1.PS手柄介紹
- 接收器引腳輸出:
- 通信時序:
- 2、代碼解讀
- 3、庫文件解讀
ps2手柄是索尼的PlayStation2游戲機的遙控手柄。
該款手柄的通訊協議被游戲愛好者破解,使得手柄可以接在其他器件上遙控使用,比如遙控我們熟悉的智能小車。
突出的特點是這款手柄性價比極高,按鍵豐富,方便擴展到其它應用中。
1.PS手柄介紹
ps2手柄由手柄與接收器兩部分組成。
接收器與單片機相連,用于接收手柄發來的信息,將信號傳遞給單片機。
單片機也可通過接收器,向手柄發送命令,配置手柄的發送模式。
接收器引腳輸出:
DI/DAT:信號流向,從手柄到主機,此信號是一個8bit的串行數據,同步傳送于時鐘的下降沿。信號的讀取在時鐘由高到低的變化過程中完成。
DO/CMD:信號流向,從主機到手柄,此信號和DI相對,信號是一個8bit的串行數據,同步傳送于時鐘的下降沿。
NC:空端口;
GND:電源地;
VDD:接收器工作電源,電源范圍3~5V;
CS/SEL:用于提供手柄觸發信號。在通訊期間,處于低電平;
CLK:時鐘信號,由主機發出,用于保持數據同步;
NC:空端口;
ACK:從手柄到主機的應答信號。此信號在每個8bits數據發送的最后一個周期變低并且CS一直保持低電平,如果CS信號不變低,約60微秒PS主機會試另一個外設。在編程時未使用ACK端口。
通信時序:
CS線在通訊期間拉低,通信過程中CS信號線在一串數據(9個字節,每個字節為8位)發送完畢后才會拉高,而不是每個字節發送完拉高。
DO、DI在在CLK時鐘的下降沿完成數據的發送和讀取。
時鐘頻率250KHz(4us),如果接收數據不穩定,可以適當的增加頻率。
在通訊過程中,一串數據通訊完成后CS才會由低轉高,不是1個字節通訊完成后就由低轉高,在通訊期間,一直處于低電平。
在時鐘下降沿時,完成數據(1bit)的發送與接收,發送和接收是同時完成的。
當單片機想讀手柄數據或向手柄發送命令時,將會拉低CS線電平,并發出一個命令“0x01”;手柄會回復它的ID“0x41=綠燈模式(非模擬模式),0x73=紅燈模式(模擬模式)”;
在手柄發送ID的同時,單片機將傳送0x42,請求數據;隨后手柄發送出0x5A,告訴單片機“數據來了”。
idle:數據線空閑,該數據線無數據傳送。一個通訊周期有9個字節(8位),這些數據是依次按位傳送。
主要的通信協議如下:
2、代碼解讀
這里我們來學習下ardunio的庫文件
首先在https://github.com/madsci1016/Arduino-PS2X 這里下載ps2x的庫文件。
將PS2X_lib放到庫文件目錄下。
接線方法如下:
PS2X_Example.ino 文件解讀:
#include <PS2X_lib.h> //for v1.6 關聯庫文件/****************************************************************** //接線管腳定義******************************************************************/ #define PS2_DAT 13 #define PS2_CMD 11 #define PS2_SEL 10 #define PS2_CLK 12 /******************************************************************* select modes of PS2 controller:* 選擇PS2的控制模式* - pressures = 按鍵模擬量方式讀取* - rumble = motor rumbling (尚未了解)******************************************************************/ //#define pressures true #define pressures false //#define rumble true #define rumble falsePS2X ps2x; // create PS2 Controller Class 創建對象//right now, the library does NOT support hot pluggable controllers, meaning //you must always either restart your Arduino after you connect the controller, //or call config_gamepad(pins) again after connecting the controller.int error = 0; byte type = 0; byte vibrate = 0;// Reset func void (* resetFunc) (void) = 0;//初始化 void setup(){Serial.begin(115200); //設置串口波特率delay(500); //added delay to give wireless ps2 module some time to startup, before configuring it//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for errorerror = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);if(error == 0){Serial.print("Found Controller, configured successful ");Serial.print("pressures = ");if (pressures)Serial.println("true ");elseSerial.println("false");Serial.print("rumble = ");if (rumble)Serial.println("true)");elseSerial.println("false");Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");Serial.println("holding L1 or R1 will print out the analog stick values.");Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");} else if(error == 1)Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");else if(error == 2)Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");else if(error == 3)Serial.println("Controller refusing to enter Pressures mode, may not support it. ");type = ps2x.readType(); switch(type) {case 0:Serial.println("Unknown Controller type found ");break;case 1:Serial.println("DualShock Controller found ");break;case 2:Serial.println("GuitarHero Controller found ");break;case 3:Serial.println("Wireless Sony DualShock Controller found ");break;} }void loop() {/* You must Read Gamepad to get new values and set vibration valuesps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)if you don't enable the rumble, use ps2x.read_gamepad(); with no valuesYou should call this at least once a second*/ if(error == 1){ //skip loop if no controller foundresetFunc();}if(type == 2){ //Guitar Hero Controllerps2x.read_gamepad(); //read controller if(ps2x.ButtonPressed(GREEN_FRET))Serial.println("Green Fret Pressed");if(ps2x.ButtonPressed(RED_FRET))Serial.println("Red Fret Pressed");if(ps2x.ButtonPressed(YELLOW_FRET))Serial.println("Yellow Fret Pressed");if(ps2x.ButtonPressed(BLUE_FRET))Serial.println("Blue Fret Pressed");if(ps2x.ButtonPressed(ORANGE_FRET))Serial.println("Orange Fret Pressed"); if(ps2x.ButtonPressed(STAR_POWER))Serial.println("Star Power Command");if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressedSerial.println("Up Strum");if(ps2x.Button(DOWN_STRUM))Serial.println("DOWN Strum");if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressedSerial.println("Start is being held");if(ps2x.Button(PSB_SELECT))Serial.println("Select is being held");if(ps2x.Button(ORANGE_FRET)) { // print stick value IF TRUESerial.print("Wammy Bar Position:");Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); } }else { //DualShock Controllerps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speedif(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressedSerial.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 pressedSerial.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) buttonif (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 pressedSerial.println("Circle just pressed");if(ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR releasedSerial.println("X just changed");if(ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST releasedSerial.println("Square just released"); if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUESerial.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); }從上面的示例代碼中,我們了解到ardunio的開發文檔里首先關聯了庫函數
#include <PS2X_lib.h> //for v1.6 關聯庫文件
然后對接收器的管腳定義:
#define PS2_DAT 13
#define PS2_CMD 11
#define PS2_SEL 10
#define PS2_CLK 12
創建手柄對象:
PS2X ps2x; // create PS2 Controller Class 創建對象運用串口工具進行調試,在setup() 初始化函數里首先配置了串口,
接著調用手柄配置函數進行配置
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
通過返回值確認手柄配置是否成功,進而識別手柄的類型。
type = ps2x.readType();
在循環體loop()函數中:
針對不同的手柄類型,進行按鍵的讀取。
總結:通過這段代碼很容易開展游戲手柄的開發。
接著我們進一步分析庫文件對手柄的數據結構的定義以及驅動部分,進而學習手柄的通信協議。
不過今天的重點我們放在如何開展通信層的驅動上。
3、庫文件解讀
/****************************************************************** * Super amazing PS2 controller Arduino Library v1.8 * details and example sketch: * http://www.billporter.info/?p=240 * * Original code by Shutter on Arduino Forums * * Revamped, made into lib by and supporting continued development: * Bill Porter * www.billporter.info * * Contributers: * Eric Wetzel (thewetzel@gmail.com) * Kurt Eckhardt * * Lib version history * 0.1 made into library, added analog stick support. * 0.2 fixed config_gamepad miss-spelling * added new functions: * NewButtonState(); * NewButtonState(unsigned int); * ButtonPressed(unsigned int); * ButtonReleased(unsigned int); * removed 'PS' from begining of ever function * 1.0 found and fixed bug that wasn't configuring controller * added ability to define pins * added time checking to reconfigure controller if not polled enough * Analog sticks and pressures all through 'ps2x.Analog()' function * added: * enableRumble(); * enablePressures(); * 1.1 * added some debug stuff for end user. Reports if no controller found * added auto-increasing sentence delay to see if it helps compatibility. * 1.2 * found bad math by Shutter for original clock. Was running at 50kHz, not the required 500kHz. * fixed some of the debug reporting. * 1.3 * Changed clock back to 50kHz. CuriousInventor says it's suppose to be 500kHz, but doesn't seem to work for everybody. * 1.4 * Removed redundant functions. * Fixed mode check to include two other possible modes the controller could be in. * Added debug code enabled by compiler directives. See below to enable debug mode. * Added button definitions for shapes as well as colors. * 1.41 * Some simple bug fixes * Added Keywords.txt file * 1.5 * Added proper Guitar Hero compatibility * Fixed issue with DEBUG mode, had to send serial at once instead of in bits * 1.6 * Changed config_gamepad() call to include rumble and pressures options * This was to fix controllers that will only go into config mode once * Old methods should still work for backwards compatibility * 1.7 * Integrated Kurt's fixes for the interrupts messing with servo signals * Reorganized directory so examples show up in Arduino IDE menu * 1.8 * Added Arduino 1.0 compatibility. * 1.9 * Kurt - Added detection and recovery from dropping from analog mode, plus * integreated Chipkit (pic32mx...) support * * * *This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or(at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/> * ******************************************************************/// $$$$$$$$$$$$ DEBUG ENABLE SECTION $$$$$$$$$$$$$$$$ // to debug ps2 controller, uncomment these two lines to print out debug to uart #define PS2X_DEBUG //#define PS2X_COM_DEBUG#ifndef PS2X_lib_h#define PS2X_lib_h#if ARDUINO > 22#include "Arduino.h" #else#include "WProgram.h" #endif#include <math.h> #include <stdio.h> #include <stdint.h> #ifdef __AVR__// AVR#include <avr/io.h>#define CTRL_CLK 4#define CTRL_BYTE_DELAY 3 #else// Pic32...#include <pins_arduino.h>#define CTRL_CLK 5#define CTRL_CLK_HIGH 5#define CTRL_BYTE_DELAY 4 #endif //These are our button constants #define PSB_SELECT 0x0001 #define PSB_L3 0x0002 #define PSB_R3 0x0004 #define PSB_START 0x0008 #define PSB_PAD_UP 0x0010 #define PSB_PAD_RIGHT 0x0020 #define PSB_PAD_DOWN 0x0040 #define PSB_PAD_LEFT 0x0080 #define PSB_L2 0x0100 #define PSB_R2 0x0200 #define PSB_L1 0x0400 #define PSB_R1 0x0800 #define PSB_GREEN 0x1000 #define PSB_RED 0x2000 #define PSB_BLUE 0x4000 #define PSB_PINK 0x8000 #define PSB_TRIANGLE 0x1000 #define PSB_CIRCLE 0x2000 #define PSB_CROSS 0x4000 #define PSB_SQUARE 0x8000//Guitar button constants #define UP_STRUM 0x0010 #define DOWN_STRUM 0x0040 #define STAR_POWER 0x0100 #define GREEN_FRET 0x0200 #define YELLOW_FRET 0x1000 #define RED_FRET 0x2000 #define BLUE_FRET 0x4000 #define ORANGE_FRET 0x8000 #define WHAMMY_BAR 8//These are stick values #define PSS_RX 5 #define PSS_RY 6 #define PSS_LX 7 #define PSS_LY 8//These are analog buttons #define PSAB_PAD_RIGHT 9 #define PSAB_PAD_UP 11 #define PSAB_PAD_DOWN 12 #define PSAB_PAD_LEFT 10 #define PSAB_L2 19 #define PSAB_R2 20 #define PSAB_L1 17 #define PSAB_R1 18 #define PSAB_GREEN 13 #define PSAB_RED 14 #define PSAB_BLUE 15 #define PSAB_PINK 16 #define PSAB_TRIANGLE 13 #define PSAB_CIRCLE 14 #define PSAB_CROSS 15 #define PSAB_SQUARE 16#define SET(x,y) (x|=(1<<y)) #define CLR(x,y) (x&=(~(1<<y))) #define CHK(x,y) (x & (1<<y)) #define TOG(x,y) (x^=(1<<y))class PS2X {public:boolean Button(uint16_t); //will be TRUE if button is being pressedunsigned int ButtonDataByte();boolean NewButtonState();boolean NewButtonState(unsigned int); //will be TRUE if button was JUST pressed OR releasedboolean ButtonPressed(unsigned int); //will be TRUE if button was JUST pressedboolean ButtonReleased(unsigned int); //will be TRUE if button was JUST releasedvoid read_gamepad();boolean read_gamepad(boolean, byte);byte readType();byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t);byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);void enableRumble();bool enablePressures();byte Analog(byte);void reconfig_gamepad();private:inline void CLK_SET(void);inline void CLK_CLR(void);inline void CMD_SET(void);inline void CMD_CLR(void);inline void ATT_SET(void);inline void ATT_CLR(void);inline bool DAT_CHK(void);unsigned char _gamepad_shiftinout (char);unsigned char PS2data[21];void sendCommandString(byte*, byte);unsigned char i;unsigned int last_buttons;unsigned int buttons;#ifdef __AVR__uint8_t maskToBitNum(uint8_t);uint8_t _clk_mask; volatile uint8_t *_clk_oreg;uint8_t _cmd_mask; volatile uint8_t *_cmd_oreg;uint8_t _att_mask; volatile uint8_t *_att_oreg;uint8_t _dat_mask; volatile uint8_t *_dat_ireg;#elseuint8_t maskToBitNum(uint8_t);uint16_t _clk_mask; volatile uint32_t *_clk_lport_set;volatile uint32_t *_clk_lport_clr;uint16_t _cmd_mask; volatile uint32_t *_cmd_lport_set;volatile uint32_t *_cmd_lport_clr;uint16_t _att_mask; volatile uint32_t *_att_lport_set;volatile uint32_t *_att_lport_clr;uint16_t _dat_mask; volatile uint32_t *_dat_lport;#endifunsigned long last_read;byte read_delay;byte controller_type;boolean en_Rumble;boolean en_Pressures; };#endif庫文件.h中,定義了按鍵 、手柄類、以及操作函數。
接著我們再來學習下.cpp文件
1、通信管腳的操作:
//管腳寄存器映射_clk_mask = digitalPinToBitMask(clk);_clk_oreg = portOutputRegister(digitalPinToPort(clk));_cmd_mask = digitalPinToBitMask(cmd);_cmd_oreg = portOutputRegister(digitalPinToPort(cmd));_att_mask = digitalPinToBitMask(att);_att_oreg = portOutputRegister(digitalPinToPort(att));_dat_mask = digitalPinToBitMask(dat);_dat_ireg = portInputRegister(digitalPinToPort(dat)); //時鐘線拉高 inline void PS2X::CLK_SET(void) {register uint8_t old_sreg = SREG;cli();*_clk_oreg |= _clk_mask;SREG = old_sreg; }//時鐘線拉低 inline void PS2X::CLK_CLR(void) {register uint8_t old_sreg = SREG;cli();*_clk_oreg &= ~_clk_mask;SREG = old_sreg; }//DO 控制線拉高 inline void PS2X::CMD_SET(void) {register uint8_t old_sreg = SREG;cli();*_cmd_oreg |= _cmd_mask; // SET(*_cmd_oreg,_cmd_mask);SREG = old_sreg; }//DO 控制線拉低 inline void PS2X::CMD_CLR(void) {register uint8_t old_sreg = SREG;cli();*_cmd_oreg &= ~_cmd_mask; // SET(*_cmd_oreg,_cmd_mask);SREG = old_sreg; }//DI 拉高 inline void PS2X::ATT_SET(void) {register uint8_t old_sreg = SREG;cli();*_att_oreg |= _att_mask ;SREG = old_sreg; }//DI 拉低 inline void PS2X::ATT_CLR(void) {register uint8_t old_sreg = SREG;cli();*_att_oreg &= ~_att_mask;SREG = old_sreg; }inline bool PS2X::DAT_CHK(void) {return (*_dat_ireg & _dat_mask) ? true : false; }2、 通信數據傳送
通訊時序上可以看出,發送和接收是同時進行的。
3、讀取按鍵
boolean PS2X::read_gamepad(boolean motor1, byte motor2) {double temp = millis() - last_read;if (temp > 1500) //waited to longreconfig_gamepad();if(temp < read_delay) //waited too shortdelay(read_delay - temp);if(motor2 != 0x00)motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spinchar dword[9] = {0x01,0x42,0,motor1,motor2,0,0,0,0};byte dword2[12] = {0,0,0,0,0,0,0,0,0,0,0,0};// Try a few times to get valid data...for (byte RetryCnt = 0; RetryCnt < 5; RetryCnt++) {CMD_SET(); //先保證高電平CLK_SET(); //先保證高電平ATT_CLR(); // 拉低CS 類似SPI中片選,表示接下來開始傳送數據。delayMicroseconds(CTRL_BYTE_DELAY);//Send the command to send button and joystick data;for (int i = 0; i<9; i++) {PS2data[i] = _gamepad_shiftinout(dword[i]);}if(PS2data[1] == 0x79) { //if controller is in full data return mode, get the rest of datafor (int i = 0; i<12; i++) {PS2data[i+9] = _gamepad_shiftinout(dword2[i]);}}ATT_SET(); // HI disable joystick// Check to see if we received valid data or not. // We should be in analog mode for our data to be valid (analog == 0x7_)if ((PS2data[1] & 0xf0) == 0x70)break;// If we got to here, we are not in analog mode, try to recover...reconfig_gamepad(); // try to get back into Analog mode.delay(read_delay);}// If we get here and still not in analog mode (=0x7_), try increasing the read_delay...if ((PS2data[1] & 0xf0) != 0x70) {if (read_delay < 10)read_delay++; // see if this helps out...}#ifdef PS2X_COM_DEBUG//Serial.println("OUT:IN");for(int i=0; i<9; i++){//Serial.print(dword[i], HEX);//Serial.print(":");//Serial.print(PS2data[i], HEX);//Serial.print(" ");}for (int i = 0; i<12; i++) {//Serial.print(dword2[i], HEX);//Serial.print(":");//Serial.print(PS2data[i+9], HEX);//Serial.print(" ");}//Serial.println(""); #endiflast_buttons = buttons; //store the previous buttons states#if defined(__AVR__)buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions #elsebuttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions #endiflast_read = millis();return ((PS2data[1] & 0xf0) == 0x70); // 1 = OK = analog mode - 0 = NOK }反復閱讀代碼,基本上可以搞清楚協議表了。之所以解讀庫函數,是為了更加清除了解協議的底層開發。
附上 論壇里的幾篇文章:
PS2索尼游戲手柄解析和代碼開發
PS2手柄通訊協議解析—附資料和源碼
https://blog.csdn.net/weixin_44793491/article/details/105781595
arduino連接ps2手柄控制智能小車實踐記錄
https://blog.csdn.net/qq_30019617/article/details/109245402
arduino連接ps2手柄控制智能小車實踐記錄-續
https://blog.csdn.net/qq_30019617/article/details/109444260
總結
以上是生活随笔為你收集整理的索尼游戏手柄SP2的开发体会的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于STM32系列芯片的 IAP实现的探
- 下一篇: 学习FreeRTOS的几点体会