I try to use the pigpio library with Kotlin/Native. To get started I followed this talk: Bridge The Physical World: Kotlin Native on Raspberry Pi
The sigHandler: Unhandled signal 11, terminating
occurs, when I try to assign a value I get from a callback to a global variable lastChange = tick
on line 54
The whole code for my testing looks like this:
package ch.lichtwellenreiter.omrr
import kotlinx.cinterop.staticCFunction
import pigpio.*
const val GPIO_BUTTON = 6
var lastChange: UInt = 0u
fun main() {
initGPIO()
println()
setupButton()
println()
while (true) {}
}
private fun initGPIO() {
println("Init GPIO")
if (gpioInitialise() < 0) {
println("GPIO Error initialising")
return
}
}
private fun setupButton() {
println("Setup pin")
val buttonPort = GPIO_BUTTON.toUInt()
initPortWithMode(buttonPort, PI_INPUT)
println("Register callback for pin")
gpioSetAlertFunc(buttonPort, flankChangeDetected)
}
private fun initPortWithMode(port: UInt, mode: Int) {
if (gpioSetMode(port, mode.toUInt()) < 0) {
println("Could not set mode for GPIO$port")
return
}
}
val flankChangeDetected = staticCFunction<Int, Int, UInt, Unit> { gpio, level, tick ->
println("Callback called")
val ticker: UInt = tick
val pin: Int = gpio
val lvl: Int = level
println("Calculate time")
val time = ticker - lastChange
println("Set lastChange")
lastChange = tick
println("Is DCC signal?")
if ((time > 55u && time < 61u) || (time > 113u && time < 119u)) println(time)
println()
}
How can I prevent this error?
I would guess the problem is related to Kotlin/Native immutability rules. This issue I found convinced me to think that the callback is being called not on the main thread. If that's true, the error is caused by violating
mutable XOR shared
K/N's rule. Cannot write a snippet at the moment, but I think you can either try using AtomicInt or, if it fits the need here,@ThreadLocal
annotation.