PlayStation Controller library

Dependents:   PSController_Sample coba_stick_PS2 basehybrid_PSPAD Base_Hybrid_V3 ... more

PlayStation Controller

SONY プレイステーション2用コントローラーを mbed に接続して使えるライブラリです。

SPI 機能を使います。

サードパーティ製や、VSTONE製のコントローラー(VS-C1、VS-C3)も使用できます。

ピンアサイン

PS PAD

=========================
| 9 8 7 | 6 5 4 | 3 2 1 |
 -----------------------
PS PADVS-C3Signalmbed
13DATMISO
24CMDMOSI
3+5-7V
410GNDGND
59+3V+3.3V
65SELCS
76CLKSCK
8
9ACK
Committer:
okini3939
Date:
Wed Dec 11 04:42:44 2013 +0000
Revision:
0:6eeefcf5a37a
Child:
1:840370e1dcce
1st build;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:6eeefcf5a37a 1 /*
okini3939 0:6eeefcf5a37a 2 * PlayStation Controller library
okini3939 0:6eeefcf5a37a 3 * Copyright (c) 2013 Hiroshi Suga
okini3939 0:6eeefcf5a37a 4 */
okini3939 0:6eeefcf5a37a 5
okini3939 0:6eeefcf5a37a 6 #include "PS_PAD.h"
okini3939 0:6eeefcf5a37a 7
okini3939 0:6eeefcf5a37a 8 PS_PAD::PS_PAD (PinName mosi, PinName miso, PinName sck, PinName cs) : _spi(mosi, miso, sck), _cs(cs) {
okini3939 0:6eeefcf5a37a 9 _spi.format(8, 3);
okini3939 0:6eeefcf5a37a 10 _spi.frequency(250000);
okini3939 0:6eeefcf5a37a 11 _cs = 1;
okini3939 0:6eeefcf5a37a 12 _vib1 = 0;
okini3939 0:6eeefcf5a37a 13 _vib2 = 0;
okini3939 0:6eeefcf5a37a 14 }
okini3939 0:6eeefcf5a37a 15
okini3939 0:6eeefcf5a37a 16 PS_PAD::PS_PAD (SPI &spi, PinName cs) : _spi(spi), _cs(cs) {
okini3939 0:6eeefcf5a37a 17 _spi.format(8, 3);
okini3939 0:6eeefcf5a37a 18 _spi.frequency(250000);
okini3939 0:6eeefcf5a37a 19 _cs = 1;
okini3939 0:6eeefcf5a37a 20 _vib1 = 0;
okini3939 0:6eeefcf5a37a 21 _vib2 = 0;
okini3939 0:6eeefcf5a37a 22 }
okini3939 0:6eeefcf5a37a 23
okini3939 0:6eeefcf5a37a 24 int PS_PAD::init () {
okini3939 0:6eeefcf5a37a 25 const char enter_config_mode[5] = {0x01, 0x43, 0x00, 0x01, 0x00};
okini3939 0:6eeefcf5a37a 26 const char enable_analog_mode[9] = {0x01, 0x44, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00};
okini3939 0:6eeefcf5a37a 27 const char enable_vibration[9] = {0x01, 0x4d, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff};
okini3939 0:6eeefcf5a37a 28 const char exit_config_mode[9] = {0x01, 0x43, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};
okini3939 0:6eeefcf5a37a 29 char buf[10];
okini3939 0:6eeefcf5a37a 30
okini3939 0:6eeefcf5a37a 31 send(enter_config_mode, 5, buf);
okini3939 0:6eeefcf5a37a 32 if (buf[2] == 0xff) {
okini3939 0:6eeefcf5a37a 33 return -1;
okini3939 0:6eeefcf5a37a 34 }
okini3939 0:6eeefcf5a37a 35 wait_ms(16);
okini3939 0:6eeefcf5a37a 36 send(enable_analog_mode, 9, buf);
okini3939 0:6eeefcf5a37a 37 wait_ms(16);
okini3939 0:6eeefcf5a37a 38 send(enable_vibration, 9, buf);
okini3939 0:6eeefcf5a37a 39 wait_ms(16);
okini3939 0:6eeefcf5a37a 40 send(exit_config_mode, 9, buf);
okini3939 0:6eeefcf5a37a 41 wait_ms(16);
okini3939 0:6eeefcf5a37a 42 return 0;
okini3939 0:6eeefcf5a37a 43 }
okini3939 0:6eeefcf5a37a 44
okini3939 0:6eeefcf5a37a 45 int PS_PAD::poll () {
okini3939 0:6eeefcf5a37a 46 const char poll_command[9] = {0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
okini3939 0:6eeefcf5a37a 47 int i;
okini3939 0:6eeefcf5a37a 48 char cmd[10], buf[10];
okini3939 0:6eeefcf5a37a 49
okini3939 0:6eeefcf5a37a 50 memcpy(cmd, poll_command, 9);
okini3939 0:6eeefcf5a37a 51 cmd[3] = _vib1;
okini3939 0:6eeefcf5a37a 52 cmd[4] = _vib2;
okini3939 0:6eeefcf5a37a 53 send(cmd, 9, buf);
okini3939 0:6eeefcf5a37a 54 if (buf[2] == 0xff) {
okini3939 0:6eeefcf5a37a 55 return -1;
okini3939 0:6eeefcf5a37a 56 }
okini3939 0:6eeefcf5a37a 57
okini3939 0:6eeefcf5a37a 58 for (i = 0; i < 6; i ++) {
okini3939 0:6eeefcf5a37a 59 _pad[i] = buf[3 + i];
okini3939 0:6eeefcf5a37a 60 }
okini3939 0:6eeefcf5a37a 61 return 0;
okini3939 0:6eeefcf5a37a 62 }
okini3939 0:6eeefcf5a37a 63
okini3939 0:6eeefcf5a37a 64 int PS_PAD::read (TYPE t) {
okini3939 0:6eeefcf5a37a 65 switch (t) {
okini3939 0:6eeefcf5a37a 66 case PAD_LEFT:
okini3939 0:6eeefcf5a37a 67 return _pad[0] & 0x80 ? 0 : 1;
okini3939 0:6eeefcf5a37a 68 case PAD_BOTTOM:
okini3939 0:6eeefcf5a37a 69 return _pad[0] & 0x40 ? 0 : 1;
okini3939 0:6eeefcf5a37a 70 case PAD_RIGHT:
okini3939 0:6eeefcf5a37a 71 return _pad[0] & 0x20 ? 0 : 1;
okini3939 0:6eeefcf5a37a 72 case PAD_TOP:
okini3939 0:6eeefcf5a37a 73 return _pad[0] & 0x10 ? 0 : 1;
okini3939 0:6eeefcf5a37a 74 case PAD_START:
okini3939 0:6eeefcf5a37a 75 return _pad[0] & 0x08 ? 0 : 1;
okini3939 0:6eeefcf5a37a 76 case ANALOG_LEFT:
okini3939 0:6eeefcf5a37a 77 return _pad[0] & 0x04 ? 0 : 1;
okini3939 0:6eeefcf5a37a 78 case ANALOG_RIGHT:
okini3939 0:6eeefcf5a37a 79 return _pad[0] & 0x02 ? 0 : 1;
okini3939 0:6eeefcf5a37a 80 case PAD_SELECT:
okini3939 0:6eeefcf5a37a 81 return _pad[0] & 0x01 ? 0 : 1;
okini3939 0:6eeefcf5a37a 82 case PAD_SQUARE:
okini3939 0:6eeefcf5a37a 83 return _pad[1] & 0x80 ? 0 : 1;
okini3939 0:6eeefcf5a37a 84 case PAD_X:
okini3939 0:6eeefcf5a37a 85 return _pad[1] & 0x40 ? 0 : 1;
okini3939 0:6eeefcf5a37a 86 case PAD_CIRCLE:
okini3939 0:6eeefcf5a37a 87 return _pad[1] & 0x20 ? 0 : 1;
okini3939 0:6eeefcf5a37a 88 case PAD_TRIANGLE:
okini3939 0:6eeefcf5a37a 89 return _pad[1] & 0x10 ? 0 : 1;
okini3939 0:6eeefcf5a37a 90 case PAD_R1:
okini3939 0:6eeefcf5a37a 91 return _pad[1] & 0x08 ? 0 : 1;
okini3939 0:6eeefcf5a37a 92 case PAD_L1:
okini3939 0:6eeefcf5a37a 93 return _pad[1] & 0x04 ? 0 : 1;
okini3939 0:6eeefcf5a37a 94 case PAD_R2:
okini3939 0:6eeefcf5a37a 95 return _pad[1] & 0x02 ? 0 : 1;
okini3939 0:6eeefcf5a37a 96 case PAD_L2:
okini3939 0:6eeefcf5a37a 97 return _pad[1] & 0x01 ? 0 : 1;
okini3939 0:6eeefcf5a37a 98 case ANALOG_RX:
okini3939 0:6eeefcf5a37a 99 if (_pad[2] < 0x80) {
okini3939 0:6eeefcf5a37a 100 return (_pad[2] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 101 } else {
okini3939 0:6eeefcf5a37a 102 return (_pad[2] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 103 }
okini3939 0:6eeefcf5a37a 104 case ANALOG_RY:
okini3939 0:6eeefcf5a37a 105 if (_pad[3] < 0x80) {
okini3939 0:6eeefcf5a37a 106 return (_pad[3] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 107 } else {
okini3939 0:6eeefcf5a37a 108 return (_pad[3] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 109 }
okini3939 0:6eeefcf5a37a 110 case ANALOG_LX:
okini3939 0:6eeefcf5a37a 111 if (_pad[4] < 0x80) {
okini3939 0:6eeefcf5a37a 112 return (_pad[4] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 113 } else {
okini3939 0:6eeefcf5a37a 114 return (_pad[4] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 115 }
okini3939 0:6eeefcf5a37a 116 case ANALOG_LY:
okini3939 0:6eeefcf5a37a 117 if (_pad[5] < 0x80) {
okini3939 0:6eeefcf5a37a 118 return (_pad[5] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 119 } else {
okini3939 0:6eeefcf5a37a 120 return (_pad[5] - 0x7f) * 100 / 0x7f;
okini3939 0:6eeefcf5a37a 121 }
okini3939 0:6eeefcf5a37a 122 }
okini3939 0:6eeefcf5a37a 123 return 0;
okini3939 0:6eeefcf5a37a 124 }
okini3939 0:6eeefcf5a37a 125
okini3939 0:6eeefcf5a37a 126 int PS_PAD::vibration (int v1, int v2) {
okini3939 0:6eeefcf5a37a 127 _vib1 = v1 ? 1 : 0;
okini3939 0:6eeefcf5a37a 128 if (v2 < 0) v2 = 0;
okini3939 0:6eeefcf5a37a 129 if (v2 > 0xff) v2 = 0;
okini3939 0:6eeefcf5a37a 130 _vib2 = v2;
okini3939 0:6eeefcf5a37a 131 poll();
okini3939 0:6eeefcf5a37a 132 return 0;
okini3939 0:6eeefcf5a37a 133 }
okini3939 0:6eeefcf5a37a 134
okini3939 0:6eeefcf5a37a 135 int PS_PAD::send (const char *cmd, int len, char *dat) {
okini3939 0:6eeefcf5a37a 136 int i;
okini3939 0:6eeefcf5a37a 137
okini3939 0:6eeefcf5a37a 138 _cs = 0;
okini3939 0:6eeefcf5a37a 139 wait_us(10);
okini3939 0:6eeefcf5a37a 140 for (i = 0; i < len; i ++) {
okini3939 0:6eeefcf5a37a 141 dat[i] = __rbit(_spi.write(__rbit(cmd[i] << 24)) << 24);
okini3939 0:6eeefcf5a37a 142 wait_us(10);
okini3939 0:6eeefcf5a37a 143 }
okini3939 0:6eeefcf5a37a 144 _cs = 1;
okini3939 0:6eeefcf5a37a 145 return i;
okini3939 0:6eeefcf5a37a 146 }
okini3939 0:6eeefcf5a37a 147