arduino(16):使用ESP32的SPI接入PS2手柄,修改LIB解决ESP32兼容问题,setup进行while循环验证,直到加载成功,修改别人代码上传到github上。

前言


相关arduino 全部分类:
https://blog.csdn.net/freewebsys/category_8799254.html

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/104734241

未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,关于arduino PS2手柄


从网上购买的山寨手柄,高仿Sony 手柄。
在这里插入图片描述
卖货的也没有提供详细资料。
还需要自己研究:

2,设备接入



买的时候带了一个接头,这个接头里面的线粗,用普通的杜邦线还不行。
一定不要把正负极接错了。输入 3.3v 的电压也可以:

在这里插入图片描述
ESP32 接线图:

折腾半天,这里用到 ESP32 的是安信可的,找的是官方的引脚。
用到的是 P18,P19,P32,P5 这个四个做为数据引脚,还有两个接上电源 3.3 电源和 GND 就行了。

3,Arduino代码


接线是一方面,有会就接好了,但是代码一直运行不成功,最后上 LIB 里面才发现,只支持 ESP8266 写了个宏,没写 ESP32 ,晕。
修改好了之后偶尔可以偶尔不行。开启日志可以,但是关闭不行。
后来发现,在setup 上访问配置的时候,设备还没有初始化,2 秒才能连上。我的天。
修改了代码。才可以。

替换:
#ifdef ESP8266
成:
#if defined(ESP8266) || defined(ESP32)
  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 ++;
  }

放在 wile 循环里面了。

直接克隆项目到 /arduino/libraries 目录下。
https://github.com/MyArduinoLib/Arduino-PS2X-ESP32

使用ESP32 测试:

https://github.com/MyArduinoLib/Arduino-PS2X-ESP32/blob/master/examples/PS2X_Example_ESP32/PS2X_Example.ino

#include <PS2X_lib.h>  //for v1.6

/******************************************************************
 * set pins connected to PS2 controller:
 *   - 1e column: original
 *   - 2e colmun: Stef?
 * replace pin numbers by the ones you use
 ******************************************************************/

//  ESP32 pin
// https://github.com/espressif/arduino-esp32/blob/master/docs/esp32_pinmap.png

#define PS2_DAT        19  //MISO  19
#define PS2_CMD        23  //MOSI  23
#define PS2_SEL         5  //SS     5
#define PS2_CLK        18  //SLK   18

/******************************************************************
 * select modes of PS2 controller:
 *   - pressures = analog reading of push-butttons
 *   - rumble    = motor rumbling
 * uncomment 1 of the lines for each mode selection
 ******************************************************************/
#define pressures   false
#define rumble      false

PS2X 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 = -1;
byte type = 0;
byte vibrate = 0;
int tryNum = 1;

