in reply to SOLVED: XS: Passing an external library's function a Perl XS callback
For completeness sake, I'm posting the relevant code that now has fixed my original problem. I can now use an external library that has ISR (Interrupt Service Routine) capability.
What I needed to do was allow a user to send in a Perl code reference, and then have the C interrupt handler dynamically call that user-supplied sub when the interrupt occurred.
XS code that sets up the interrupt through the external lib:
void interruptHandler(); int setInterrupt(int pin, int edge, char *callback); char *perl_callback; PerlInterpreter *mine; void interruptHandler(){ PERL_SET_CONTEXT(mine); dSP; PUSHMARK(SP); PUTBACK; call_pv(perl_callback, G_DISCARD|G_NOARGS); FREETMPS; LEAVE; } int setInterrupt(int pin, int edge, char *callback){ perl_callback = callback; int interrupt = wiringPiISR(pin, edge, &interruptHandler); return interrupt; } # XS int setInterrupt(pin, edge, callback) int pin int edge char *callback void interruptHandler()
Perl module code that calls into the XS:
sub set_interrupt { shift if @_ == 4; my ($pin, $edge, $callback) = @_; setInterrupt($pin, $edge, $callback); }
...and finally a basic test script:
use warnings; use strict; use RPi::WiringPi; use RPi::WiringPi::Constant qw(:all); my $pi = RPi::WiringPi->new(); my $pin = $pi->pin(27); RPi::WiringPi::Core::set_interrupt( $pin->num, EDGE_RISING, 'pin_27_edge_rise' ); $pin->mode(INPUT); sleep 10; # manually change the pin state here to trigger # interrupt $pi->cleanup; sub pin_27_edge_rise { print "pin 27 edge rise callback...\n"; # do other stuff }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XS: Passing an external library's function a Perl XS callback
by RonW (Parson) on Aug 16, 2016 at 20:09 UTC | |
by pRiVi (Initiate) on Jan 06, 2019 at 00:05 UTC |