Roger Kreienbühl
09/27/2020, 1:46 PMsigHandler: Unhandled signal 11, terminating
My code 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()
setupButton()
while (true) {}
}
private fun initGPIO() {
if (gpioInitialise() < 0) {
println("GPIO Error initialising")
return
}
}
private fun setupButton() {
val buttonPort = GPIO_BUTTON.toUInt()
initPortWithMode(buttonPort, PI_INPUT)
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 ->
val ticker = tick
val pin = gpio
val lvl = level
when (lvl) {
0 -> {
val time = ticker - lastChange
lastChange = ticker
println(tick)
if ((time > 55u && time < 61u) || (time > 113u && time < 119u)) println(time)
println("Button Pressed down, level 0")
}
1 -> {
val time = ticker - lastChange
lastChange = ticker
println(tick)
if ((time > 55u && time < 61u) || (time > 113u && time < 119u)) println(time)
println("Button released, level 1")
}
2 -> println("Button GPIO timeout, no level change")
}
}
The error occurs not every time, I could not identify the exact place in code where it happens, but I think it is the access to lastChange inside the when expression.
Has anyone some experience with such errors?
Is there a way to copy the value from a C Int or UInt?Roger Kreienbühl
09/27/2020, 2:05 PMval flankChangeDetected = staticCFunction<Int, Int, UInt, Unit> { gpio, level, tick ->
in the talk. Because signal 11 is a segment violation, I think the value of lastChange or tick causes the error.
Sometimes I compile the program and run it without errors, but sometimes the exactly same program (no changes) a second time compiled causes this error.Glen
09/27/2020, 2:08 PMRoger Kreienbühl
09/27/2020, 3:28 PMGlen
09/28/2020, 8:39 AMlastChange as a global variable?Glen
09/28/2020, 8:39 AMRoger Kreienbühl
09/28/2020, 8:41 AMRoger Kreienbühl
09/28/2020, 8:41 AMval time = ticker - lastChangeGlen
09/28/2020, 8:49 AMRoger Kreienbühl
09/28/2020, 8:52 AMflankChangeDetectedGlen
09/28/2020, 8:56 AMRoger Kreienbühl
09/28/2020, 8:58 AMflankChangeDetected is called from C, how should this work?Glen
09/28/2020, 9:07 AMval flankChangeDetected : ((change: Int, callback: staticCFunction<Int, Int, UInt, Unit>) -> Unit) = //callbackFunction .Glen
09/28/2020, 10:19 AMRoger Kreienbühl
09/28/2020, 10:20 AMgpioSetAlertFunc(buttonPort, flankChangeDetected)Glen
09/28/2020, 10:36 AMRoger Kreienbühl
09/28/2020, 10:42 AMflankChangeDetected be registered as callback when I use it as higher-order function? The way you posted it, it looks like i can pass a callback function to flankChangeDetected , but flankChangeDetected is the callback function, which I need to use in gpioSetAlertFunc(buttonPort, flankChangeDetected) , or do I understand something wrong?Glen
09/28/2020, 11:31 AMRoger Kreienbühl
09/28/2020, 2:24 PMlastChange = tickerGlen
09/28/2020, 8:43 PMnapperley
09/28/2020, 9:33 PMfun aFunction(gpio: Int, level: Int, tick: UInt) {
// May need to call this function if the callback is called from
// a different thread other than the main thread, otherwise a segmentation fault "might" occur.
initRuntimeIfNeeded()
println("GPIO $gpio became $level at $tick")
}
// Use staticCFunction function to convert a top level Kotlin function to a static C function.
gpioSetAlertFunc(4, staticCFunction(::aFunction))Roger Kreienbühl
09/29/2020, 10:11 AMlastChange in the callback without any usage of tick, level or gpio and the error still occurs. It seems to me, that the access to the global variable lastChange inside the callback is the problem, access to tick, level or gpio works without any problemsGlen
09/29/2020, 11:06 AMlastChange as a global variable? You could just create it inside the callback function.Roger Kreienbühl
09/29/2020, 11:08 AMGlen
09/29/2020, 11:09 AMfun setChange(newValue : UInt) { lastChange = newValue }Roger Kreienbühl
09/29/2020, 11:13 AMGlen
09/29/2020, 11:14 AMRoger Kreienbühl
09/29/2020, 11:14 AMGlen
09/29/2020, 11:50 AMRoger Kreienbühl
10/05/2020, 6:12 AM@SharedImmutable
val sharedData = SharedData()
class SharedData {
var lastChange = AtomicInt(0)
}Glen
10/06/2020, 4:49 PM