Skip to content

Commit 50a1c4a

Browse files
committed
Display an error message when the microcontroller is disconnected
This way the debugger does not just get stuck when that happens, it cleanly stops.
1 parent b8fefed commit 50a1c4a

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/main/kotlin/be/ugent/topl/mio/debugger/Debugger.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,30 @@ open class Debugger(private val connection: Connection, start: Boolean = true, p
2424
}
2525
}
2626
}
27+
val errorHandlers = mutableListOf<(String) -> Unit>()
2728
private val readThread = thread(start) {
2829
while (!Thread.currentThread().isInterrupted) {
29-
while (connection.bytesAvailable() == 0) {
30+
var available = connection.bytesAvailable()
31+
while (available == 0) {
3032
try {
3133
Thread.sleep(10)
3234
} catch (_: InterruptedException) {
3335
Thread.currentThread().interrupt()
3436
break
3537
}
38+
available = connection.bytesAvailable()
39+
}
40+
if (available == -1) {
41+
println("WARDuino disconnected")
42+
thread {
43+
for (el in errorHandlers) {
44+
el.invoke("WARDuino disconnected")
45+
}
46+
}
47+
return@thread
3648
}
3749

38-
val readBuffer = ByteArray(connection.bytesAvailable())
50+
val readBuffer = ByteArray(available)
3951
connection.read(readBuffer)
4052
messageQueue.push(String(readBuffer), true)
4153

src/main/kotlin/be/ugent/topl/mio/ui/InteractiveDebugger.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class InteractiveDebugger(
4545
private val config: DebuggerConfig
4646
) : JFrame("WARDuino Debugger") {
4747
private val binaryInfo = getBinaryInfo(config.wdcliPath, File(wasmFile).absolutePath).getOrElse {
48-
JOptionPane.showMessageDialog(this, it.message, "Error obtaining binary info", JOptionPane.ERROR_MESSAGE)
48+
JOptionPane.showMessageDialog(this, it.message, "Error obtaining binary info", ERROR_MESSAGE)
4949
exitProcess(1)
5050
}
5151
private val debugger = MultiverseDebugger(
@@ -56,7 +56,12 @@ class InteractiveDebugger(
5656
this::onGraphUpdate,
5757
this::onMockingUpdate,
5858
this::onHitBreakpoint
59-
)
59+
).apply {
60+
errorHandlers.add {
61+
JOptionPane.showMessageDialog(this@InteractiveDebugger, it, "An error occurred", ERROR_MESSAGE)
62+
exitProcess(1)
63+
}
64+
}
6065
private val pauseButton = JButton().apply {
6166
toolTipText = "Pause/Continue"
6267
}

0 commit comments

Comments
 (0)