Subversion Repositories Projects

Rev

Rev 82 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 82 Rev 135
Line 10... Line 10...
10
 *******************************************************************************************************************************/
10
 *******************************************************************************************************************************/
Line 11... Line 11...
11
 
11
 
12
import javax.microedition.io.*;
12
import javax.microedition.io.*;
Line -... Line 13...
-
 
13
import java.io.*;
13
import java.io.*;
14
 
14
 
15
 
15
public class MKCommunicator
16
public class MKCommunicator
16
    implements Runnable
17
    implements Runnable
17
{
18
{
18
    /***************** Section: public Attributes **********************************************/
19
    /***************** 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
 
20
    public boolean connected=false; // flag for the connection state
Line 22... Line 21...
22
 
21
    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.
23
    public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes
22
    public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes
24
   
23
   
-
 
24
    public MKLCD LCD;
-
 
25
    public MKVersion version;
-
 
26
    public MKDebugData debug_data;
25
    public MKLCD LCD;
27
 
26
    public MKVersion version;
28
    public MKGPSPosition gps_position;
27
    public MKDebugData debug_data;
29
 
28
    public MKStickData stick_data;
30
    public MKStickData stick_data;
29
    public MKParamsParser params;
31
    public MKParamsParser params;
30
    public MKWatchDog watchdog;
32
    public MKWatchDog watchdog;
Line 31... Line 33...
31
    public MKProxy proxy=null;
33
    public MKProxy proxy=null;
32
 
34
 
Line 41... Line 43...
41
   
43
   
42
    // temp - to be removed
44
    // temp - to be removed
43
    String p_msg="--";
45
    String p_msg="--";
Line -... Line 46...
-
 
46
    public String msg="BT_INIT";
-
 
47
 
-
 
48
 
44
    public String msg="BT_INIT";
49
 
-
 
50
    // for statistics
-
 
51
    public int debug_data_count=0;
45
 
52
    public int debug_names_count=0;
46
    public int debug_data_count=0;
53
    public int angle_data_count=0;
47
    public int version_data_count=0;
54
    public int version_data_count=0;
48
    public int other_data_count=0;
55
    public int other_data_count=0;
49
    public int lcd_data_count=0;
-
 
-
 
56
    public int lcd_data_count=0;
Line 50... Line 57...
50
    public int params_data_count=0;
57
    public int params_data_count=0;
51
 
58
    public int navi_data_count=0;
52
 
-
 
Line -... Line 59...
-
 
59
 
-
 
60
 
53
 
61
    String name;
54
    String name;
62
    DUBwise root;
55
    DUBwise root;
63
 
56
    DUBwiseDebug debug;
64
 
57
 
65
    DUBwiseDebug debug;
Line 64... Line 72...
64
        debug_data=new MKDebugData();
72
        debug_data=new MKDebugData();
65
        stick_data=new MKStickData();
73
        stick_data=new MKStickData();
66
        params=new MKParamsParser();
74
        params=new MKParamsParser();
67
        LCD= new MKLCD(this);
75
        LCD= new MKLCD(this);
68
        watchdog=new MKWatchDog(this);
76
        watchdog=new MKWatchDog(this);
-
 
77
        gps_position=new MKGPSPosition();
69
        new Thread( this ).start(); // fire up main Thread 
78
        new Thread( this ).start(); // fire up main Thread 
70
    }
79
    }
Line 71... Line 80...
71
 
80
 
72
 
81
 
73
    public void do_proxy(String proxy_url)
82
        public void do_proxy(String proxy_url)
74
    {
83
    {
-
 
84
        proxy=new MKProxy(proxy_url);
Line 75... Line 85...
75
        proxy=new MKProxy(proxy_url);
85
    }
76
    }
86
   
77
 
87
 
78
    //  URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control
88
    //  URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control
79
    public void connect_to(String _url,String _name)
89
    public void connect_to(String _url,String _name)
80
    {
90
    {
81
        mk_url=_url; // remember URL for connecting / reconnecting later
91
        mk_url=_url; // remember URL for connecting / reconnecting later
82
        name=_name;
92
        name=_name;
Line -... Line 93...
-
 
93
        force_disconnect=false;
-
 
94
        connected=false;
-
 
95
    }
-
 
96
 
83
        force_disconnect=false;
97
    public boolean ready()
84
        connected=false;
98
    {
85
    }
99
        return (connected&&(version.major!=-1));
86
 
100
    }
87
    /******************  Section: private Methods ************************************************/
101
    /******************  Section: private Methods ************************************************/
Line 93... Line 107...
93
            // old call
107
            // old call
94
            // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE);
108
            // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE);
95
            reader=connection.openInputStream();
109
            reader=connection.openInputStream();
96
            writer=connection.openOutputStream();
110
            writer=connection.openOutputStream();
Line 97... Line -...
97
 
-
 
98
 
111
 
99
            //
112
            //
100
            String magic="\rmk-mode\r";
113
            String magic="\rmk-mode\r";
101
            writer.write(magic.getBytes());
114
            writer.write(magic.getBytes());
102
            writer.flush();
115
            writer.flush();
Line 121... Line 134...
121
 
134
 
Line 122... Line -...
122
 
-
 
123
 
-
 
124
    }
135
 
125
 
136
 
126
   
137
    }
