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