Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Controller.cpp@2:fd0c21600586, 2021-12-23 (annotated)
- Committer:
- kikuchi8810
- Date:
- Thu Dec 23 08:56:26 2021 +0000
- Revision:
- 2:fd0c21600586
- Parent:
- 0:a33375289d79
modified
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kikuchi8810 | 0:a33375289d79 | 1 | // DS4のみ通信が確認できているバージョンで,プロジェクトに公開しているクラス |
| kikuchi8810 | 0:a33375289d79 | 2 | #include "Controller.h" |
| kikuchi8810 | 0:a33375289d79 | 3 | |
| kikuchi8810 | 0:a33375289d79 | 4 | Controller::Controller(PinName tx, PinName rx, int baudrate) : serial(tx, rx, baudrate) |
| kikuchi8810 | 0:a33375289d79 | 5 | { |
| kikuchi8810 | 0:a33375289d79 | 6 | conData.ButtonState = 0; |
| kikuchi8810 | 0:a33375289d79 | 7 | conData.RJoyX = 127, conData.RJoyY = 127, conData.LJoyX = 127, conData.LJoyY = 127; |
| kikuchi8810 | 0:a33375289d79 | 8 | |
| kikuchi8810 | 0:a33375289d79 | 9 | lastButtonState = 0; |
| kikuchi8810 | 0:a33375289d79 | 10 | |
| kikuchi8810 | 0:a33375289d79 | 11 | comCheck = false; |
| kikuchi8810 | 0:a33375289d79 | 12 | conAvailable = false; |
| kikuchi8810 | 0:a33375289d79 | 13 | time_out_ms = -1; |
| kikuchi8810 | 0:a33375289d79 | 14 | } |
| kikuchi8810 | 0:a33375289d79 | 15 | |
| kikuchi8810 | 0:a33375289d79 | 16 | void Controller::init(int _time_out_ms, int _int_time_ms) |
| kikuchi8810 | 0:a33375289d79 | 17 | { |
| kikuchi8810 | 0:a33375289d79 | 18 | time_out_ms = _time_out_ms; |
| kikuchi8810 | 0:a33375289d79 | 19 | int_time_ms = _int_time_ms; |
| kikuchi8810 | 0:a33375289d79 | 20 | //serial.attach(this, &Controller::update, Serial::RxIrq); |
| kikuchi8810 | 0:a33375289d79 | 21 | //timer.start(); |
| kikuchi8810 | 0:a33375289d79 | 22 | } |
| kikuchi8810 | 0:a33375289d79 | 23 | |
| kikuchi8810 | 0:a33375289d79 | 24 | bool Controller::update() |
| kikuchi8810 | 0:a33375289d79 | 25 | { |
| kikuchi8810 | 0:a33375289d79 | 26 | //receptionTime = timer.read(); |
| kikuchi8810 | 0:a33375289d79 | 27 | static int count_ms = time_out_ms, pre_count_ms = 0; //受信時刻と前回の受信時刻 |
| kikuchi8810 | 0:a33375289d79 | 28 | count_ms += int_time_ms; |
| kikuchi8810 | 0:a33375289d79 | 29 | |
| kikuchi8810 | 0:a33375289d79 | 30 | char receive_data[10]; |
| kikuchi8810 | 0:a33375289d79 | 31 | unsigned int loop_count = 0, checksum = 0x00; |
| kikuchi8810 | 0:a33375289d79 | 32 | comCheck = false; |
| kikuchi8810 | 0:a33375289d79 | 33 | |
| kikuchi8810 | 0:a33375289d79 | 34 | #if CON_TYPE == CON_ADACHI // 安達君開発のコントローラを使う場合の処理(どのコントローラを使うかはdefine.hで設定) |
| kikuchi8810 | 0:a33375289d79 | 35 | while (loop_count < 10 && serial.readable()) |
| kikuchi8810 | 0:a33375289d79 | 36 | { |
| kikuchi8810 | 0:a33375289d79 | 37 | if (serial_recieve() == '\n') |
| kikuchi8810 | 0:a33375289d79 | 38 | { |
| kikuchi8810 | 0:a33375289d79 | 39 | for (int i = 0; i < 8; i++) |
| kikuchi8810 | 0:a33375289d79 | 40 | receive_data[i] = serial_recieve(); |
| kikuchi8810 | 0:a33375289d79 | 41 | for (int i = 0; i < 8; i++) |
| kikuchi8810 | 0:a33375289d79 | 42 | receive_data[i] -= 0x20; |
| kikuchi8810 | 0:a33375289d79 | 43 | for (int i = 0; i < 7; i++) |
| kikuchi8810 | 0:a33375289d79 | 44 | checksum ^= receive_data[i]; |
| kikuchi8810 | 0:a33375289d79 | 45 | |
| kikuchi8810 | 0:a33375289d79 | 46 | if (receive_data[7] == checksum & 0xFF) |
| kikuchi8810 | 0:a33375289d79 | 47 | { |
| kikuchi8810 | 0:a33375289d79 | 48 | comCheck = true; |
| kikuchi8810 | 0:a33375289d79 | 49 | |
| kikuchi8810 | 0:a33375289d79 | 50 | //pre_conData.ButtonState = conData.ButtonState; |
| kikuchi8810 | 0:a33375289d79 | 51 | lastButtonState = ((receive_data[0] & 0x3F) << 2) | ((receive_data[1] & 0x30) >> 4); |
| kikuchi8810 | 0:a33375289d79 | 52 | conData.ButtonState |= lastButtonState; |
| kikuchi8810 | 0:a33375289d79 | 53 | |
| kikuchi8810 | 0:a33375289d79 | 54 | conData.RJoyX = ((receive_data[1] & 0x0F) << 4) | ((receive_data[2] & 0x3C) >> 2); |
| kikuchi8810 | 0:a33375289d79 | 55 | conData.RJoyY = ((receive_data[2] & 0x03) << 6) | (receive_data[3] & 0x3F); |
| kikuchi8810 | 0:a33375289d79 | 56 | conData.LJoyX = ((receive_data[4] & 0x3F) << 2) | ((receive_data[5] & 0x30) >> 4); |
| kikuchi8810 | 0:a33375289d79 | 57 | conData.LJoyY = ((receive_data[5] & 0x0F) << 4) | (receive_data[6] & 0x0F); |
| kikuchi8810 | 0:a33375289d79 | 58 | |
| kikuchi8810 | 0:a33375289d79 | 59 | break; |
| kikuchi8810 | 0:a33375289d79 | 60 | } |
| kikuchi8810 | 0:a33375289d79 | 61 | |
| kikuchi8810 | 0:a33375289d79 | 62 | pre_count_ms = count_ms; //受信時間の更新 |
| kikuchi8810 | 0:a33375289d79 | 63 | } |
| kikuchi8810 | 0:a33375289d79 | 64 | loop_count++; |
| kikuchi8810 | 0:a33375289d79 | 65 | } |
| kikuchi8810 | 0:a33375289d79 | 66 | #elif CON_TYPE == CON_ELECOM // ELECOMのコントローラを使う場合の処理(どのコントローラを使うかはdefine.hで設定) |
| kikuchi8810 | 0:a33375289d79 | 67 | // コントローラデータを取得する部分 |
| kikuchi8810 | 0:a33375289d79 | 68 | static int recv_num = 0; |
| kikuchi8810 | 0:a33375289d79 | 69 | char c; |
| kikuchi8810 | 0:a33375289d79 | 70 | while (serial.readable()) |
| kikuchi8810 | 0:a33375289d79 | 71 | { |
| kikuchi8810 | 0:a33375289d79 | 72 | c = serial.getc(); |
| kikuchi8810 | 0:a33375289d79 | 73 | if (c == '\n') |
| kikuchi8810 | 0:a33375289d79 | 74 | { |
| kikuchi8810 | 0:a33375289d79 | 75 | if (recv_num == 10) |
| kikuchi8810 | 0:a33375289d79 | 76 | { // チェックサムは無く,9個受信したら値を格納 |
| kikuchi8810 | 0:a33375289d79 | 77 | for (int i = 0; i < 9; i++) |
| kikuchi8810 | 0:a33375289d79 | 78 | checksum += (unsigned int)(receive_data[i] - 0x20); // チェックサムの計算 |
| kikuchi8810 | 0:a33375289d79 | 79 | if ((checksum & 0x3F) == (receive_data[9] - 0x20)) |
| kikuchi8810 | 0:a33375289d79 | 80 | { // チェックサムの計算が合っていた場合のみ値を格納 |
| kikuchi8810 | 0:a33375289d79 | 81 | comCheck = true; |
| kikuchi8810 | 0:a33375289d79 | 82 | |
| kikuchi8810 | 0:a33375289d79 | 83 | //conData.ButtonState = 0; |
| kikuchi8810 | 0:a33375289d79 | 84 | conData.LJoyX = 0, conData.LJoyY = 0, conData.RJoyX = 0, conData.RJoyY = 0; |
| kikuchi8810 | 0:a33375289d79 | 85 | lastButtonState = (unsigned int)(receive_data[0] - 0x20); |
| kikuchi8810 | 0:a33375289d79 | 86 | lastButtonState |= (unsigned int)(receive_data[1] - 0x20) << 6; |
| kikuchi8810 | 0:a33375289d79 | 87 | lastButtonState |= (unsigned int)(receive_data[2] - 0x20) << 12; |
| kikuchi8810 | 0:a33375289d79 | 88 | |
| kikuchi8810 | 0:a33375289d79 | 89 | conData.LJoyX |= (unsigned int)(receive_data[3] - 0x20); |
| kikuchi8810 | 0:a33375289d79 | 90 | conData.LJoyX |= (unsigned int)((receive_data[4] - 0x20) & 0x03) << 6; |
| kikuchi8810 | 0:a33375289d79 | 91 | conData.LJoyX = abs(conData.LJoyX - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 92 | |
| kikuchi8810 | 0:a33375289d79 | 93 | conData.LJoyY |= (unsigned int)((receive_data[4] - 0x20) & 0x3C) >> 2; |
| kikuchi8810 | 0:a33375289d79 | 94 | conData.LJoyY |= (unsigned int)((receive_data[5] - 0x20) & 0x0F) << 4; |
| kikuchi8810 | 0:a33375289d79 | 95 | conData.LJoyY = abs(conData.LJoyY - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 96 | |
| kikuchi8810 | 0:a33375289d79 | 97 | conData.RJoyX |= (unsigned int)((receive_data[5] - 0x20) & 0x30) >> 4; |
| kikuchi8810 | 0:a33375289d79 | 98 | conData.RJoyX |= (unsigned int)((receive_data[6] - 0x20) & 0x3F) << 2; |
| kikuchi8810 | 0:a33375289d79 | 99 | conData.RJoyX = abs(conData.RJoyX - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 100 | |
| kikuchi8810 | 0:a33375289d79 | 101 | conData.RJoyY |= (unsigned int)(receive_data[7] - 0x20); |
| kikuchi8810 | 0:a33375289d79 | 102 | conData.RJoyY |= (unsigned int)((receive_data[8] - 0x20) & 0x03) << 6; |
| kikuchi8810 | 0:a33375289d79 | 103 | conData.RJoyY = abs(conData.RJoyY - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 104 | |
| kikuchi8810 | 0:a33375289d79 | 105 | int buttonPushNum = 0; |
| kikuchi8810 | 0:a33375289d79 | 106 | for (int i = 0; i < 16; i++) |
| kikuchi8810 | 0:a33375289d79 | 107 | { |
| kikuchi8810 | 0:a33375289d79 | 108 | buttonPushNum += (conData.ButtonState >> i) & 0x0001; |
| kikuchi8810 | 0:a33375289d79 | 109 | } |
| kikuchi8810 | 0:a33375289d79 | 110 | if (buttonPushNum > 5) |
| kikuchi8810 | 0:a33375289d79 | 111 | { |
| kikuchi8810 | 0:a33375289d79 | 112 | //conData.ButtonState = pre_conData.ButtonState; |
| kikuchi8810 | 0:a33375289d79 | 113 | comCheck = false; |
| kikuchi8810 | 0:a33375289d79 | 114 | } |
| kikuchi8810 | 0:a33375289d79 | 115 | else |
| kikuchi8810 | 0:a33375289d79 | 116 | { |
| kikuchi8810 | 0:a33375289d79 | 117 | conData.ButtonState |= lastButtonState; |
| kikuchi8810 | 0:a33375289d79 | 118 | } |
| kikuchi8810 | 0:a33375289d79 | 119 | |
| kikuchi8810 | 0:a33375289d79 | 120 | pre_count_ms = count_ms; //受信時間の更新 |
| kikuchi8810 | 0:a33375289d79 | 121 | } |
| kikuchi8810 | 0:a33375289d79 | 122 | } |
| kikuchi8810 | 0:a33375289d79 | 123 | recv_num = 0; |
| kikuchi8810 | 0:a33375289d79 | 124 | } |
| kikuchi8810 | 0:a33375289d79 | 125 | else |
| kikuchi8810 | 0:a33375289d79 | 126 | { |
| kikuchi8810 | 0:a33375289d79 | 127 | receive_data[recv_num] = c; |
| kikuchi8810 | 0:a33375289d79 | 128 | recv_num++; |
| kikuchi8810 | 0:a33375289d79 | 129 | } |
| kikuchi8810 | 0:a33375289d79 | 130 | } |
| kikuchi8810 | 0:a33375289d79 | 131 | #elif CON_TYPE == CON_DS4 // DualShock4を使う場合の処理(どのコントローラを使うかはdefine.hで設定) |
| kikuchi8810 | 0:a33375289d79 | 132 | // コントローラデータを取得する部分 |
| kikuchi8810 | 0:a33375289d79 | 133 | static int recv_num = 0; |
| kikuchi8810 | 0:a33375289d79 | 134 | char c; |
| kikuchi8810 | 0:a33375289d79 | 135 | while (serial.readable()) |
| kikuchi8810 | 0:a33375289d79 | 136 | { |
| kikuchi8810 | 0:a33375289d79 | 137 | c = serial.getc(); |
| kikuchi8810 | 0:a33375289d79 | 138 | //Serial.print(c); |
| kikuchi8810 | 0:a33375289d79 | 139 | if (c == '\n') |
| kikuchi8810 | 0:a33375289d79 | 140 | { |
| kikuchi8810 | 0:a33375289d79 | 141 | if (recv_num == 10) |
| kikuchi8810 | 0:a33375289d79 | 142 | { // データ数はチェックサム含めて10個(0~9) |
| kikuchi8810 | 0:a33375289d79 | 143 | checksum = 0; |
| kikuchi8810 | 0:a33375289d79 | 144 | for (int i = 0; i < 9; i++) |
| kikuchi8810 | 0:a33375289d79 | 145 | checksum ^= (unsigned int)(receive_data[i] - 0x20); // チェックサムの計算 |
| kikuchi8810 | 0:a33375289d79 | 146 | if ((checksum & 0x3F) == (receive_data[9] - 0x20)) |
| kikuchi8810 | 0:a33375289d79 | 147 | { // チェックサムの計算が合っていた場合のみ値を格納 |
| kikuchi8810 | 0:a33375289d79 | 148 | comCheck = true; |
| kikuchi8810 | 0:a33375289d79 | 149 | |
| kikuchi8810 | 0:a33375289d79 | 150 | //conData.ButtonState = 0; |
| kikuchi8810 | 0:a33375289d79 | 151 | conData.LJoyX = 0, conData.LJoyY = 0, conData.RJoyX = 0, conData.RJoyY = 0; |
| kikuchi8810 | 0:a33375289d79 | 152 | lastButtonState = (unsigned int)(receive_data[0] - 0x20) & 0x3F; |
| kikuchi8810 | 0:a33375289d79 | 153 | lastButtonState |= (unsigned int)((receive_data[1] - 0x20) & 0x3F) << 6; |
| kikuchi8810 | 0:a33375289d79 | 154 | lastButtonState |= (unsigned int)((receive_data[2] - 0x20) & 0x0F) << 12; |
| kikuchi8810 | 0:a33375289d79 | 155 | |
| kikuchi8810 | 0:a33375289d79 | 156 | conData.LJoyX |= (unsigned int)(receive_data[3] - 0x20); |
| kikuchi8810 | 0:a33375289d79 | 157 | conData.LJoyX |= (unsigned int)((receive_data[4] - 0x20) & 0x03) << 6; |
| kikuchi8810 | 0:a33375289d79 | 158 | conData.LJoyX = abs(conData.LJoyX - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 159 | |
| kikuchi8810 | 0:a33375289d79 | 160 | conData.LJoyY |= (unsigned int)((receive_data[4] - 0x20) & 0x3C) >> 2; |
| kikuchi8810 | 0:a33375289d79 | 161 | conData.LJoyY |= (unsigned int)((receive_data[5] - 0x20) & 0x0F) << 4; |
| kikuchi8810 | 0:a33375289d79 | 162 | conData.LJoyY = abs(conData.LJoyY - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 163 | |
| kikuchi8810 | 0:a33375289d79 | 164 | conData.RJoyX |= (unsigned int)((receive_data[5] - 0x20) & 0x30) >> 4; |
| kikuchi8810 | 0:a33375289d79 | 165 | conData.RJoyX |= (unsigned int)((receive_data[6] - 0x20) & 0x3F) << 2; |
| kikuchi8810 | 0:a33375289d79 | 166 | conData.RJoyX = abs(conData.RJoyX - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 167 | |
| kikuchi8810 | 0:a33375289d79 | 168 | conData.RJoyY |= (unsigned int)(receive_data[7] - 0x20); |
| kikuchi8810 | 0:a33375289d79 | 169 | conData.RJoyY |= (unsigned int)((receive_data[8] - 0x20) & 0x03) << 6; |
| kikuchi8810 | 0:a33375289d79 | 170 | conData.RJoyY = abs(conData.RJoyY - 0xFF); |
| kikuchi8810 | 0:a33375289d79 | 171 | |
| kikuchi8810 | 0:a33375289d79 | 172 | // 通信ミスであり得ない数のボタン数押されていた場合に無視する処理 |
| kikuchi8810 | 0:a33375289d79 | 173 | int buttonPushNum = 0; |
| kikuchi8810 | 0:a33375289d79 | 174 | for (int i = 0; i < 16; i++) |
| kikuchi8810 | 0:a33375289d79 | 175 | { |
| kikuchi8810 | 0:a33375289d79 | 176 | buttonPushNum += (lastButtonState >> i) & 0x0001; |
| kikuchi8810 | 0:a33375289d79 | 177 | } |
| kikuchi8810 | 0:a33375289d79 | 178 | if (buttonPushNum > 5) |
| kikuchi8810 | 0:a33375289d79 | 179 | { |
| kikuchi8810 | 0:a33375289d79 | 180 | //conData.ButtonState = pre_conData.ButtonState; |
| kikuchi8810 | 0:a33375289d79 | 181 | comCheck = false; |
| kikuchi8810 | 0:a33375289d79 | 182 | } |
| kikuchi8810 | 0:a33375289d79 | 183 | else |
| kikuchi8810 | 0:a33375289d79 | 184 | { |
| kikuchi8810 | 0:a33375289d79 | 185 | conData.ButtonState = lastButtonState & 0xFFFF; |
| kikuchi8810 | 0:a33375289d79 | 186 | } |
| kikuchi8810 | 0:a33375289d79 | 187 | |
| kikuchi8810 | 0:a33375289d79 | 188 | pre_count_ms = count_ms - int_time_ms; //受信時間の更新 |
| kikuchi8810 | 0:a33375289d79 | 189 | } |
| kikuchi8810 | 0:a33375289d79 | 190 | } |
| kikuchi8810 | 0:a33375289d79 | 191 | recv_num = 0; |
| kikuchi8810 | 0:a33375289d79 | 192 | } |
| kikuchi8810 | 0:a33375289d79 | 193 | else |
| kikuchi8810 | 0:a33375289d79 | 194 | { |
| kikuchi8810 | 0:a33375289d79 | 195 | receive_data[recv_num] = c; |
| kikuchi8810 | 0:a33375289d79 | 196 | recv_num++; |
| kikuchi8810 | 0:a33375289d79 | 197 | } |
| kikuchi8810 | 0:a33375289d79 | 198 | } |
| kikuchi8810 | 0:a33375289d79 | 199 | |
| kikuchi8810 | 0:a33375289d79 | 200 | #endif |
| kikuchi8810 | 0:a33375289d79 | 201 | |
| kikuchi8810 | 0:a33375289d79 | 202 | if(!(time_out_ms == -1)) conAvailable = (time_out_ms > (count_ms - pre_count_ms)); //タイムアウトとインターバルの比較 |
| kikuchi8810 | 0:a33375289d79 | 203 | else conAvailable = true; |
| kikuchi8810 | 0:a33375289d79 | 204 | if(count_ms > time_out_ms * 1000) count_ms = time_out_ms; //オーバーフロー対策 |
| kikuchi8810 | 0:a33375289d79 | 205 | |
| kikuchi8810 | 0:a33375289d79 | 206 | return comCheck; |
| kikuchi8810 | 0:a33375289d79 | 207 | } |
| kikuchi8810 | 0:a33375289d79 | 208 | |
| kikuchi8810 | 0:a33375289d79 | 209 | bool Controller::getComCheck(void) |
| kikuchi8810 | 0:a33375289d79 | 210 | { |
| kikuchi8810 | 0:a33375289d79 | 211 | return comCheck; |
| kikuchi8810 | 0:a33375289d79 | 212 | } |
| kikuchi8810 | 0:a33375289d79 | 213 | |
| kikuchi8810 | 0:a33375289d79 | 214 | bool Controller::available(void) |
| kikuchi8810 | 0:a33375289d79 | 215 | { |
| kikuchi8810 | 0:a33375289d79 | 216 | return conAvailable; |
| kikuchi8810 | 0:a33375289d79 | 217 | } |
| kikuchi8810 | 0:a33375289d79 | 218 | |
| kikuchi8810 | 0:a33375289d79 | 219 | bool Controller::readButton_bin(unsigned int ButtonNum) |
| kikuchi8810 | 0:a33375289d79 | 220 | { //放しているときは0,押しているときは1 |
| kikuchi8810 | 0:a33375289d79 | 221 | return ((conData.ButtonState & (0x0001 << (ButtonNum - 1))) == (0x0001 << (ButtonNum - 1))) ? true : false; |
| kikuchi8810 | 0:a33375289d79 | 222 | } |
| kikuchi8810 | 0:a33375289d79 | 223 | |
| kikuchi8810 | 0:a33375289d79 | 224 | int Controller::readButton(unsigned int ButtonNum) |
| kikuchi8810 | 0:a33375289d79 | 225 | { //放しているときは0,押しているときは1,押した瞬間は2,放した瞬間は-1 |
| kikuchi8810 | 0:a33375289d79 | 226 | int result = 0; |
| kikuchi8810 | 0:a33375289d79 | 227 | if ((conData.ButtonState & (0x0001 << (ButtonNum - 1))) == (0x0001 << (ButtonNum - 1))) |
| kikuchi8810 | 0:a33375289d79 | 228 | result += 2; |
| kikuchi8810 | 0:a33375289d79 | 229 | if ((pre_conData.ButtonState & (0x0001 << (ButtonNum - 1))) == (0x0001 << (ButtonNum - 1))) |
| kikuchi8810 | 0:a33375289d79 | 230 | result -= 1; |
| kikuchi8810 | 0:a33375289d79 | 231 | return result; |
| kikuchi8810 | 0:a33375289d79 | 232 | } |
| kikuchi8810 | 0:a33375289d79 | 233 | |
| kikuchi8810 | 0:a33375289d79 | 234 | unsigned int Controller::getButtonState() |
| kikuchi8810 | 0:a33375289d79 | 235 | { |
| kikuchi8810 | 0:a33375289d79 | 236 | return conData.ButtonState; |
| kikuchi8810 | 0:a33375289d79 | 237 | } |
| kikuchi8810 | 0:a33375289d79 | 238 | |
| kikuchi8810 | 0:a33375289d79 | 239 | void Controller::clearButtonState() |
| kikuchi8810 | 0:a33375289d79 | 240 | { |
| kikuchi8810 | 0:a33375289d79 | 241 | pre_conData.ButtonState = conData.ButtonState; |
| kikuchi8810 | 0:a33375289d79 | 242 | } |
| kikuchi8810 | 0:a33375289d79 | 243 | |
| kikuchi8810 | 0:a33375289d79 | 244 | ControllerData Controller::getConData() |
| kikuchi8810 | 0:a33375289d79 | 245 | { |
| kikuchi8810 | 0:a33375289d79 | 246 | return conData; |
| kikuchi8810 | 0:a33375289d79 | 247 | } |
| kikuchi8810 | 0:a33375289d79 | 248 | |
| kikuchi8810 | 0:a33375289d79 | 249 | double Controller::readJoyRX() |
| kikuchi8810 | 0:a33375289d79 | 250 | { |
| kikuchi8810 | 0:a33375289d79 | 251 | if (conData.RJoyX == 127) |
| kikuchi8810 | 0:a33375289d79 | 252 | return 0; |
| kikuchi8810 | 0:a33375289d79 | 253 | return ((double)conData.RJoyX - 127.5) / 127.5; |
| kikuchi8810 | 0:a33375289d79 | 254 | } |
| kikuchi8810 | 0:a33375289d79 | 255 | |
| kikuchi8810 | 0:a33375289d79 | 256 | double Controller::readJoyRY() |
| kikuchi8810 | 0:a33375289d79 | 257 | { |
| kikuchi8810 | 0:a33375289d79 | 258 | if (conData.RJoyY == 127) |
| kikuchi8810 | 0:a33375289d79 | 259 | return 0; |
| kikuchi8810 | 0:a33375289d79 | 260 | return ((double)conData.RJoyY - 127.5) / 127.5; |
| kikuchi8810 | 0:a33375289d79 | 261 | } |
| kikuchi8810 | 0:a33375289d79 | 262 | |
| kikuchi8810 | 0:a33375289d79 | 263 | double Controller::readJoyLX() |
| kikuchi8810 | 0:a33375289d79 | 264 | { |
| kikuchi8810 | 0:a33375289d79 | 265 | if (conData.LJoyX == 127) |
| kikuchi8810 | 0:a33375289d79 | 266 | return 0; |
| kikuchi8810 | 0:a33375289d79 | 267 | return ((double)conData.LJoyX - 127.5) / 127.5; |
| kikuchi8810 | 0:a33375289d79 | 268 | } |
| kikuchi8810 | 0:a33375289d79 | 269 | |
| kikuchi8810 | 0:a33375289d79 | 270 | double Controller::readJoyLY() |
| kikuchi8810 | 0:a33375289d79 | 271 | { |
| kikuchi8810 | 0:a33375289d79 | 272 | if (conData.LJoyY == 127) |
| kikuchi8810 | 0:a33375289d79 | 273 | return 0; |
| kikuchi8810 | 0:a33375289d79 | 274 | return ((double)conData.LJoyY - 127.5) / 127.5; |
| kikuchi8810 | 0:a33375289d79 | 275 | } |
| kikuchi8810 | 0:a33375289d79 | 276 | |
| kikuchi8810 | 0:a33375289d79 | 277 | uint8_t Controller::readJoyRXbyte() |
| kikuchi8810 | 0:a33375289d79 | 278 | { |
| kikuchi8810 | 0:a33375289d79 | 279 | return conData.RJoyX; |
| kikuchi8810 | 0:a33375289d79 | 280 | } |
| kikuchi8810 | 0:a33375289d79 | 281 | |
| kikuchi8810 | 0:a33375289d79 | 282 | uint8_t Controller::readJoyRYbyte() |
| kikuchi8810 | 0:a33375289d79 | 283 | { |
| kikuchi8810 | 0:a33375289d79 | 284 | return conData.RJoyY; |
| kikuchi8810 | 0:a33375289d79 | 285 | } |
| kikuchi8810 | 0:a33375289d79 | 286 | |
| kikuchi8810 | 0:a33375289d79 | 287 | uint8_t Controller::readJoyLXbyte() |
| kikuchi8810 | 0:a33375289d79 | 288 | { |
| kikuchi8810 | 0:a33375289d79 | 289 | return conData.LJoyX; |
| kikuchi8810 | 0:a33375289d79 | 290 | } |
| kikuchi8810 | 0:a33375289d79 | 291 | |
| kikuchi8810 | 0:a33375289d79 | 292 | uint8_t Controller::readJoyLYbyte() |
| kikuchi8810 | 0:a33375289d79 | 293 | { |
| kikuchi8810 | 0:a33375289d79 | 294 | return conData.LJoyY; |
| kikuchi8810 | 0:a33375289d79 | 295 | } |
| kikuchi8810 | 0:a33375289d79 | 296 | |
| kikuchi8810 | 0:a33375289d79 | 297 | unsigned int Controller::getButtonFlagRise() |
| kikuchi8810 | 0:a33375289d79 | 298 | { |
| kikuchi8810 | 0:a33375289d79 | 299 | // 立ち上がり,立下りのフラッギング処理 (フラグクリアは別関数で) |
| kikuchi8810 | 0:a33375289d79 | 300 | unsigned int buttonFlagRise = 0; |
| kikuchi8810 | 0:a33375289d79 | 301 | if (pre_conData.ButtonState != conData.ButtonState) |
| kikuchi8810 | 0:a33375289d79 | 302 | { |
| kikuchi8810 | 0:a33375289d79 | 303 | for (int i = 0; i < 16; i++) |
| kikuchi8810 | 0:a33375289d79 | 304 | { |
| kikuchi8810 | 0:a33375289d79 | 305 | int mask = 0x01 << i; |
| kikuchi8810 | 0:a33375289d79 | 306 | if ((conData.ButtonState & mask) != (pre_conData.ButtonState & mask)) |
| kikuchi8810 | 0:a33375289d79 | 307 | { |
| kikuchi8810 | 0:a33375289d79 | 308 | if ((conData.ButtonState & mask) == mask) |
| kikuchi8810 | 0:a33375289d79 | 309 | buttonFlagRise |= (conData.ButtonState & mask); |
| kikuchi8810 | 0:a33375289d79 | 310 | } |
| kikuchi8810 | 0:a33375289d79 | 311 | } |
| kikuchi8810 | 0:a33375289d79 | 312 | } |
| kikuchi8810 | 0:a33375289d79 | 313 | return buttonFlagRise; |
| kikuchi8810 | 0:a33375289d79 | 314 | } |
| kikuchi8810 | 0:a33375289d79 | 315 | |
| kikuchi8810 | 0:a33375289d79 | 316 | unsigned int Controller::getButtonFlagFall() |
| kikuchi8810 | 0:a33375289d79 | 317 | { |
| kikuchi8810 | 0:a33375289d79 | 318 | unsigned int buttonFlagFall = 0; |
| kikuchi8810 | 0:a33375289d79 | 319 | if (pre_conData.ButtonState != conData.ButtonState) |
| kikuchi8810 | 0:a33375289d79 | 320 | { |
| kikuchi8810 | 0:a33375289d79 | 321 | for (int i = 0; i < 16; i++) |
| kikuchi8810 | 0:a33375289d79 | 322 | { |
| kikuchi8810 | 0:a33375289d79 | 323 | int mask = 0x01 << i; |
| kikuchi8810 | 0:a33375289d79 | 324 | if ((conData.ButtonState & mask) != (pre_conData.ButtonState & mask)) |
| kikuchi8810 | 0:a33375289d79 | 325 | { |
| kikuchi8810 | 0:a33375289d79 | 326 | if ((pre_conData.ButtonState & mask) == mask) |
| kikuchi8810 | 0:a33375289d79 | 327 | buttonFlagFall |= (pre_conData.ButtonState & mask); |
| kikuchi8810 | 0:a33375289d79 | 328 | } |
| kikuchi8810 | 0:a33375289d79 | 329 | } |
| kikuchi8810 | 0:a33375289d79 | 330 | } |
| kikuchi8810 | 0:a33375289d79 | 331 | return buttonFlagFall; |
| kikuchi8810 | 0:a33375289d79 | 332 | } |