Hi.
Based on the topic [Help] c binding of function that takes callback from 2019 I tried to get some code running in 2025 on a Raspberry Pi 5 device with Raspberry Pi OS installed. Goal is it to react to a signal on a GPIO pin.
Below you can see a copy of the relevant code from that topic in the past which is used as basis.
require "socket"
@[Link("wiringPi")]
lib LibWiringPi
fun Setup = wiringPiSetupSys : LibC::Int
fun ISR = wiringPiISR(pin : LibC::Int, edgeType : LibC::Int, fn : ->) : LibC::Int
end
fun my_interrupt_handler
UNIXSocket.open("/tmp/gpio_detector.sock") do |sock|
sock.puts "Event"
end
end
class Detector
property server
def initialize(config)
LibWiringPi.Setup()
LibWiringPi.ISR(config["gpioPinNumber"].as_i, 1, ->my_interrupt_handler)
socket_file = "/tmp/gpio_detector.sock"
if File.exists?(socket_file)
puts "socket found"
File.delete(socket_file)
end
@server = UNIXServer.new(socket_file)
end
end
Depending on the Crystal code I place in the my_interrupt_handler method which will be called when a signal is raised, the Crystal code either works or causes an segmentation fault.
Code which works in my_interrupt_handler:
File.touch("/tmp/some_ordinary_file")
LibC.printf("Callback triggered.")
Code which does not work in my_interrupt_handler (segmentation fault):
puts "Callback triggered."
File.write("/tmp/some_ordinary_file", "Callback triggered.")
UNIXSocket.open("/tmp/gpio_detector.sock") do |sock|
sock.puts "Event"
end
I am not sure if it might be something about using IO? Does anyone has an idea what the reason for this behaviour could be? File.write
for example manages to create the file before the segmentation fault is caused and no data is written to the file.
Some more details about my setup:
- Raspberry Pi OS: “Linux raspberrypi 6.6.51+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.51-1+rpt2 (2024-10-01) aarch64 GNU/Linux”)
- Pi: “Raspberry Pi 5 Model B Rev 1.0”
- Crystal in version 1.15.1
- WiringPi in version 3.12 (GitHub - WiringPi/WiringPi: The arguably fastest GPIO Library for the Raspberry Pi).
I compiled Crystal and WiringPi from source on this Pi. Maybe I did something wrong when compiling the sources?
I don’t have much experiences with Crystal and its community. I hope this is the right location to address my issue and I have explained my problem clearly and in adequate detail. Otherwise, I would be grateful for any advice how and where to get help.
Many thanks in advance.