master
Zhengyu Peng 3 years ago
parent 04519d3875
commit 42b4798a00

@ -32,7 +32,7 @@ android {
}
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rookiedev.hexapod">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"

@ -7,11 +7,15 @@ import android.util.TypedValue
import android.view.*
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import java.lang.Math.min
import com.rookiedev.hexapod.network.TCPClient
import com.rookiedev.hexapod.network.TCPClient.OnConnectEstablished
import com.rookiedev.hexapod.network.TCPClient.OnMessageReceived
import java.util.concurrent.locks.ReentrantLock
import kotlin.math.PI
import kotlin.math.atan2
import kotlin.math.pow
import kotlin.math.sqrt
import kotlin.math.atan2
import kotlin.math.PI
import kotlinx.coroutines.*
/**
@ -67,6 +71,14 @@ class ControlActivity : AppCompatActivity() {
private var height = 0
private var radius = 0f
private var tcpClient: TCPClient? = null
private val scope = CoroutineScope(Job() + Dispatchers.IO)
// private val lock = ReentrantLock()
// private val waitLock = lock.newCondition()
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -114,29 +126,77 @@ class ControlActivity : AppCompatActivity() {
val length = sqrt(coorX.pow(2) + coorY.pow(2))
if (length < radius / 3) {
println("Standby")
sendMessageAsync("Standby")
} else if (length >= radius / 3 && length < 2 * radius / 3) {
var angle = atan2(coorY, coorX)
if (angle>-PI/4 && angle<=PI/4)
{
if (angle > -PI / 4 && angle <= PI / 4) {
println("Move right")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Move right")
}
}
} else if (angle > PI / 4 && angle <= 3 * PI / 4) {
println("Move back")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Move back")
}
}
} else if (angle > -3 * PI / 4 && angle < -PI / 4) {
println("Move forward")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Move forward")
}
}
} else {
println("Move left")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Move left")
}
}
}
} else if (length >= 2 * radius / 3 && length < radius) {
var angle = atan2(coorY, coorX)
if (angle>-PI/4 && angle<=PI/4)
{
if (angle > -PI / 4 && angle <= PI / 4) {
println("Turn right")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Turn right")
}
}
} else if (angle > PI / 4 && angle <= 3 * PI / 4) {
println("Fast back")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Fast back")
}
}
} else if (angle > -3 * PI / 4 && angle < -PI / 4) {
println("Fast forward")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Fast forward")
}
}
} else {
println("Turn left")
runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
tcpClient?.sendMessage("Turn left")
}
}
}
}
// val width = view.width
@ -149,6 +209,21 @@ class ControlActivity : AppCompatActivity() {
}
}
)
this.tcpClient = TCPClient(this, "192.168.1.202", 1234, object : OnMessageReceived {
override fun messageReceived(message: String?) {
if (message == null) {
// alertDialog(DISCONNECTED)
println("no message")
}
}
}, object : OnConnectEstablished {
override fun onConnected() {
// udpClient.start()
println("connected")
}
}
)
this.tcpClient!!.start()
}
@ -166,5 +241,26 @@ class ControlActivity : AppCompatActivity() {
insetsController.show(type)
}
}
fun sendMessageAsync(message: String) {
// Starts a new coroutine within the scope
scope.launch {
// New coroutine that can call suspend functions
// suspend fun sendMessage(message: String) = // Dispatchers.Main
withContext(Dispatchers.IO) { // Dispatchers.IO (main-safety block)
tcpClient?.sendMessage(message)
/* perform network IO here */ // Dispatchers.IO (main-safety block)
}
}
}
// suspend fun sendMessage(message: String) = // Dispatchers.Main
// withContext(Dispatchers.IO) { // Dispatchers.IO (main-safety block)
// tcpClient?.sendMessage(message)
// /* perform network IO here */ // Dispatchers.IO (main-safety block)
// } // Dispatchers.Main
}

@ -0,0 +1,118 @@
package com.rookiedev.hexapod.network
import com.rookiedev.hexapod.ControlActivity
import android.util.Log
import java.io.*
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Socket
import java.net.UnknownHostException
import java.util.concurrent.locks.ReentrantLock
class TCPClient(
c: ControlActivity,
ip: String?,
port: Int,
messagelistener: OnMessageReceived?,
onconnected: OnConnectEstablished?
) :
Thread() {
private val controller: ControlActivity = c
private var TCPSocket:Socket? = null
private var SERVERIP: InetAddress? = null
private val SERVERPORT: Int
private var serverAddr: InetSocketAddress? = null
private var TCPOut: PrintWriter? = null
private var TCPIn: BufferedReader? = null
private var TCPMessage: String? = null
private var mMessageListener: OnMessageReceived? = null
private var onConnected: OnConnectEstablished? = null
private var isConnected = false
private var pause = false // if the thread is paused by system
private var isNewData = false
private var newMessage:String? = null
override fun run() {
try {
this.TCPSocket = Socket()
this.TCPSocket!!.soTimeout = 3000
this.TCPSocket!!.connect(serverAddr, 3000) // connecting socket and set timeout in 3s
onConnected!!.onConnected()
TCPOut = PrintWriter(
BufferedWriter(OutputStreamWriter(this.TCPSocket!!.getOutputStream())),
true
)
// sendMessage("test")
isConnected = true
while(isConnected)
{
// if (isNewData)
// {
// if (TCPOut != null && !TCPOut!!.checkError()) {
// TCPOut!!.println(newMessage)
// TCPOut!!.flush()
// }
// isNewData = false
// }else{
// sleep(100)
// }
sleep(1000)
}
} catch (e: Exception) {
// controller.cancelProgressDialog(java.lang.ModuleLayer.Controller.SERVERALERT)
println("unable to connect")
}
}
// private fun keepAlive() {
// sendMessage(Constants.requestMessage(Constants.REQUEST_ISALIVE))
// try {
// TCPMessage = TCPIn!!.readLine()
// mMessageListener!!.messageReceived(TCPMessage)
// } catch (e: IOException) {
// controller.alertDialog(java.lang.ModuleLayer.Controller.DISCONNECTED)
// }
// }
/**
* Sends the message entered by client to the server
*
* @param message text entered by client
*/
fun sendMessage(message: String?) {
// newMessage = message
// isNewData = true
if (TCPOut != null && !TCPOut!!.checkError()) {
TCPOut!!.println(message)
TCPOut!!.flush()
}
}
// fun stopClient() {
// sendMessage(Constants.requestMessage(Constants.REQUEST_DISCONNECT))
// pause = true
// isConnected = false
// }
interface OnMessageReceived {
fun messageReceived(message: String?)
}
interface OnConnectEstablished {
fun onConnected()
}
init {
SERVERPORT = port
mMessageListener = messagelistener
onConnected = onconnected
try {
SERVERIP = InetAddress.getByName(ip)
serverAddr = InetSocketAddress(SERVERIP, SERVERPORT)
} catch (e: UnknownHostException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
pause = false
}
}
Loading…
Cancel
Save