Suga Koubou 製 PS_PADライブラリ https://developer.mbed.org/users/okini3939/code/PS_PAD/ を改造したもの。単に__rbitが使えなかったので置き換えただけ。

Dependents:   pscontroller project_beta

Fork of PS_PAD by Suga koubou

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PS_PAD.cpp Source File

PS_PAD.cpp

00001 /*
00002  * PlayStation Controller library
00003  * Copyright (c) 2013 Hiroshi Suga
00004  */
00005 
00006 #include "PS_PAD.h"
00007 
00008 /*PS_PAD::PS_PAD (PinName mosi, PinName miso, PinName sck, PinName cs) : _spi(mosi, miso, sck), _cs(cs) {
00009     _spi->format(8, 3);
00010     _spi->frequency(250000);
00011     _cs = 1;
00012     _vib1 = 0;
00013     _vib2 = 0;
00014     _connected = false;
00015 }*/
00016 
00017 PS_PAD::PS_PAD (SPI *spi, PinName cs) : _spi(spi), _cs(cs) {
00018     _spi->format(8, 3);
00019     _spi->frequency(250000);
00020     _cs = 1;
00021     _vib1 = 0;
00022     _vib2 = 0;
00023     _connected = false;
00024 }
00025 
00026 int PS_PAD::init () {
00027     const char enter_config_mode[5]  = {0x01, 0x43, 0x00, 0x01, 0x00};
00028     const char enable_analog_mode[9] = {0x01, 0x44, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x00};
00029     const char enable_vibration[9]   = {0x01, 0x4d, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff};
00030     const char exit_config_mode[9]   = {0x01, 0x43, 0x00, 0x00, 0x5A, 0x5A, 0x5A, 0x5A, 0x5A};
00031     char buf[10];
00032 
00033     send(enter_config_mode, 5, buf);
00034     if ((buf[2] == 0xff)||(buf[1] == 0x00)) {
00035         return -1;
00036     }
00037     wait_ms(16);
00038     send(enable_analog_mode, 9, buf);
00039     wait_ms(16);
00040     send(enable_vibration, 9, buf);
00041     wait_ms(16);
00042     send(exit_config_mode, 9, buf);
00043     wait_ms(16);
00044     return 0;
00045 }
00046 
00047 int PS_PAD::poll () {
00048     const char poll_command[9] = {0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
00049     int i;
00050     char cmd[10], buf[10];
00051 
00052     memcpy(cmd, poll_command, 9);
00053     cmd[3] = _vib1;
00054     cmd[4] = _vib2;
00055     send(cmd, 9, buf);
00056     if (buf[2] != 0x5a) {
00057         return -1;
00058     }
00059 
00060     for (i = 0; i < 6; i ++) {
00061         _pad[i] = buf[3 + i];
00062     }
00063     _connected = true;
00064     return 0;
00065 }
00066 
00067 int PS_PAD::read (TYPE t) {
00068     if (!_connected) {
00069         if (t <= BUTTONS) {
00070             return 0;
00071         } else {
00072             return 0x80;
00073         }
00074     }
00075 
00076     switch (t) {
00077     case PAD_LEFT:
00078         return _pad[0] & 0x80 ? 0 : 1;
00079     case PAD_BOTTOM:
00080         return _pad[0] & 0x40 ? 0 : 1;
00081     case PAD_RIGHT:
00082         return _pad[0] & 0x20 ? 0 : 1;
00083     case PAD_TOP:
00084         return _pad[0] & 0x10 ? 0 : 1;
00085     case PAD_START:
00086         return _pad[0] & 0x08 ? 0 : 1;
00087     case ANALOG_LEFT:
00088         return _pad[0] & 0x04 ? 0 : 1;
00089     case ANALOG_RIGHT:
00090         return _pad[0] & 0x02 ? 0 : 1;
00091     case PAD_SELECT:
00092         return _pad[0] & 0x01 ? 0 : 1;
00093     case PAD_SQUARE:
00094         return _pad[1] & 0x80 ? 0 : 1;
00095     case PAD_X:
00096         return _pad[1] & 0x40 ? 0 : 1;
00097     case PAD_CIRCLE:
00098         return _pad[1] & 0x20 ? 0 : 1;
00099     case PAD_TRIANGLE:
00100         return _pad[1] & 0x10 ? 0 : 1;
00101     case PAD_R1:
00102         return _pad[1] & 0x08 ? 0 : 1;
00103     case PAD_L1:
00104         return _pad[1] & 0x04 ? 0 : 1;
00105     case PAD_R2:
00106         return _pad[1] & 0x02 ? 0 : 1;
00107     case PAD_L2:
00108         return _pad[1] & 0x01 ? 0 : 1;
00109     case BUTTONS:
00110         return ~((_pad[1] << 8) | _pad[0]) & 0xffff;
00111     case ANALOG_RX:
00112         return _pad[2] - 0x80;
00113     case ANALOG_RY:
00114         return _pad[3] - 0x80;
00115     case ANALOG_LX:
00116         return _pad[4] - 0x80;
00117     case ANALOG_LY:
00118         return _pad[5] - 0x80;
00119     }
00120     return 0;
00121 }
00122 
00123 int PS_PAD::vibration (int v1, int v2) {
00124     _vib1 = v1 ? 1 : 0;
00125     if (v2 < 0) v2 = 0;
00126     if (v2 > 0xff) v2 = 0;
00127     _vib2 = v2;
00128     poll();
00129     return 0;
00130 }
00131 
00132 int PS_PAD::send (const char *cmd, int len, char *dat) {
00133     int i;
00134 
00135     _cs = 0;
00136     wait_us(10);
00137     for (i = 0; i < len; i ++) {
00138         dat[i] = bitflip32(_spi->write(bitflip32(cmd[i] << 24)) << 24);
00139         wait_us(10);
00140     }
00141     _cs = 1;
00142     return i;
00143 }
00144 
00145 uint32_t PS_PAD::bitflip32(uint32_t x) {
00146     uint32_t r = 0;
00147     int b = 32;
00148     while (b--) {
00149         r <<= 1;
00150         r |= (x & 1);
00151         x >>= 1;
00152     }
00153     return r;
00154 }