Advertisement
Guest User

Jlechman motorControl.java

a guest
Oct 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.04 KB | None | 0 0
  1. package com.led.GUI;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7.  
  8. import android.bluetooth.BluetoothSocket;
  9. import android.content.Intent;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.SeekBar;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import android.app.ProgressDialog;
  16. import android.bluetooth.BluetoothAdapter;
  17. import android.bluetooth.BluetoothDevice;
  18. import android.os.AsyncTask;
  19.  
  20. import java.io.IOException;
  21. import java.util.UUID;
  22.  
  23.  
  24. public class motorControl extends ActionBarActivity {
  25.     //GUI instance variables for action listeners
  26.     Button stopButton;
  27.     SeekBar lMotor, rMotor;
  28.     TextView lText, rText;
  29.     private ProgressDialog progress;
  30.     //bluetooth instance variables
  31.     BluetoothAdapter myBluetooth = null;
  32.     BluetoothSocket btSocket = null;
  33.     private boolean isBtConnected = false;
  34.     String address = null;
  35.     //SPP UUID. Unique identifier "key" for bluetooth connection.
  36.     static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  37.  
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.  
  41.         Intent newint = getIntent();
  42.         address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS); //receive the address of the bluetooth device
  43.  
  44.         //android app view of the motorontrol
  45.         setContentView(R.layout.activity_motor_control);
  46.  
  47.         //initialize the GUI objects (basically link them to the objects in the xml files)
  48.         lMotor = (SeekBar) findViewById(R.id.lmotorcontrol);
  49.         rMotor = (SeekBar) findViewById(R.id.rmotorcontrol);
  50.         stopButton = (Button) findViewById(R.id.stop_button);
  51.         lText = (TextView) findViewById(R.id.lTextOutput);
  52.         rText = (TextView) findViewById(R.id.rTextOutput);
  53.  
  54.         new ConnectBT().execute(); //Call the class to connect BT
  55.  
  56.         //action listener for "STOP" button
  57.         stopButton.setOnClickListener(new View.OnClickListener() {
  58.             @Override
  59.             public void onClick(View v)     //on button press, change speed to 10 (stop movement)
  60.             {
  61.                 changeSpeed('l', 10);
  62.                 lText.setText("Power: " + "10" + "/" + "20");
  63.                 lMotor.setProgress(50);
  64.  
  65.                 changeSpeed('r', 10);
  66.                 rText.setText("Power: " + "10" + "/" + "20");
  67.                 rMotor.setProgress(50);
  68.             }
  69.         });
  70.         //action listener for left motor control slider
  71.         lText.setText("Power: " + lMotor.getProgress() / 5 + "/" + lMotor.getMax() / 5);
  72.         lMotor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  73.             int lProgress = 0;
  74.  
  75.             @Override
  76.             public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
  77.                 lProgress = progressValue / 5;
  78.                 changeSpeed('l', lProgress);  //change speed based on slider/seekbar value l = left motor, r = right motor
  79.                 //lText.setText("Power: " + lProgress + "/" + seekBar.getMax() / 5);
  80.                 //Toast.makeText(getApplicationContext(), "Seekbar progress changing", Toast.LENGTH_SHORT).show();
  81.             }
  82.  
  83.             @Override
  84.             public void onStartTrackingTouch(SeekBar seekBar) {
  85.                 //debug line
  86.                 //Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
  87.             }
  88.  
  89.             @Override
  90.             public void onStopTrackingTouch(SeekBar seekBar) {
  91.                 //debug line
  92.                 //Toast.makeText(getApplicationContext(), "Stopped tracking seekbar", Toast.LENGTH_SHORT).show();
  93.             }
  94.         });
  95.  
  96.         //action listener for right motor control
  97.         rText.setText("Power: " + rMotor.getProgress() / 5 + "/" + rMotor.getMax() / 5);
  98.         rMotor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  99.  
  100.             int rProgress = 0;
  101.  
  102.             @Override
  103.             public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
  104.                 rProgress = progressValue / 5;
  105.                 changeSpeed('r', rProgress);   //change speed based on slider/seekbar value l = left motor, r = right motor
  106.                 //rText.setText("Power: " + rProgress + "/" + seekBar.getMax() / 5);
  107.                 //debug line
  108.                 //msg("Seekbar progress changing");
  109.             }
  110.  
  111.             @Override
  112.             public void onStartTrackingTouch(SeekBar seekBar) {
  113.                 //debug line
  114.                 //msg("Started tracking seekbar");
  115.             }
  116.  
  117.             @Override
  118.             public void onStopTrackingTouch(SeekBar seekBar) {
  119.                 //debug line
  120.                 //msg("Stopped tracking seekbar");
  121.             }
  122.         });
  123.     }
  124.  
  125.  
  126.         private void changeSpeed(char motor, int sliderValue)       //send bluetooth signals to change motor speed.
  127.         {
  128.             if (motor == 'r')
  129.             {
  130.                 String state; //decide what the bluetooth state will be from the right slider value
  131.                 switch(sliderValue) {
  132.                     case (0):
  133.                         state = "u";
  134.                         break;
  135.                     case (1):
  136.                         state = "v";
  137.                         break;
  138.                     case (2):
  139.                         state = "w";
  140.                         break;
  141.                     case (3):
  142.                         state = "x";
  143.                         break;
  144.                     case (4):
  145.                         state = "y";
  146.                         break;
  147.                     case (5):
  148.                         state = "z";
  149.                         break;
  150.                     case (6):
  151.                         state = "1";
  152.                         break;
  153.                     case (7):
  154.                         state = "2";
  155.                         break;
  156.                     case (8):
  157.                         state = "3";
  158.                         break;
  159.                     case (9):
  160.                         state = "4"; //no movement 0 velocity
  161.                         break;
  162.                     case (10):
  163.                         state = "5";
  164.                         break;
  165.                     case (11):
  166.                         state = "6";
  167.                         break;
  168.                     case (12):
  169.                         state = "7";
  170.                         break;
  171.                     case (13):
  172.                         state = "8";
  173.                         break;
  174.                     case (14):
  175.                         state = "9";
  176.                         break;
  177.                     case (15):
  178.                         state = "A";
  179.                         break;
  180.                     case (16):
  181.                         state = "B";
  182.                         break;
  183.                     case (17):
  184.                         state = "C";
  185.                         break;
  186.                     case (18):
  187.                         state = "D";
  188.                         break;
  189.                     case (19):
  190.                         state = "E";
  191.                         break;
  192.                     case(20):   //need to fix
  193.                         state = "E";
  194.                         break;
  195.                     default:
  196.                         state = "Null";
  197.                         break;
  198.                 }
  199.                 rText.setText("Slider: " + sliderValue + " State: " + state);   //update the textView with the status of the slider
  200.                 //msg("power : " + sliderValue + "    "  + "state:" + state);
  201.                 //debug line
  202.                 //Toast.makeText(getApplicationContext(), "Speed r = "+ msg, Toast.LENGTH_SHORT).show();
  203.  
  204.                 if (btSocket!=null) //attempt to send a bluetooth signal, if this catches an error, it will tell the user.
  205.                 {
  206.                     try
  207.                     {
  208.                         btSocket.getOutputStream().write(String.valueOf(state).getBytes());
  209.                         //Toast.makeText(getApplicationContext(), "state r = "+ state, Toast.LENGTH_SHORT).show();
  210.                     }
  211.                     catch (IOException e)
  212.                     {
  213.                         //tell user there has been an error
  214.                         msg("Error sending rmotor bt message");
  215.                     }
  216.                 }
  217.             } else if (motor == 'l') {                  //same thing for the left motor
  218.                 String state; //decide what the bluetooth state will be from the LEFT slider value
  219.                 switch (sliderValue) {
  220.                     case (0):
  221.                         state = "a";
  222.                         break;
  223.                     case (1):
  224.                         state = "b";
  225.                         break;
  226.                     case (2):
  227.                         state = "c";
  228.                         break;
  229.                     case (3):
  230.                         state = "d";
  231.                         break;
  232.                     case (4):
  233.                         state = "e";
  234.                         break;
  235.                     case (5):
  236.                         state = "f";
  237.                         break;
  238.                     case (6):
  239.                         state = "g";
  240.                         break;
  241.                     case (7):
  242.                         state = "h";
  243.                         break;
  244.                     case (8):
  245.                         state = "i";
  246.                         break;
  247.                     case (9):
  248.                         state = "j"; //no movement 0 velocity
  249.                         break;
  250.                     case (10):
  251.                         state = "k";
  252.                         break;
  253.                     case (11):
  254.                         state = "l";
  255.                         break;
  256.                     case (12):
  257.                         state = "m";
  258.                         break;
  259.                     case (13):
  260.                         state = "n";
  261.                         break;
  262.                     case (14):
  263.                         state = "o";
  264.                         break;
  265.                     case (15):
  266.                         state = "p";
  267.                         break;
  268.                     case (16):
  269.                         state = "q";
  270.                         break;
  271.                     case (17):
  272.                         state = "r";
  273.                         break;
  274.                     case (18):
  275.                         state = "s";
  276.                         break;
  277.                     case (19):
  278.                         state = "t";
  279.                         break;
  280.                     case (20):   //need to fix
  281.                         state = "t";
  282.                         break;
  283.                     default:
  284.                         state = "Null";
  285.                         break;
  286.                 }
  287.                 lText.setText("Slider: " + sliderValue + " State: " + state);   //update the user
  288.                 //msg("power : " + sliderValue + "    "  + "state:" + state);
  289.                 //debug line
  290.                 //Toast.makeText(getApplicationContext(), "Speed r = "+ msg, Toast.LENGTH_SHORT).show();
  291.  
  292.                 if (btSocket != null) { //attempt to send thebluetooth signal, if it fails, notify the user on the display
  293.                     try {
  294.                         btSocket.getOutputStream().write(String.valueOf(state).getBytes());
  295.                         //Toast.makeText(getApplicationContext(), "state r = "+ state, Toast.LENGTH_SHORT).show();
  296.                     } catch (IOException e) {
  297.                         //debug error
  298.                         msg("Error sending rmotor bt message"); //notify user
  299.                     }
  300.                 }
  301.             }
  302.         }
  303.  
  304.  
  305.     private void Disconnect()   //if the btSocket is busy try closing the connection we have open
  306.     {
  307.         if (btSocket!=null) //If  btSocket is unavilable
  308.         {
  309.             try
  310.             {
  311.                 btSocket.close(); //close
  312.             }
  313.             catch (IOException e)
  314.             { msg("Error");}
  315.         }
  316.         finish(); //return to the first layout DeviceList.xml
  317.  
  318.     }
  319.  
  320.  
  321.     // fast way to call Toast for debug messages
  322.     private void msg(String s)
  323.     {
  324.         Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
  325.     }
  326.  
  327.     @Override
  328.     public boolean onCreateOptionsMenu(Menu menu) {
  329.         // Inflate the menu; this adds items to the action bar if it is present.
  330.         getMenuInflater().inflate(R.menu.menu_motor_control, menu);
  331.         return true;
  332.     }
  333.  
  334.     @Override
  335.     public boolean onOptionsItemSelected(MenuItem item) {
  336.         // Handle action bar item clicks here. The action bar will
  337.         // automatically handle clicks on the Home/Up button, so long
  338.         // as you specify a parent activity in AndroidManifest.xml.
  339.         int id = item.getItemId();
  340.  
  341.         //noinspection SimplifiableIfStatement
  342.         if (id == R.id.action_settings) {
  343.             return true;
  344.         }
  345.  
  346.         return super.onOptionsItemSelected(item);
  347.     }
  348.  
  349.     private class ConnectBT extends AsyncTask<Void, Void, Void>  // Bluetooth connection
  350.     {
  351.         private boolean ConnectSuccess = true; //store connection success status
  352.         @Override
  353.         protected void onPreExecute       (){
  354.             progress = ProgressDialog.show(motorControl.this, "Connecting...", "Please wait!!!");  //show a progress/status popup, mostly unessicary but nice to have
  355.         }
  356.  
  357.         @Override
  358.         protected Void doInBackground(Void... devices) //while the progress dialog is in the foreground the connection is dealth with in the app background
  359.         {
  360.             try
  361.             {
  362.                 if (btSocket == null || !isBtConnected) //check if already connected
  363.                 {
  364.                  myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the phone's bluetooth device
  365.                  BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the external device's address if it is avilable
  366.                  btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a 'connection'
  367.                  BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
  368.                  btSocket.connect();//start said connection
  369.                 }
  370.             }
  371.             catch (IOException e)
  372.             {
  373.                 ConnectSuccess = false;//Connection has failed.
  374.             }
  375.             return null;
  376.         }
  377.         @Override
  378.         protected void onPostExecute(Void result) //check to see if the connection was successful
  379.         {
  380.             super.onPostExecute(result);
  381.  
  382.             if (!ConnectSuccess)    //failed
  383.             {
  384.                 msg("Connection Failed. Try again.");   //notify user
  385.                 finish();
  386.             }
  387.             else
  388.             {
  389.                 msg("Connected!");      //notify user
  390.                 isBtConnected = true;
  391.             }
  392.             progress.dismiss();
  393.         }
  394.     }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement