It works!

This commit is contained in:
estevez 2017-12-25 00:58:22 +02:00
parent b181cacb8f
commit 67c6a84399
7 changed files with 271 additions and 18 deletions

26
.idea/misc.xml generated
View File

@ -1,6 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7">
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
</list>
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

9
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/VaporizR2.iml" filepath="$PROJECT_DIR$/VaporizR2.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -5,17 +5,17 @@
#define AIN1 4
#define BIN1 8
#define AIN2 5
#define BIN2 9
#define PWMA 7
#define BIN2 7
#define PWMA 11
#define PWMB 10
#define STBY 6
const int offsetA = 1;
const int offsetB = 1;
const int offsetB = -1;
SoftwareSerial BTSerial(3, 2); // RX | TX
String reader;
int d = 0;
int d = 600;
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
@ -24,6 +24,7 @@ void setup()
Serial.begin(9600);
Serial.println("Ready:");
BTSerial.begin(9600); // HC-05 default speed in AT command more
brake(motor1, motor2);
}
void loop()
@ -41,11 +42,13 @@ void loop()
Serial.println("Receved: "+reader+"");
d = reader.toInt();
}
if (d == 11) {
motor1.drive(255);
} else if (d == 12) {
motor1.drive(-255);
if (d <= 500) { //0 - full back, 255 - stop, 500 - full forward
int v1 = d - 255;
motor1.drive(v1);
} else if (d >= 1000) { // 1000 - full back, 1255 - stop, 1500 - full forward
int v2 = d - 1255;
motor2.drive(v2);
}
}

View File

@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yehor.vialov.vaporizr2">
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -9,7 +11,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity" android:configChanges="orientation"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -1,13 +1,164 @@
package yehor.vialov.vaporizr2;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private final static int REQUEST_ENABLE_BT = 1;
Button btnConnect;
OutputStream btOutStream;
Boolean btConnected = false;
BluetoothSocket btSocket = null;
SeekBar motorA;
SeekBar motorB;
TextView txtA;
TextView txtB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConnect = (Button)findViewById(R.id.btnConnect);
motorA = (SeekBar)findViewById(R.id.motorA);
motorA.setProgress(3);
motorA.setMax(6);
motorB = (SeekBar)findViewById(R.id.motorB);
motorB.setProgress(3);
motorB.setMax(6);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
motorA.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
String progress = "";
@Override
public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) {
progress = Integer.toString(progresValue*85+1000);
txtA.setText(progress);
try {
if (btOutStream != null)
btOutStream.write(progress.getBytes());
} catch (IOException e) {
//e.printStackTrace();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
//if you want to do anything at the start of
// touching the seekbar
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(3);
}
});
motorB.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
String progress = "";
@Override
public void onProgressChanged(SeekBar seekBar,
int progresValue, boolean fromUser) {
progress = Integer.toString(progresValue*85);
txtB.setText(progress);
try {
if (btOutStream != null)
btOutStream.write(progress.getBytes());
} catch (IOException e) {
//e.printStackTrace();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here,
//if you want to do anything at the start of
// touching the seekbar
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(3);
}
});
btnConnect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!btConnected) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) {
// Device does not support Bluetooth
finish(); //exit
}
if (!adapter.isEnabled()) {
//make sure the device's bluetooth is enabled
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
}
final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
String mac = "98:D3:31:F5:2D:2F"; //my laptop's mac adress
BluetoothDevice device = adapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
// Get a BluetoothSocket to connect with the given BluetoothDevice
btSocket = null;
btOutStream = null;
try {
btSocket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
} catch (IOException e) {
e.printStackTrace();
}
try {
btSocket.connect();
btOutStream = btSocket.getOutputStream();
btConnected = true;
btnConnect.setText("Disconnect");
//now you can use out to send output via out.write
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
btOutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
btSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
btConnected = false;
btnConnect.setText("Connect");
}
}
});
}
}

View File

@ -6,13 +6,70 @@
android:layout_height="match_parent"
tools:context="yehor.vialov.vaporizr2.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<SeekBar
android:id="@+id/motorA"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="182dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:max="500"
android:min="0"
android:progress="255"
android:rotation="270"
android:thumb="@android:drawable/ic_notification_overlay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/motorB"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="182dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:max="500"
android:min="0"
android:progress="255"
android:rotation="270"
android:thumb="@android:drawable/ic_notification_overlay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnConnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Connect"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/motorA" />
<TextView
android:id="@+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/motorB" />
</android.support.constraint.ConstraintLayout>