throttle and rudder update

This commit is contained in:
Omer Schloss 2021-06-23 17:52:54 +03:00
parent 33ab56a376
commit 428016ce8c
5 changed files with 73 additions and 11 deletions

View File

@ -3,19 +3,10 @@ package com.example.flightgearcontrollerapp.model
class FGModel {
private val telnetClient = TelnetClient()
private var isConnected =false
private var isExceptionOccurred = false
//private val joystickView = JoystickView()
private var isTouchingJoystick = false
/*
private val jsCenterX = 0
private val jsCenterY = 0
private val jsOuterRadius = 0
private val jsInnerRadius = 0
*/
fun connect(ipAddress: String, portAddress: String): Boolean {
var res = false
val t = Thread {
@ -38,11 +29,25 @@ class FGModel {
e.printStackTrace()
return false
}
isConnected = true
return res
}
fun disconnect() {
telnetClient.disconnect()
isConnected = false
}
fun setThrottle(fl: Float) {
if(isConnected) {
telnetClient.updateThrottle(fl)
}
}
fun setRudder(fl: Float) {
if(isConnected) {
telnetClient.updateRudder(fl)
}
}
}

View File

@ -3,16 +3,21 @@ package com.example.flightgearcontrollerapp.model
import java.io.PrintWriter
import java.net.InetSocketAddress
import java.net.Socket
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class TelnetClient{
private lateinit var client:Socket
private lateinit var output:PrintWriter
private lateinit var executor: ExecutorService
fun connect(ipAddress: String, portAddress: Int): Boolean {
return try {
client = Socket()
client.connect(InetSocketAddress(ipAddress, portAddress),5000)
output = PrintWriter(client.getOutputStream(), true)
executor = Executors.newSingleThreadExecutor();
true
}catch (e: Exception){
e.printStackTrace()
@ -28,4 +33,12 @@ class TelnetClient{
e.printStackTrace()
}
}
fun updateThrottle(fl: Float) {
executor.execute { output.print("set /controls/engines/current-engine/throttle $fl\r\n");output.flush() }
}
fun updateRudder(fl: Float) {
executor.execute { output.print("set /controls/flight/rudder $fl\r\n");output.flush() }
}
}

View File

@ -2,6 +2,7 @@ package com.example.flightgearcontrollerapp.view_model
import androidx.lifecycle.ViewModel
import com.example.flightgearcontrollerapp.model.FGModel
import java.util.concurrent.ExecutorService
class FGViewModel : ViewModel() {
private val model = FGModel()
@ -15,4 +16,13 @@ class FGViewModel : ViewModel() {
fun disconnectFromFG() {
model.disconnect()
}
fun setThrottle(fl: Float) {
model.setThrottle(fl)
}
fun setRudder(fl: Float) {
model.setRudder(fl)
}
}

View File

@ -12,14 +12,45 @@ import androidx.appcompat.app.AppCompatActivity
import com.example.flightgearcontrollerapp.R
import com.example.flightgearcontrollerapp.R.id.*
import com.example.flightgearcontrollerapp.view_model.FGViewModel
import com.google.android.material.slider.Slider
class MainActivity : AppCompatActivity() {
private var isConnected = false
private val vmConnection = FGViewModel()
private lateinit var throttleSlider: Slider
private lateinit var rudderSlider: Slider
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
throttleSlider = findViewById(slider_throttle)
rudderSlider = findViewById(slider_rudder)
throttleSlider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
override fun onStartTrackingTouch(slider: Slider) {
vmConnection.setThrottle(slider.value)
}
override fun onStopTrackingTouch(slider: Slider) {
vmConnection.setThrottle(slider.value)
}
})
throttleSlider.addOnChangeListener { _, value, _ ->
vmConnection.setThrottle(value)
}
rudderSlider.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
override fun onStartTrackingTouch(slider: Slider) {
vmConnection.setRudder(slider.value)
}
override fun onStopTrackingTouch(slider: Slider) {
vmConnection.setRudder(slider.value)
}
})
rudderSlider.addOnChangeListener { _, value, _ ->
vmConnection.setRudder(value)
}
}
fun onClickConnect(view: View) {

View File

@ -13,7 +13,7 @@
type="com.example.flightgearcontrollerapp.view_model.JoyStickVM"/>
<variable
name="model"
type="com.example.flightgearcontrollerapp.view_model.FGViewModel"/>
type="com.example.flightgearcontrollerapp.model.FGModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
@ -182,9 +182,11 @@
android:gravity="center">
<com.google.android.material.slider.Slider
android:id="@+id/slider_throttle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="270"
android:value="0"
android:valueFrom="0"
android:valueTo="1" />
@ -213,6 +215,7 @@
android:textStyle="bold" />
<com.google.android.material.slider.Slider
android:id="@+id/slider_rudder"
android:layout_width="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"