void setup(){
    
    

 // 115200
  Serial.begin(115200);

  //added delay to give wireless ps2 module some time to startup, before configuring it
  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

  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);

  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() {
    
    

  if(type == 1){
    
     //DualShock Controller
    ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed

    //will be TRUE as long as button is pressed
    if(ps2x.Button(PSB_START))
      Serial.println("Start is being held");
    if(ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");

    //will be TRUE as long as button is pressed
    if(ps2x.Button(PSB_PAD_UP)) {
    
    
      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("△ pressed");
    }
    //△□○×
    if(ps2x.ButtonPressed(PSB_CIRCLE))               //will be TRUE if button was JUST pressed
      Serial.println("○ just pressed");
    if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
      Serial.println("× just changed");
    if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
      Serial.println("□ 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)); //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);
}

执行效果:

15:41:44.363 -> #try config 1
15:41:45.392 -> #try config 2
15:41:45.392 -> 73
15:41:45.392 -> Controller_type: 3
15:41:45.392 ->  DualShock Controller found 
15:41:45.392 -> Start is being held
15:41:45.392 -> Select is being held
15:41:45.392 -> Up held this hard: 0
15:41:45.392 -> Right held this hard: 0
15:41:45.392 -> LEFT held this hard: 0
15:41:45.392 -> DOWN held this hard: 0
15:41:45.392 -> Stick Values:0,0,0,0
15:41:45.459 -> × just changed
15:41:45.459 -> □ just released

控制按键显示效果:

16:43:36.515 -> Up held this hard: 0
16:43:36.581 -> Up held this hard: 0
16:43:36.647 -> Up held this hard: 0
16:43:36.680 -> Up held this hard: 0
16:43:37.975 -> DOWN held this hard: 0
16:43:38.008 -> DOWN held this hard: 0
16:43:38.074 -> DOWN held this hard: 0
16:43:39.037 -> Right held this hard: 0
16:43:39.071 -> Right held this hard: 0
16:43:39.137 -> Right held this hard: 0
16:43:39.170 -> Right held this hard: 0
16:43:40.468 -> LEFT held this hard: 0
16:43:40.501 -> LEFT held this hard: 0
16:43:40.567 -> LEFT held this hard: 0
16:43:41.529 -> Up held this hard: 0
16:43:41.562 -> Up held this hard: 0
16:43:41.628 -> Up held this hard: 0
16:43:41.695 -> Up held this hard: 0
16:43:42.226 -> △ pressed
16:43:43.022 -> △ pressed
16:43:43.254 -> △ pressed
16:43:44.116 -> ○ just pressed
16:43:44.581 -> × just changed
16:43:44.747 -> × just changed
16:43:45.244 -> □ just released
16:43:45.675 -> □ just released
16:43:46.107 -> □ just released
16:43:46.373 -> □ just released
16:43:46.672 -> × just changed
16:43:46.837 -> × just changed
16:43:47.103 -> × just changed
16:43:47.236 -> × just changed
16:43:48.729 -> L2 pressed
16:43:49.293 -> R2 pressed
16:43:51.582 -> Select is being held
16:43:51.615 -> Select is being held
16:43:51.681 -> Select is being held
16:43:51.714 -> Select is being held
16:43:52.278 -> Start is being held
16:43:52.345 -> Start is being held
16:43:52.378 -> Start is being held
16:43:56.231 -> Stick Values:127,128,127,128
16:43:56.265 -> Stick Values:127,128,127,128
16:43:56.330 -> Stick Values:127,128,127,128
16:43:56.364 -> Stick Values:127,128,127,128
16:43:56.430 -> Stick Values:127,128,127,128
16:43:56.463 -> Stick Values:0,128,127,128
16:43:56.529 -> Stick Values:0,128,127,128
16:43:56.563 -> Stick Values:0,128,127,128
16:43:56.629 -> Stick Values:0,128,127,128
16:43:56.662 -> Stick Values:0,45,127,128
16:43:56.728 -> Stick Values:127,128,127,128
16:43:56.762 -> Stick Values:127,128,127,128
16:43:56.828 -> Stick Values:127,128,127,128
16:43:56.894 -> Stick Values:127,128,127,128
16:43:56.927 -> Stick Values:127,255,127,128
16:43:56.994 -> Stick Values:127,255,127,128
16:43:57.027 -> Stick Values:127,255,127,128
16:43:57.093 -> Stick Values:127,128,127,128
16:43:57.126 -> Stick Values:127,128,127,128
16:43:57.193 -> Stick Values:127,128,127,128
16:43:57.226 -> Stick Values:127,128,127,128
16:43:59.083 -> Stick Values:0,128,127,128
16:43:59.117 -> Stick Values:0,128,127,128
16:43:59.183 -> Stick Values:0,255,127,128
16:43:59.216 -> Stick Values:113,255,127,128
16:43:59.282 -> Stick Values:127,255,127,128
16:43:59.415 -> Stick Values:127,0,127,128
16:43:59.481 -> Stick Values:127,0,127,128
16:43:59.514 -> Stick Values:0,0,127,128

按键都可以使用呢,特别注意,两个遥感杆要获得数据必须同时按住L1,R1,才可以。
然后就有 Stick 数据了。其他按键都是直接按下获得数据。

3,总结


本次进行开发正好是因为 ESP8266 设备接着别的东西呢,不想拆,正好手上有ESP32设备。
结果导致人家的库不支持,折腾半天,看了人家的Lib库代码,发现开发个库特别方便呢。
于是修改代码继续研究,由于不是正品手柄,导致获得配置总是时好时坏。
后来根据 WIFI的那样进行 while 循环,发现 2秒才识别上。怪不得老是失败呢。
解决了 lib 支持 esp32 的宏,和循环调用的问题,终于可以了。

没有玩过游戏,不太熟悉这个手柄,看文档知道了需要同时 按下 R1 L1 即可。
然后在控制小车啥的就特别的方便了。

本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/104734241

博主地址是:https://blog.csdn.net/freewebsys

猜你喜欢

转载自blog.csdn.net/freewebsys/article/details/104734241