Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
231 | ligi | 1 | /*************************************************************** |
2 | * |
||
3 | * minimal canvas to test Abstraction layer on various Phones |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * Project-Start: 9/2007 |
||
7 | * Mailto: ligi@smart4mobile.de |
||
8 | * Licence: Creative Commons / Non Commercial |
||
9 | * Big Up: Holger&Ingo |
||
10 | ***************************************************************/ |
||
181 | ligi | 11 | |
12 | import javax.microedition.lcdui.*; |
||
208 | ligi | 13 | import javax.microedition.rms.*; |
181 | ligi | 14 | |
221 | ligi | 15 | |
181 | ligi | 16 | public class MKMiniCanvas |
231 | ligi | 17 | extends Canvas |
18 | implements Runnable |
||
181 | ligi | 19 | { |
20 | |||
21 | |||
208 | ligi | 22 | private BTSearcher bt_scanner; |
23 | private MKCommunicator mk=null; |
||
24 | private MKStatistics mk_stat=null; |
||
25 | private String err=""; |
||
26 | private MKMiniMidlet root; |
||
181 | ligi | 27 | |
221 | ligi | 28 | private Image bg_img; |
29 | private Image bt_img; |
||
30 | private Image lcd_img; |
||
31 | private Image load_img; |
||
32 | |||
33 | |||
34 | public int[] nick_line_pos_data; |
||
35 | public int[] roll_line_pos_data; |
||
36 | |||
37 | public int[] accnick_line_pos_data; |
||
38 | public int[] accroll_line_pos_data; |
||
39 | |||
40 | |||
41 | public int frame_pos=0; |
||
42 | |||
43 | int line_scaler=20; |
||
44 | |||
45 | public int line_middle_y; |
||
46 | |||
208 | ligi | 47 | public MKMiniCanvas(MKMiniMidlet _root) |
231 | ligi | 48 | { |
221 | ligi | 49 | |
231 | ligi | 50 | root=_root; |
181 | ligi | 51 | |
231 | ligi | 52 | try |
53 | { |
||
54 | lcd_img=Image.createImage("/lcd.png"); |
||
55 | bt_img=Image.createImage("/bt.png"); |
||
56 | bg_img=Image.createImage("/bg.jpg"); |
||
57 | load_img=Image.createImage("/load.png"); |
||
58 | } |
||
59 | catch (Exception e) |
||
60 | { |
||
61 | err+=e.toString(); |
||
62 | } |
||
221 | ligi | 63 | |
64 | |||
231 | ligi | 65 | nick_line_pos_data=new int[bg_img.getWidth()]; |
66 | roll_line_pos_data=new int[bg_img.getWidth()]; |
||
67 | accnick_line_pos_data=new int[bg_img.getWidth()]; |
||
68 | accroll_line_pos_data=new int[bg_img.getWidth()]; |
||
221 | ligi | 69 | |
231 | ligi | 70 | for (int c=0;c<bg_img.getWidth();c++) |
71 | { |
||
72 | nick_line_pos_data[c]=-1; |
||
73 | roll_line_pos_data[c]=-1; |
||
74 | accnick_line_pos_data[c]=-1; |
||
75 | accroll_line_pos_data[c]=-1; |
||
76 | } |
||
221 | ligi | 77 | |
78 | |||
231 | ligi | 79 | // mk = new MKCommunicator("btspp://000BCE016B5B:1"); |
80 | //connect_mk("btspp://000BCE016B4F:1"); |
||
181 | ligi | 81 | |
208 | ligi | 82 | |
231 | ligi | 83 | try |
84 | { |
||
85 | RecordStore recStore = RecordStore.openRecordStore("bturl", true ); |
||
181 | ligi | 86 | |
231 | ligi | 87 | if (recStore.getNumRecords()==1) |
208 | ligi | 88 | { |
231 | ligi | 89 | byte[] recData = new byte[recStore.getRecordSize(1)]; |
90 | int len = recStore.getRecord(1, recData, 0); |
||
91 | rescan=false; |
||
92 | connect_mk(new String(recData, 0, len)); |
||
208 | ligi | 93 | } |
231 | ligi | 94 | } |
95 | catch (Exception e) |
||
96 | { |
||
97 | err+=e.toString(); |
||
98 | } |
||
181 | ligi | 99 | |
231 | ligi | 100 | if (mk==null) bt_scanner = new BTSearcher(); |
208 | ligi | 101 | |
231 | ligi | 102 | new Thread(this).start(); |
181 | ligi | 103 | |
231 | ligi | 104 | } |
181 | ligi | 105 | |
208 | ligi | 106 | boolean quit=false; |
107 | boolean rescan=true; |
||
221 | ligi | 108 | int bg_offset=0; |
109 | |||
181 | ligi | 110 | |
111 | |||
221 | ligi | 112 | |
181 | ligi | 113 | /********************************************************** Thread ***********************************************************/ |
114 | // ticking runnable Section |
||
115 | public void run() |
||
116 | { |
||
117 | |||
118 | while(true) |
||
119 | { |
||
231 | ligi | 120 | repaint(); |
121 | serviceRepaints(); |
||
122 | |||
181 | ligi | 123 | long loopStartTime = System.currentTimeMillis(); |
124 | long sleeptime=0; |
||
125 | // ticked thing |
||
221 | ligi | 126 | |
127 | frame_pos++; |
||
128 | |||
231 | ligi | 129 | |
130 | |||
131 | switch(state) |
||
132 | { |
||
133 | case STATEID_MOTORTEST: |
||
134 | |||
135 | if (motor_test_sel_all) |
||
136 | for (int m=0;m<4;m++) |
||
137 | { |
||
138 | motor_test[m]+=act_motor_increase; |
||
139 | if (motor_test[m]<0)motor_test[m]=0; |
||
140 | } |
||
141 | else |
||
142 | { |
||
143 | motor_test[act_motor]+=act_motor_increase; |
||
144 | if (motor_test[act_motor]<0)motor_test[act_motor]=0; |
||
145 | } |
||
146 | |||
147 | mk.motor_test(motor_test); |
||
148 | break; |
||
149 | } |
||
150 | |||
221 | ligi | 151 | try { |
231 | ligi | 152 | nick_line_pos_data[-bg_offset] = mk.debug_data.nick_int(); |
153 | roll_line_pos_data[-bg_offset] = mk.debug_data.roll_int(); |
||
154 | accnick_line_pos_data[-bg_offset] = mk.debug_data.accnick(); |
||
155 | accroll_line_pos_data[-bg_offset] = mk.debug_data.accroll(); |
||
221 | ligi | 156 | } |
157 | catch (Exception e) |
||
158 | { |
||
159 | err+=e.toString(); |
||
160 | } |
||
161 | |||
181 | ligi | 162 | |
221 | ligi | 163 | |
164 | |||
208 | ligi | 165 | if (quit) |
166 | { |
||
221 | ligi | 167 | |
168 | |||
169 | |||
208 | ligi | 170 | try |
171 | { |
||
172 | RecordStore recStore = RecordStore.openRecordStore("bturl", true ); |
||
173 | |||
174 | if (recStore.getNumRecords()==1) |
||
175 | recStore.deleteRecord(1); |
||
176 | byte[] rec = mk.mk_url.getBytes(); |
||
177 | recStore.addRecord(rec, 0, rec.length); |
||
178 | |||
179 | } |
||
180 | catch (Exception e) |
||
181 | { |
||
182 | err+=e.toString(); |
||
183 | } |
||
184 | |||
185 | root.quit(); |
||
186 | } |
||
187 | if (rescan) |
||
188 | { |
||
231 | ligi | 189 | if (mk!=null) mk.close_connections(true); |
190 | mk=null; |
||
191 | bt_scanner.search_again(); |
||
208 | ligi | 192 | } |
193 | |||
231 | ligi | 194 | try { |
195 | rescan=false; |
||
196 | bg_offset--; |
||
197 | if (bg_offset==-bg_img.getWidth()) |
||
198 | bg_offset=0; |
||
199 | com.nokia.mid.ui.DeviceControl.setLights(0,100); |
||
200 | //bt.tick(); |
||
201 | // every state has sth to do in tick section |
||
202 | } |
||
203 | catch (Exception e) |
||
204 | { |
||
205 | |||
206 | } |
||
181 | ligi | 207 | |
208 | |||
231 | ligi | 209 | // System.gc(); |
210 | |||
211 | |||
181 | ligi | 212 | |
213 | |||
214 | sleeptime=1000/ 15 - (int) (System.currentTimeMillis()- loopStartTime); |
||
215 | |||
216 | |||
217 | if (sleeptime<0) |
||
231 | ligi | 218 | sleeptime=100; // everyone has fi sleep |
181 | ligi | 219 | |
220 | try { Thread.sleep(sleeptime); } |
||
221 | catch (Exception e) |
||
222 | { |
||
231 | ligi | 223 | err="Problem Sleeping "; |
181 | ligi | 224 | } |
225 | |||
226 | } |
||
227 | } |
||
228 | |||
229 | |||
230 | |||
231 | ligi | 231 | public int state=-1; |
232 | |||
233 | public final static int STATEID_MOTORTEST=2; |
||
234 | |||
235 | |||
221 | ligi | 236 | int local_max=-1; |
181 | ligi | 237 | |
231 | ligi | 238 | int[] motor_test = {0,0,0,0}; |
239 | |||
181 | ligi | 240 | // drawing section |
241 | public void paint(Graphics g) { |
||
231 | ligi | 242 | |
243 | try { |
||
221 | ligi | 244 | |
231 | ligi | 245 | if (mk!=null) |
246 | { |
||
247 | line_middle_y=this.getHeight()/2; |
||
248 | if (local_max<Math.abs(mk.debug_data.nick_int())) |
||
249 | local_max=Math.abs(mk.debug_data.nick_int()); |
||
250 | if (local_max<Math.abs(mk.debug_data.roll_int())) |
||
251 | local_max=Math.abs(mk.debug_data.roll_int()); |
||
252 | if (local_max<Math.abs(mk.debug_data.accnick())) |
||
253 | local_max=Math.abs(mk.debug_data.accnick()); |
||
254 | if (local_max<Math.abs(mk.debug_data.accroll())) |
||
255 | local_max=Math.abs(mk.debug_data.accroll()); |
||
256 | line_scaler= local_max/(this.getHeight()/2)+1; |
||
257 | } |
||
258 | int spacer=(g.getFont().getHeight()); |
||
259 | g.setColor(0xFFFFFF); |
||
260 | g.fillRect(0,0,this.getWidth(),this.getHeight()); |
||
261 | g.drawImage(bg_img,bg_offset,0, g.TOP | g.LEFT); |
||
221 | ligi | 262 | |
231 | ligi | 263 | if (bg_offset+bg_img.getWidth()<this.getWidth()) |
264 | g.drawImage(bg_img,bg_offset+bg_img.getWidth(),0, g.TOP | g.LEFT); |
||
265 | |||
266 | |||
267 | |||
268 | |||
269 | switch(state) |
||
270 | { |
||
271 | case STATEID_MOTORTEST: |
||
272 | for (int bar=0;bar<4;bar++) |
||
273 | { |
||
274 | g.setColor(((bar==act_motor)|motor_test_sel_all)?0x44CC44:0x4444DD); |
||
275 | g.fillRect(this.getWidth()/(8*2)+bar*2*this.getWidth()/8,10,this.getWidth()/8,20+motor_test[bar]); |
||
276 | g.setColor(0x000000); |
||
277 | g.drawString(""+motor_test[bar] ,this.getWidth()/8+bar*2*this.getWidth()/8,10,Graphics.TOP | Graphics.HCENTER); |
||
278 | if(bar!=4) g.drawString(""+mk.debug_data.motor_val(bar) ,this.getWidth()/8+bar*2*this.getWidth()/8,30,Graphics.TOP | Graphics.HCENTER); |
||
279 | } |
||
280 | break; |
||
281 | |||
282 | default: |
||
283 | g.setColor(0x000000); |
||
181 | ligi | 284 | |
231 | ligi | 285 | if (mk==null) |
286 | { |
||
221 | ligi | 287 | |
288 | |||
231 | ligi | 289 | if (bt_scanner.searching) |
290 | { |
||
221 | ligi | 291 | |
292 | |||
231 | ligi | 293 | g.drawString("scanning for BT-Devices",this.getWidth()/2,this.getHeight()/2+load_img.getHeight()/8,Graphics.TOP | Graphics.HCENTER); |
221 | ligi | 294 | |
231 | ligi | 295 | g.setClip(this.getWidth()/2-load_img.getWidth()/6,this.getHeight()/2-load_img.getHeight()/8, load_img.getWidth()/4,load_img.getHeight()/3);; |
296 | g.drawImage(load_img,this.getWidth()/2-load_img.getWidth()/8 - ((((frame_pos/3)%12)%4)*(load_img.getWidth()/4)) ,this.getHeight()/2-load_img.getHeight()/6- ((((frame_pos/3)%12)/4)*(load_img.getHeight()/3)), g.TOP | g.LEFT); |
||
297 | g.drawImage(bt_img,this.getWidth()/2 ,this.getHeight()/2 , g.HCENTER | g.VCENTER); |
||
298 | } |
||
299 | else |
||
300 | { |
||
301 | g.drawString("Press key 0-"+(bt_scanner.remote_device_count-1) + " to continue" ,0,0,Graphics.TOP | Graphics.LEFT); |
||
302 | for (int i=0;i<bt_scanner.remote_device_count;i++) |
||
303 | g.drawString("#"+i+">" + bt_scanner.remote_device_name[i]+"("+bt_scanner.remote_device_mac[i]+")",0,spacer+spacer*i,Graphics.TOP | Graphics.LEFT); |
||
221 | ligi | 304 | |
231 | ligi | 305 | } |
221 | ligi | 306 | |
307 | |||
308 | |||
309 | |||
231 | ligi | 310 | } |
311 | else // MK is connected |
||
312 | { |
||
313 | int y_off=0; |
||
208 | ligi | 314 | |
231 | ligi | 315 | g.drawString(mk.version.str+"(d:"+mk.debug_data_count+ " l:" + mk.lcd_data_count+ " v:" + mk.version_data_count+" o:"+mk.other_data_count+")",0,y_off,Graphics.TOP | Graphics.LEFT); |
208 | ligi | 316 | |
231 | ligi | 317 | y_off+=spacer; |
221 | ligi | 318 | |
231 | ligi | 319 | g.drawString("n:"+mk.debug_data.nick_int() + " r:"+mk.debug_data.roll_int() + " an:"+mk.debug_data.accnick() + " ar:"+mk.debug_data.accroll() ,0,y_off,Graphics.TOP | Graphics.LEFT); |
320 | y_off+=spacer; |
||
221 | ligi | 321 | |
322 | |||
231 | ligi | 323 | g.drawString("m1:"+mk.debug_data.motor_val(0) + " m2:"+mk.debug_data.motor_val(1)+" m3:"+mk.debug_data.motor_val(2) + " m4:"+mk.debug_data.motor_val(3) ,0,y_off,Graphics.TOP | Graphics.LEFT); |
324 | y_off+=spacer; |
||
221 | ligi | 325 | |
231 | ligi | 326 | if (mk.connected) |
327 | { |
||
328 | g.drawString("time conn:" +((System.currentTimeMillis()- mk.connection_start_time)/1000)+"s" ,0,y_off,Graphics.TOP | Graphics.LEFT); |
||
329 | y_off+=spacer; |
||
330 | g.drawString("time motor>15:" +(mk_stat.motor_on_time/1000) +"s" ,0,y_off,Graphics.TOP | Graphics.LEFT); |
||
331 | y_off+=spacer; |
||
332 | g.drawString("time motor=15:" +(mk_stat.motor_stand_time/1000) +"s" ,0,y_off,Graphics.TOP | Graphics.LEFT); |
||
333 | } |
||
181 | ligi | 334 | |
221 | ligi | 335 | |
231 | ligi | 336 | y_off=this.getHeight()-4*lcd_img.getHeight(); |
221 | ligi | 337 | |
231 | ligi | 338 | for ( int foo=0;foo<4;foo++) |
339 | { |
||
340 | for (int x=0;x<20;x++) |
||
341 | { |
||
342 | g.setClip((lcd_img.getWidth()/222)*x,y_off,(lcd_img.getWidth()/222),lcd_img.getHeight()); |
||
343 | g.drawImage(lcd_img,(lcd_img.getWidth()/222)*x-(mk.LCD.LCD_str[foo].charAt(x)-' ')*(lcd_img.getWidth()/222),y_off, g.TOP | g.LEFT); |
||
221 | ligi | 344 | |
231 | ligi | 345 | } |
346 | y_off+=lcd_img.getHeight(); |
||
347 | } |
||
221 | ligi | 348 | |
231 | ligi | 349 | g.setClip(0,0,this.getWidth(),this.getHeight()); |
221 | ligi | 350 | |
351 | |||
352 | |||
353 | |||
231 | ligi | 354 | } |
355 | // draw lines |
||
221 | ligi | 356 | |
231 | ligi | 357 | for ( int x=0;x<this.getWidth();x++) |
221 | ligi | 358 | |
231 | ligi | 359 | { |
221 | ligi | 360 | |
231 | ligi | 361 | int p= (((-bg_offset+x-this.getWidth()-5))); |
362 | if (p<1) |
||
363 | p+=bg_img.getWidth(); |
||
364 | p%=(bg_img.getWidth()-1); |
||
221 | ligi | 365 | |
231 | ligi | 366 | g.setColor(0x156315); |
367 | draw_graph_part(g,x,nick_line_pos_data[p]/line_scaler,nick_line_pos_data[p+1]/line_scaler); |
||
368 | g.setColor(0xCC1315); |
||
369 | draw_graph_part(g,x,roll_line_pos_data[p]/line_scaler,roll_line_pos_data[p+1]/line_scaler); |
||
370 | g.setColor(0xf8ef02); |
||
371 | draw_graph_part(g,x,accnick_line_pos_data[p]/line_scaler,accnick_line_pos_data[p+1]/line_scaler); |
||
372 | g.setColor(0x19194d); |
||
373 | draw_graph_part(g,x,accroll_line_pos_data[p]/line_scaler,accroll_line_pos_data[p+1]/line_scaler); |
||
221 | ligi | 374 | |
375 | |||
181 | ligi | 376 | |
231 | ligi | 377 | } |
378 | |||
379 | } |
||
380 | |||
381 | } catch (Exception e) {} |
||
181 | ligi | 382 | } |
383 | |||
208 | ligi | 384 | private void connect_mk(String url) |
385 | { |
||
386 | mk = new MKCommunicator(url); |
||
387 | mk_stat= new MKStatistics(mk); |
||
221 | ligi | 388 | |
208 | ligi | 389 | } |
390 | |||
221 | ligi | 391 | public void draw_graph_part(Graphics g,int x,int y1,int y2) |
392 | { |
||
393 | g.fillRect(x,line_middle_y-y1,2,2 ); |
||
394 | if (y1>y2) |
||
395 | g.fillRect(x,line_middle_y-y1,2,y1-y2); |
||
396 | else |
||
397 | g.fillRect(x,line_middle_y-y2,2,y2-y1); |
||
398 | } |
||
399 | |||
181 | ligi | 400 | /*********************************************** input Section **********************************************/ |
401 | // keys |
||
221 | ligi | 402 | public boolean fullscreen=false; |
231 | ligi | 403 | public int act_motor=0; |
404 | public int act_motor_increase=0; |
||
405 | public boolean motor_test_sel_all=false; |
||
221 | ligi | 406 | |
231 | ligi | 407 | public void chg_state(int _state) |
408 | { |
||
409 | state=_state; |
||
410 | } |
||
411 | |||
412 | |||
413 | public void keyReleased(int keyCode) |
||
414 | { |
||
415 | |||
416 | switch(state) |
||
417 | { |
||
418 | case STATEID_MOTORTEST: |
||
419 | act_motor_increase=0; |
||
420 | break; |
||
421 | } |
||
422 | |||
423 | } |
||
424 | |||
181 | ligi | 425 | public void keyPressed(int keyCode) |
426 | { |
||
427 | |||
231 | ligi | 428 | switch(state) |
208 | ligi | 429 | { |
231 | ligi | 430 | case STATEID_MOTORTEST: |
431 | switch (getGameAction (keyCode)) |
||
432 | { |
||
433 | case UP: |
||
434 | act_motor_increase=-1; |
||
435 | break; |
||
221 | ligi | 436 | |
231 | ligi | 437 | case DOWN: |
438 | act_motor_increase=1; |
||
439 | break; |
||
208 | ligi | 440 | |
231 | ligi | 441 | case FIRE: |
442 | motor_test_sel_all=!motor_test_sel_all; |
||
443 | break; |
||
444 | |||
445 | case LEFT: |
||
446 | act_motor--; |
||
447 | if (act_motor<0) {act_motor=0; chg_state(-1); } |
||
448 | break; |
||
449 | |||
450 | case RIGHT: |
||
451 | act_motor++; |
||
452 | act_motor%=4; |
||
453 | break; |
||
454 | } |
||
455 | |||
456 | break; |
||
457 | |||
458 | default: |
||
459 | if ((mk==null)&&(keyCode >= this.KEY_NUM0) && (keyCode < this.KEY_NUM0+bt_scanner.remote_device_count)) |
||
460 | connect_mk("btspp://"+bt_scanner.remote_device_mac[keyCode-this.KEY_NUM0] + ":1"); |
||
461 | else |
||
208 | ligi | 462 | { |
231 | ligi | 463 | switch (keyCode) |
464 | { |
||
208 | ligi | 465 | |
231 | ligi | 466 | case KEY_STAR: |
467 | mk.other_data_count=23; |
||
468 | quit=true; |
||
469 | break; |
||
470 | |||
471 | case KEY_POUND: |
||
472 | //rescan=true; |
||
473 | fullscreen=!fullscreen; |
||
474 | setFullScreenMode(fullscreen); |
||
475 | |||
476 | break; |
||
477 | } |
||
478 | switch (getGameAction (keyCode)) |
||
479 | { |
||
480 | case UP: |
||
481 | mk.LCD.LCD_PREVPAGE(); |
||
482 | break; |
||
483 | |||
484 | case DOWN: |
||
485 | mk.LCD.LCD_NEXTPAGE(); |
||
486 | break; |
||
487 | |||
488 | case RIGHT: |
||
489 | state=STATEID_MOTORTEST; |
||
490 | break; |
||
491 | } |
||
208 | ligi | 492 | } |
231 | ligi | 493 | |
208 | ligi | 494 | } |
495 | |||
496 | return; |
||
181 | ligi | 497 | } |
498 | |||
499 | |||
500 | |||
501 | |||
502 | |||
503 | |||
504 | |||
505 | |||
506 | } |
||
507 | |||
508 |