Update ControlActivity.kt

master
Zhengyu Peng 3 years ago
parent 312af3e8f2
commit 7033e4bdf3

@ -69,6 +69,31 @@ enum class TypeOption(
} }
class ControlActivity : AppCompatActivity() { class ControlActivity : AppCompatActivity() {
companion object {
private const val CMD_STANDBY = "standby:"
private const val CMD_LAYDOWN = "laydown:"
private const val CMD_FORWARD = "forward:"
private const val CMD_BACKWARD = "backward:"
private const val CMD_FASTFORWARD = "fastforward:"
private const val CMD_FASTBACKWARD = "fastbackward:"
private const val CMD_SHIFTLEFT = "shiftleft:"
private const val CMD_SHIFTRIGHT = "shiftright:"
private const val CMD_TURNLEFT = "turnleft:"
private const val CMD_TURNRIGHT = "turnright:"
private const val CMD_CLIMBFORWARD = "climbforward:"
private const val CMD_CLIMBBACKWARD = "climbbackward:"
private const val CMD_ROTATEX = "rotatex:"
private const val CMD_ROTATEY = "rotatey:"
private const val CMD_ROTATEZ = "rotatez:"
private const val CMD_TWIST = "twist:"
}
private var width = 0 private var width = 0
private var height = 0 private var height = 0
private var radius = 0f private var radius = 0f
@ -84,10 +109,9 @@ class ControlActivity : AppCompatActivity() {
private var btClient: BluetoothClient? = null private var btClient: BluetoothClient? = null
private var mac: String = "" private var mac: String = ""
private val scope = CoroutineScope(Job() + Dispatchers.IO) private val scope = CoroutineScope(Job() + Dispatchers.IO)
private var currentState: String = "standby" private var currentState: String = CMD_STANDBY
private lateinit var progressBar: ConstraintLayout private lateinit var progressBar: ConstraintLayout
private var controlImage: ImageView? = null private var controlImage: ImageView? = null
@ -122,7 +146,6 @@ class ControlActivity : AppCompatActivity() {
progressBar = findViewById<ConstraintLayout>(R.id.progressBar) progressBar = findViewById<ConstraintLayout>(R.id.progressBar)
buttonRotateX = findViewById(R.id.button_rotatex) buttonRotateX = findViewById(R.id.button_rotatex)
// buttonRotateX!!.backgroundTintList = applicationContext.getColorStateList(R.color.purple_500)
buttonRotateY = findViewById(R.id.button_rotatey) buttonRotateY = findViewById(R.id.button_rotatey)
buttonRotateZ = findViewById(R.id.button_rotatez) buttonRotateZ = findViewById(R.id.button_rotatez)
buttonClimb = findViewById(R.id.button_climb) buttonClimb = findViewById(R.id.button_climb)
@ -135,7 +158,6 @@ class ControlActivity : AppCompatActivity() {
height = controlImage!!.measuredHeight height = controlImage!!.measuredHeight
width = controlImage!!.measuredWidth width = controlImage!!.measuredWidth
radius = width.coerceAtMost(height) / 2f radius = width.coerceAtMost(height) / 2f
println(radius)
return true return true
} }
}) })
@ -157,10 +179,9 @@ class ControlActivity : AppCompatActivity() {
val length = sqrt(coorX.pow(2) + coorY.pow(2)) val length = sqrt(coorX.pow(2) + coorY.pow(2))
if (length < radius / 3) { if (length < radius / 3) {
if (currentState != "standby") { if (currentState != CMD_STANDBY) {
println("Standby") sendMessageAsync(CMD_STANDBY)
sendMessageAsync("standby") currentState = CMD_STANDBY
currentState = "standby"
controlImage!!.setImageResource(R.drawable.ic_control_circle_standby) controlImage!!.setImageResource(R.drawable.ic_control_circle_standby)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =
@ -177,31 +198,27 @@ class ControlActivity : AppCompatActivity() {
} 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)
if (angle > -PI / 4 && angle <= PI / 4) { if (angle > -PI / 4 && angle <= PI / 4) {
if (currentState != "shiftright") { if (currentState != CMD_SHIFTRIGHT) {
println("Move right") sendMessageAsync(CMD_SHIFTRIGHT)
sendMessageAsync("shiftright") currentState = CMD_SHIFTRIGHT
currentState = "shiftright"
controlImage!!.setImageResource(R.drawable.ic_control_circle_right) 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 != CMD_BACKWARD) {
println("Move back") sendMessageAsync(CMD_BACKWARD)
sendMessageAsync("backward") currentState = CMD_BACKWARD
currentState = "backward"
controlImage!!.setImageResource(R.drawable.ic_control_circle_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 != CMD_FORWARD) {
println("Move forward") sendMessageAsync(CMD_FORWARD)
sendMessageAsync("forward") currentState = CMD_FORWARD
currentState = "forward"
controlImage!!.setImageResource(R.drawable.ic_control_circle_forward) controlImage!!.setImageResource(R.drawable.ic_control_circle_forward)
} }
} else { } else {
if (currentState != "shiftleft") { if (currentState != CMD_SHIFTLEFT) {
println("Move left") sendMessageAsync(CMD_SHIFTLEFT)
sendMessageAsync("shiftleft") currentState = CMD_SHIFTLEFT
currentState = "shiftleft"
controlImage!!.setImageResource(R.drawable.ic_control_circle_left) controlImage!!.setImageResource(R.drawable.ic_control_circle_left)
} }
} }
@ -218,31 +235,27 @@ class ControlActivity : AppCompatActivity() {
} 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) {
if (currentState != "rightturn") { if (currentState != CMD_TURNRIGHT) {
println("Turn right") sendMessageAsync(CMD_TURNRIGHT)
sendMessageAsync("rightturn") currentState = CMD_TURNRIGHT
currentState = "rightturn"
controlImage!!.setImageResource(R.drawable.ic_control_circle_turnright) 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 != CMD_FASTBACKWARD) {
println("Fast back") sendMessageAsync(CMD_FASTBACKWARD)
// sendMessageAsync("Fast back") currentState = CMD_FASTBACKWARD
currentState = "fastback"
controlImage!!.setImageResource(R.drawable.ic_control_circle_fastbackward) 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 != CMD_FASTFORWARD) {
println("Fast forward") sendMessageAsync(CMD_FASTFORWARD)
sendMessageAsync("fastforward") currentState = CMD_FASTFORWARD
currentState = "fastforward"
controlImage!!.setImageResource(R.drawable.ic_control_circle_fastforward) controlImage!!.setImageResource(R.drawable.ic_control_circle_fastforward)
} }
} else { } else {
if (currentState != "leftturn") { if (currentState != CMD_TURNLEFT) {
println("Turn left") sendMessageAsync(CMD_TURNLEFT)
sendMessageAsync("leftturn") currentState = CMD_TURNLEFT
currentState = "leftturn"
controlImage!!.setImageResource(R.drawable.ic_control_circle_turnleft) controlImage!!.setImageResource(R.drawable.ic_control_circle_turnleft)
} }
} }
@ -263,9 +276,9 @@ class ControlActivity : AppCompatActivity() {
) )
buttonRotateX!!.setOnClickListener { buttonRotateX!!.setOnClickListener {
if (currentState != "rotatex") { if (currentState != CMD_ROTATEX) {
sendMessageAsync("rotatex") sendMessageAsync(CMD_ROTATEX)
currentState = "rotatex" currentState = CMD_ROTATEX
controlImage!!.setImageResource(R.drawable.ic_control_circle) controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =
applicationContext.getColorStateList(R.color.purple_500) applicationContext.getColorStateList(R.color.purple_500)
@ -281,9 +294,9 @@ class ControlActivity : AppCompatActivity() {
} }
buttonRotateY!!.setOnClickListener { buttonRotateY!!.setOnClickListener {
if (currentState != "rotatey") { if (currentState != CMD_ROTATEY) {
sendMessageAsync("rotatey") sendMessageAsync(CMD_ROTATEY)
currentState = "rotatey" currentState = CMD_ROTATEY
controlImage!!.setImageResource(R.drawable.ic_control_circle) controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =
applicationContext.getColorStateList(R.color.grey_500) applicationContext.getColorStateList(R.color.grey_500)
@ -299,9 +312,9 @@ class ControlActivity : AppCompatActivity() {
} }
buttonRotateZ!!.setOnClickListener { buttonRotateZ!!.setOnClickListener {
if (currentState != "rotatez") { if (currentState != CMD_ROTATEZ) {
sendMessageAsync("rotatez") sendMessageAsync(CMD_ROTATEZ)
currentState = "rotatez" currentState = CMD_ROTATEZ
controlImage!!.setImageResource(R.drawable.ic_control_circle) controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =
applicationContext.getColorStateList(R.color.grey_500) applicationContext.getColorStateList(R.color.grey_500)
@ -317,9 +330,9 @@ class ControlActivity : AppCompatActivity() {
} }
buttonClimb!!.setOnClickListener { buttonClimb!!.setOnClickListener {
if (currentState != "climb") { if (currentState != CMD_CLIMBFORWARD) {
sendMessageAsync("climb") sendMessageAsync(CMD_CLIMBFORWARD)
currentState = "climb" currentState = CMD_CLIMBFORWARD
controlImage!!.setImageResource(R.drawable.ic_control_circle) controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =
applicationContext.getColorStateList(R.color.grey_500) applicationContext.getColorStateList(R.color.grey_500)
@ -335,9 +348,9 @@ class ControlActivity : AppCompatActivity() {
} }
buttonTwist!!.setOnClickListener { buttonTwist!!.setOnClickListener {
if (currentState != "twist") { if (currentState != CMD_TWIST) {
sendMessageAsync("twist") sendMessageAsync(CMD_TWIST)
currentState = "twist" currentState = CMD_TWIST
controlImage!!.setImageResource(R.drawable.ic_control_circle) controlImage!!.setImageResource(R.drawable.ic_control_circle)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =
applicationContext.getColorStateList(R.color.grey_500) applicationContext.getColorStateList(R.color.grey_500)
@ -414,7 +427,7 @@ class ControlActivity : AppCompatActivity() {
} }
currentState = "standby" currentState = CMD_STANDBY
controlImage!!.setImageResource(R.drawable.ic_control_circle_standby) controlImage!!.setImageResource(R.drawable.ic_control_circle_standby)
buttonRotateX!!.backgroundTintList = buttonRotateX!!.backgroundTintList =

Loading…
Cancel
Save