convert_KeilToMbed

Dependencies:   DigitDisplay Motor PID mbed millis

Fork of DagonFly__RoadToJapan_15Mei_Ultimate by KRAI 2017

Committer:
fanny868
Date:
Thu May 28 14:50:05 2015 +0000
Revision:
0:9072e932503c
Child:
3:1287fccc11be
.

Who changed what in which revision?

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