convert_KeilToMbed

Dependencies:   DigitDisplay Motor PID mbed millis

Fork of DagonFly__RoadToJapan_15Mei_Ultimate by KRAI 2017

Committer:
be_bryan
Date:
Sun Jul 02 01:37:31 2017 +0000
Revision:
52:a39e26b935a9
Parent:
51:df6391c3fa68
Convert_KeilToMbed

Who changed what in which revision?

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