MatrixMiniR4 1.1.4
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MiniR4PS2X_lib.h
Go to the documentation of this file.
1
6/******************************************************************
7* Super amazing PS2 controller Arduino Library v1.8
8* details and example sketch:
9* http://www.billporter.info/?p=240
10*
11* Original code by Shutter on Arduino Forums
12*
13* Revamped, made into lib by and supporting continued development:
14* Bill Porter
15* www.billporter.info
16*
17* Contributers:
18* Eric Wetzel (thewetzel@gmail.com)
19* Kurt Eckhardt
20*
21* Lib version history
22* 0.1 made into library, added analog stick support.
23* 0.2 fixed config_gamepad miss-spelling
24* added new functions:
25* NewButtonState();
26* NewButtonState(unsigned int);
27* ButtonPressed(unsigned int);
28* ButtonReleased(unsigned int);
29* removed 'PS' from beginning of ever function
30* 1.0 found and fixed bug that wasn't configuring controller
31* added ability to define pins
32* added time checking to reconfigure controller if not polled enough
33* Analog sticks and pressures all through 'ps2x.Analog()' function
34* added:
35* enableRumble();
36* enablePressures();
37* 1.1
38* added some debug stuff for end user. Reports if no controller found
39* added auto-increasing sentence delay to see if it helps compatibility.
40* 1.2
41* found bad math by Shutter for original clock. Was running at 50kHz, not
42the required 500kHz.
43* fixed some of the debug reporting.
44* 1.3
45* Changed clock back to 50kHz. CuriousInventor says it's suppose to be
46500kHz, but doesn't seem to work for everybody. * 1.4 * Removed
47redundant functions.
48* Fixed mode check to include two other possible modes the
49controller could be in.
50* Added debug code enabled by compiler directives. See below to enable
51debug mode. * Added button definitions for shapes as well as colors.
52* 1.41
53* Some simple bug fixes
54* Added Keywords.txt file
55* 1.5
56* Added proper Guitar Hero compatibility
57* Fixed issue with DEBUG mode, had to send serial at once instead
58of in bits * 1.6
59* Changed config_gamepad() call to include rumble and pressures
60options * This was to fix controllers that will only go
61into config mode once
62* Old methods should still work for backwards
63compatibility
64* 1.7
65* Integrated Kurt's fixes for the interrupts messing with servo
66signals * Reorganized directory so examples show up in Arduino IDE
67menu
68* 1.8
69* Added Arduino 1.0 compatibility.
70* 1.9
71* Kurt - Added detection and recovery from dropping from analog mode, plus
72* integrated Chipkit (pic32mx...) support
73*
74*
75*
76*This program is free software: you can redistribute it and/or modify it under
77the terms of the GNU General Public License as published by the Free Software
78Foundation, either version 3 of the License, or(at your option) any later
79version. This program is distributed in the hope that it will be useful, but
80WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
81FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
82details. <http://www.gnu.org/licenses/>
83*
84******************************************************************/
85
86// $$$$$$$$$$$$ DEBUG ENABLE SECTION $$$$$$$$$$$$$$$$
87// to debug ps2 controller, uncomment these two lines to print out debug to uart
88// #define PS2X_DEBUG
89// #define PS2X_COM_DEBUG
90
91#ifndef PS2X_lib_h
92#define PS2X_lib_h
93
94#if ARDUINO > 22
95# include "Arduino.h"
96#else
97# include "WProgram.h"
98#endif
99
100#include <math.h>
101#include <stdint.h>
102#include <stdio.h>
103
104#define CTRL_CLK 5
105#define CTRL_CLK_HIGH 5
106#define CTRL_BYTE_DELAY 18
107
108// These are our button constants
109#define PSB_SELECT 0x0001
110#define PSB_L3 0x0002
111#define PSB_R3 0x0004
112#define PSB_START 0x0008
113#define PSB_PAD_UP 0x0010
114#define PSB_PAD_RIGHT 0x0020
115#define PSB_PAD_DOWN 0x0040
116#define PSB_PAD_LEFT 0x0080
117#define PSB_L2 0x0100
118#define PSB_R2 0x0200
119#define PSB_L1 0x0400
120#define PSB_R1 0x0800
121#define PSB_GREEN 0x1000
122#define PSB_RED 0x2000
123#define PSB_BLUE 0x4000
124#define PSB_PINK 0x8000
125#define PSB_TRIANGLE 0x1000
126#define PSB_CIRCLE 0x2000
127#define PSB_CROSS 0x4000
128#define PSB_SQUARE 0x8000
129
130// Guitar button constants
131#define UP_STRUM 0x0010
132#define DOWN_STRUM 0x0040
133#define LEFT_STRUM 0x0080
134#define RIGHT_STRUM 0x0020
135#define STAR_POWER 0x0100
136#define GREEN_FRET 0x0200
137#define YELLOW_FRET 0x1000
138#define RED_FRET 0x2000
139#define BLUE_FRET 0x4000
140#define ORANGE_FRET 0x8000
141#define WHAMMY_BAR 8
142
143// These are stick values
144#define PSS_RX 5
145#define PSS_RY 6
146#define PSS_LX 7
147#define PSS_LY 8
148
149// These are analog buttons
150#define PSAB_PAD_RIGHT 9
151#define PSAB_PAD_UP 11
152#define PSAB_PAD_DOWN 12
153#define PSAB_PAD_LEFT 10
154#define PSAB_L2 19
155#define PSAB_R2 20
156#define PSAB_L1 17
157#define PSAB_R1 18
158#define PSAB_GREEN 13
159#define PSAB_RED 14
160#define PSAB_BLUE 15
161#define PSAB_PINK 16
162#define PSAB_TRIANGLE 13
163#define PSAB_CIRCLE 14
164#define PSAB_CROSS 15
165#define PSAB_SQUARE 16
166
167#define SET(x, y) (x |= (1 << y))
168#define CLR(x, y) (x &= (~(1 << y)))
169#define CHK(x, y) (x & (1 << y))
170#define TOG(x, y) (x ^= (1 << y))
171
180class PS2X
181{
182public:
189 boolean Button(uint16_t); // will be TRUE if button is being pressed
190
196 unsigned int ButtonDataByte();
197
203 boolean NewButtonState();
204
211 boolean NewButtonState(unsigned int); // will be TRUE if button was JUST pressed OR released
212
219 boolean ButtonPressed(unsigned int); // will be TRUE if button was JUST
220 // pressed
221
228 boolean ButtonReleased(unsigned int); // will be TRUE if button was JUST released
229
233 void read_gamepad();
234
242 boolean read_gamepad(boolean, byte);
243
249 byte readType();
250
260 byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t);
261
273 byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);
274
278 void enableRumble();
279
285 bool enablePressures();
286
293 byte Analog(byte);
294
298 void reconfig_gamepad();
299
300private:
301 inline void CLK_SET(void);
302 inline void CLK_CLR(void);
303 inline void CMD_SET(void);
304 inline void CMD_CLR(void);
305 inline void ATT_SET(void);
306 inline void ATT_CLR(void);
307 inline bool DAT_CHK(void);
308
309 unsigned char _gamepad_shiftinout(char);
310 unsigned char PS2data[21];
311 void sendCommandString(byte string[], byte len);
312 unsigned char i;
313 unsigned int last_buttons;
314 unsigned int buttons;
315
316 int _clk_pin;
317 int _cmd_pin;
318 int _att_pin;
319 int _dat_pin;
320
321 unsigned long last_read;
322 byte read_delay;
323 byte controller_type;
324 boolean en_Rumble;
325 boolean en_Pressures;
326};
327
328#endif
Class to interface with the MJ2 or PS2 controller.
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.