/***************************************************************
*
* minimal canvas to test Abstraction layer on various Phones
*
* Author: Marcus -LiGi- Bueschleb
*
* see README for further Infos
*
***************************************************************/
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
public class MKMiniCanvas
extends Canvas
implements Runnable
{
private boolean debug_screen=
false;
private byte[] debug_screen_sequence=
{KEY_POUND,KEY_NUM4,KEY_NUM2
};
private byte debug_screen_sequence_pos=
0;
String debug_msg=
"";
// name/handle for the recordStore to memorize some stuff
private final static String RECORD_STORE_NAME=
"DUBSETT";
private BTSearcher bt_scanner
;
public MKCommunicator mk=
null;
private MKStatistics mk_stat=
null;
private MKParamsEditor params_editor=
null;
//#if voice_mode!="no_voice"
private MKStatusVoice status_voice
;
//#endif
private String err=
"";
private DUBwise root
;
private UFOProber ufo_prober
;
byte[] settings_arr
;
public boolean do_vibra=
true;
public boolean do_sound=
true;
public boolean do_graph=
true;
public boolean keep_lighton=
false;
private Image bg_img
;
private Image bt_img
;
private Image lcd_img
;
private Image load_img
;
final static int SKINID_DARK=
0;
final static int SKINID_LIGHT =
1;
//holds id of actual skin
byte act_skin=SKINID_DARK
;
public int[] nick_line_pos_data
;
public int[] roll_line_pos_data
;
public int[] accnick_line_pos_data
;
public int[] accroll_line_pos_data
;
public int lcd_char_width=
0;
public int lcd_char_height=
0;
public int frame_pos=
0;
int line_scaler=
20;
int rawdebug_cursor_y=
0;
int rawdebug_off_y=
0;
public int line_middle_y
;
boolean quit=
false;
boolean rescan=
true;
int bg_offset=
0;
// variable to hold the current state
public int state=-
1;
// id for each state - must just be uniq - order isnt important
public final static int STATEID_SCANNING =
0;
public final static int STATEID_DEVICESELECT =
1;
public final static int STATEID_MAINMENU =
2;
public final static int STATEID_MOTORTEST =
3;
public final static int STATEID_SELECT_PARAMSET =
4;
public final static int STATEID_EDIT_PARAMS =
5;
public final static int STATEID_HANDLE_PARAMS =
6;
public final static int STATEID_FLIGHTVIEW =
7;
public final static int STATEID_RAWDEBUG =
8;
// public final static int STATEID_RAWDEBUG =8;
public final static int STATEID_KEYCONTROL =
9;
public final static int STATEID_SETTINGSMENU =
10;
public boolean fullscreen=
false;
public int act_motor=
0;
public int act_motor_increase=
0;
public boolean motor_test_sel_all=
false;
public String[] main_menu_items=
{"Telemetry",
"Raw Debug",
"MK-KeyControl",
"Motor Test" ,
"Flight Settings",
"Tool Settings",
"Proxy",
"Change Device" ,
"Quit " };
public final static int MAINMENU_TELEMETRY =
0;
public final static int MAINMENU_RAWDEBUG =
1+MAINMENU_TELEMETRY
;
public final static int MAINMENU_KEYCONTROL =
1+MAINMENU_RAWDEBUG
;
public final static int MAINMENU_MOTORTEST =
1+MAINMENU_KEYCONTROL
;
public final static int MAINMENU_PARAMS =
1+MAINMENU_MOTORTEST
;
public final static int MAINMENU_SETTINGSMENU =
1+MAINMENU_PARAMS
;
public final static int MAINMENU_PROXY =
1+MAINMENU_SETTINGSMENU
;
public final static int MAINMENU_DEVICESELECT =
1+MAINMENU_PROXY
;
public final static int MAINMENU_QUIT =
1+MAINMENU_DEVICESELECT
;
public String[] settings_menu_items=
{"Skin ",
"Sound ",
"Vibra " ,
"Graph ",
"FullScreen " ,
"Keep BGLight " ,
"Back" };
public final static int SETTINGSMENU_CHANGESKIN =
0;
public final static int SETTINGSMENU_SOUNDTOGGLE =
1;
public final static int SETTINGSMENU_VIBRATOGGLE =
2;
public final static int SETTINGSMENU_GRAPHTOGGLE =
3;
public final static int SETTINGSMENU_FULLSCREENTOGGLE =
4;
public final static int SETTINGSMENU_LIGHTTOGGLE =
5;
public final static int SETTINGSMENU_BACK =
6;
int local_max=-
1;
int y_off=
0;
int spacer=
0;
int spacer1=
0;
int[] motor_test =
{0,
0,
0,
0};
String[] menu_items
;
int act_menu_select=
0;
String[] lcd_lines =
null;
public void paint_menu
(Graphics g
)
{
for ( int i=
0;i
<menu_items.
length;i++
)
{
if ((frame_pos
%3
)!=
0)
{
lcd_lines
[i
]=
(act_menu_select==i
?">":
" ") + menu_items
[i
];
for ( int ii=
0;ii
<(18-menu_items
[i
].
length());ii++
)
lcd_lines
[i
]+=
" ";
if (act_menu_select==i
)
lcd_lines
[i
]+=
"<";
}
else
lcd_lines
[i
]=
" " + menu_items
[i
];
}
paint_lcd
(g,
true);
}
public void menu_keypress
(int keyCode
)
{
switch (getGameAction
(keyCode
))
{
case UP:
if (act_menu_select
!=
0) act_menu_select--
;
else
act_menu_select=menu_items.
length-
1;
break;
case DOWN:
if (act_menu_select
<(menu_items.
length-
1)) act_menu_select++
;
else act_menu_select=
0;
break;
}
}
public void paint_lcd
(Graphics g,
boolean bottomup
)
{
int y
;
for(int i=
0;i
<lcd_lines.
length;i++
)
for (int pos=
0;pos
<20;pos++
)
{
if (bottomup
)
y=
this.
getHeight()-
(lcd_lines.
length-i
)*lcd_char_height
;
else
y=i
*lcd_char_height
;
g.
setClip((lcd_img.
getWidth()/
222)*pos,y,
(lcd_img.
getWidth()/
222),lcd_img.
getHeight());
g.
drawImage(lcd_img,
(lcd_img.
getWidth()/
222)*pos-
((pos
<lcd_lines
[i
].
length()?lcd_lines
[i
].
charAt(pos
):
' ')-
' ')*(lcd_img.
getWidth()/
222),y,g.
TOP | g.
LEFT);
}
}
public void load_images
()
{
try
{
// load all needed images
switch (act_skin
)
{
case SKINID_DARK:
lcd_img=
Image.
createImage("/lcd_green.png");
bg_img=
Image.
createImage("/starfield.jpg"); break;
case SKINID_LIGHT:
lcd_img=
Image.
createImage("/lcd_blue.png");
bg_img=
Image.
createImage("/clouds.jpg");
break;
}
bt_img=
Image.
createImage("/bt.png");
load_img=
Image.
createImage("/load.png");
}
catch (Exception e
)
{
err+=e.
toString();
}
lcd_char_width=lcd_img.
getWidth()/
222;
lcd_char_height=lcd_img.
getHeight();
nick_line_pos_data=
new int[bg_img.
getWidth()];
roll_line_pos_data=
new int[bg_img.
getWidth()];
accnick_line_pos_data=
new int[bg_img.
getWidth()];
accroll_line_pos_data=
new int[bg_img.
getWidth()];
for (int c=
0;c
<bg_img.
getWidth();c++
)
{
nick_line_pos_data
[c
]=-
1;
roll_line_pos_data
[c
]=-
1;
accnick_line_pos_data
[c
]=-
1;
accroll_line_pos_data
[c
]=-
1;
}
}
public MKMiniCanvas
(DUBwise _root
)
{
root=_root
;
bt_scanner =
new BTSearcher
();
params_editor =
new MKParamsEditor
(this);
ufo_prober =
new UFOProber
();
mk =
new MKCommunicator
(root
);
mk_stat=
new MKStatistics
(mk
);
//#if voice_mode!="no_voice"
status_voice=
new MKStatusVoice
(mk,
this);
//#endif
try
{
RecordStore recStore = RecordStore.
openRecordStore(RECORD_STORE_NAME ,
true );
if (recStore.
getNumRecords()==
3)
{
//byte[] recData = new byte[recStore.getRecordSize(1)];
//int len = recStore.getRecord(1, recData, 0);
//byte[] recData2 = new byte[recStore.getRecordSize(2)];
//int len2 = recStore.getRecord(2, recData, 0);
byte[] url_data=recStore.
getRecord(1);
byte[] name_data=recStore.
getRecord(2);
settings_arr=recStore.
getRecord(3);
act_skin=settings_arr
[0];
do_sound=
(settings_arr
[1]==
1);
do_vibra=
(settings_arr
[2]==
1);
do_graph=
(settings_arr
[3]==
1);
fullscreen=
(settings_arr
[4]==
1);
keep_lighton=
(settings_arr
[5]==
1);
connect_mk
(new String(url_data,
0, url_data.
length),
new String(name_data,
0, name_data.
length));
}
recStore.
closeRecordStore();
}
catch (Exception e
)
{
settings_arr=
new byte[10];
err+=e.
toString();
}
load_images
();
chg_state
((mk.
force_disconnect)?STATEID_SCANNING:STATEID_MAINMENU
);
new Thread(this).
start();
}
/****************************** Thread ******************/
// ticking runnable Section
public void run
()
{
while(true)
{
try {
repaint
();
serviceRepaints
();
long loopStartTime =
System.
currentTimeMillis();
long sleeptime=
0;
// ticked thing
frame_pos++
;
switch(state
)
{
case STATEID_KEYCONTROL:
mk.
send_keys(keycontrol_bitfield
);
break;
case STATEID_MOTORTEST:
if (motor_test_sel_all
)
for (int m=
0;m
<4;m++
)
{
motor_test
[m
]+=act_motor_increase
;
if (motor_test
[m
]<0)motor_test
[m
]=
0;
if (motor_test
[m
]>255)motor_test
[m
]=
255;
}
else
{
motor_test
[act_motor
]+=act_motor_increase
;
if (motor_test
[act_motor
]<0) motor_test
[act_motor
]=
0;
if (motor_test
[act_motor
]>255) motor_test
[act_motor
]=
255;
}
mk.
motor_test(motor_test
);
break;
case STATEID_SCANNING:
intro_str_delay--
;
if (intro_str_delay
<0)
{
intro_str_delay=
1;
if (intro_str_pos
>intro_str.
length())
intro_str_pos=
0;
lcd_lines
[3]=intro_str.
substring(intro_str_pos,
(((intro_str_pos+
20)>intro_str.
length())?intro_str.
length():intro_str_pos+
20));
intro_str_pos++
;
}
if (!bt_scanner.
searching)
chg_state
(STATEID_DEVICESELECT
);
break;
}
try {
nick_line_pos_data
[-bg_offset
] = mk.
debug_data.
nick_int();
roll_line_pos_data
[-bg_offset
] = mk.
debug_data.
roll_int();
accnick_line_pos_data
[-bg_offset
] = mk.
debug_data.
accnick();
accroll_line_pos_data
[-bg_offset
] = mk.
debug_data.
accroll();
}
catch (Exception e
)
{
err+=e.
toString();
}
if (quit
)
{
try
{
RecordStore.
deleteRecordStore(RECORD_STORE_NAME
);
RecordStore recStore = RecordStore.
openRecordStore(RECORD_STORE_NAME,
true );
recStore.
addRecord(mk.
mk_url.
getBytes(),
0, mk.
mk_url.
getBytes().
length);
recStore.
addRecord(mk.
name.
getBytes(),
0, mk.
name.
getBytes().
length);
settings_arr=
new byte[10];
settings_arr
[0]=
(byte)act_skin
;
settings_arr
[1]=
(byte)(do_sound
?1:
0);
settings_arr
[2]=
(byte)(do_vibra
?1:
0);
settings_arr
[3]=
(byte)(do_graph
?1:
0);
settings_arr
[4]=
(byte)(fullscreen
?1:
0);
settings_arr
[5]=
(byte)(keep_lighton
?1:
0);
recStore.
addRecord(settings_arr,
0, settings_arr.
length);
recStore.
closeRecordStore();
}
catch (Exception e
)
{
err+=e.
toString();
}
root.
quit();
}
if (rescan
)
{
rescan=
false;
}
try {
//rescan=false;
bg_offset--
;
if (bg_offset==-bg_img.
getWidth())
bg_offset=
0;
if (keep_lighton
) com.
nokia.
mid.
ui.
DeviceControl.
setLights(0,
100);
//bt.tick();
// every state has sth to do in tick section
}
catch (Exception e
)
{
}
// System.gc();
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 ";
}
}
catch (Exception e
)
{
err+=e.
toString();
}
}
}
boolean firstrun=
true;
// drawing section
public void paint
(Graphics g
) {
if (debug_screen
)
{
g.
setColor(0x0000FF
);
g.
fillRect(0,
0,
this.
getWidth(),
this.
getHeight());
g.
setColor(0xFFFFFF
);
String tmp_str=
"";
y_off=
0;
for(int tmp_i=
0;tmp_i
<debug_msg.
length();tmp_i++
)
{
if ((debug_msg.
charAt(tmp_i
)==
'\r')||
(debug_msg.
charAt(tmp_i
)==
'\n')) {
g.
drawString(tmp_str,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=g.
getFont().
getHeight();
tmp_str=
"";
}
else
tmp_str+=debug_msg.
charAt(tmp_i
);
}
g.
drawString(tmp_str,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
return;
}
if (firstrun
)
{
if (fullscreen
) setFullScreenMode
(fullscreen
);
firstrun=
false;
}
y_off=
0;
try {
if (mk
!=
null)
{
line_middle_y=
this.
getHeight()/
2;
if (local_max
<Math.
abs(mk.
debug_data.
nick_int()))
local_max=
Math.
abs(mk.
debug_data.
nick_int());
if (local_max
<Math.
abs(mk.
debug_data.
roll_int()))
local_max=
Math.
abs(mk.
debug_data.
roll_int());
if (local_max
<Math.
abs(mk.
debug_data.
accnick()))
local_max=
Math.
abs(mk.
debug_data.
accnick());
if (local_max
<Math.
abs(mk.
debug_data.
accroll()))
local_max=
Math.
abs(mk.
debug_data.
accroll());
line_scaler= local_max/
(this.
getHeight()/
2)+
1;
}
Font f1 =
Font.
getFont(Font.
FACE_SYSTEM,
Font.
STYLE_PLAIN,
Font.
SIZE_MEDIUM);
Font f2 =
Font.
getFont(Font.
FACE_SYSTEM,
Font.
STYLE_PLAIN,
Font.
SIZE_SMALL);
spacer=
(f1.
getHeight());
spacer1=
(f2.
getHeight());
//default Font
g.
setFont(f1
);
if (state==STATEID_EDIT_PARAMS
)
{
g.
setColor(0x000000
);
g.
fillRect(0,
0,
this.
getWidth(),
this.
getHeight());
}
else
{
g.
setColor(0xFFFFFF
);
g.
fillRect(0,
0,
this.
getWidth(),
this.
getHeight());
g.
drawImage(bg_img,bg_offset,
0, g.
TOP | g.
LEFT);
if (bg_offset+bg_img.
getWidth()<this.
getWidth())
g.
drawImage(bg_img,bg_offset+bg_img.
getWidth(),
0, g.
TOP | g.
LEFT);
}
switch (act_skin
)
{
case SKINID_DARK:
g.
setColor(0x2dcf20
);
break;
case SKINID_LIGHT:
g.
setColor(0x000000
);
break;
}
switch(state
)
{
case STATEID_KEYCONTROL:
y_off+=spacer
;
g.
drawString("UP&DOWN => nick",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("LEFT&RIGHT => roll",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("1&4 => altitude",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("2&3 => gier",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("Press # and * at once",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("to quit KeyControl",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("bf1:"+ keycontrol_bitfield
[0] ,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
g.
drawString("bf2:"+ keycontrol_bitfield
[1] ,
this.
getWidth()/
2,y_off,
Graphics.
TOP |
Graphics.
LEFT);
break;
case STATEID_MOTORTEST:
for (int bar=
0;bar
<4;bar++
)
{
g.
setColor(((bar==act_motor
)|motor_test_sel_all
)?0x44CC44:0x4444DD
);
g.
fillRect(this.
getWidth()/
(8*2)+bar
*2*this.
getWidth()/
8,
10,
this.
getWidth()/
8,
20+motor_test
[bar
]);
g.
setColor(0x000000
);
g.
drawString(""+motor_test
[bar
] ,
this.
getWidth()/
8+bar
*2*this.
getWidth()/
8,
10,
Graphics.
TOP |
Graphics.
HCENTER);
if(bar
!=
4) g.
drawString(""+mk.
debug_data.
motor_val(bar
) ,
this.
getWidth()/
8+bar
*2*this.
getWidth()/
8,
30,
Graphics.
TOP |
Graphics.
HCENTER);
}
break;
case STATEID_EDIT_PARAMS:
params_editor.
paint(g
);
break;
case STATEID_SCANNING:
paint_lcd
(g,
true);
g.
setClip(this.
getWidth()/
2-load_img.
getWidth()/
6+
1,
this.
getHeight()/
2-load_img.
getHeight()/
8+
1, load_img.
getWidth()/
4,load_img.
getHeight()/
3);;
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);
g.
drawImage(bt_img,
this.
getWidth()/
2 ,
this.
getHeight()/
2 , g.
HCENTER | g.
VCENTER);
break;
case STATEID_RAWDEBUG:
g.
setFont(f2
);
rawdebug_off_y=
0;
if ((rawdebug_cursor_y+
2)*spacer1
>this.
getHeight())
rawdebug_off_y=
((rawdebug_cursor_y+
2)*spacer1-
this.
getHeight())/spacer1
;
for (int i=
0;i
<(this.
getHeight()/spacer1
);i++
)
{
if (i+rawdebug_off_y==rawdebug_cursor_y
)
{
g.
setColor(0x0000CC
);
g.
fillRect(0,y_off,
this.
getWidth(),spacer1
);
switch (act_skin
)
{
case SKINID_DARK:
g.
setColor(0x2dcf20
);
break;
case SKINID_LIGHT:
g.
setColor(0x000000
);
break;
}
}
if (i+rawdebug_off_y
<32) //todo better style
g.
drawString(mk.
debug_data.
names[i+rawdebug_off_y
] + mk.
debug_data.
analog[i+rawdebug_off_y
] ,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer1
;
}
break;
case STATEID_MAINMENU:
g.
drawString("probed_device" + ufo_prober.
probe_result +
" by " + ufo_prober.
reply,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
drawString("MK-Connection(" +
(mk.
connected?("open"+
((System.
currentTimeMillis()- mk.
connection_start_time)/
1000)+
"s"):
"close")+
"):",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
setFont(f2
);
g.
drawString(" Name:" + mk.
name,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer1
;
g.
drawString(" URL:" + mk.
mk_url,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer1
;
g.
drawString(" Version:" + mk.
version.
str,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer1
;
g.
setFont(f1
);
g.
drawString("Packet Traffic:",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
g.
setFont(f2
);
g.
drawString( " debug:"+mk.
debug_data_count+
" LCD:" + mk.
lcd_data_count +
" vers:" + mk.
version_data_count,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer1
;
g.
drawString( " other:"+mk.
other_data_count+
" params:"+mk.
params_data_count,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
case STATEID_SETTINGSMENU:
// falltru wanted
case STATEID_SELECT_PARAMSET:
case STATEID_HANDLE_PARAMS:
case STATEID_DEVICESELECT:
paint_menu
(g
);
break;
case STATEID_FLIGHTVIEW:
// !!TODO!! check exactly which version those Datas where introduced
if (mk.
version.
compare(0,
60)==mk.
version.
VERSION_PREVIOUS)
{
g.
drawString("Voltage: " +
(mk.
debug_data.
UBatt()/
10) +
"," +
(mk.
debug_data.
UBatt()%10
)+
"V" ,
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
g.
drawString("Sender: " + mk.
debug_data.
SenderOkay(),
this.
getWidth()/
2,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
}
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+
"p"+mk.
params_data_count+
")",
0,y_off,
Graphics.
TOP |
Graphics.
LEFT);
y_off+=spacer
;
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);
y_off+=spacer
;
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);
y_off+=spacer
;
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);
}
y_off=
this.
getHeight()-
4*lcd_img.
getHeight();
for ( int foo=
0;foo
<4;foo++
)
{
for (int x=
0;x
<20;x++
)
{
g.
setClip((lcd_img.
getWidth()/
222)*x,y_off,
(lcd_img.
getWidth()/
222),lcd_img.
getHeight());
g.
drawImage(lcd_img,
(lcd_img.
getWidth()/
222)*x-
(mk.
LCD.
get_act_page()[foo
].
charAt(x
)-
' ')*(lcd_img.
getWidth()/
222),y_off, g.
TOP | g.
LEFT);
}
y_off+=lcd_img.
getHeight();
}
g.
setClip(0,
0,
this.
getWidth(),
this.
getHeight());
// draw lines
if (do_graph
) for ( int x=
0;x
<this.
getWidth();x++
)
{
int p=
(((-bg_offset+x-
this.
getWidth()-
5)));
if (p
<1)
p+=bg_img.
getWidth();
p
%=
(bg_img.
getWidth()-
1);
g.
setColor(0x156315
);
draw_graph_part
(g,x,nick_line_pos_data
[p
]/line_scaler,nick_line_pos_data
[p+
1]/line_scaler
);
g.
setColor(0xCC1315
);
draw_graph_part
(g,x,roll_line_pos_data
[p
]/line_scaler,roll_line_pos_data
[p+
1]/line_scaler
);
g.
setColor(0xf8ef02
);
draw_graph_part
(g,x,accnick_line_pos_data
[p
]/line_scaler,accnick_line_pos_data
[p+
1]/line_scaler
);
g.
setColor(0x19194d
);
draw_graph_part
(g,x,accroll_line_pos_data
[p
]/line_scaler,accroll_line_pos_data
[p+
1]/line_scaler
);
}
}
} catch (Exception e
) {}
g.
setClip(0,
0,
this.
getWidth(),
this.
getHeight());
g.
drawImage(cam_img,-cam_img_off,-cam_img_off,g.
TOP | g.
LEFT);
cam_img_off++
;
cam_img_off
%=cam_img.
getWidth();
}
Player mPlayer
;
VideoControl mVideoControl
;
Image cam_img
;
int cam_img_off=
0;
private void connect_mk
(String url,
String name
)
{
// ufo_prober.bluetooth_probe(url);
// mk.connect_to(url,name);
try
{
debug_msg+=
"creating player\n";
mPlayer = Manager.
createPlayer("capture://video?encoding=png&width=2048&height=1536");
debug_msg+=
"realizing player\n";
mPlayer.
realize();
debug_msg+=
"get_videocontrol\n";
mVideoControl =
(VideoControl
)mPlayer.
getControl("VideoControl");
debug_msg+=
"switching Canvas\n";
mVideoControl.
initDisplayMode(VideoControl.
USE_DIRECT_VIDEO,
this);
debug_msg+=
"get snap\n";
byte[] raw = mVideoControl.
getSnapshot(null);
debug_msg+=
"making img\n";
cam_img =
Image.
createImage(raw,
0, raw.
length);
}
catch ( Exception e
)
{
debug_msg+=e.
toString();
}
}
public void draw_graph_part
(Graphics g,
int x,
int y1,
int y2
)
{
if ( this.
getWidth()>200)
{
g.
fillRect(x,line_middle_y-y1,
1,
1 );
if (y1
>y2
)
g.
fillRect(x,line_middle_y-y1,
1,y1-y2
);
else
g.
fillRect(x,line_middle_y-y2,
1,y2-y1
);
}
else
{
g.
fillRect(x,line_middle_y-y1,
1,
1 );
if (y1
>y2
)
g.
fillRect(x,line_middle_y-y1,
1,y1-y2
);
else
g.
fillRect(x,line_middle_y-y2,
1,y2-y1
);
}
}
/*********************************************** input Section **********************************************/
public final String intro_str=
" Digital Ufo Broadcasting with intelligent service equipment by Marcus -LiGi- Bueschleb ; Big Up Holger&Ingo for the MikroKopter Project (http://www.mikrokopter.de) ";
int intro_str_pos=
0;
int intro_str_delay=
3;
public void chg_state
(int next_state
)
{
if (next_state
!=state
)act_menu_select=
0;
// prepare next state
switch(next_state
)
{
case STATEID_KEYCONTROL:
keycontrol_exit=
0;
break;
case STATEID_SCANNING:
lcd_lines=
new String[4];
lcd_lines
[0]=
"Scanning for Devices";
lcd_lines
[1]=
" ";
//#expand lcd_lines[2]="DUBwise v%VERSION% ";
lcd_lines
[3]=intro_str.
substring(0,
20);
mk.
close_connections(true);
bt_scanner.
search();
break;
case STATEID_HANDLE_PARAMS:
menu_items=
new String[2];
menu_items
[0]=
"write to MK";
menu_items
[1]=
"Discard";
lcd_lines=
new String[2];
break;
case STATEID_SELECT_PARAMSET:
menu_items=
new String[5];
for (int i=
0;i
<5;i++
)
menu_items
[i
]=mk.
params.
names[i
];
lcd_lines=
new String[5];
break;
case STATEID_DEVICESELECT:
menu_items=
new String[bt_scanner.
remote_device_count+
1];
for (int i=
0;i
<bt_scanner.
remote_device_count;i++
)
menu_items
[i
]=bt_scanner.
remote_device_name[i
];
menu_items
[bt_scanner.
remote_device_count]=
"scan again";
lcd_lines=
new String[bt_scanner.
remote_device_count+
1];
break;
case STATEID_MAINMENU:
menu_items=main_menu_items
;
lcd_lines=
new String[menu_items.
length];
break;
case STATEID_SETTINGSMENU:
menu_items=
new String[settings_menu_items.
length];
for(int cnt=
0;cnt
<settings_menu_items.
length;cnt++
)
menu_items
[cnt
]=settings_menu_items
[cnt
];
menu_items
[0]+=
(act_skin==SKINID_DARK
)?"Dark":
"Light";
menu_items
[1]+=
(!do_sound
)?"Off":
"On";
menu_items
[2]+=
(!do_vibra
)?"Off":
"On";
menu_items
[3]+=
(!do_graph
)?"Off":
"On";
menu_items
[4]+=
(!fullscreen
)?"Off":
"On";
menu_items
[5]+=
(!keep_lighton
)?"Off":
"On";
lcd_lines=
new String[menu_items.
length];
break;
}
// switch state
state=next_state
;
}
public void keyReleased
(int keyCode
)
{
switch(state
)
{
case STATEID_MOTORTEST:
act_motor_increase=
0;
break;
case STATEID_KEYCONTROL:
if (keyCode==KEY_POUND
)
keycontrol_exit
&=
255^
1;
else
if (keyCode==KEY_STAR
)
keycontrol_exit
&=
255^
2;
else
if ((keyCode
>=
this.
KEY_NUM0) && (keyCode
< this.
KEY_NUM8))
keycontrol_bitfield
[0]&=
255^
( 1<<(keyCode-
this.
KEY_NUM0));
else
if ((keyCode
>=
this.
KEY_NUM8) && (keyCode
<=
this.
KEY_NUM9))
keycontrol_bitfield
[1]&=
255^
( 1<<(keyCode-
this.
KEY_NUM8));
else
switch (getGameAction
(keyCode
))
{
case UP:
keycontrol_bitfield
[1]&=
255^
4;
break;
case DOWN:
keycontrol_bitfield
[1]&=
255^
8;
break;
case LEFT:
keycontrol_bitfield
[1]&=
255^
16;
break;
case RIGHT:
keycontrol_bitfield
[1]&=
255^
32;
break;
case FIRE:
keycontrol_bitfield
[1]&=
255^
64;
break;
}
mk.
send_keys(keycontrol_bitfield
);
break;
}
}
byte keycontrol_exit=
0;
public final static int[] keycontrol_bitfield=
{0,
0};
public void pointerPressed
(int pointer_x,
int pointer_y
)
{
if (pointer_x
<this.
getWidth()/
2)
{
if (state==STATEID_EDIT_PARAMS
)
{
chg_state
(STATEID_HANDLE_PARAMS
);
return;
}
else
if (state
!=STATEID_KEYCONTROL
)
{
chg_state
(STATEID_MAINMENU
);
return;
}
}
else
{
fullscreen=
!fullscreen
;
setFullScreenMode
(fullscreen
);
}
}
public void keyPressed
(int keyCode
)
{
if (keyCode==KEY_NUM0
)
{
try
{
debug_msg+=
"get snap\n";
byte[] raw = mVideoControl.
getSnapshot(null);
debug_msg+=
"making img\n";
cam_img =
Image.
createImage(raw,
0, raw.
length);
}
catch ( Exception e
)
{
debug_msg+=e.
toString();
}
}
if (!debug_screen
)
{
if (keyCode==debug_screen_sequence
[debug_screen_sequence_pos
])
{
debug_screen_sequence_pos++
;
if(debug_screen_sequence_pos==debug_screen_sequence.
length)
{
debug_screen=
true;
debug_screen_sequence_pos=
0;
}
}
else
debug_screen_sequence_pos=
0;
}
else
{
if (keyCode==KEY_STAR
)
debug_screen=
false;
}
if (keyCode==KEY_STAR
)
{
if (state==STATEID_EDIT_PARAMS
)
{
chg_state
(STATEID_HANDLE_PARAMS
);
return;
}
else
if (state
!=STATEID_KEYCONTROL
)
{
chg_state
(STATEID_MAINMENU
);
return;
}
}
if ((keyCode==KEY_POUND
)&&(state
!=STATEID_KEYCONTROL
))
{
fullscreen=
!fullscreen
;
setFullScreenMode
(fullscreen
);
return;
}
switch(state
)
{
case STATEID_RAWDEBUG:
switch (getGameAction
(keyCode
))
{
case UP:
if (rawdebug_cursor_y==
0)
rawdebug_cursor_y=
31;
else
rawdebug_cursor_y--
;
break;
case DOWN:
if (rawdebug_cursor_y==
31)
rawdebug_cursor_y=
0;
else
rawdebug_cursor_y++
;
break;
}
break;
case STATEID_KEYCONTROL:
if (keyCode==KEY_POUND
)
keycontrol_exit |=
1;
else
if (keyCode==KEY_STAR
)
keycontrol_exit |=
2;
else
if ((keyCode
>=
this.
KEY_NUM0) && (keyCode
< this.
KEY_NUM8))
keycontrol_bitfield
[0]|=
1<<(keyCode-
this.
KEY_NUM0);
else
if ((keyCode
>=
this.
KEY_NUM8) && (keyCode
<=
this.
KEY_NUM9))
keycontrol_bitfield
[1]|=
1<<(keyCode-
this.
KEY_NUM8);
else
switch (getGameAction
(keyCode
))
{
case UP:
keycontrol_bitfield
[1]|=
4;
break;
case DOWN:
keycontrol_bitfield
[1]|=
8;
break;
case LEFT:
keycontrol_bitfield
[1]|=
16;
break;
case RIGHT:
keycontrol_bitfield
[1]|=
32;
break;
case FIRE:
keycontrol_bitfield
[1]|=
64;
break;
}
if (keycontrol_exit==
3)
chg_state
(STATEID_MAINMENU
);
else
mk.
send_keys(keycontrol_bitfield
);
break;
case STATEID_MOTORTEST:
switch (getGameAction
(keyCode
))
{
case UP:
act_motor_increase=-
1;
break;
case DOWN:
act_motor_increase=
1;
break;
case FIRE:
motor_test_sel_all=
!motor_test_sel_all
;
break;
case LEFT:
act_motor--
;
if (act_motor
<0) {act_motor=
0; chg_state
(STATEID_MAINMENU
); }
break;
case RIGHT:
act_motor++
;
act_motor
%=
4;
break;
}
break;
case STATEID_HANDLE_PARAMS:
if ( getGameAction
(keyCode
)==FIRE
)
switch(act_menu_select
)
{
case 0:
mk.
write_params();
default:
chg_state
(STATEID_MAINMENU
);
}
else
menu_keypress
(keyCode
);
break;
case STATEID_SETTINGSMENU:
if ( getGameAction
(keyCode
)==FIRE
)
switch(act_menu_select
)
{
case SETTINGSMENU_CHANGESKIN:
act_skin++
;
act_skin=
(byte)(act_skin
%2
);
load_images
();
chg_state
(STATEID_SETTINGSMENU
);
break;
case SETTINGSMENU_SOUNDTOGGLE:
do_sound=
!do_sound
;
chg_state
(STATEID_SETTINGSMENU
);
break;
case SETTINGSMENU_VIBRATOGGLE:
do_vibra=
!do_vibra
;
chg_state
(STATEID_SETTINGSMENU
);
break;
case SETTINGSMENU_GRAPHTOGGLE:
do_graph=
!do_graph
;
chg_state
(STATEID_SETTINGSMENU
);
break;
case SETTINGSMENU_FULLSCREENTOGGLE:
fullscreen=
!fullscreen
;
setFullScreenMode
(fullscreen
);
chg_state
(STATEID_SETTINGSMENU
);
break;
case SETTINGSMENU_LIGHTTOGGLE:
keep_lighton =
!keep_lighton
;
chg_state
(STATEID_SETTINGSMENU
);
break;
case SETTINGSMENU_BACK:
chg_state
(STATEID_MAINMENU
);
break;
}
else menu_keypress
(keyCode
);
break;
case STATEID_MAINMENU:
if ( getGameAction
(keyCode
)==FIRE
)
switch(act_menu_select
)
{
case MAINMENU_SETTINGSMENU:
chg_state
(STATEID_SETTINGSMENU
);
break;
case MAINMENU_KEYCONTROL:
chg_state
(STATEID_KEYCONTROL
);
break;
case MAINMENU_TELEMETRY :
chg_state
(STATEID_FLIGHTVIEW
);
break;
case MAINMENU_MOTORTEST :
chg_state
(STATEID_MOTORTEST
);
break;
case MAINMENU_PARAMS :
chg_state
(STATEID_SELECT_PARAMSET
);
break;
case MAINMENU_PROXY:
mk.
do_proxy("socket://192.168.1.42:2323");
break;
case MAINMENU_DEVICESELECT:
chg_state
(STATEID_SCANNING
);
break;
case MAINMENU_RAWDEBUG:
chg_state
(STATEID_RAWDEBUG
);
break;
case MAINMENU_QUIT:
// set quit Flag
quit=
true;
break;
}
else menu_keypress
(keyCode
);
break;
case STATEID_SELECT_PARAMSET:
if ( getGameAction
(keyCode
)==FIRE
)
{
if ( mk.
params.
field[act_menu_select
]!=
null)
{
mk.
params.
act_paramset=act_menu_select
;
chg_state
(STATEID_EDIT_PARAMS
);
}
}
else menu_keypress
(keyCode
);
break;
case STATEID_DEVICESELECT:
/*
if ((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",bt_scanner.remote_device_name[keyCode-this.KEY_NUM0]);
chg_state(STATEID_MAINMENU);
}
else
*/
if ( getGameAction
(keyCode
)==FIRE
)
{
if (bt_scanner.
remote_device_count > act_menu_select
)
{
connect_mk
("btspp://"+bt_scanner.
remote_device_mac[act_menu_select
] +
":1",bt_scanner.
remote_device_name[act_menu_select
]);
chg_state
(STATEID_MAINMENU
);
}
else
chg_state
(STATEID_SCANNING
);
}
else menu_keypress
(keyCode
);
break;
case STATEID_EDIT_PARAMS:
params_editor.
keypress(keyCode,getGameAction
(keyCode
)) ;
break;
case STATEID_FLIGHTVIEW:
if ((keyCode
>=
this.
KEY_NUM0) && (keyCode
<=
this.
KEY_NUM9))
mk.
LCD.
set_page(keyCode-
this.
KEY_NUM0);
else
switch (getGameAction
(keyCode
))
{
case LEFT:
case UP:
mk.
LCD.
LCD_PREVPAGE();
break;
case RIGHT:
case DOWN:
mk.
LCD.
LCD_NEXTPAGE();
break;
}
break;
}
}
}