![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Kode Taker 2018
Dependencies: mbed PS_PAD encoderKRAI Motor_1 millis
JoystickPS3.h@0:8c6f29487664, 2018-07-03 (annotated)
- Committer:
- gatulz
- Date:
- Tue Jul 03 05:45:18 2018 +0000
- Revision:
- 0:8c6f29487664
TAKER DENGAN JOYSTICK KABEL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gatulz | 0:8c6f29487664 | 1 | #ifndef MBED_H |
gatulz | 0:8c6f29487664 | 2 | #include "mbed.h" |
gatulz | 0:8c6f29487664 | 3 | #endif |
gatulz | 0:8c6f29487664 | 4 | |
gatulz | 0:8c6f29487664 | 5 | #ifndef JoystickPS3__serialDEFAULT_BAUD |
gatulz | 0:8c6f29487664 | 6 | #define JoystickPS3__serialDEFAULT_BAUD 115200 |
gatulz | 0:8c6f29487664 | 7 | #endif |
gatulz | 0:8c6f29487664 | 8 | |
gatulz | 0:8c6f29487664 | 9 | //Serial debug(USBTX,USBRX); |
gatulz | 0:8c6f29487664 | 10 | |
gatulz | 0:8c6f29487664 | 11 | namespace JoystickPS3 { |
gatulz | 0:8c6f29487664 | 12 | |
gatulz | 0:8c6f29487664 | 13 | class joysticknucleo { |
gatulz | 0:8c6f29487664 | 14 | public: |
gatulz | 0:8c6f29487664 | 15 | joysticknucleo(PinName tx, PinName rx) : _serial(tx, rx) |
gatulz | 0:8c6f29487664 | 16 | { |
gatulz | 0:8c6f29487664 | 17 | |
gatulz | 0:8c6f29487664 | 18 | } |
gatulz | 0:8c6f29487664 | 19 | |
gatulz | 0:8c6f29487664 | 20 | // Deklarasi variabel tombol analog |
gatulz | 0:8c6f29487664 | 21 | unsigned char LX, LY, RX, RY, R2, L2; |
gatulz | 0:8c6f29487664 | 22 | |
gatulz | 0:8c6f29487664 | 23 | unsigned char button; |
gatulz | 0:8c6f29487664 | 24 | unsigned char RL; |
gatulz | 0:8c6f29487664 | 25 | unsigned char button_click; |
gatulz | 0:8c6f29487664 | 26 | unsigned char RL_click; |
gatulz | 0:8c6f29487664 | 27 | |
gatulz | 0:8c6f29487664 | 28 | void setup(){ |
gatulz | 0:8c6f29487664 | 29 | _serial.baud(JoystickPS3__serialDEFAULT_BAUD); |
gatulz | 0:8c6f29487664 | 30 | // debug.baud(9600); |
gatulz | 0:8c6f29487664 | 31 | } |
gatulz | 0:8c6f29487664 | 32 | |
gatulz | 0:8c6f29487664 | 33 | /*********************************************************************************************/ |
gatulz | 0:8c6f29487664 | 34 | /** **/ |
gatulz | 0:8c6f29487664 | 35 | /** FUNGSI PEMBACAAN DATA **/ |
gatulz | 0:8c6f29487664 | 36 | /** - Data yang diterima dari Serial Arduino berbentuk 8-bit **/ |
gatulz | 0:8c6f29487664 | 37 | /** - Data yang diterima diolah menjadi boolean / 1-bit untuk data tombol button dan RL **/ |
gatulz | 0:8c6f29487664 | 38 | /** karena data yang digunakan adalah 1-bit (true/false) **/ |
gatulz | 0:8c6f29487664 | 39 | /** - Untuk analog data yang diterima tidak diolah karena rentang data yang dikirimkan **/ |
gatulz | 0:8c6f29487664 | 40 | /** memiliki rentang 0-255 / 8-bit, dan data yang akan digunakan adalah data 8-bit **/ |
gatulz | 0:8c6f29487664 | 41 | /** **/ |
gatulz | 0:8c6f29487664 | 42 | /** |------|-------|-------|------|-------|--------|-----------|----------| **/ |
gatulz | 0:8c6f29487664 | 43 | /** Bit Ke | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | **/ |
gatulz | 0:8c6f29487664 | 44 | /** |------|-------|-------|------|-------|--------|-----------|----------| **/ |
gatulz | 0:8c6f29487664 | 45 | /** Data | kiri | bawah | kanan | atas | kotak | silang | lingkaran | segitiga | **/ |
gatulz | 0:8c6f29487664 | 46 | /** |------|-------|-------|------|-------|--------|-----------|----------| **/ |
gatulz | 0:8c6f29487664 | 47 | /** **/ |
gatulz | 0:8c6f29487664 | 48 | /** - Penggabungan data R1, R2, L1, L2, R3, L3, START, dan SELECT disimpan dalam **/ |
gatulz | 0:8c6f29487664 | 49 | /** variabel "RL" **/ |
gatulz | 0:8c6f29487664 | 50 | /** - Urutan data pada variabel "RL" dan "RL_click" adalah **/ |
gatulz | 0:8c6f29487664 | 51 | /** sebagai berikut **/ |
gatulz | 0:8c6f29487664 | 52 | /** **/ |
gatulz | 0:8c6f29487664 | 53 | /** |----|--------|-------|----|----|----|----| **/ |
gatulz | 0:8c6f29487664 | 54 | /** Bit Ke | 6 | 5 | 4 | 3 | 2 | 1 | 0 | **/ |
gatulz | 0:8c6f29487664 | 55 | /** |----|--------|-------|----|----|----|----| **/ |
gatulz | 0:8c6f29487664 | 56 | /** Data | PS | SELECT | START | L3 | L1 | R3 | R1 | **/ |
gatulz | 0:8c6f29487664 | 57 | /** |----|--------|-------|----|----|----|----| **/ |
gatulz | 0:8c6f29487664 | 58 | /** **/ |
gatulz | 0:8c6f29487664 | 59 | /*********************************************************************************************/ |
gatulz | 0:8c6f29487664 | 60 | |
gatulz | 0:8c6f29487664 | 61 | void olah_data() |
gatulz | 0:8c6f29487664 | 62 | { |
gatulz | 0:8c6f29487664 | 63 | // Pengolahan data dari data "button" |
gatulz | 0:8c6f29487664 | 64 | segitiga = (bool)((button >> 0) & 0x1); |
gatulz | 0:8c6f29487664 | 65 | lingkaran = (bool)((button >> 1) & 0x1); |
gatulz | 0:8c6f29487664 | 66 | silang = (bool)((button >> 2) & 0x1); |
gatulz | 0:8c6f29487664 | 67 | kotak = (bool)((button >> 3) & 0x1); |
gatulz | 0:8c6f29487664 | 68 | atas = (bool)((button >> 4) & 0x1); |
gatulz | 0:8c6f29487664 | 69 | kanan = (bool)((button >> 5) & 0x1); |
gatulz | 0:8c6f29487664 | 70 | bawah = (bool)((button >> 6) & 0x1); |
gatulz | 0:8c6f29487664 | 71 | kiri = (bool)((button >> 7) & 0x1); |
gatulz | 0:8c6f29487664 | 72 | |
gatulz | 0:8c6f29487664 | 73 | // Pengolahan data dari data "RL" |
gatulz | 0:8c6f29487664 | 74 | R1 = (bool)((RL >> 0) & 0x1); |
gatulz | 0:8c6f29487664 | 75 | R3 = (bool)((RL >> 1) & 0x1); |
gatulz | 0:8c6f29487664 | 76 | L1 = (bool)((RL >> 2) & 0x1); |
gatulz | 0:8c6f29487664 | 77 | L3 = (bool)((RL >> 3) & 0x1); |
gatulz | 0:8c6f29487664 | 78 | START = (bool)((RL >> 4) & 0x1); |
gatulz | 0:8c6f29487664 | 79 | SELECT = (bool)((RL >> 5) & 0x1); |
gatulz | 0:8c6f29487664 | 80 | PS = (bool)((RL >> 6) & 0x1); |
gatulz | 0:8c6f29487664 | 81 | |
gatulz | 0:8c6f29487664 | 82 | // R2 click dan L2 click |
gatulz | 0:8c6f29487664 | 83 | if (R2 > 100) { |
gatulz | 0:8c6f29487664 | 84 | if ( R2sebelum) { R2_click = false; |
gatulz | 0:8c6f29487664 | 85 | } else { R2_click = true;} |
gatulz | 0:8c6f29487664 | 86 | R2sebelum = true; |
gatulz | 0:8c6f29487664 | 87 | }else { |
gatulz | 0:8c6f29487664 | 88 | R2sebelum = false; |
gatulz | 0:8c6f29487664 | 89 | R2_click = false; |
gatulz | 0:8c6f29487664 | 90 | } |
gatulz | 0:8c6f29487664 | 91 | if (L2 > 100) { |
gatulz | 0:8c6f29487664 | 92 | if ( L2sebelum) { L2_click = false; |
gatulz | 0:8c6f29487664 | 93 | } else { L2_click = true;} |
gatulz | 0:8c6f29487664 | 94 | L2sebelum = true; |
gatulz | 0:8c6f29487664 | 95 | }else { L2sebelum = false; |
gatulz | 0:8c6f29487664 | 96 | L2_click = false; |
gatulz | 0:8c6f29487664 | 97 | } |
gatulz | 0:8c6f29487664 | 98 | |
gatulz | 0:8c6f29487664 | 99 | segitiga_click = (bool)((button_click >> 0) & 0x1); |
gatulz | 0:8c6f29487664 | 100 | lingkaran_click = (bool)((button_click >> 1) & 0x1); |
gatulz | 0:8c6f29487664 | 101 | silang_click = (bool)((button_click >> 2) & 0x1); |
gatulz | 0:8c6f29487664 | 102 | kotak_click = (bool)((button_click >> 3) & 0x1); |
gatulz | 0:8c6f29487664 | 103 | atas_click = (bool)((button_click >> 4) & 0x1); |
gatulz | 0:8c6f29487664 | 104 | kanan_click = (bool)((button_click >> 5) & 0x1); |
gatulz | 0:8c6f29487664 | 105 | bawah_click = (bool)((button_click >> 6) & 0x1); |
gatulz | 0:8c6f29487664 | 106 | kiri_click = (bool)((button_click >> 7) & 0x1); |
gatulz | 0:8c6f29487664 | 107 | |
gatulz | 0:8c6f29487664 | 108 | // Pengolahan data dari data "RL" |
gatulz | 0:8c6f29487664 | 109 | R1_click = (bool)((RL_click >> 0) & 0x1); |
gatulz | 0:8c6f29487664 | 110 | R3_click = (bool)((RL_click >> 1) & 0x1); |
gatulz | 0:8c6f29487664 | 111 | L1_click = (bool)((RL_click >> 2) & 0x1); |
gatulz | 0:8c6f29487664 | 112 | L3_click = (bool)((RL_click >> 3) & 0x1); |
gatulz | 0:8c6f29487664 | 113 | START_click = (bool)((RL_click >> 4) & 0x1); |
gatulz | 0:8c6f29487664 | 114 | SELECT_click = (bool)((RL_click >> 5) & 0x1); |
gatulz | 0:8c6f29487664 | 115 | PS_click = (bool)((RL_click >> 6) & 0x1); |
gatulz | 0:8c6f29487664 | 116 | } |
gatulz | 0:8c6f29487664 | 117 | |
gatulz | 0:8c6f29487664 | 118 | /*********************************************************************************************/ |
gatulz | 0:8c6f29487664 | 119 | /** **/ |
gatulz | 0:8c6f29487664 | 120 | /** FUNGSI IDLE **/ |
gatulz | 0:8c6f29487664 | 121 | /** - Fungsi dijalankan saat Arduino mengirimkan data yang merupakan **/ |
gatulz | 0:8c6f29487664 | 122 | /** kondisi PS3 Disconnected **/ |
gatulz | 0:8c6f29487664 | 123 | /** - Fungsi membuat semua data joystik bernilai 0 **/ |
gatulz | 0:8c6f29487664 | 124 | /** **/ |
gatulz | 0:8c6f29487664 | 125 | /*********************************************************************************************/ |
gatulz | 0:8c6f29487664 | 126 | |
gatulz | 0:8c6f29487664 | 127 | void idle(){ |
gatulz | 0:8c6f29487664 | 128 | // Set 0 |
gatulz | 0:8c6f29487664 | 129 | button = 0; |
gatulz | 0:8c6f29487664 | 130 | RL = 0; |
gatulz | 0:8c6f29487664 | 131 | button_click = 0; |
gatulz | 0:8c6f29487664 | 132 | RL_click = 0; |
gatulz | 0:8c6f29487664 | 133 | R2_click =0; |
gatulz | 0:8c6f29487664 | 134 | L2_click =0; |
gatulz | 0:8c6f29487664 | 135 | R2 = 0; |
gatulz | 0:8c6f29487664 | 136 | L2 = 0; |
gatulz | 0:8c6f29487664 | 137 | RX = 0; |
gatulz | 0:8c6f29487664 | 138 | RY = 0; |
gatulz | 0:8c6f29487664 | 139 | LX = 0; |
gatulz | 0:8c6f29487664 | 140 | LY = 0; |
gatulz | 0:8c6f29487664 | 141 | |
gatulz | 0:8c6f29487664 | 142 | } |
gatulz | 0:8c6f29487664 | 143 | |
gatulz | 0:8c6f29487664 | 144 | /*********************************************************************************************/ |
gatulz | 0:8c6f29487664 | 145 | /** **/ |
gatulz | 0:8c6f29487664 | 146 | /** FUNGSI PEMBACAAN DATA **/ |
gatulz | 0:8c6f29487664 | 147 | /** - Fungsi pembacaan data yang dikirim dari arduino **/ |
gatulz | 0:8c6f29487664 | 148 | /** - Data yang dikirim dari arduino merupakan paket data dengan format pengiriman **/ |
gatulz | 0:8c6f29487664 | 149 | /** **/ |
gatulz | 0:8c6f29487664 | 150 | /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/ |
gatulz | 0:8c6f29487664 | 151 | /** | 0x88 | 0x08 | button | RL | button_click | RL_click | R2 | L2 | RX | RY | LX | LY | **/ |
gatulz | 0:8c6f29487664 | 152 | /** |------|------|--------|----|--------------|----------|----|----|----|----|----|----| **/ |
gatulz | 0:8c6f29487664 | 153 | /** **/ |
gatulz | 0:8c6f29487664 | 154 | /** |------|------| **/ |
gatulz | 0:8c6f29487664 | 155 | /** | 0x88 | 0x09 | **/ |
gatulz | 0:8c6f29487664 | 156 | /** |------|------| **/ |
gatulz | 0:8c6f29487664 | 157 | /** **/ |
gatulz | 0:8c6f29487664 | 158 | /** - Jika urutan data yang diterima seperti tabel diatas, maka data tersebut akan **/ |
gatulz | 0:8c6f29487664 | 159 | /** diolah untuk input ke aktuator **/ |
gatulz | 0:8c6f29487664 | 160 | /** **/ |
gatulz | 0:8c6f29487664 | 161 | /*********************************************************************************************/ |
gatulz | 0:8c6f29487664 | 162 | |
gatulz | 0:8c6f29487664 | 163 | void baca_data() |
gatulz | 0:8c6f29487664 | 164 | { |
gatulz | 0:8c6f29487664 | 165 | // Interrupt Serial |
gatulz | 0:8c6f29487664 | 166 | if(_serial.readable()&&(_serial.getc()==0x88)) { |
gatulz | 0:8c6f29487664 | 167 | // Pembacaan data dilakukan jika data awal yang diterima adalah 0x88 kemudian 0x08 |
gatulz | 0:8c6f29487664 | 168 | if(_serial.getc()==0x08){ |
gatulz | 0:8c6f29487664 | 169 | // Proses Pembacaan Data |
gatulz | 0:8c6f29487664 | 170 | button = _serial.getc(); |
gatulz | 0:8c6f29487664 | 171 | RL = _serial.getc(); |
gatulz | 0:8c6f29487664 | 172 | button_click = _serial.getc(); |
gatulz | 0:8c6f29487664 | 173 | RL_click = _serial.getc(); |
gatulz | 0:8c6f29487664 | 174 | R2 = _serial.getc(); |
gatulz | 0:8c6f29487664 | 175 | L2 = _serial.getc(); |
gatulz | 0:8c6f29487664 | 176 | RX = _serial.getc(); |
gatulz | 0:8c6f29487664 | 177 | RY = _serial.getc(); |
gatulz | 0:8c6f29487664 | 178 | LX = _serial.getc(); |
gatulz | 0:8c6f29487664 | 179 | LY = _serial.getc(); |
gatulz | 0:8c6f29487664 | 180 | } else if(_serial.getc()==0x09) { |
gatulz | 0:8c6f29487664 | 181 | // PS3 Disconnected |
gatulz | 0:8c6f29487664 | 182 | idle(); |
gatulz | 0:8c6f29487664 | 183 | } else { |
gatulz | 0:8c6f29487664 | 184 | idle(); } |
gatulz | 0:8c6f29487664 | 185 | // Indikator - Print data pada monitor PC |
gatulz | 0:8c6f29487664 | 186 | // debug.printf("%2x %2x %2x %2x %3d %3d %3d %3d %3d %3d\n\r",button, RL, button_click, RL_click, R2, L2, RX, RY, LX, LY); |
gatulz | 0:8c6f29487664 | 187 | } |
gatulz | 0:8c6f29487664 | 188 | } |
gatulz | 0:8c6f29487664 | 189 | |
gatulz | 0:8c6f29487664 | 190 | |
gatulz | 0:8c6f29487664 | 191 | int readable(){ |
gatulz | 0:8c6f29487664 | 192 | return _serial.readable(); |
gatulz | 0:8c6f29487664 | 193 | } |
gatulz | 0:8c6f29487664 | 194 | |
gatulz | 0:8c6f29487664 | 195 | public: |
gatulz | 0:8c6f29487664 | 196 | // Deklarasi variabel tombol joystik |
gatulz | 0:8c6f29487664 | 197 | bool kiri, kanan, atas, bawah; |
gatulz | 0:8c6f29487664 | 198 | bool segitiga, lingkaran, kotak, silang; |
gatulz | 0:8c6f29487664 | 199 | bool L1, R1, L3, R3, START, SELECT, PS; |
gatulz | 0:8c6f29487664 | 200 | |
gatulz | 0:8c6f29487664 | 201 | bool kiri_click, kanan_click, atas_click, bawah_click; |
gatulz | 0:8c6f29487664 | 202 | bool segitiga_click, lingkaran_click, kotak_click, silang_click; |
gatulz | 0:8c6f29487664 | 203 | bool L1_click, R1_click, L3_click, R3_click, R2_click, L2_click; |
gatulz | 0:8c6f29487664 | 204 | bool R2sebelum,L2sebelum; |
gatulz | 0:8c6f29487664 | 205 | bool START_click, SELECT_click, PS_click; |
gatulz | 0:8c6f29487664 | 206 | |
gatulz | 0:8c6f29487664 | 207 | protected: |
gatulz | 0:8c6f29487664 | 208 | virtual int _getc(){return _serial.getc();} |
gatulz | 0:8c6f29487664 | 209 | Serial _serial; |
gatulz | 0:8c6f29487664 | 210 | }; |
gatulz | 0:8c6f29487664 | 211 | |
gatulz | 0:8c6f29487664 | 212 | }; |
gatulz | 0:8c6f29487664 | 213 | |
gatulz | 0:8c6f29487664 | 214 | using namespace JoystickPS3; |