Skip to content

Commit

Permalink
sensor controls improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
asafonov committed Apr 23, 2024
1 parent ad51862 commit e101537
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DONATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Accelerrace is an open source project, and your donations support the developer,
Platform | Link
--- | ---
Yoomoney | [Donate](https://yoomoney.ru/to/41001650076246) |
Bitcoin | bc1q59s7n40733knymc26fc6kz05xug5x46skhe3rg |
Bitcoin | 1JWPruZxtT9NwgkrRseeXBbM73ygh1rr3n |

Thank you!

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "org.asafonov.accelerace"
minSdkVersion 21
targetSdkVersion 33
versionCode 5
versionName "0.5"
versionCode 6
versionName "0.6"
}
buildTypes {
release {
Expand Down
26 changes: 15 additions & 11 deletions app/src/main/java/org/asafonov/accelerace/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
angle = 0;
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
mWebView = findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Expand Down Expand Up @@ -60,18 +60,22 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

public void onSensorChanged(SensorEvent event) {
angle += event.values[1];
double b = 8;
double y = event.values[2];
double threshold = 12;
String condition = "window !== null && window !== undefined && !! window.asafonov && !! asafonov.messageBus && !! asafonov.messageBus.send";

if (angle > b) {
angle = 0;
mWebView.evaluateJavascript("if (" + condition + ") asafonov.messageBus.send(asafonov.events.CAR_MOVE_RIGHT)", null);
}

if (angle < -b) {
angle = 0;
mWebView.evaluateJavascript("if (" + condition + ") asafonov.messageBus.send(asafonov.events.CAR_MOVE_LEFT)", null);
if (y > threshold) {
mWebView.evaluateJavascript("if (" + condition + ") asafonov.messageBus.send(asafonov.events.CAR_MOVE_RIGHT)", null);
angle = threshold;
} else if (y < -threshold) {
mWebView.evaluateJavascript("if (" + condition + ") asafonov.messageBus.send(asafonov.events.CAR_MOVE_LEFT)", null);
angle = -threshold;
} else if (angle < 0) {
mWebView.evaluateJavascript("if (" + condition + ") asafonov.messageBus.send(asafonov.events.CAR_MOVE_RIGHT)", null);
angle = 0;
} else if (angle > 0) {
mWebView.evaluateJavascript("if (" + condition + ") asafonov.messageBus.send(asafonov.events.CAR_MOVE_LEFT)", null);
angle = 0;
}
}
};
Expand Down
2 changes: 2 additions & 0 deletions metadata/en-US/changelogs/6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Improvements of control via gyroscope sensor
* Minor bugfixes

0 comments on commit e101537

Please sign in to comment.