Rev 210 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
181 | 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 | *********************************************************************************************************************************/ |
||
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 |
17 | extends Canvas |
||
18 | implements Runnable |
||
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) |
48 | { |
||
221 | ligi | 49 | |
208 | ligi | 50 | root=_root; |
181 | ligi | 51 | |
221 | 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 | } |
||
63 | |||
64 | |||
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()]; |
||
69 | |||
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 | } |
||
77 | |||
78 | |||
208 | ligi | 79 | // mk = new MKCommunicator("btspp://000BCE016B5B:1"); |
80 | //connect_mk("btspp://000BCE016B4F:1"); |
||
181 | ligi | 81 | |
208 | ligi | 82 | |
83 | try |
||
84 | { |
||
85 | RecordStore recStore = RecordStore.openRecordStore("bturl", true ); |
||
181 | ligi | 86 | |
208 | ligi | 87 | if (recStore.getNumRecords()==1) |
88 | { |
||
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)); |
||
93 | } |
||
94 | } |
||
95 | catch (Exception e) |
||
96 | { |
||
97 | err+=e.toString(); |
||
98 | } |
||
181 | ligi | 99 | |
208 | ligi | 100 | if (mk==null) bt_scanner = new BTSearcher(); |
101 | |||
102 | new Thread(this).start(); |
||
181 | ligi | 103 | |
104 | } |
||
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 | { |
||
120 | long loopStartTime = System.currentTimeMillis(); |
||
121 | long sleeptime=0; |
||
122 | // ticked thing |
||
221 | ligi | 123 | |
124 | frame_pos++; |
||
125 | |||
126 | try { |
||
127 | nick_line_pos_data[-bg_offset] = mk.debug_data.nick_int(); |
||
128 | roll_line_pos_data[-bg_offset] = mk.debug_data.roll_int(); |
||
129 | accnick_line_pos_data[-bg_offset] = mk.debug_data.accnick(); |
||
130 | accroll_line_pos_data[-bg_offset] = mk.debug_data.accroll(); |
||
131 | } |
||
132 | catch (Exception e) |
||
133 | { |
||
134 | err+=e.toString(); |
||
135 | } |
||
136 | |||
181 | ligi | 137 | |
221 | ligi | 138 | |
139 | |||
208 | ligi | 140 | if (quit) |
141 | { |
||
221 | ligi | 142 | |
143 | |||
144 | |||
208 | ligi | 145 | try |
146 | { |
||
147 | RecordStore recStore = RecordStore.openRecordStore("bturl", true ); |
||
148 | |||
149 | if (recStore.getNumRecords()==1) |
||
150 | recStore.deleteRecord(1); |
||
151 | byte[] rec = mk.mk_url.getBytes(); |
||
152 | recStore.addRecord(rec, 0, rec.length); |
||
153 | |||
154 | } |
||
155 | catch (Exception e) |
||
156 | { |
||
157 | err+=e.toString(); |
||
158 | } |
||
159 | |||
160 | root.quit(); |
||
161 | } |
||
162 | if (rescan) |
||
163 | { |
||
221 | ligi | 164 | if (mk!=null) mk.close_connections(true); |
208 | ligi | 165 | mk=null; |
166 | bt_scanner.search_again(); |
||
167 | } |
||
168 | |||
169 | rescan=false; |
||
221 | ligi | 170 | bg_offset--; |
171 | if (bg_offset==-bg_img.getWidth()) |
||
172 | bg_offset=0; |
||
181 | ligi | 173 | com.nokia.mid.ui.DeviceControl.setLights(0,100); |
174 | //bt.tick(); |
||
175 | // every state has sth to do in tick section |
||
176 | |||
177 | |||
178 | // System.gc(); |
||
179 | repaint(); |
||
180 | serviceRepaints(); |
||
181 | |||
182 | |||
183 | sleeptime=1000/ 15 - (int) (System.currentTimeMillis()- loopStartTime); |
||
184 | |||
185 | |||
186 | if (sleeptime<0) |
||
187 | sleeptime=100; // everyone has fi sleep |
||
188 | |||
189 | try { Thread.sleep(sleeptime); } |
||
190 | catch (Exception e) |
||
191 | { |
||
192 | err="Problem Sleeping "; |
||
193 | } |
||
194 | |||
195 | } |
||
196 | } |
||
197 | |||
198 | |||
199 | |||
200 | |||
221 | ligi | 201 | int local_max=-1; |
181 | ligi | 202 | |
203 | // drawing section |
||
204 | public void paint(Graphics g) { |
||
221 | ligi | 205 | |
206 | if (mk!=null) |
||
207 | { |
||
208 | line_middle_y=this.getHeight()/2; |
||
209 | if (local_max<Math.abs(mk.debug_data.nick_int())) |
||
210 | local_max=Math.abs(mk.debug_data.nick_int()); |
||
211 | if (local_max<Math.abs(mk.debug_data.roll_int())) |
||
212 | local_max=Math.abs(mk.debug_data.roll_int()); |
||
213 | if (local_max<Math.abs(mk.debug_data.accnick())) |
||
214 | local_max=Math.abs(mk.debug_data.accnick()); |
||
215 | if (local_max<Math.abs(mk.debug_data.accroll())) |
||
216 | local_max=Math.abs(mk.debug_data.accroll()); |
||
217 | line_scaler= local_max/(this.getHeight()/2)+1; |
||
218 | } |
||
208 | ligi | 219 | int spacer=(g.getFont().getHeight()); |
181 | ligi | 220 | g.setColor(0xFFFFFF); |
221 | g.fillRect(0,0,this.getWidth(),this.getHeight()); |
||
221 | ligi | 222 | g.drawImage(bg_img,bg_offset,0, g.TOP | g.LEFT); |
223 | if (bg_offset+bg_img.getWidth()<this.getWidth()) |
||
224 | g.drawImage(bg_img,bg_offset+bg_img.getWidth(),0, g.TOP | g.LEFT); |
||
225 | |||
181 | ligi | 226 | g.setColor(0x000000); |
227 | |||
228 | if (mk==null) |
||
229 | { |
||
221 | ligi | 230 | |
231 | |||
232 | if (bt_scanner.searching) |
||
233 | { |
||
234 | |||
235 | |||
236 | g.drawString("scanning for BT-Devices",this.getWidth()/2,this.getHeight()/2+load_img.getHeight()/8,Graphics.TOP | Graphics.HCENTER); |
||
237 | |||
238 | g.setClip(this.getWidth()/2-load_img.getWidth()/6,this.getHeight()/2-load_img.getHeight()/8, load_img.getWidth()/4,load_img.getHeight()/3);; |
||
239 | 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); |
||
240 | g.drawImage(bt_img,this.getWidth()/2 ,this.getHeight()/2 , g.HCENTER | g.VCENTER); |
||
241 | } |
||
242 | else |
||
243 | { |
||
244 | g.drawString("Press key 0-"+(bt_scanner.remote_device_count-1) + " to continue" ,0,0,Graphics.TOP | Graphics.LEFT); |
||
245 | for (int i=0;i<bt_scanner.remote_device_count;i++) |
||
246 | g.drawString("#"+i+">" + bt_scanner.remote_device_name[i]+"("+bt_scanner.remote_device_mac[i]+")",0,spacer+spacer*i,Graphics.TOP | Graphics.LEFT); |
||
247 | |||
248 | } |
||
249 | |||
250 | |||
251 | |||
252 | |||
181 | ligi | 253 | } |
254 | else // MK is connected |
||
255 | { |
||
256 | int y_off=0; |
||
208 | ligi | 257 | |
210 | ligi | 258 | 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 | 259 | |
181 | ligi | 260 | y_off+=spacer; |
221 | ligi | 261 | |
262 | 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); |
||
181 | ligi | 263 | y_off+=spacer; |
221 | ligi | 264 | |
265 | |||
266 | 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); |
||
181 | ligi | 267 | y_off+=spacer; |
221 | ligi | 268 | |
181 | ligi | 269 | if (mk.connected) |
270 | { |
||
271 | g.drawString("time conn:" +((System.currentTimeMillis()- mk.connection_start_time)/1000)+"s" ,0,y_off,Graphics.TOP | Graphics.LEFT); |
||
272 | y_off+=spacer; |
||
273 | g.drawString("time motor>15:" +(mk_stat.motor_on_time/1000) +"s" ,0,y_off,Graphics.TOP | Graphics.LEFT); |
||
208 | ligi | 274 | y_off+=spacer; |
275 | g.drawString("time motor=15:" +(mk_stat.motor_stand_time/1000) +"s" ,0,y_off,Graphics.TOP | Graphics.LEFT); |
||
181 | ligi | 276 | } |
277 | |||
221 | ligi | 278 | |
279 | y_off=this.getHeight()-4*19; |
||
280 | |||
281 | for ( int foo=0;foo<4;foo++) |
||
282 | { |
||
283 | |||
284 | for (int x=0;x<20;x++) |
||
285 | { |
||
286 | g.setClip(12*x,y_off,12,19); |
||
287 | g.drawImage(lcd_img,12*x-(mk.LCD.LCD_str[foo].charAt(x)-' ')*(lcd_img.getWidth()/95),y_off, g.TOP | g.LEFT); |
||
288 | |||
289 | } |
||
290 | y_off+=19; |
||
291 | |||
292 | |||
293 | |||
294 | } |
||
295 | |||
296 | g.setClip(0,0,this.getWidth(),this.getHeight()); |
||
297 | |||
298 | |||
299 | |||
300 | |||
181 | ligi | 301 | } |
221 | ligi | 302 | // draw lines |
303 | |||
304 | for ( int x=0;x<this.getWidth();x++) |
||
305 | |||
306 | { |
||
307 | |||
308 | int p= (((-bg_offset+x-this.getWidth()-5))); |
||
309 | if (p<1) |
||
310 | p+=bg_img.getWidth(); |
||
311 | p%=(bg_img.getWidth()-1); |
||
312 | |||
313 | g.setColor(0x156315); |
||
314 | draw_graph_part(g,x,nick_line_pos_data[p]/line_scaler,nick_line_pos_data[p+1]/line_scaler); |
||
315 | g.setColor(0xCC1315); |
||
316 | draw_graph_part(g,x,roll_line_pos_data[p]/line_scaler,roll_line_pos_data[p+1]/line_scaler); |
||
317 | g.setColor(0xf8ef02); |
||
318 | draw_graph_part(g,x,accnick_line_pos_data[p]/line_scaler,accnick_line_pos_data[p+1]/line_scaler); |
||
319 | g.setColor(0x19194d); |
||
320 | draw_graph_part(g,x,accroll_line_pos_data[p]/line_scaler,accroll_line_pos_data[p+1]/line_scaler); |
||
321 | |||
322 | |||
181 | ligi | 323 | |
221 | ligi | 324 | } |
181 | ligi | 325 | } |
326 | |||
208 | ligi | 327 | private void connect_mk(String url) |
328 | { |
||
329 | mk = new MKCommunicator(url); |
||
330 | mk_stat= new MKStatistics(mk); |
||
221 | ligi | 331 | |
208 | ligi | 332 | } |
333 | |||
221 | ligi | 334 | public void draw_graph_part(Graphics g,int x,int y1,int y2) |
335 | { |
||
336 | g.fillRect(x,line_middle_y-y1,2,2 ); |
||
337 | if (y1>y2) |
||
338 | g.fillRect(x,line_middle_y-y1,2,y1-y2); |
||
339 | else |
||
340 | g.fillRect(x,line_middle_y-y2,2,y2-y1); |
||
341 | } |
||
342 | |||
181 | ligi | 343 | /*********************************************** input Section **********************************************/ |
344 | // keys |
||
221 | ligi | 345 | public boolean fullscreen=false; |
346 | |||
181 | ligi | 347 | public void keyPressed(int keyCode) |
348 | { |
||
349 | |||
350 | if ((mk==null)&&(keyCode >= this.KEY_NUM0) && (keyCode < this.KEY_NUM0+bt_scanner.remote_device_count)) |
||
208 | ligi | 351 | connect_mk("btspp://"+bt_scanner.remote_device_mac[keyCode-this.KEY_NUM0] + ":1"); |
352 | else |
||
353 | { |
||
354 | switch (keyCode) |
||
355 | { |
||
221 | ligi | 356 | |
208 | ligi | 357 | case KEY_STAR: |
358 | mk.other_data_count=23; |
||
359 | quit=true; |
||
360 | break; |
||
361 | |||
362 | case KEY_POUND: |
||
221 | ligi | 363 | //rescan=true; |
364 | fullscreen=!fullscreen; |
||
365 | setFullScreenMode(fullscreen); |
||
208 | ligi | 366 | break; |
367 | } |
||
368 | switch (getGameAction (keyCode)) |
||
369 | { |
||
370 | case UP: |
||
371 | mk.LCD.LCD_PREVPAGE(); |
||
372 | break; |
||
373 | |||
374 | case DOWN: |
||
375 | mk.LCD.LCD_NEXTPAGE(); |
||
376 | break; |
||
377 | } |
||
378 | } |
||
379 | |||
380 | |||
381 | return; |
||
181 | ligi | 382 | } |
383 | |||
384 | |||
385 | |||
386 | |||
387 | |||
388 | |||
389 | |||
390 | |||
391 | } |
||
392 | |||
393 |