Created
October 19, 2014 03:52
-
-
Save anoochit/caa2f6fcd7abbdd3e89a to your computer and use it in GitHub Desktop.
joystick controller + bluetooth spp library for arduino robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void setup() { | |
textView1 = (TextView) findViewById(R.id.textView1); | |
textView2 = (TextView) findViewById(R.id.textView2); | |
textView3 = (TextView) findViewById(R.id.textView3); | |
textView4 = (TextView) findViewById(R.id.textView4); | |
textView5 = (TextView) findViewById(R.id.textView5); | |
layout_joystick = (RelativeLayout) findViewById(R.id.layout_joystick); | |
js = new JoyStickClass(getApplicationContext() | |
, layout_joystick, R.drawable.image_button); | |
js.setStickSize(150, 150); | |
js.setLayoutSize(500, 500); | |
js.setLayoutAlpha(150); | |
js.setStickAlpha(100); | |
js.setOffset(90); | |
js.setMinimumDistance(50); | |
layout_joystick.setOnTouchListener(new OnTouchListener() { | |
public boolean onTouch(View arg0, MotionEvent arg1) { | |
js.drawStick(arg1); | |
if (arg1.getAction() == MotionEvent.ACTION_DOWN | |
|| arg1.getAction() == MotionEvent.ACTION_MOVE) { | |
textView1.setText("X : " + String.valueOf(js.getX())); | |
textView2.setText("Y : " + String.valueOf(js.getY())); | |
textView3.setText("Angle : " + String.valueOf(js.getAngle())); | |
textView4.setText("Distance : " + String.valueOf(js.getDistance())); | |
int direction = js.get8Direction(); | |
if (direction == JoyStickClass.STICK_UP) { | |
textView5.setText("Direction : Up"); | |
// sent up | |
bt.send("*11|3|"+getPWM(js.getDistance())+"#",true); | |
bt.send("*10|13|3#",true); | |
} else if (direction == JoyStickClass.STICK_UPRIGHT) { | |
textView5.setText("Direction : Up Right"); | |
} else if (direction == JoyStickClass.STICK_RIGHT) { | |
textView5.setText("Direction : Right"); | |
// sent right | |
bt.send("*11|3|"+getPWM(js.getDistance())+"#",true); | |
bt.send("*10|9|3#",true); | |
} else if (direction == JoyStickClass.STICK_DOWNRIGHT) { | |
textView5.setText("Direction : Down Right"); | |
} else if (direction == JoyStickClass.STICK_DOWN) { | |
textView5.setText("Direction : Down"); | |
// sent left | |
bt.send("*11|3|"+getPWM(js.getDistance())+"#",true); | |
bt.send("*10|12|3#",true); | |
} else if (direction == JoyStickClass.STICK_DOWNLEFT) { | |
textView5.setText("Direction : Down Left"); | |
} else if (direction == JoyStickClass.STICK_LEFT) { | |
textView5.setText("Direction : Left"); | |
// sent left | |
bt.send("*11|3|"+getPWM(js.getDistance())+"#",true); | |
bt.send("*10|10|3#",true); | |
} else if (direction == JoyStickClass.STICK_UPLEFT) { | |
textView5.setText("Direction : Up Left"); | |
} else if (direction == JoyStickClass.STICK_NONE) { | |
textView5.setText("Direction : Center"); | |
bt.send("*10|11|3#",true); | |
} | |
} else if (arg1.getAction() == MotionEvent.ACTION_UP) { | |
textView1.setText("X :"); | |
textView2.setText("Y :"); | |
textView3.setText("Angle :"); | |
textView4.setText("Distance :"); | |
textView5.setText("Direction :"); | |
bt.send("*10|11|3#",true); | |
} | |
return true; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment