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