15# include "pins_arduino.h"
18static byte enter_config[] = {0x01, 0x43, 0x00, 0x01, 0x00};
19static byte set_mode[] = {0x01, 0x44, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00};
20static byte set_bytes_large[] = {0x01, 0x4F, 0x00, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00};
21static byte exit_config[] = {0x01, 0x43, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};
22static byte enable_rumble[] = {0x01, 0x4D, 0x00, 0x00, 0x01};
23static byte type_read[] = {0x01, 0x45, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};
28 return ((last_buttons ^ buttons) > 0);
34 return (((last_buttons ^ buttons) & button) > 0);
46 return ((
NewButtonState(button)) & ((~last_buttons & button) > 0));
52 return ((~buttons & button) > 0);
58 return (~buttons) & 0xFFFF;
64 return PS2data[button];
68unsigned char PS2X::_gamepad_shiftinout(
char byte)
70 unsigned char tmp = 0;
71 for (
unsigned char i = 0; i < 8; i++) {
81 if (DAT_CHK()) bitSet(tmp, i);
102 double temp = millis() - last_read;
107 if (temp < read_delay)
108 delay(read_delay - temp);
111 motor2 = map(motor2, 0, 255, 0x40, 0xFF);
113 byte dword[9] = {0x01, 0x42, 0, motor1, motor2, 0, 0, 0, 0};
114 byte dword2[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
117 for (
byte RetryCnt = 0; RetryCnt < 5; RetryCnt++) {
124 for (
int i = 0; i < 9; i++) {
125 PS2data[i] = _gamepad_shiftinout(dword[i]);
128 if (PS2data[1] == 0x79) {
130 for (
int i = 0; i < 12; i++) {
131 PS2data[i + 9] = _gamepad_shiftinout(dword2[i]);
138 if ((PS2data[1] & 0xf0) == 0x70)
break;
147 if ((PS2data[1] & 0xf0) != 0x70) {
148 if (read_delay < 10) read_delay++;
152 Serial.print(
"OUT:IN ");
153 for (
int i = 0; i < 9; i++) {
154 Serial.print(dword[i], HEX);
156 Serial.print(PS2data[i], HEX);
159 for (
int i = 0; i < 12; i++) {
160 Serial.print(dword2[i], HEX);
162 Serial.print(PS2data[i + 9], HEX);
168 last_buttons = buttons;
171 (uint16_t)(PS2data[4] << 8) + PS2data[3];
173 last_read = millis();
174 return ((PS2data[1] & 0xf0) == 0x70);
185 uint8_t clk, uint8_t cmd, uint8_t att, uint8_t dat,
bool pressures,
bool rumble)
188 byte temp[
sizeof(type_read)];
195 pinMode(clk, OUTPUT);
196 pinMode(att, OUTPUT);
197 pinMode(cmd, OUTPUT);
212 if (PS2data[1] != 0x41 && PS2data[1] != 0x42 && PS2data[1] != 0x73 && PS2data[1] != 0x79) {
214 Serial.println(
"Controller mode not matched or no controller found");
215 Serial.print(
"Expected 0x41, 0x42, 0x73 or 0x79, but got ");
216 Serial.println(PS2data[1], HEX);
224 for (
int y = 0;
y <= 10;
y++) {
225 sendCommandString(enter_config,
sizeof(enter_config));
236 for (
int i = 0; i < 9; i++) {
237 temp[i] = _gamepad_shiftinout(type_read[i]);
242 controller_type = temp[3];
244 sendCommandString(set_mode,
sizeof(set_mode));
246 sendCommandString(enable_rumble,
sizeof(enable_rumble));
250 sendCommandString(set_bytes_large,
sizeof(set_bytes_large));
253 sendCommandString(exit_config,
sizeof(exit_config));
258 if (PS2data[1] == 0x79)
break;
259 if (PS2data[1] == 0x73)
return 3;
262 if (PS2data[1] == 0x73)
break;
266 Serial.println(
"Controller not accepting commands");
267 Serial.print(
"mode still set at");
268 Serial.println(PS2data[1], HEX);
278void PS2X::sendCommandString(
byte string[],
byte len)
285 for (
int y = 0;
y < len;
y++) temp[
y] = _gamepad_shiftinout(
string[
y]);
290 Serial.println(
"OUT:IN Configure");
291 for (
int i = 0; i < len; i++) {
292 Serial.print(
string[i], HEX);
294 Serial.print(temp[i], HEX);
301 for (
int y = 0;
y < len;
y++) _gamepad_shiftinout(
string[
y]);
336 Serial.print(
"Controller_type: ");
337 Serial.println(controller_type, HEX);
338 if (controller_type == 0x03)
340 else if (controller_type == 0x01 && PS2data[1] == 0x42)
342 else if (controller_type == 0x01 && PS2data[1] != 0x42)
344 else if (controller_type == 0x0C)
353 sendCommandString(enter_config,
sizeof(enter_config));
354 sendCommandString(enable_rumble,
sizeof(enable_rumble));
355 sendCommandString(exit_config,
sizeof(exit_config));
362 sendCommandString(enter_config,
sizeof(enter_config));
363 sendCommandString(set_bytes_large,
sizeof(set_bytes_large));
364 sendCommandString(exit_config,
sizeof(exit_config));
369 if (PS2data[1] != 0x79)
return false;
378 sendCommandString(enter_config,
sizeof(enter_config));
379 sendCommandString(set_mode,
sizeof(set_mode));
380 if (en_Rumble) sendCommandString(enable_rumble,
sizeof(enable_rumble));
381 if (en_Pressures) sendCommandString(set_bytes_large,
sizeof(set_bytes_large));
382 sendCommandString(exit_config,
sizeof(exit_config));
387inline void PS2X::CLK_SET(
void)
389 digitalWrite(_clk_pin, HIGH);
392inline void PS2X::CLK_CLR(
void)
394 digitalWrite(_clk_pin, LOW);
397inline void PS2X::CMD_SET(
void)
399 digitalWrite(_cmd_pin, HIGH);
402inline void PS2X::CMD_CLR(
void)
404 digitalWrite(_cmd_pin, LOW);
407inline void PS2X::ATT_SET(
void)
409 digitalWrite(_att_pin, HIGH);
412inline void PS2X::ATT_CLR(
void)
414 digitalWrite(_att_pin, LOW);
417inline bool PS2X::DAT_CHK(
void)
419 return digitalRead(_dat_pin) ? true :
false;
Merge PS2X Lib into Mini R4.
byte Analog(byte)
Reads the analog value from the specified analog stick.
byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t)
Configures the gamepad.
boolean NewButtonState()
Checks for new button state.
boolean Button(uint16_t)
Checks if a button is currently pressed.
unsigned int ButtonDataByte()
Reads the button data byte.
bool enablePressures()
Enables pressure sensitivity on the controller.
void reconfig_gamepad()
Reconfigures the gamepad.
boolean ButtonPressed(unsigned int)
Checks if a button was just pressed.
boolean ButtonReleased(unsigned int)
Checks if a button was just released.
void read_gamepad()
Reads the gamepad state.
void enableRumble()
Enables rumble functionality on the controller.
byte readType()
Returns the controller type.