127
 
138
 
128
    public int[] Decode64(int[] in_arr, int offset,int len)
139
    public int[] Decode64(int[] in_arr, int offset,int len)
Line 129... Line 140...
129
    {
140
    {
Line 130... Line 141...
130
        int ptrIn=offset;      
141
        int ptrIn=offset;      
131
        int a,b,c,d,x,y,z;
142
        int a,b,c,d,x,y,z;
-
 
143
        int ptr=0;
-
 
144
       
-
 
145
        int[] out_arr=new int[len];
-
 
146
 
-
 
147
        while(len!=0)
132
        int ptr=0;
148
            {
133
       
149
                a=0;
134
        int[] out_arr=new int[len];
150
                b=0;
135
 
151
                c=0;
-
 
152
                d=0;
-
 
153
                try {
136
        while(len!=0)
154
                a = in_arr[ptrIn++] - '=';
Line 137... Line 155...
137
            {
155
                b = in_arr[ptrIn++] - '=';
138
                a = in_arr[ptrIn++] - '=';
156
                c = in_arr[ptrIn++] - '=';
139
                b = in_arr[ptrIn++] - '=';
157
                d = in_arr[ptrIn++] - '=';
Line 155... Line 173...
155
    }
173
    }
Line 156... Line 174...
156
 
174
 
Line 157... Line 175...
157
    // FC - Function Mappers
175
    // FC - Function Mappers
-
 
176
 
-
 
177
    // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives
-
 
178
    public void set_gps_target(int longitude,int latitude)
-
 
179
    {
-
 
180
        int[] target=new int[8];
-
 
181
        target[0]= (0xFF)&(longitude<<24);
-
 
182
        target[1]= (0xFF)&(longitude<<16);
-
 
183
        target[2]= (0xFF)&(longitude<<8);
-
 
184
        target[3]= (0xFF)&(longitude);
-
 
185
 
-
 
186
        send_command(0,'s',target);
-
 
187
    }
158
 
188
 
159
    // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives
189
 
160
    public void get_version()
190
    public void get_version()
161
    {
191
    {
Line 177... Line 207...
177
    public void get_params(int id)
207
    public void get_params(int id)
178
    {
208
    {
179
        int[] params=new int[1];
209
        int[] params=new int[1];
180
        params[0]=id;
210
        params[0]=id;
Line 181... Line 211...
181
 
211
 
182
        while(sending)
212
        while(sending||recieving)
183
            {try { Thread.sleep(50); }
213
            {try { Thread.sleep(50); }
184
            catch (Exception e)  {   }
214
            catch (Exception e)  {   }
Line 185... Line 215...
185
            }
215
            }
Line 191... Line 221...
191
    public void get_debug_name(int id)
221
    public void get_debug_name(int id)
192
    {
222
    {
193
        int[] params=new int[1];
223
        int[] params=new int[1];
194
        params[0]=id;
224
        params[0]=id;
Line 195... Line 225...
195
 
225
 
196
        while(sending)
226
        while(sending||recieving)
197
            {try { Thread.sleep(50); }
227
            {try { Thread.sleep(50); }
198
            catch (Exception e)  {   }
228
            catch (Exception e)  {   }
Line 199... Line 229...
199
            }
229
            }
Line 204... Line 234...
204
 
234
 
205
 
235
 
206
 
236
 
-
 
237
    public void trigger_LCD(int key)
-
 
238
    {
-
 
239
        while(sending||recieving)
Line -... Line 240...
-
 
240
            {try { Thread.sleep(50); }
Line 207... Line 241...
207
    public void trigger_LCD(int key)
241
            catch (Exception e)  {   }
208
    {
242
            }
209
        if (sending) return;
243
 
210
 
244
        //if (sending||recieving) return;
211
       
245
       
212
        int[] params=new int[3];
246
        int[] params=new int[3];
213
        params[0]=key;
-
 
214
        params[1]=0;
247
        params[0]=key;
Line 215... Line 248...
215
        params[2]=0;
248
        params[1]=0;
216
       
249
        params[2]=0;
217
        send_command(0,'h',params);
250
         
218
       
251
        send_command(0,'h',params);
219
    }
252
    }
220
 
253
 
Line 221... Line 254...
221
 
254
 
222
    public void write_params()
255
    public void write_params()
Line 223... Line 256...
223
    {
256
    {
-
 
257
        while(sending||recieving)
Line 224... Line 258...
224
        while(sending)
258
            {try { Thread.sleep(50); }
225
            {try { Thread.sleep(50); }
259
            catch (Exception e)  {   }
226
            catch (Exception e)  {   }
260
            }
-
 
261
 
227
            }
262
        send_command(0,(char)('l'+params.act_paramset),params.field[params.act_paramset]);
228
 
263
    }
229
        send_command(0,(char)('l'+params.act_paramset),params.field[params.act_paramset]);
264
 
230
    }
265
 
231
 
266
    boolean sending=false;
Line 283... Line 318...
283
 
318
 
284
        sending=false;
319
        sending=false;
Line -... Line 320...
-
 
320
    }
-
 
321
 
285
    }
322
 
286
 
323
    public int slave_addr=-1;
Line -... Line 324...
-
 
324
 
-
 
325
    public void process_data(int[] data,int len)
287
 
326
    {
288
    public void process_data(int[] data,int len)
327
 
Line 289... Line 328...
289
    {
328
        slave_addr=data[1];
290
 
329
 
291
        switch((char)data[2])
330
        switch((char)data[2])
292
            {
331
            {
Line 293... Line 332...
293
           
332
           
294
            case 'D': // debug Data
333
            case 'D': // debug Data
295
                debug_data_count++;
334
                debug_data_count++;
296
                debug_data.set_by_mk_data(Decode64(data,3,len-3),version);
335
                debug_data.set_by_mk_data(Decode64(data,3,len-3),version);
Line 297... Line 336...
297
                break;
336
                break;
298
 
337
 
Line 319... Line 358...
319
                String tmp_s="";
358
                String tmp_s="";
320
                for (int tmp_c=0;tmp_c<10;tmp_c++)
359
                for (int tmp_c=0;tmp_c<10;tmp_c++)
321
                    tmp_s+="s"+tmp_c+"v"+stick_data.stick[tmp_c]+" ";
360
                    tmp_s+="s"+tmp_c+"v"+stick_data.stick[tmp_c]+" ";
322
                debug.log(tmp_s);
361
                debug.log(tmp_s);
323
                break;
362
                break;
324
 
363
 
325
            case 'L':
364
            case 'L':
326
            case 'M':
365
            case 'M':
327
            case 'N':
366
            case 'N':
328
            case 'O':
367
            case 'O':
329
            case 'P':
368
            case 'P':
-
 
369
                //int[] foo=Decode64(data,3,len-3);
330
                params.set_by_mk_data((int)(data[2]-'L'),Decode64(data,3,len-3),version);
370
                params.set_by_mk_data((int)(data[2]-'L'),Decode64(data,3,len-3),version);
331
                params_data_count++;
371
                params_data_count++;
332
                break;
372
                break;
Line -... Line 373...
-
 
373
 
-
 
374
 
-
 
375
 
-
 
376
            // data from navi
-
 
377
            case 'Q':
-
 
378
                navi_data_count++;
-
 
379
                debug.log("got navi data(" + len +"):");
-
 
380
               
-
 
381
 
-
 
382
                gps_position.set_by_mk_data(Decode64(data,3,len-3),version);
-
 
383
 
-
 
384
                debug.log("long:" + gps_position.Longitude);
-
 
385
                debug.log("lat:" + gps_position.Latitude);
-
 
386
 
-
 
387
                root.canvas.ufo_prober.set_to_navi();
-
 
388
                break;
-
 
389
 
-
 
390
 
-
 
391
            case 'w':
-
 
392
                angle_data_count++;
-
 
393
                root.canvas.ufo_prober.set_to_mk();
-
 
394
                debug.log("got angle data:");
-
 
395
               
-
 
396
 
-
 
397
                break;
333
 
398
               
334
            default:
399
            default:
335
                other_data_count++;
400
                other_data_count++;
336
                debug.log("got other data:" + (char)data[2] + "=>" + (byte)data[2]);
-
 
Line -... Line 401...
-
 
401
                debug.log("got other data:"+ (char)data[2] + "=>" + (byte)data[2]);
337
 
402
 
338
 
403
                /*
339
                String tmp_str="";
404
                String tmp_str="";
340
                for (int tmp_i=0;tmp_i<len;tmp_i++)
405
                for (int tmp_i=0;tmp_i<len;tmp_i++)
341
                    tmp_str+=(char)data[tmp_i];
406
                    tmp_str+=(char)data[tmp_i];
Line 342... Line 407...
342
                debug.log(tmp_str);
407
                    debug.log(tmp_str);*/
Line 353... Line 418...
353
 
418
 
Line 354... Line 419...
354
    public boolean force_disconnect=true;
419
    public boolean force_disconnect=true;
355
 
420
 
356
    public void close_connections(boolean force)
421
    public void close_connections(boolean force)
357
    {
422
    {
358
        if ((!force)&&root.canvas.do_vibra) root.vibrate(500);
423
        //      if ((!force)&&root.canvas.do_vibra) root.vibrate(500);
359
        force_disconnect=force;
424
        force_disconnect=force;
Line 360... Line 425...
360
        try{ reader.close(); }
425
        try{ reader.close(); }
Line 370... Line 435...
370
    }
435
    }
Line 371... Line 436...
371
 
436
 
372
    // Thread to recieve data from Connection
437
    // Thread to recieve data from Connection
373
    public void run()
438
    public void run()
374
    {
439
    {
375
        int[] data_set=new int[150];
440
        int[] data_set=new int[300];
376
        int input;
441
        int input;
-
 
442
        int pos=0;
377
        int pos=0;
443
 
378
        msg+="!!run started!!";
444
        debug.log("Thread started");
379
        while(true)
445
        while(true)
-
 
446
            {
380
            {
447
                debug.log("Connection Thread run");
381
                if (!connected)
448
                if (!connected)
382
                    {
449
                    {
383
                        if (!force_disconnect) connect();
450
                        if (!force_disconnect) connect();
384
                    }
451
                    }
385
                else
452
                else
-
 
453
                    try{
386
                    try{
454
 
-
 
455
                       
-
 
456
                        while(sending)
-
 
457
                            {try { Thread.sleep(50); }
-
 
458
                            catch (Exception e)  {   }
-
 
459
                            }
-
 
460
 
-
 
461
                       
-
 
462
 
-
 
463
                        recieving=true;
387
               
464
                        debug.log("Connected - reading data");         
388
                        pos=0;
465
                        pos=0;
389
                        input=0;
466
                        input=0;
390
                        // recieve data-set
467
                        // recieve data-set
391
                        while ((input != 13)) // &&(input!=-1))
468
                        while ((input != 13)) //&&(input!=-1))
Line 392... Line 469...
392
                            {
469
                            {
393
 
470
 
394
                                input = reader.read() ;
471
                                input = reader.read() ;
395
                                if (proxy!=null)
472
                                if (proxy!=null)
396
                                    proxy.writer.write(input);
473
                                    proxy.writer.write(input);
397
                                //                              if (input==-1) throw new Exception("test");
474
                                if (input==-1) throw new Exception("test");
398
                                if (input!=-1)
475
                                if (input!=-1)
399
                                    {
476
                                    {
400
                                        data_set[pos]=input;
477
                                        data_set[pos]=input;
-
 
478
                                        pos++;
Line 401... Line 479...
401
                                        pos++;
479
                                    }
-
 
480
       
-
 
481
     
402
                                    }
482
                            }
403
     
483
                        recieving=false;
404
                            }
484
                        debug.log("Data recieved (" + pos + "Bytes) - processing ..");         
405
 
485
                        /*
406
                        if (proxy!=null)
486
                        if (proxy!=null)
407
                            {
487
                            {
408
                                proxy.writer.write('\r');
488
                                proxy.writer.write('\r');
409
                                proxy.writer.write('\n');
489
                                proxy.writer.write('\n');
410
                                proxy.writer.flush();
490
                                proxy.writer.flush();
411
                            }
491
                            }
412
 
492
                        */
-
 
493
                        if (pos>5)
-
 
494
                            process_data(data_set,pos);
413
                        if (pos>5)
495
                   
414
                            process_data(data_set,pos);
496
                        debug.log("Processing done");          
415
                   
497
 
416
                    }
498
                    }
417
                    catch (Exception ex)
499
                    catch (Exception ex)
Line 422... Line 504...
422
                            close_connections(false);
504
                            close_connections(false);
Line 423... Line 505...
423
 
505
 
Line 424... Line 506...
424
 
506
 
425
                        }      
507
                        }      
426
   
508
   
Line 427... Line 509...
427
                // sleep a bit to get someting more done
509
                // sleep a bit to  get someting more done
Line -... Line 510...
-
 
510
                try { Thread.sleep(50); }
-
 
511
                catch (Exception e)  {   }
Line 428... Line 512...
428
                try { Thread.sleep(50); }
512
       
Line 429... Line 513...
429
                catch (Exception e)  {   }
513
            } // while