From 428016ce8c3f0f3b1b8ceed36e5db5c19989d0cd Mon Sep 17 00:00:00 2001 From: Omer Schloss Date: Wed, 23 Jun 2021 17:52:54 +0300 Subject: [PATCH] throttle and rudder update --- .../flightgearcontrollerapp/model/FGModel.kt | 25 +++++++++------ .../model/TelnetClient.kt | 13 ++++++++ .../{ViewModel.kt => FGViewModel.kt} | 10 ++++++ .../views/MainActivity.kt | 31 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 5 ++- 5 files changed, 73 insertions(+), 11 deletions(-) rename app/src/main/java/com/example/flightgearcontrollerapp/view_model/{ViewModel.kt => FGViewModel.kt} (71%) diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt b/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt index ec1ae75..b9c5fed 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/model/FGModel.kt @@ -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) + } } } diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt b/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt index 8484385..d6ffea2 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/model/TelnetClient.kt @@ -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() } + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/view_model/ViewModel.kt b/app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt similarity index 71% rename from app/src/main/java/com/example/flightgearcontrollerapp/view_model/ViewModel.kt rename to app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt index ac7c39f..fb2e074 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/view_model/ViewModel.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/view_model/FGViewModel.kt @@ -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) + } + } diff --git a/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt b/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt index b8f0ba3..361451b 100644 --- a/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt +++ b/app/src/main/java/com/example/flightgearcontrollerapp/views/MainActivity.kt @@ -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) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 24a1566..e2e41d0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,7 +13,7 @@ type="com.example.flightgearcontrollerapp.view_model.JoyStickVM"/> + type="com.example.flightgearcontrollerapp.model.FGModel"/> @@ -213,6 +215,7 @@ android:textStyle="bold" />