Skip to content

Commit

Permalink
Add progress listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
loucass003 committed Oct 7, 2023
1 parent b3102b1 commit c745035
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Here is a list of the known serial libraries that are supported:
```kotlin
import dev.llelievr.espflashkotlin.Flasher
import dev.llelievr.espflashkotlin.FlasherSerialInterface
import dev.llelievr.espflashkotlin.FlashingProgressListener
import com.fazecast.jSerialComm.SerialPort
import java.io.File

class MyFlasher: FlasherSerialInterface {
class MyFlasher: FlasherSerialInterface, FlashingProgressListener {

private var port: SerialPort? = null

Expand All @@ -27,6 +28,8 @@ class MyFlasher: FlasherSerialInterface {

// Declare a flasher and its interface
Flasher(this)
// Watch for the upload progress
.addProgressListener(this)
// Add one or more binaries to flash
.addBin(File("bootloader.bin").readBytes(), 4096)
.addBin(File("partitions.bin").readBytes(), 32768)
Expand Down Expand Up @@ -102,5 +105,9 @@ class MyFlasher: FlasherSerialInterface {
val p = port ?: error("no port to flush")
p.flushIOBuffers()
}

override fun progress(bin: Int, binTotal: Int, progress: Float) {
println("Progress File (${bin + 1} / ${binTotal}) ${progress * 100}")
}
}
```
6 changes: 3 additions & 3 deletions src/main/kotlin/Flasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ interface FlasherSerialInterface {
fun flushIOBuffers();
}

interface ProgressListener {
interface FlashingProgressListener {
fun progress(bin: Int, binTotal: Int, progress: Float);
}

Expand All @@ -88,7 +88,7 @@ class Flasher(
private val targets = HashMap<Int, FlasherTarget>();
private val binsToFlash = ArrayList<Pair<Int, ByteArray>>();
private var flashing: Boolean = false;
private val progressListeners = ArrayList<ProgressListener>();
private val progressListeners = ArrayList<FlashingProgressListener>();


init {
Expand Down Expand Up @@ -118,7 +118,7 @@ class Flasher(
*
* Used to watch flashing progress
*/
fun addProgressListener(listener: ProgressListener): Flasher {
fun addProgressListener(listener: FlashingProgressListener): Flasher {
progressListeners.add(listener);
return this;
}
Expand Down
8 changes: 7 additions & 1 deletion src/test/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import com.fazecast.jSerialComm.SerialPort
import dev.llelievr.espflashkotlin.Flasher
import dev.llelievr.espflashkotlin.FlasherSerialInterface
import dev.llelievr.espflashkotlin.FlashingProgressListener
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.net.URL

class LibraryTest: FlasherSerialInterface {
class LibraryTest: FlasherSerialInterface, FlashingProgressListener {

private var port: SerialPort? = null

Expand All @@ -17,6 +18,7 @@ class LibraryTest: FlasherSerialInterface {
val firstPort = ports.first() ?: error("unable to find port")

Flasher(this, false)
.addProgressListener(this)
.addBin(File("C:\\Users\\louca\\Documents\\SlimeVR\\SlimeVR-Tracker-ESP\\.pio\\build\\esp32c3\\bootloader.bin").readBytes(), 0x0000)
.addBin(File("C:\\Users\\louca\\Documents\\SlimeVR\\SlimeVR-Tracker-ESP\\.pio\\build\\esp32c3\\partitions.bin").readBytes(), 0x8000)
.addBin(File("C:\\Users\\louca\\.platformio\\packages\\framework-arduinoespressif32@3.20007.0\\tools\\partitions\\boot_app0.bin").readBytes(), 0xe000)
Expand Down Expand Up @@ -109,6 +111,10 @@ class LibraryTest: FlasherSerialInterface {
val p = port ?: error("no port to flush");
p.flushIOBuffers()
}

override fun progress(bin: Int, binTotal: Int, progress: Float) {
println("Progress File (${bin + 1} / ${binTotal}) ${progress * 100}")
}
}

fun main() {
Expand Down

0 comments on commit c745035

Please sign in to comment.