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 }

In reply to Re: XS: Passing an external library's function a Perl XS callback by stevieb
in thread SOLVED: XS: Passing an external library's function a Perl XS callback by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.