Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added servos slider
  • Loading branch information
iws10001 committed Apr 4, 2016
1 parent d96486d commit 8b7fa32
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 78 deletions.
Expand Up @@ -18,15 +18,54 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
public class MainActivity extends AppCompatActivity
{
//declare variables
SeekBar seekbar1; //Your SeekBar
int value; //The SeekBar value output
SeekBar seekbar1, seekbar2; //Your SeekBar
SeekBarChangeListener listener1, listener2;
TextView result; //The TextView which will display the result
BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
BluetoothSocket robotSock;
OutputStream outputStream;

/**
* A listener for the seek bars
* Designed to be somewhat agnostic, so that the seekbar it corresponds to can represent either a motor or servo
* Thus to change between tank steering and servo steering, you should only need to modify the Arduino code
*/
private class SeekBarChangeListener implements SeekBar.OnSeekBarChangeListener
{
private int idNum, value;
private static final int RANGE = 7; //range of the control axis in one direction (e.g. if RANGE = 7, the range of possible values to send is -7 to 7)

public SeekBarChangeListener(int num)
{
this.idNum = num;
this.value = 0;
}

public int getValue()
{
return value;
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
//result.setText ("Value:"+ progress);
this.value = progress - RANGE;
seekBarChanged();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar){}

@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
seekBar.setProgress(RANGE);
}
}

public void exitMethod(){
System.exit(0);
Expand All @@ -39,12 +78,17 @@ public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

// setContentView(R.layout.activity_seek_bar);
seekbar1 = (SeekBar)findViewById(R.id.sbBar);
result = (TextView)findViewById(R.id.tvResult);
seekbar1 = (SeekBar)findViewById(R.id.motorBar1);
seekbar2 = (SeekBar)findViewById(R.id.motorBar2);
//result = (TextView)findViewById(R.id.tvResult);


//set change listener
seekbar1.setOnSeekBarChangeListener(this);
listener1 = new SeekBarChangeListener(1);
listener2 = new SeekBarChangeListener(2);
seekbar1.setOnSeekBarChangeListener(listener1);
seekbar2.setOnSeekBarChangeListener(listener2);

if(!BA.isEnabled()){
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Expand Down Expand Up @@ -73,27 +117,24 @@ public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBar
} catch (IOException e) {
e.printStackTrace();
}
}



/*
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
public void seekBarChanged()
{
byte toSend = (byte)(listener1.getValue()<<4 | (listener2.getValue() & 0xf));
Log.d("seekbars", String.format("Motor value: %d\nServo value: %d", listener1.getValue(), listener2.getValue()));
//Log.d("bt", String.format("Sending value: %x", toSend));
sendToRobot(toSend);
}
public void sendToRobot(char msg) {

public void sendToRobot(byte msg) {
if (outputStream != null)
{
char[] oneChar = {msg};
try {
outputStream.write(new String(oneChar).getBytes());
byte[] bytes = {msg};
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace(); //who cares anyway
e.printStackTrace();
}
}
else
Expand All @@ -102,26 +143,6 @@ public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBar
}
}

//method for when the progress bar is changed
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
value = progress;
result.setText ("Value:"+value);
int lMotorVal = progress - 8; //TODO no magic numbers
char toSend = (char)(lMotorVal<<4);
sendToRobot(toSend);

}
//method for when the progress bar is first touched
public void onStartTrackingTouch(SeekBar seekBar) {

}
//method for when the progress bar is released
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(7);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down
118 changes: 82 additions & 36 deletions TestApplication/app/src/main/res/layout/content_main.xml
Expand Up @@ -18,47 +18,93 @@
android:orientation="vertical"
android:id="@+id/linearLayout">
//SeekBar layout
<SeekBar
android:id="@+id/sbBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:paddingTop="20dp"
android:max="15" />
//TextViews for SeekBarlegend, just text which means it does not effect the actual values
<RelativeLayout
android:layout_width="fill_parent"

//TextView to display result</LinearLayout>

<SeekBar
android:id="@+id/motorBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:paddingTop="20dp"
android:max="14"
android:layout_marginTop="64dp"
android:layout_below="@+id/linearLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:id="@+id/relativeLayout"
android:layout_below="@+id/motorBar1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:id="@+id/tvLabel1"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvLabel1"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-8" />
<TextView
android:id="@+id/tvLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0" />
<TextView
android:id="@+id/tvLabel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:text="7" />
</RelativeLayout>
//TextView to display result
android:text="-7" />
<TextView
android:id="@+id/tvResult"
android:id="@+id/tvLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:layout_centerHorizontal="true"
android:text="0" />
<TextView
android:id="@+id/tvLabel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:text="7" />
</RelativeLayout>

<SeekBar
android:id="@+id/motorBar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"
android:paddingTop="20dp"
android:max="14"
android:layout_marginTop="197dp"
android:layout_below="@+id/motorBar1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_below="@+id/motorBar2"
android:layout_alignParentLeft="true">

<TextView
android:id="@+id/textView"
android:paddingLeft="10dp"
android:text="Result" />
</LinearLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-7" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10dp"
android:text="7" />
</RelativeLayout>

</RelativeLayout>

0 comments on commit 8b7fa32

Please sign in to comment.