forked from Blendman974/faceclaw
G1 support: Flashing within the Android app
This commit is contained in:
1 parent
345ba36c33
commit
38c52ebc31
37 files changed
+2819
-32
No files matched your search
@@ -48,6 +48,10 @@ class AndroidStockLink(context: Context) : StockLink, FaceclawBleListener {
|
||||
else -> BondState.UNKNOWN
|
||||
}
|
||||
|
||||
override fun negotiatedMtu(address: String): Int = bleManager.getNegotiatedMtu(address)
|
||||
|
||||
override fun requestBond(address: String) = bleManager.requestBond(address)
|
||||
|
||||
override fun disconnect(address: String) = bleManager.disconnect(address)
|
||||
|
||||
override fun close() = bleManager.close()
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
|
||||
/**
|
||||
* Android host for the shared [G1ArmFlashFlow]: owns the GATT link and the worker thread for one G1 arm's install, or a
|
||||
* question about what it runs, and delivers listener callbacks on the main thread. For app/native/g1-flasher.ts.
|
||||
*/
|
||||
class FaceclawG1Flasher(context: Context, address: String?, side: String?, firmwarePath: String?, mode: String?, connectWindowMs: Int) {
|
||||
private val link = AndroidStockLink(context.applicationContext)
|
||||
private val mainHandler = Handler(Looper.getMainLooper())
|
||||
private val lock = Any()
|
||||
|
||||
@Volatile
|
||||
private var listener: FaceclawFirmwareFlasherListener? = null
|
||||
|
||||
@Volatile
|
||||
private var worker: Thread? = null
|
||||
|
||||
private val flow =
|
||||
G1ArmFlashFlow(
|
||||
link,
|
||||
address ?: "",
|
||||
side ?: "",
|
||||
firmwarePath ?: "",
|
||||
G1ArmFlashFlow.mode(mode ?: ""),
|
||||
connectWindowMs,
|
||||
object : FaceclawFirmwareFlasherListener {
|
||||
override fun onLog(line: String?) {
|
||||
android.util.Log.i(G1ArmFlashFlow.TAG, line ?: "")
|
||||
post { it.onLog(line) }
|
||||
}
|
||||
|
||||
override fun onProgress(
|
||||
lens: String?,
|
||||
componentIndex: Int,
|
||||
componentCount: Int,
|
||||
blockIndex: Int,
|
||||
blockCount: Int,
|
||||
bytesSent: Long,
|
||||
bytesTotal: Long,
|
||||
) = post { it.onProgress(lens, componentIndex, componentCount, blockIndex, blockCount, bytesSent, bytesTotal) }
|
||||
|
||||
override fun onState(state: String?, detail: String?) = post { it.onState(state, detail ?: "") }
|
||||
|
||||
override fun onComplete(success: Boolean, detail: String?) = post { it.onComplete(success, detail ?: "") }
|
||||
},
|
||||
)
|
||||
|
||||
fun setListener(listener: FaceclawFirmwareFlasherListener?) {
|
||||
this.listener = listener
|
||||
}
|
||||
|
||||
fun start() {
|
||||
synchronized(lock) {
|
||||
if (worker != null) {
|
||||
return
|
||||
}
|
||||
val w = Thread({ flow.run() }, "faceclaw-g1-flash")
|
||||
worker = w
|
||||
w.start()
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
flow.cancel()
|
||||
}
|
||||
|
||||
fun close() {
|
||||
cancel()
|
||||
try {
|
||||
link.close()
|
||||
} catch (ignored: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun post(action: (FaceclawFirmwareFlasherListener) -> Unit) {
|
||||
mainHandler.post {
|
||||
val current = listener
|
||||
if (current != null) {
|
||||
action(current)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user