标题:
Arduino ps2手柄库PS2X_lib下载
[打印本页]
作者:
topshooters
时间:
2018-4-18 10:30
标题:
Arduino ps2手柄库PS2X_lib下载
可以使用ps2和兼容ps2手柄来控制程式,但要使用4个信号引脚
0.jpg
(26.88 KB, 下载次数: 173)
下载附件
2018-4-18 16:30 上传
Arduino源程序如下:
#include "PS2X_lib.h"
#include <math.h>
#include <stdio.h>
#include <stdint.h>
//#include <avr/io.h>
#if defined(__SAM3X8E__)
#include <sam/pio.h> // For the Due
#else
#include <avr/io.h> // For anything else before the Due
#endif
#if ARDUINO > 22
#include "Arduino.h"
#else
#include "WProgram.h"
#include "pins_arduino.h"
#endif
static byte enter_config[]={0x01,0x43,0x00,0x01,0x00};
static byte set_mode[]={0x01,0x44,0x00,0x01,0x03,0x00,0x00,0x00,0x00};
static byte set_bytes_large[]={0x01,0x4F,0x00,0xFF,0xFF,0x03,0x00,0x00,0x00};
static byte exit_config[]={0x01,0x43,0x00,0x00,0x5A,0x5A,0x5A,0x5A,0x5A};
static byte enable_rumble[]={0x01,0x4D,0x00,0x00,0x01};
static byte type_read[]={0x01,0x45,0x00,0x5A,0x5A,0x5A,0x5A,0x5A,0x5A};
/****************************************************************************************/
boolean PS2X::NewButtonState() {
return ((last_buttons ^ buttons) > 0);
}
/****************************************************************************************/
boolean PS2X::NewButtonState(unsigned int button) {
return (((last_buttons ^ buttons) & button) > 0);
}
/****************************************************************************************/
boolean PS2X::ButtonPressed(unsigned int button) {
return(NewButtonState(button) & Button(button));
}
/****************************************************************************************/
boolean PS2X::ButtonReleased(unsigned int button) {
return((NewButtonState(button)) & ((~last_buttons & button) > 0));
}
/****************************************************************************************/
boolean PS2X::Button(uint16_t button) {
return ((~buttons & button) > 0);
}
/****************************************************************************************/
unsigned int PS2X::ButtonDataByte() {
return (~buttons);
}
/****************************************************************************************/
byte PS2X::Analog(byte button) {
return PS2data[button];
}
/****************************************************************************************/
unsigned char PS2X::_gamepad_shiftinout (char byte) {
unsigned char tmp = 0;
for(unsigned char i=0;i<8;i++) {
if(CHK(byte,i)) CMD_SET();
else CMD_CLR();
CLK_CLR();
delayMicroseconds(CTRL_CLK);
//if(DAT_CHK()) SET(tmp,i);
if(DAT_CHK()) bitSet(tmp,i);
CLK_SET();
#if CTRL_CLK_HIGH
delayMicroseconds(CTRL_CLK_HIGH);
#endif
}
CMD_SET();
delayMicroseconds(CTRL_BYTE_DELAY);
return tmp;
}
/****************************************************************************************/
void PS2X::read_gamepad() {
read_gamepad(false, 0x00);
}
/****************************************************************************************/
boolean PS2X::read_gamepad(boolean motor1, byte motor2) {
double temp = millis() - last_read;
if (temp > 1500) //waited to long
reconfig_gamepad();
if(temp < read_delay) //waited too short
delay(read_delay - temp);
if(motor2 != 0x00)
motor2 = map(motor2,0,255,0x40,0xFF); //noting below 40 will make it spin
char 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(); // low enable joystick
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 data
for (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("");
#endif
last_buttons = buttons; //store the previous buttons states
#if defined(__AVR__)
buttons = *(uint16_t*)(PS2data+3); //store as one value for multiple functions
#else
buttons = (uint16_t)(PS2data[4] << 8) + PS2data[3]; //store as one value for multiple functions
#endif
last_read = millis();
return ((PS2data[1] & 0xf0) == 0x70); // 1 = OK = analog mode - 0 = NOK
}
…………
…………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
PS2X_lib.zip
(11.19 KB, 下载次数: 250)
2018-4-18 10:29 上传
点击文件名下载附件
Arduino lib
下载积分: 黑币 -5
作者:
luxin0222
时间:
2018-5-15 15:00
正好需要
作者:
汤洪伟
时间:
2018-9-23 16:42
lz,程序烧进去后串口监视器显示的是乱码的东西,能请教一下是怎么回事吗
作者:
忘我境界
时间:
2018-10-9 17:47
我正在研究
作者:
cyszll
时间:
2018-12-14 06:40
您好!请问如果将其编译成.o文件后,调用出错,该如何解决?
作者:
xcs18228262660
时间:
2019-12-17 17:07
怎么添加库文件呢
作者:
duang1107
时间:
2021-12-16 22:19
xcs18228262660 发表于 2019-12-17 17:07
怎么添加库文件呢
项目栏里的加载库,点开有一个添加库,可以直接导入zip文件
作者:
sdfjklkj
时间:
2023-5-29 02:24
您好!请问如果将其编译成.o文件后,调用出错,该如何解决?
作者:
sdfjklkj
时间:
2023-5-29 02:24
您好!请问如果将其编译成.o文件后,调用出错,该如何解决?
作者:
LEEEEEXXXJ
时间:
2024-3-28 22:03
您好,这个第五行//#include <avr/io.h>是什么呀,我的arduino一直显示不存在这个头文件
作者:
wscxz
时间:
2025-3-14 01:41
下载PS2X软件
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1