PS2X_lib.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /******************************************************************
  2. * Super amazing PS2 controller Arduino Library v1.8
  3. * details and example sketch:
  4. * http://www.billporter.info/?p=240
  5. *
  6. * Original code by Shutter on Arduino Forums
  7. *
  8. * Revamped, made into lib by and supporting continued development:
  9. * Bill Porter
  10. * www.billporter.info
  11. *
  12. * Contributers:
  13. * Eric Wetzel (thewetzel@gmail.com)
  14. * Kurt Eckhardt
  15. *
  16. * Lib version history
  17. * 0.1 made into library, added analog stick support.
  18. * 0.2 fixed config_gamepad miss-spelling
  19. * added new functions:
  20. * NewButtonState();
  21. * NewButtonState(unsigned int);
  22. * ButtonPressed(unsigned int);
  23. * ButtonReleased(unsigned int);
  24. * removed 'PS' from beginning of ever function
  25. * 1.0 found and fixed bug that wasn't configuring controller
  26. * added ability to define pins
  27. * added time checking to reconfigure controller if not polled enough
  28. * Analog sticks and pressures all through 'ps2x.Analog()' function
  29. * added:
  30. * enableRumble();
  31. * enablePressures();
  32. * 1.1
  33. * added some debug stuff for end user. Reports if no controller found
  34. * added auto-increasing sentence delay to see if it helps compatibility.
  35. * 1.2
  36. * found bad math by Shutter for original clock. Was running at 50kHz, not the required 500kHz.
  37. * fixed some of the debug reporting.
  38. * 1.3
  39. * Changed clock back to 50kHz. CuriousInventor says it's suppose to be 500kHz, but doesn't seem to work for everybody.
  40. * 1.4
  41. * Removed redundant functions.
  42. * Fixed mode check to include two other possible modes the controller could be in.
  43. * Added debug code enabled by compiler directives. See below to enable debug mode.
  44. * Added button definitions for shapes as well as colors.
  45. * 1.41
  46. * Some simple bug fixes
  47. * Added Keywords.txt file
  48. * 1.5
  49. * Added proper Guitar Hero compatibility
  50. * Fixed issue with DEBUG mode, had to send serial at once instead of in bits
  51. * 1.6
  52. * Changed config_gamepad() call to include rumble and pressures options
  53. * This was to fix controllers that will only go into config mode once
  54. * Old methods should still work for backwards compatibility
  55. * 1.7
  56. * Integrated Kurt's fixes for the interrupts messing with servo signals
  57. * Reorganized directory so examples show up in Arduino IDE menu
  58. * 1.8
  59. * Added Arduino 1.0 compatibility.
  60. * 1.9
  61. * Kurt - Added detection and recovery from dropping from analog mode, plus
  62. * integrated Chipkit (pic32mx...) support
  63. *
  64. *
  65. *
  66. *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.
  67. This program is distributed in the hope that it will be useful,
  68. but WITHOUT ANY WARRANTY; without even the implied warranty of
  69. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  70. GNU General Public License for more details.
  71. <http://www.gnu.org/licenses/>
  72. *
  73. ******************************************************************/
  74. // $$$$$$$$$$$$ DEBUG ENABLE SECTION $$$$$$$$$$$$$$$$
  75. // to debug ps2 controller, uncomment these two lines to print out debug to uart
  76. //#define PS2X_DEBUG
  77. //#define PS2X_COM_DEBUG
  78. #ifndef PS2X_lib_h
  79. #define PS2X_lib_h
  80. #if ARDUINO > 22
  81. #include "Arduino.h"
  82. #else
  83. #include "WProgram.h"
  84. #endif
  85. #include <math.h>
  86. #include <stdio.h>
  87. #include <stdint.h>
  88. #ifdef __AVR__
  89. // AVR
  90. #include <avr/io.h>
  91. #define CTRL_CLK 4
  92. #define CTRL_BYTE_DELAY 3
  93. #else
  94. #if defined(ESP8266) || defined(ESP32)
  95. #define CTRL_CLK 5
  96. #define CTRL_CLK_HIGH 5
  97. #define CTRL_BYTE_DELAY 18
  98. #else
  99. // Pic32...
  100. #include <pins_arduino.h>
  101. #define CTRL_CLK 5
  102. #define CTRL_CLK_HIGH 5
  103. #define CTRL_BYTE_DELAY 4
  104. #endif
  105. #endif
  106. //These are our button constants
  107. #define PSB_SELECT 0x0001
  108. #define PSB_L3 0x0002
  109. #define PSB_R3 0x0004
  110. #define PSB_START 0x0008
  111. #define PSB_PAD_UP 0x0010
  112. #define PSB_PAD_RIGHT 0x0020
  113. #define PSB_PAD_DOWN 0x0040
  114. #define PSB_PAD_LEFT 0x0080
  115. #define PSB_L2 0x0100
  116. #define PSB_R2 0x0200
  117. #define PSB_L1 0x0400
  118. #define PSB_R1 0x0800
  119. #define PSB_GREEN 0x1000
  120. #define PSB_RED 0x2000
  121. #define PSB_BLUE 0x4000
  122. #define PSB_PINK 0x8000
  123. #define PSB_TRIANGLE 0x1000
  124. #define PSB_CIRCLE 0x2000
  125. #define PSB_CROSS 0x4000
  126. #define PSB_SQUARE 0x8000
  127. //Guitar button constants
  128. #define UP_STRUM 0x0010
  129. #define DOWN_STRUM 0x0040
  130. #define LEFT_STRUM 0x0080
  131. #define RIGHT_STRUM 0x0020
  132. #define STAR_POWER 0x0100
  133. #define GREEN_FRET 0x0200
  134. #define YELLOW_FRET 0x1000
  135. #define RED_FRET 0x2000
  136. #define BLUE_FRET 0x4000
  137. #define ORANGE_FRET 0x8000
  138. #define WHAMMY_BAR 8
  139. //These are stick values
  140. #define PSS_RX 5
  141. #define PSS_RY 6
  142. #define PSS_LX 7
  143. #define PSS_LY 8
  144. //These are analog buttons
  145. #define PSAB_PAD_RIGHT 9
  146. #define PSAB_PAD_UP 11
  147. #define PSAB_PAD_DOWN 12
  148. #define PSAB_PAD_LEFT 10
  149. #define PSAB_L2 19
  150. #define PSAB_R2 20
  151. #define PSAB_L1 17
  152. #define PSAB_R1 18
  153. #define PSAB_GREEN 13
  154. #define PSAB_RED 14
  155. #define PSAB_BLUE 15
  156. #define PSAB_PINK 16
  157. #define PSAB_TRIANGLE 13
  158. #define PSAB_CIRCLE 14
  159. #define PSAB_CROSS 15
  160. #define PSAB_SQUARE 16
  161. #define SET(x,y) (x|=(1<<y))
  162. #define CLR(x,y) (x&=(~(1<<y)))
  163. #define CHK(x,y) (x & (1<<y))
  164. #define TOG(x,y) (x^=(1<<y))
  165. class PS2X {
  166. public:
  167. boolean Button(uint16_t); //will be TRUE if button is being pressed
  168. unsigned int ButtonDataByte();
  169. boolean NewButtonState();
  170. boolean NewButtonState(unsigned int); //will be TRUE if button was JUST pressed OR released
  171. boolean ButtonPressed(unsigned int); //will be TRUE if button was JUST pressed
  172. boolean ButtonReleased(unsigned int); //will be TRUE if button was JUST released
  173. void read_gamepad();
  174. boolean read_gamepad(boolean, byte);
  175. byte readType();
  176. byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t);
  177. byte config_gamepad(uint8_t, uint8_t, uint8_t, uint8_t, bool, bool);
  178. void enableRumble();
  179. bool enablePressures();
  180. byte Analog(byte);
  181. void reconfig_gamepad();
  182. private:
  183. inline void CLK_SET(void);
  184. inline void CLK_CLR(void);
  185. inline void CMD_SET(void);
  186. inline void CMD_CLR(void);
  187. inline void ATT_SET(void);
  188. inline void ATT_CLR(void);
  189. inline bool DAT_CHK(void);
  190. unsigned char _gamepad_shiftinout (char);
  191. unsigned char PS2data[21];
  192. void sendCommandString(byte*, byte);
  193. unsigned char i;
  194. unsigned int last_buttons;
  195. unsigned int buttons;
  196. #ifdef __AVR__
  197. uint8_t maskToBitNum(uint8_t);
  198. uint8_t _clk_mask;
  199. volatile uint8_t *_clk_oreg;
  200. uint8_t _cmd_mask;
  201. volatile uint8_t *_cmd_oreg;
  202. uint8_t _att_mask;
  203. volatile uint8_t *_att_oreg;
  204. uint8_t _dat_mask;
  205. volatile uint8_t *_dat_ireg;
  206. #else
  207. #if defined(ESP8266) || defined(ESP32)
  208. int _clk_pin;
  209. int _cmd_pin;
  210. int _att_pin;
  211. int _dat_pin;
  212. #else
  213. uint8_t maskToBitNum(uint8_t);
  214. uint16_t _clk_mask;
  215. volatile uint32_t *_clk_lport_set;
  216. volatile uint32_t *_clk_lport_clr;
  217. uint16_t _cmd_mask;
  218. volatile uint32_t *_cmd_lport_set;
  219. volatile uint32_t *_cmd_lport_clr;
  220. uint16_t _att_mask;
  221. volatile uint32_t *_att_lport_set;
  222. volatile uint32_t *_att_lport_clr;
  223. uint16_t _dat_mask;
  224. volatile uint32_t *_dat_lport;
  225. #endif
  226. #endif
  227. unsigned long last_read;
  228. byte read_delay;
  229. byte controller_type;
  230. boolean en_Rumble;
  231. boolean en_Pressures;
  232. };
  233. #endif