master
Zhengyu Peng 3 years ago
parent 8bb1993570
commit fd46deeaaa

@ -7,6 +7,7 @@ import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.view.* import android.view.*
import android.widget.Button
import android.widget.ImageView import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
@ -67,7 +68,6 @@ enum class TypeOption(
} }
class ControlActivity : AppCompatActivity() { class ControlActivity : AppCompatActivity() {
private var pxMargin = 0f
private var width = 0 private var width = 0
private var height = 0 private var height = 0
private var radius = 0f private var radius = 0f
@ -81,6 +81,14 @@ class ControlActivity : AppCompatActivity() {
private var currentState: String = "standby" private var currentState: String = "standby"
private lateinit var progressBar: ConstraintLayout private lateinit var progressBar: ConstraintLayout
private var controlImage: ImageView? = null
private var buttonRotateX: Button? = null
private var buttonRotateY: Button? = null
private var buttonRotateZ: Button? = null
private var buttonClimb: Button? = null
private var buttonTwist: Button? = null
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -94,22 +102,29 @@ class ControlActivity : AppCompatActivity() {
controlWindowInsets(true) controlWindowInsets(true)
val controlCircle = findViewById<ImageView>(R.id.control_image) controlImage = findViewById<ImageView>(R.id.control_image)
progressBar = findViewById<ConstraintLayout>(R.id.progressBar) progressBar = findViewById<ConstraintLayout>(R.id.progressBar)
val vto: ViewTreeObserver = controlCircle.viewTreeObserver buttonRotateX = findViewById(R.id.button_rotatex)
// buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
buttonRotateY = findViewById(R.id.button_rotatey)
buttonRotateZ = findViewById(R.id.button_rotatez)
buttonClimb = findViewById(R.id.button_climb)
buttonTwist = findViewById(R.id.button_twist)
val vto: ViewTreeObserver = controlImage!!.viewTreeObserver
vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener { vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean { override fun onPreDraw(): Boolean {
controlCircle.viewTreeObserver.removeOnPreDrawListener(this) controlImage!!.viewTreeObserver.removeOnPreDrawListener(this)
height = controlCircle.measuredHeight height = controlImage!!.measuredHeight
width = controlCircle.measuredWidth width = controlImage!!.measuredWidth
radius = width.coerceAtMost(height) / 2f radius = width.coerceAtMost(height) / 2f
println(radius) println(radius)
return true return true
} }
}) })
controlCircle.setOnTouchListener( controlImage!!.setOnTouchListener(
object : View.OnTouchListener { object : View.OnTouchListener {
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean { override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
val touchX = motionEvent.x val touchX = motionEvent.x
@ -130,6 +145,13 @@ class ControlActivity : AppCompatActivity() {
println("Standby") println("Standby")
sendMessageAsync("standby") sendMessageAsync("standby")
currentState = "standby" currentState = "standby"
controlImage!!.setImageResource(R.drawable.ic_control_circle_standby)
buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
} }
} else if (length >= radius / 3 && length < 2 * radius / 3) { } else if (length >= radius / 3 && length < 2 * radius / 3) {
val angle = atan2(coorY, coorX) val angle = atan2(coorY, coorX)
@ -138,26 +160,35 @@ class ControlActivity : AppCompatActivity() {
println("Move right") println("Move right")
sendMessageAsync("shiftright") sendMessageAsync("shiftright")
currentState = "shiftright" currentState = "shiftright"
controlImage!!.setImageResource(R.drawable.ic_control_circle_right)
} }
} else if (angle > PI / 4 && angle <= 3 * PI / 4) { } else if (angle > PI / 4 && angle <= 3 * PI / 4) {
if (currentState != "backward") { if (currentState != "backward") {
println("Move back") println("Move back")
sendMessageAsync("backward") sendMessageAsync("backward")
currentState = "backward" currentState = "backward"
controlImage!!.setImageResource(R.drawable.ic_control_circle_backward)
} }
} else if (angle > -3 * PI / 4 && angle < -PI / 4) { } else if (angle > -3 * PI / 4 && angle < -PI / 4) {
if (currentState != "forward") { if (currentState != "forward") {
println("Move forward") println("Move forward")
sendMessageAsync("forward") sendMessageAsync("forward")
currentState = "forward" currentState = "forward"
controlImage!!.setImageResource(R.drawable.ic_control_circle_forward)
} }
} else { } else {
if (currentState != "shiftleft") { if (currentState != "shiftleft") {
println("Move left") println("Move left")
sendMessageAsync("shiftleft") sendMessageAsync("shiftleft")
currentState = "shiftleft" currentState = "shiftleft"
controlImage!!.setImageResource(R.drawable.ic_control_circle_left)
} }
} }
buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
} else if (length >= 2 * radius / 3 && length < radius) { } else if (length >= 2 * radius / 3 && length < radius) {
val angle = atan2(coorY, coorX) val angle = atan2(coorY, coorX)
if (angle > -PI / 4 && angle <= PI / 4) { if (angle > -PI / 4 && angle <= PI / 4) {
@ -165,58 +196,105 @@ class ControlActivity : AppCompatActivity() {
println("Turn right") println("Turn right")
sendMessageAsync("rightturn") sendMessageAsync("rightturn")
currentState = "rightturn" currentState = "rightturn"
controlImage!!.setImageResource(R.drawable.ic_control_circle_turnright)
} }
} else if (angle > PI / 4 && angle <= 3 * PI / 4) { } else if (angle > PI / 4 && angle <= 3 * PI / 4) {
if (currentState != "fastback") { if (currentState != "fastback") {
println("Fast back") println("Fast back")
// sendMessageAsync("Fast back") // sendMessageAsync("Fast back")
currentState = "fastback" currentState = "fastback"
controlImage!!.setImageResource(R.drawable.ic_control_circle_fastbackward)
} }
} else if (angle > -3 * PI / 4 && angle < -PI / 4) { } else if (angle > -3 * PI / 4 && angle < -PI / 4) {
if (currentState != "fastforward") { if (currentState != "fastforward") {
println("Fast forward") println("Fast forward")
sendMessageAsync("fastforward") sendMessageAsync("fastforward")
currentState = "fastforward" currentState = "fastforward"
controlImage!!.setImageResource(R.drawable.ic_control_circle_fastforward)
} }
} else { } else {
if (currentState != "leftturn") { if (currentState != "leftturn") {
println("Turn left") println("Turn left")
sendMessageAsync("leftturn") sendMessageAsync("leftturn")
currentState = "leftturn" currentState = "leftturn"
controlImage!!.setImageResource(R.drawable.ic_control_circle_turnleft)
} }
} }
buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
} }
return true return true
} }
} }
) )
// this.tcpClient = TCPClient(this, ip, port, object : OnMessageReceived {
// override fun messageReceived(message: String?) { buttonRotateX!!.setOnClickListener{
// if (message == null) { if (currentState != "rotatex"){
//// alertDialog(DISCONNECTED) sendMessageAsync("rotatex")
// println("no message") currentState = "rotatex"
// } controlImage!!.setImageResource(R.drawable.ic_control_circle)
// } buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
// }, object : OnConnectEstablished { buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// override fun onConnected() { buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
//// udpClient.start() buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// println("connected") buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// Handler(Looper.getMainLooper()).post { }
// progressBar.visibility = View.GONE }
// }
// } buttonRotateY!!.setOnClickListener{
// }, object : OnDisconnected{ if (currentState != "rotatey"){
// override fun onDisconnected() { sendMessageAsync("rotatey")
// Handler(Looper.getMainLooper()).post { currentState = "rotatey"
// progressBar.visibility = View.GONE controlImage!!.setImageResource(R.drawable.ic_control_circle)
// alertDialog(0) buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// } buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
// } buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// } buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// ) buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
// this.tcpClient!!.start() }
}
// alertDialog(0)
buttonRotateZ!!.setOnClickListener{
if (currentState != "rotatez"){
sendMessageAsync("rotatez")
currentState = "rotatez"
controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
}
}
buttonClimb!!.setOnClickListener{
if (currentState != "climb"){
sendMessageAsync("climb")
currentState = "climb"
controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
}
}
buttonTwist!!.setOnClickListener{
if (currentState != "twist"){
sendMessageAsync("twist")
currentState = "twist"
controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateY!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonRotateZ!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonClimb!!.backgroundTintList = applicationContext.getColorStateList(R.color.grey_500)
buttonTwist!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
}
}
} }
override fun onResume() { override fun onResume() {

@ -37,11 +37,11 @@ class MainActivity : AppCompatActivity() {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun afterTextChanged(s: Editable) { override fun afterTextChanged(s: Editable) {
if (isNumericAddress(s.toString())) { // if (isNumericAddress(s.toString())) {
Toast.makeText(this@MainActivity, "Correct", Toast.LENGTH_SHORT).show() // Toast.makeText(this@MainActivity, "Correct", Toast.LENGTH_SHORT).show()
} else { // } else {
Toast.makeText(this@MainActivity, "Wrong", Toast.LENGTH_SHORT).show() // Toast.makeText(this@MainActivity, "Wrong", Toast.LENGTH_SHORT).show()
} // }
} }
}) })
} }

@ -46,6 +46,9 @@ class TCPClient(
} catch (e: SocketTimeoutException) { } catch (e: SocketTimeoutException) {
println(e) println(e)
onDisconnected!!.onDisconnected() onDisconnected!!.onDisconnected()
} catch (e: ConnectException) {
println(e)
onDisconnected!!.onDisconnected()
} }
} }

Loading…
Cancel
Save