Rev 403 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
403 | ligi | 1 | /******************************************************************************************************************************** |
231 | ligi | 2 | * |
3 | * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de ) |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
403 | ligi | 7 | * see README for further Infos |
8 | * |
||
9 | * |
||
10 | *******************************************************************************************************************************/ |
||
164 | ligi | 11 | |
12 | import javax.microedition.io.*; |
||
13 | import java.io.*; |
||
14 | |||
15 | public class MKCommunicator |
||
16 | implements Runnable |
||
17 | { |
||
18 | /***************** Section: public Attributes **********************************************/ |
||
19 | public boolean connected=false; // flag for the connection state |
||
20 | public boolean fatal=false; // flag which is set when an error is so fatal that reconnecting won't be tried - e.g. unknown version number. |
||
21 | |||
22 | |||
23 | public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes |
||
24 | |||
208 | ligi | 25 | public MKLCD LCD; |
210 | ligi | 26 | public MKVersion version; |
27 | public MKDebugData debug_data; |
||
303 | ligi | 28 | public MKParamsParser params; |
221 | ligi | 29 | public MKWatchDog watchdog; |
314 | ligi | 30 | public MKProxy proxy=null; |
208 | ligi | 31 | |
181 | ligi | 32 | public long connection_start_time=-1; |
164 | ligi | 33 | |
34 | |||
35 | /****************** Section: private Attributes **********************************************/ |
||
36 | private javax.microedition.io.StreamConnection connection; |
||
37 | private java.io.InputStream reader; |
||
38 | private java.io.OutputStream writer; |
||
39 | |||
40 | |||
41 | // temp - to be removed |
||
42 | String p_msg="--"; |
||
43 | public String msg="BT_INIT"; |
||
44 | |||
208 | ligi | 45 | public int debug_data_count=0; |
46 | public int version_data_count=0; |
||
47 | public int other_data_count=0; |
||
48 | public int lcd_data_count=0; |
||
303 | ligi | 49 | public int params_data_count=0; |
164 | ligi | 50 | |
51 | |||
314 | ligi | 52 | |
303 | ligi | 53 | String name; |
54 | |||
164 | ligi | 55 | /****************** Section: public Methods ************************************************/ |
303 | ligi | 56 | public MKCommunicator() |
164 | ligi | 57 | { |
210 | ligi | 58 | version=new MKVersion(); |
181 | ligi | 59 | debug_data=new MKDebugData(); |
303 | ligi | 60 | params=new MKParamsParser(); |
61 | // mk_url=url; // remember URL for connecting / reconnecting later |
||
181 | ligi | 62 | |
303 | ligi | 63 | LCD= new MKLCD(this); |
64 | watchdog=new MKWatchDog(this); |
||
164 | ligi | 65 | new Thread( this ).start(); // fire up main Thread |
66 | } |
||
67 | |||
68 | |||
314 | ligi | 69 | public void do_proxy(String proxy_url) |
70 | { |
||
71 | proxy=new MKProxy(proxy_url); |
||
72 | } |
||
303 | ligi | 73 | |
74 | // URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control |
||
75 | public void connect_to(String _url,String _name) |
||
76 | { |
||
77 | mk_url=_url; |
||
78 | name=_name; |
||
79 | force_disconnect=false; |
||
80 | connected=false; |
||
81 | } |
||
82 | |||
164 | ligi | 83 | /****************** Section: private Methods ************************************************/ |
84 | private void connect() |
||
85 | { |
||
303 | ligi | 86 | |
164 | ligi | 87 | try{ |
88 | connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE); |
||
89 | reader=connection.openInputStream(); |
||
90 | writer=connection.openOutputStream(); |
||
181 | ligi | 91 | |
92 | connection_start_time=System.currentTimeMillis(); |
||
164 | ligi | 93 | connected=true; // if we get here everything seems to be OK |
181 | ligi | 94 | get_version(); |
221 | ligi | 95 | lcd_data_count=0; |
96 | debug_data_count=0; |
||
97 | version_data_count=0; |
||
98 | |||
303 | ligi | 99 | |
164 | ligi | 100 | } |
101 | catch (Exception ex) |
||
102 | { |
||
103 | // TODO difference fatal errors from those which will lead to reconnection |
||
104 | msg="Problem connecting" + "\n" + ex; |
||
105 | } |
||
181 | ligi | 106 | |
107 | |||
108 | |||
164 | ligi | 109 | } |
110 | |||
111 | |||
112 | |||
113 | public int[] Decode64(int[] in_arr, int offset,int len) |
||
114 | { |
||
115 | int ptrIn=offset; |
||
116 | int a,b,c,d,x,y,z; |
||
117 | int ptr=0; |
||
118 | |||
119 | int[] out_arr=new int[len]; |
||
120 | |||
121 | while(len!=0) |
||
122 | { |
||
123 | a = in_arr[ptrIn++] - '='; |
||
124 | b = in_arr[ptrIn++] - '='; |
||
125 | c = in_arr[ptrIn++] - '='; |
||
126 | d = in_arr[ptrIn++] - '='; |
||
127 | //if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
128 | |||
129 | x = (a << 2) | (b >> 4); |
||
130 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
131 | z = ((c & 0x03) << 6) | d; |
||
132 | |||
133 | if((len--)!=0) out_arr[ptr++] = x; else break; |
||
134 | if((len--)!=0) out_arr[ptr++] = y; else break; |
||
135 | if((len--)!=0) out_arr[ptr++] = z; else break; |
||
136 | } |
||
137 | |||
138 | return out_arr; |
||
139 | |||
140 | } |
||
141 | |||
325 | ligi | 142 | // FC - Function Mappers |
164 | ligi | 143 | |
144 | // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives |
||
145 | public void get_version() |
||
146 | { |
||
147 | send_command(0,'v',new int[0]); |
||
148 | } |
||
149 | |||
303 | ligi | 150 | // send a MotorTest request - params are the speed for each Motor |
231 | ligi | 151 | public void motor_test(int[] params) |
152 | { |
||
153 | send_command(0,'t',params); |
||
154 | } |
||
155 | |||
475 | ligi | 156 | public void send_keys(int[] params) |
157 | { |
||
158 | send_command(0,'k',params); |
||
159 | } |
||
160 | |||
303 | ligi | 161 | // get params |
162 | public void get_params(int id) |
||
163 | { |
||
164 | int[] params=new int[1]; |
||
165 | params[0]=id; |
||
231 | ligi | 166 | |
325 | ligi | 167 | while(sending) |
168 | {try { Thread.sleep(50); } |
||
169 | catch (Exception e) { } |
||
170 | } |
||
171 | |||
303 | ligi | 172 | send_command(0,'q',params); |
173 | } |
||
174 | |||
325 | ligi | 175 | public void trigger_LCD(int key) |
176 | { |
||
177 | if (sending) return; |
||
303 | ligi | 178 | |
325 | ligi | 179 | |
180 | int[] params=new int[3]; |
||
181 | params[0]=key; |
||
182 | params[1]=0; |
||
183 | params[2]=0; |
||
184 | |||
185 | send_command(0,'h',params); |
||
186 | |||
187 | } |
||
303 | ligi | 188 | |
189 | |||
325 | ligi | 190 | public void write_params() |
191 | { |
||
192 | while(sending) |
||
193 | {try { Thread.sleep(50); } |
||
194 | catch (Exception e) { } |
||
195 | } |
||
196 | |||
197 | send_command(0,(char)('l'+params.act_paramset),params.field[params.act_paramset]); |
||
198 | } |
||
199 | |||
200 | |||
201 | boolean sending=false; |
||
202 | |||
164 | ligi | 203 | // send command to FC ( add crc and pack into pseudo Base64 |
204 | public void send_command(int modul,char cmd,int[] params) |
||
205 | { |
||
325 | ligi | 206 | sending=true; |
164 | ligi | 207 | char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
208 | send_buff[0]='#'; |
||
209 | send_buff[1]=(char)modul; |
||
210 | send_buff[2]=cmd; |
||
211 | |||
208 | ligi | 212 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
213 | { |
||
231 | ligi | 214 | int a = (param_pos*3<params.length)?params[param_pos*3]:0; |
215 | int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0; |
||
216 | int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0; |
||
208 | ligi | 217 | |
218 | send_buff[3+param_pos*4] = (char)((a >> 2)+'=' ); |
||
219 | send_buff[3+param_pos*4+1] = (char)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
||
220 | send_buff[3+param_pos*4+2] = (char)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
||
221 | send_buff[3+param_pos*4+3] = (char)('=' + ( c & 0x3f)); |
||
222 | |||
223 | //send_buff[3+foo]='='; |
||
224 | } |
||
225 | |||
226 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
||
227 | { |
||
228 | int a = (foo<params.length) params[foo]; |
||
229 | int a = params[foo]; |
||
230 | |||
231 | //send_buff[3+foo]='='; |
||
232 | } |
||
233 | */ |
||
164 | ligi | 234 | try |
235 | { |
||
236 | int tmp_crc=0; |
||
237 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
||
238 | { |
||
239 | tmp_crc+=(int)send_buff[tmp_i]; |
||
240 | writer.write(send_buff[tmp_i]); |
||
241 | } |
||
242 | tmp_crc%=4096; |
||
243 | writer.write( (char)(tmp_crc/64 + '=')); |
||
244 | writer.write( (char)(tmp_crc%64 + '=')); |
||
245 | writer.write('\r'); |
||
246 | writer.flush(); |
||
247 | } |
||
248 | catch (Exception e) |
||
249 | { // problem sending data to FC |
||
250 | } |
||
325 | ligi | 251 | |
252 | sending=false; |
||
164 | ligi | 253 | } |
254 | |||
210 | ligi | 255 | |
164 | ligi | 256 | public void process_data(int[] data,int len) |
257 | { |
||
258 | int[] decoded_data; |
||
259 | |||
181 | ligi | 260 | |
164 | ligi | 261 | switch((char)data[2]) |
262 | { |
||
263 | |||
264 | case 'D': // debug Data |
||
208 | ligi | 265 | debug_data_count++; |
475 | ligi | 266 | debug_data.set_by_mk_data(Decode64(data,3,len-3),version); |
164 | ligi | 267 | break; |
268 | |||
269 | case 'V': // Version Info |
||
208 | ligi | 270 | version_data_count++; |
210 | ligi | 271 | version.set_by_mk_data(Decode64(data,3,6)); |
208 | ligi | 272 | break; |
273 | |||
274 | case '0': |
||
275 | case '1': |
||
276 | case '2': |
||
277 | case '3': |
||
278 | LCD.handle_lcd_data(Decode64(data,3,20),data[2]-(int)'0'); |
||
279 | lcd_data_count++; |
||
164 | ligi | 280 | break; |
208 | ligi | 281 | |
303 | ligi | 282 | case 'L': |
283 | case 'M': |
||
284 | case 'N': |
||
285 | case 'O': |
||
286 | case 'P': |
||
475 | ligi | 287 | params.set_by_mk_data((int)(data[2]-'L'),Decode64(data,3,len-3),version); |
303 | ligi | 288 | params_data_count++; |
289 | break; |
||
290 | |||
164 | ligi | 291 | default: |
208 | ligi | 292 | other_data_count++; |
164 | ligi | 293 | break; |
294 | |||
295 | } |
||
296 | |||
297 | |||
208 | ligi | 298 | |
164 | ligi | 299 | |
300 | } |
||
301 | |||
208 | ligi | 302 | String o_msg=""; |
181 | ligi | 303 | |
303 | ligi | 304 | public boolean force_disconnect=true; |
181 | ligi | 305 | |
221 | ligi | 306 | public void close_connections(boolean force) |
208 | ligi | 307 | { |
221 | ligi | 308 | force_disconnect=force; |
208 | ligi | 309 | try{ reader.close(); } |
310 | catch (Exception inner_ex) { } |
||
181 | ligi | 311 | |
208 | ligi | 312 | try{ writer.close(); } |
313 | catch (Exception inner_ex) { } |
||
314 | |||
315 | try{ connection.close(); } |
||
316 | catch (Exception inner_ex) { } |
||
317 | |||
318 | connected=false; |
||
319 | } |
||
320 | |||
164 | ligi | 321 | // Thread to recieve data from Connection |
322 | public void run() |
||
323 | { |
||
324 | int[] data_set=new int[150]; |
||
325 | int input; |
||
326 | int pos=0; |
||
327 | msg+="!!run started!!"; |
||
328 | while(true) |
||
329 | { |
||
330 | if (!connected) |
||
181 | ligi | 331 | { |
208 | ligi | 332 | if (!force_disconnect) connect(); |
181 | ligi | 333 | } |
164 | ligi | 334 | else |
335 | try{ |
||
181 | ligi | 336 | |
164 | ligi | 337 | pos=0; |
338 | input=0; |
||
339 | // recieve data-set |
||
340 | while ((input != 13)) // &&(input!=-1)) |
||
341 | { |
||
314 | ligi | 342 | |
164 | ligi | 343 | input = reader.read() ; |
314 | ligi | 344 | if (proxy!=null) |
345 | proxy.writer.write(input); |
||
164 | ligi | 346 | if (input==-1) throw new Exception("test"); |
347 | data_set[pos]=input; |
||
348 | pos++; |
||
349 | |||
350 | } |
||
314 | ligi | 351 | |
352 | if (proxy!=null) |
||
353 | { |
||
354 | proxy.writer.write('\r'); |
||
355 | proxy.writer.write('\n'); |
||
356 | proxy.writer.flush(); |
||
357 | } |
||
358 | |||
164 | ligi | 359 | process_data(data_set,pos); |
360 | |||
361 | } |
||
362 | catch (Exception ex) |
||
363 | { |
||
364 | msg="Problem reading from MK"; |
||
365 | // close the connection |
||
221 | ligi | 366 | close_connections(false); |
208 | ligi | 367 | |
368 | |||
164 | ligi | 369 | } |
370 | |||
371 | // sleep a bit to get someting more done |
||
372 | try { Thread.sleep(50); } |
||
373 | catch (Exception e) { } |
||
374 | |||
375 | } // while |
||
376 | |||
377 | |||
378 | } // run() |
||
379 | |||
380 | |||
381 | } |