Rev 208 |
Blame |
Last modification |
View Log
| RSS feed
/*********************************************************************************************************************************
* *
* minimal canvas to test Abstraction layer on various Phones *
* *
* Author: Marcus -LiGi- Bueschleb *
* Project-Start: 9/2007 *
* Mailto: ligi@smart4mobile.de *
* Licence: Creative Commons / Non Commercial *
* Big Up: Holger&Ingo *
*********************************************************************************************************************************/
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class MKMiniCanvas
extends Canvas
implements Runnable
{
private BTSearcher bt_scanner
;
private MKCommunicator mk=
null;
private MKStatistics mk_stat=
null;
private String err=
"";
private MKMiniMidlet root
;
public MKMiniCanvas
(MKMiniMidlet _root
)
{
root=_root
;
// mk = new MKCommunicator("btspp://000BCE016B5B:1");
//connect_mk("btspp://000BCE016B4F:1");
try
{
RecordStore recStore = RecordStore.
openRecordStore("bturl",
true );
if (recStore.
getNumRecords()==
1)
{
byte[] recData =
new byte[recStore.
getRecordSize(1)];
int len = recStore.
getRecord(1, recData,
0);
rescan=
false;
connect_mk
(new String(recData,
0, len
));
}
}
catch (Exception e
)
{
err+=e.
toString();
}
if (mk==
null) bt_scanner =
new BTSearcher
();
new Thread(this).
start();
}
boolean quit=
false;
boolean rescan=
true;
/********************************************************** Thread ***********************************************************/
// ticking runnable Section
public void run
()
{
while(true)
{
long loopStartTime =
System.
currentTimeMillis();
long sleeptime=
0;
// ticked thing
if (quit
)
{
try
{
RecordStore recStore = RecordStore.
openRecordStore("bturl",
true );
if (recStore.
getNumRecords()==
1)
recStore.
deleteRecord(1);
byte[] rec = mk.
mk_url.
getBytes();
recStore.
addRecord(rec,
0, rec.
length);
}
catch (Exception e
)
{
err+=e.
toString();
}
root.
quit();
}
if (rescan
)
{
if (mk
!=
null) mk.
close_connections();
mk=
null;
bt_scanner.
search_again();
}
rescan=
false;
com.
nokia.
mid.
ui.
DeviceControl.
setLights(0,
100);
//bt.tick();
// every state has sth to do in tick section
// System.gc();
repaint
();
serviceRepaints
();
sleeptime=
1000/
15 -
(int) (System.
currentTimeMillis()- loopStartTime
);
if (sleeptime
<0)
sleeptime=
100; // everyone has fi sleep
try { Thread.
sleep(sleeptime
); }
catch (Exception e
)
{
err=
"Problem Sleeping ";
}
}
}
// drawing section
public void paint
(Graphics g
) {
int spacer=
(g.
getFont().
getHeight());
g.
setColor(0xFFFFFF
);
g.
fillRect(0,
0,
this.
getWidth(),
this.
getHeight());
g.
setColor(0x000000
);
if (mk==
null)
{
if (bt_scanner.
searching)
g.
drawString("scanning for BT-Devices",
0,
0,
Graphics.
TOP |
Graphics.
LEFT);
else
{
g.
drawString("Press key 0-"+
(bt_scanner.
remote_device_count-
1) +
" to continue" ,
0,
0,
Graphics.
TOP |
Graphics.
LEFT);
for (int i=
0;i
<bt_scanner.
remote_device_count;i++
)
g.
drawString("#"+i+
">" + bt_scanner.
remote_device_name[i
]+
"("+bt_scanner.
remote_device_mac[i
]+
")",
0,spacer+spacer
*i,
Graphics.
TOP |
Graphics.
LEFT);
}
}
else // MK is connected
{
int y_off=
0;
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);
y_off+=spacer
;
for ( int foo=
0;foo
<4;foo++
)
{
g.
drawString(mk.
LCD.
LCD_str[foo
],
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
}
try {
g.
drawString("motor1:"+mk.
debug_data.
analog[12],
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("motor2:"+mk.
debug_data.
analog[13],
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("motor3:"+mk.
debug_data.
analog[14],
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("motor4:"+mk.
debug_data.
analog[15],
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
}
catch (Exception e
) { }
if (mk.
connected)
{
g.
drawString("time conn:" +
((System.
currentTimeMillis()- mk.
connection_start_time)/
1000)+
"s" ,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("time motor>15:" +
(mk_stat.
motor_on_time/
1000) +
"s" ,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("time motor=15:" +
(mk_stat.
motor_stand_time/
1000) +
"s" ,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
}
}
}
private void connect_mk
(String url
)
{
mk =
new MKCommunicator
(url
);
mk_stat=
new MKStatistics
(mk
);
}
/*********************************************** input Section **********************************************/
// keys
public void keyPressed
(int keyCode
)
{
if ((mk==
null)&&(keyCode
>=
this.
KEY_NUM0) && (keyCode
< this.
KEY_NUM0+bt_scanner.
remote_device_count))
connect_mk
("btspp://"+bt_scanner.
remote_device_mac[keyCode-
this.
KEY_NUM0] +
":1");
else
{
switch (keyCode
)
{
case KEY_STAR:
mk.
other_data_count=
23;
quit=
true;
break;
case KEY_POUND:
rescan=
true;
break;
}
switch (getGameAction
(keyCode
))
{
case UP:
mk.
LCD.
LCD_PREVPAGE();
break;
case DOWN:
mk.
LCD.
LCD_NEXTPAGE();
break;
}
}
return;
}
}