Skip to content

Commit

Permalink
Change how port is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
loucass003 committed Oct 7, 2023
1 parent 041236c commit 080b35e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class MyFlasher: FlasherSerialInterface {
private var port: SerialPort? = null

fun flash() {
val ports = SerialPort.getCommPorts()
val firstPort = ports.first() ?: error("unable to find port")

// Declare a flasher and its interface
Flasher(this)
// Add one or more binaries to flash
Expand All @@ -30,18 +33,17 @@ class MyFlasher: FlasherSerialInterface {
.addBin(File("boot_app0.bin").readBytes(), 57344)
.addBin(File("firmware.bin").readBytes(), 65536)
// start the flashing
.flash()
.flash(firstPort)
}

override fun openSerial() {
val ports = SerialPort.getCommPorts()
val firstPort = ports.first() ?: error("unable to find port")
if (!firstPort.openPort(1000))
override fun openSerial(port: Any) {
if (port !is SerialPort)
error("Not a serial port")
if (!port.openPort(1000))
error("unable to open port")
port = firstPort
this.port = port
}


override fun closeSerial() {
val p = port ?: error("no port to close")
try {
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/Flasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface FlasherSerialInterface {
/**
* Called when the flasher open the serial port
*/
fun openSerial()
fun openSerial(port: Any)

/**
* Called when the flasher close the serial port
Expand Down Expand Up @@ -121,16 +121,17 @@ class Flasher(

/**
* Start the flashing process
* Specify the serial port to open
* Blocking
*/
fun flash() {
fun flash(port: Any) {
if (flashing)
error("This flasher is already flashing")
flashing = true;

// TODO add checks if binaries are overlapping

serialInterface.openSerial();
serialInterface.openSerial(port);

begin();

Expand Down
15 changes: 9 additions & 6 deletions src/test/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ class LibraryTest: FlasherSerialInterface {
private var port: SerialPort? = null

fun flash() {
val ports = SerialPort.getCommPorts()
val firstPort = ports.first() ?: error("unable to find port")

Flasher(this, false)
.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)
.addBin(File("C:\\Users\\louca\\Documents\\SlimeVR\\SlimeVR-Tracker-ESP\\.pio\\build\\esp32c3\\firmware.bin").readBytes(), 0x10000)
// .addBin(downloadFirmware("http://127.0.0.1:9099/slimevr-firmware-builds/7e3c6081-0adb-4b12-9382-69c6174dcbd0/firmware-part-0.bin") ?: error("unable to download"), 0)
.flash()
.flash(firstPort)
}

fun downloadFirmware(url: String): ByteArray? {
Expand All @@ -39,12 +42,12 @@ class LibraryTest: FlasherSerialInterface {
return outputStream.toByteArray()
}

override fun openSerial() {
val ports = SerialPort.getCommPorts()
val firstPort = ports.first() ?: error("unable to find port")
if (!firstPort.openPort(1000))
override fun openSerial(port: Any) {
if (port !is SerialPort)
error("Not a serial port")
if (!port.openPort(1000))
error("unable to open port")
port = firstPort
this.port = port
}


Expand Down

0 comments on commit 080b35e

Please sign in to comment.