16,16 → 16,18 |
import android.os.Bundle; |
import android.os.*; |
import android.view.View; |
|
import android.util.Log; |
import android.media.*; |
import java.util.Random; |
import java.net.*; |
import java.io.*; |
|
import android.view.Window; |
import android.view.WindowManager; |
import android.view.KeyEvent; |
|
import android.view.*; |
|
import android.graphics.Region.Op; |
|
import org.bluez.*; |
// not working atm - import org.bluez.*; |
|
public class DUBwiseView |
extends View |
33,40 → 35,138 |
|
|
{ |
|
|
int state=0; |
|
|
int state_intro_frame=0; |
|
boolean do_sound=true; |
boolean do_vibra=true; |
boolean do_graph=true; |
boolean menu_active=true; |
|
public final int SKINID_DARK=0; |
public final int SKINID_LIGHT=0; |
int act_skin=0; |
boolean keep_lighton=true; |
|
private Paint mPaint = new Paint(); |
|
// chars in bitmap |
public static int LCD_CHAR_COUNT=222; |
|
// some images we need |
private Bitmap bg_img,lcd_tiles,lcd_img; |
|
private Bitmap icon_img,bg_img,lcd_tiles_img,bt_on_img,bt_off_img; |
private Bitmap lcd_img=null; |
// pos for scrolling |
private int pos=0; |
|
String str1=""; |
String[] lcd_lines; |
String[] menu_items; |
long last_run=0; |
int last_key=0; |
|
|
int auto_next_state=-1; |
|
int wi,he; |
// Activity context; |
|
|
Activity root; |
DUBwise root; |
MKParamEditor param_editor; |
// Activity root; |
int lcd_top; |
int menu_y=0; |
int act_menu_select=0; |
int[] motortest_vals={0,0,0,0}; |
|
public DUBwiseView(Context context) { |
public DUBwiseView(DUBwise context) { |
super(context); |
root=(Activity) context; |
lcd_lines=new String[main_menu_items.length]; |
for (int y=0;y<main_menu_items.length;y++) |
lcd_lines[y]=" " + main_menu_items[y]; |
root=context; |
param_editor=new MKParamEditor(root); |
chg_state_(STATEID_MAINMENU); |
|
// needed to get Key Events |
setFocusable(true); |
|
|
} |
|
|
public void chg_state(int next_state) |
{ |
auto_next_state=next_state; |
|
} |
|
public void chg_state_(int next_state) |
{ |
auto_next_state=-1; |
menu_active=false; |
state_intro_frame=0; |
if (next_state!=state)act_menu_select=0; |
// prepare next state |
switch(next_state) |
{ |
|
case STATEID_EDIT_PARAMS: |
lcd_lines=param_editor.public_lcd_lines; |
calc_lcd(); |
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]=root.mk.params.names[i]; |
|
lcd_lines=new String[5]; |
break; |
|
case STATEID_MAINMENU: |
menu_active=true; |
menu_items=main_menu_items; |
lcd_lines=new String[menu_items.length]; |
|
|
for (int y=0;y<main_menu_items.length;y++) |
lcd_lines[y]=" " + main_menu_items[y]; |
|
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]+=(!root.fullscreen)?"Off":"On"; |
menu_items[5]+=(!keep_lighton)?"Off":"On"; |
|
lcd_lines=new String[menu_items.length]; |
break; |
|
} |
|
// switch state |
if (lcd_img!=null)calc_lcd(); |
state=next_state; |
} |
|
|
public Bitmap resize_to_screen(Bitmap orig,float x_scale_,float y_scale_) |
{ |
// createa matrix for the manipulation |
89,45 → 189,233 |
|
public boolean onKeyDown(int keyCode, KeyEvent event) |
{ |
/* try |
{ |
MediaPlayer mp = MediaPlayer.create(root, R.raw.voice_sample_1); |
mp.prepare(); |
mp.start(); |
} |
catch ( Exception e) |
{ |
|
} |
|
*/ |
|
if ( keyCode==KeyEvent.KEYCODE_BACK) |
{ |
if ( state==STATEID_MAINMENU) |
root.finish(); |
else |
chg_state(STATEID_MAINMENU); |
} |
switch (state) |
{ |
case STATEID_EDIT_PARAMS: |
param_editor.keypress(keyCode,keyCode); |
lcd_lines=param_editor.public_lcd_lines; |
calc_lcd(); |
break; |
case STATEID_MAINMENU: |
switch ( keyCode) |
{ |
case KeyEvent.KEYCODE_DPAD_DOWN : |
menu_y++; |
act_menu_select++; |
break; |
|
case KeyEvent.KEYCODE_DPAD_UP : |
menu_y--; |
act_menu_select--; |
break; |
|
case KeyEvent.KEYCODE_DPAD_CENTER : |
root.finish(); |
menu_reaction(); |
break; |
|
} |
break; |
|
last_key=keyCode; |
case STATEID_FLIGHTVIEW: |
|
switch ( keyCode) |
{ |
|
case KeyEvent.KEYCODE_DPAD_DOWN : |
root.mk.LCD.LCD_NEXTPAGE(); |
lcd_lines=root.mk.LCD.get_act_page(); |
|
break; |
|
case KeyEvent.KEYCODE_DPAD_UP : |
root.mk.LCD.LCD_PREVPAGE(); |
lcd_lines=root.mk.LCD.get_act_page(); |
|
break; |
|
|
} |
calc_lcd(); |
break; |
} |
|
// last_key=keyCode; |
// if(lcd_img!=null) |
calc_lcd(); |
invalidate(); |
return true; |
} |
|
public void menu_reaction() |
{ |
switch (act_menu_select) |
{ |
|
case MAINMENU_PARAMS: |
chg_state(STATEID_EDIT_PARAMS); |
break; |
|
case MAINMENU_STICKS: |
chg_state(STATEID_STICKVIEW); |
break; |
|
case MAINMENU_TELEMETRY: |
chg_state(STATEID_FLIGHTVIEW); |
break; |
|
case MAINMENU_RAWDEBUG: |
chg_state(STATEID_RAWDEBUG); |
break; |
|
case MAINMENU_KEYCONTROL: |
chg_state(STATEID_KEYCONTROL); |
break; |
|
|
case MAINMENU_MOTORTEST: |
// root.mk.motor_test( motortest_vals); |
chg_state(STATEID_MOTORTEST); |
break; |
|
case MAINMENU_QUIT: |
|
root.quit(); |
break; |
} |
} |
|
|
|
|
int flight_x,flight_y; |
|
@Override public boolean onTouchEvent(MotionEvent event) { |
|
|
if ((event.getAction() ==MotionEvent.ACTION_UP)&&(event.getY()<bt_on_img.height())) |
{ |
if ( state==STATEID_MAINMENU) |
root.finish(); |
else |
chg_state(STATEID_MAINMENU); |
} |
|
switch(state) |
{ |
case STATEID_KEYCONTROL: |
if (event.getAction() ==MotionEvent.ACTION_UP) |
{ |
flight_x=getWidth()/2-getWidth()/8; |
flight_y=getHeight()/2-getWidth()/8; |
} |
else |
{ |
if(new RectF(getWidth()/8,(getHeight()-getWidth())/2-getWidth()/8,getWidth()-getWidth()/8,getHeight()-getWidth()/8).contains(event.getX(),event.getY())) |
{ |
|
flight_x=(int)event.getX(); |
flight_y=(int)event.getY(); |
|
} |
} |
break; |
case STATEID_MAINMENU: |
if ((event.getAction() ==MotionEvent.ACTION_DOWN)||(event.getAction() ==MotionEvent.ACTION_MOVE)) |
{ |
if (event.getY()>lcd_top) |
{ |
act_menu_select=(int)((event.getY()-lcd_top)/lcd_tiles_img.height()); |
calc_lcd(); |
} |
} |
|
if (event.getAction() ==MotionEvent.ACTION_UP) |
|
{ |
if (event.getY()>lcd_top) |
{ |
act_menu_select=(int)((event.getY()-lcd_top)/lcd_tiles_img.height()); |
menu_reaction(); |
} |
} |
calc_lcd(); |
|
break; |
|
|
case STATEID_MOTORTEST: |
if (new RectF(getWidth()/2 - getWidth()/8,getHeight()/2 -getWidth()/8 - (getWidth()/2 - getWidth()/8),getWidth()/2 + getWidth()/8,getHeight()/2 -getWidth()/8).contains(event.getX(),event.getY())) |
motortest_vals[0]= (int)(event.getY()-getHeight()/2+getWidth()/8)*(-1)-5; |
|
if (new RectF(getWidth()/2 - getWidth()/8,getHeight()/2 + getWidth()/8,getWidth()/2 + getWidth()/8,getHeight()/2+getWidth()/8 + (getWidth()/2 - getWidth()/8)).contains(event.getX(),event.getY())) |
motortest_vals[1]= (int)(event.getY()-getHeight()/2-getWidth()/8)-5; |
|
// left |
if (new RectF(0,getHeight()/2 - getWidth()/8,getWidth()/2 - getWidth()/8,getHeight()/2+getWidth()/8).contains(event.getX(),event.getY())) |
motortest_vals[2]= (int)(event.getX()-getWidth()/2+getWidth()/8)*(-1)-5; |
|
if (new RectF(getWidth()/2+getWidth()/8,getHeight()/2 - getWidth()/8,getWidth(),getHeight()/2+getWidth()/8).contains(event.getX(),event.getY())) |
motortest_vals[3]= (int)(event.getX()-getWidth()/2-getWidth()/8)-5; |
|
for (int tmp=0;tmp<4;tmp++) |
if (motortest_vals[tmp]<0)motortest_vals[tmp]=0; |
|
root.mk.motor_test( motortest_vals); |
break; |
} |
|
|
return true; |
|
|
} |
|
|
|
|
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) |
{ |
|
bg_img = resize_to_screen(BitmapFactory.decodeResource(getResources(), R.drawable.starfield),0f,1f); |
lcd_tiles = resize_to_screen(BitmapFactory.decodeResource(getResources(), R.drawable.lcd_green),0.05f*LCD_CHAR_COUNT,0f); |
lcd_tiles_img = resize_to_screen(BitmapFactory.decodeResource(getResources(), R.drawable.lcd_green),0.05f*LCD_CHAR_COUNT,0f); |
icon_img = resize_to_screen(BitmapFactory.decodeResource(getResources(), R.drawable.icon),0.15f,0f); |
|
bt_off_img = resize_to_screen(BitmapFactory.decodeResource(getResources(), R.drawable.bt_off),0.06f,0f); |
bt_on_img = resize_to_screen(BitmapFactory.decodeResource(getResources(), R.drawable.bt_on),0.06f,0f); |
calc_lcd(); |
|
} |
|
|
public void calc_lcd() |
{ |
lcd_top=getHeight()-lcd_lines.length*lcd_tiles.height(); |
|
lcd_top=getHeight()-lcd_lines.length*lcd_tiles_img.height(); |
Paint paint = mPaint; |
lcd_img= Bitmap.createBitmap(getWidth(),lcd_lines.length*lcd_tiles.height()+100,false); |
lcd_img= Bitmap.createBitmap(getWidth(),lcd_lines.length*lcd_tiles_img.height()+100,false); |
Canvas lcd_canvas=new Canvas(); |
|
lcd_canvas.setDevice(lcd_img); |
lcd_canvas.drawColor(Color.WHITE); |
int char_width=(int)(lcd_tiles.width()/LCD_CHAR_COUNT); |
int char_width=(int)(lcd_tiles_img.width()/LCD_CHAR_COUNT); |
for ( int lcd_line=0 ; lcd_line < lcd_lines.length ; lcd_line++) |
for (int char_pos=0;char_pos<20;char_pos++) |
{ |
136,42 → 424,226 |
if (char_pos<lcd_lines[lcd_line].length()) |
act_char=lcd_lines[lcd_line].charAt(char_pos)-32; |
|
if ((menu_y==lcd_line)&& (char_pos==0)) |
if ((menu_active)&&(act_menu_select==lcd_line)&& (char_pos==0)) |
act_char=30; |
|
lcd_canvas.clipRect(new RectF(char_pos*char_width,lcd_tiles.height()*lcd_line,(char_pos+1)*char_width,lcd_tiles.height()*(lcd_line+1)),Op.REPLACE ); |
lcd_canvas.clipRect(new RectF(char_pos*char_width,lcd_tiles_img.height()*lcd_line,(char_pos+1)*char_width,lcd_tiles_img.height()*(lcd_line+1)),Op.REPLACE ); |
|
lcd_canvas.drawBitmap(lcd_tiles,(char_pos-act_char)*(char_width),lcd_tiles.height()*(lcd_line) , paint); |
lcd_canvas.drawBitmap(lcd_tiles_img,(char_pos-act_char)*(char_width),lcd_tiles_img.height()*(lcd_line) , paint); |
|
|
} |
} |
|
// fixme -> put in own timed thread - not in draw invalidate loop |
public void tick() |
{ |
pos--; |
pos%=bg_img.getWidth(); |
//SystemClock.sleep(50); |
|
if (auto_next_state==-1) |
switch(state) |
{ |
|
case STATEID_FLIGHTVIEW: |
lcd_lines=root.mk.LCD.get_act_page(); |
calc_lcd(); |
if (state_intro_frame<200) |
state_intro_frame+=5; |
break; |
case STATEID_EDIT_PARAMS: |
|
|
case STATEID_MAINMENU: |
if (state_intro_frame<200) |
state_intro_frame+=5; |
break; |
|
case STATEID_RAWDEBUG: |
case STATEID_MOTORTEST: |
if (state_intro_frame<150) |
state_intro_frame+=5; |
break; |
|
case STATEID_KEYCONTROL: |
case STATEID_STICKVIEW: |
if (state_intro_frame<100) |
state_intro_frame+=3; |
break; |
|
} |
else |
{ |
if (state_intro_frame>10) |
state_intro_frame-=7; |
else |
{ |
state_intro_frame=0; |
chg_state_(auto_next_state); |
} |
|
|
|
} |
|
} |
|
|
@Override protected void onDraw(Canvas canvas) { |
|
|
|
tick(); |
|
Paint paint = mPaint; |
paint.setAntiAlias(true); |
|
|
paint.setARGB(255,0,0,0); |
canvas.drawBitmap(bg_img,pos,0 , paint); |
|
if ((bg_img.width()+pos)<(getWidth())) |
canvas.drawBitmap(bg_img,pos+bg_img.width(),0 , paint); |
|
|
switch ( state ) |
{ |
case STATEID_EDIT_PARAMS: |
case STATEID_FLIGHTVIEW: |
paint.setARGB(state_intro_frame ,0,0,0); |
canvas.drawBitmap(lcd_img,0,lcd_top , paint); |
break; |
|
case STATEID_MOTORTEST: |
|
paint.setARGB(state_intro_frame,100,100,100); |
|
|
//front |
|
canvas.drawRoundRect(new RectF(getWidth()/2 - getWidth()/8,getHeight()/2 -getWidth()/8 - (getWidth()/2 - getWidth()/8),getWidth()/2 + getWidth()/8,getHeight()/2 -getWidth()/8),5,5,paint); |
|
// back |
canvas.drawRoundRect(new RectF(getWidth()/2 - getWidth()/8,getHeight()/2 + getWidth()/8,getWidth()/2 + getWidth()/8,getHeight()/2+getWidth()/8 + (getWidth()/2 - getWidth()/8)),5,5,paint); |
|
// left |
canvas.drawRoundRect(new RectF(0,getHeight()/2 - getWidth()/8,getWidth()/2 - getWidth()/8,getHeight()/2+getWidth()/8),5,5,paint); |
|
canvas.drawRoundRect(new RectF(getWidth()/2+getWidth()/8,getHeight()/2 - getWidth()/8,getWidth(),getHeight()/2+getWidth()/8),5,5,paint); |
|
|
paint.setARGB(100,30,30,255); |
|
canvas.drawRoundRect(new RectF(getWidth()/2 - getWidth()/8,getHeight()/2 -getWidth()/8 - motortest_vals[0],getWidth()/2 + getWidth()/8,getHeight()/2 -getWidth()/8),5,5,paint); |
|
// back |
canvas.drawRoundRect(new RectF(getWidth()/2 - getWidth()/8,getHeight()/2 + getWidth()/8,getWidth()/2 + getWidth()/8,getHeight()/2+getWidth()/8 + motortest_vals[1]),5,5,paint); |
|
// left |
canvas.drawRoundRect(new RectF(getWidth()/2-getWidth()/8- motortest_vals[2],getHeight()/2 - getWidth()/8,getWidth()/2 - getWidth()/8,getHeight()/2+getWidth()/8),5,5,paint); |
|
canvas.drawRoundRect(new RectF(getWidth()/2+getWidth()/8,getHeight()/2 - getWidth()/8,getWidth()/2+getWidth()/8+ motortest_vals[3],getHeight()/2+getWidth()/8),5,5,paint); |
|
|
paint.setARGB(state_intro_frame+70,0,250,0); |
paint.setTextAlign(Paint.Align.CENTER); |
|
canvas.drawText("Front:"+ motortest_vals[0],getWidth()/2 ,getHeight()/2 -getWidth()/8-10,paint); |
|
canvas.drawText("Back:"+ motortest_vals[1],getWidth()/2 ,getHeight()/2 +getWidth()/8+15,paint); |
|
canvas.drawText("Left:"+ motortest_vals[2],getWidth()/4 ,getHeight()/2 ,paint); |
canvas.drawText("Right:"+ motortest_vals[3],3*getWidth()/4 ,getHeight()/2 ,paint); |
break; |
|
case STATEID_RAWDEBUG: |
paint.setARGB(state_intro_frame,50,50,200); |
|
|
for(int y_p=0;y_p<16;y_p++) |
canvas.drawRoundRect(new RectF(0,(getHeight()/32)*y_p*2,getWidth(),(getHeight()/32)*(y_p*2+1)),5,5,paint); |
|
|
|
paint.setARGB(state_intro_frame,0,250,0); |
|
for(int y_p=0;y_p<32;y_p++) |
{ |
canvas.drawText( root.mk.debug_data.names[y_p],0,(getHeight()/32)*(y_p+1)-2,paint); |
canvas.drawText( ""+root.mk.debug_data.analog[y_p],getWidth()/3,(getHeight()/32)*(y_p+1)-2,paint); |
} |
|
|
|
|
break; |
|
case STATEID_KEYCONTROL: |
paint.setARGB(state_intro_frame,0,0,255); |
// canvas.drawRoundRect(new RectF(getWidth()/2-getWidth()/8,getHeight()/2-getWidth()/8,getWidth()/2+getWidth()/8,getHeight()/2+getWidth()/8),5,5,paint); |
|
canvas.drawRoundRect(new RectF(flight_x,flight_y,flight_x+getWidth()/8,flight_y+getWidth()/8),5,5,paint); |
paint.setARGB(255,0,0,0); |
break; |
|
case STATEID_STICKVIEW: |
paint.setARGB(state_intro_frame,50,50,200); |
|
for(int y_p=0;y_p<10;y_p++) |
canvas.drawRoundRect(new RectF(getWidth()/3 +((root.mk.stick_data.stick[y_p]<0)?(((root.mk.stick_data.stick[y_p]*getWidth()/3)/127)):0) ,(getHeight()/10)*y_p,getWidth()-getWidth()/3+((root.mk.stick_data.stick[y_p]>0)?(((root.mk.stick_data.stick[y_p]*getWidth()/3)/127)):0) ,(getHeight()/10)*(y_p+1)),15,15,paint); |
paint.setARGB(state_intro_frame*2+50,0,255,0); |
paint.setTextAlign(Paint.Align.CENTER); |
for(int y_p=0;y_p<10;y_p++) |
canvas.drawText("Chan " + (y_p+1) + "("+root.mk.stick_data.stick[y_p]+")",getWidth()/2,(getHeight()/20)*(y_p*2+1),paint); |
paint.setTextAlign(Paint.Align.LEFT); |
|
|
canvas.drawText("RC-Signal: " + root.mk.debug_data.SenderOkay(),0,10,paint); |
break; |
|
case STATEID_MAINMENU: |
paint.setARGB(state_intro_frame ,0,0,0); |
canvas.drawBitmap(lcd_img,0,lcd_top , paint); |
|
|
int spacer=15; |
|
int y_pos=10; |
|
paint.setColor(Color.GREEN); |
canvas.drawText("LastKeyCode:"+last_key,10,10,paint); |
// canvas.drawText("LastKeyCode:"+last_key,0,10,paint); |
paint.setTextAlign(Paint.Align.LEFT); |
if (root.mk.connected) |
{ |
canvas.drawText("Connected to MK with Version:"+root.mk.version.major+"."+root.mk.version.minor,0,y_pos,paint); |
y_pos+=spacer; |
canvas.drawText(" Power Source: " +( root.mk.debug_data.UBatt()/10) + "." + ( root.mk.debug_data.UBatt()%10) + " Volts | RC-Signal: " + root.mk.debug_data.SenderOkay(),0,y_pos,paint); |
y_pos+=spacer; |
canvas.drawText(" debug:"+root.mk.debug_data_count+ " LCD:" + root.mk.lcd_data_count + "(Pages:" + root.mk.LCD.pages + ") vers:" + root.mk.version_data_count,0,y_pos,paint); |
y_pos+=spacer; |
canvas.drawText(" other:"+root.mk.other_data_count+" params:"+root.mk.params_data_count,0,y_pos,paint); |
} |
else |
{ |
canvas.drawText("No QuadroKopter Communication established.",0,y_pos,paint); |
y_pos+=spacer; |
} |
break; |
} |
|
|
|
paint.setARGB(255,255,255,255); |
// icon indicating QC is connected |
// !!FIXME!! -10 by screensize |
canvas.drawBitmap(icon_img,getWidth()-icon_img.width(),-10 , paint); |
if (root.mk.ready()) |
canvas.drawBitmap(bt_on_img,getWidth()-icon_img.width()-bt_on_img.width()-5,5 , paint); |
else |
canvas.drawBitmap(bt_off_img,getWidth()-icon_img.width()-bt_on_img.width()-5,5 , paint); |
|
|
paint.setARGB(255,0,0,0); |
invalidate(); |
} |
} |