<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Derived Class Example</title></head>
<BODY TOPMARGIN=4 BGCOLOR=#FFFFFF TEXT=#000000 VLINK=#0000CC LINK=#0000CC ALINK=#0000CC>
<FONT FACE="Arial, Lucida, Helvetica" >
<TABLE WIDTH="100%" ALIGN=CENTER CELLPADDING=1 CELLSPACING=0>
<TR>
<TD WIDTH="100%" ALIGN=CENTER>
<A HREF="contents.htm"><img align=center src="home.png" BORDER=0 ALT="Contents"></A>
<A HREF="interrupt.htm"><img align=center src="up.png" BORDER=0 ALT="Up"></A>
<A HREF="interrupt_hipi_interrupt_message.htm"><img align=center src="back.png" BORDER=0 ALT="Previous"></A>
<A HREF="interrupt_example_callback.htm"><img align=center src="forward.png" BORDER=0 ALT="Next"></A>
</TD>
</TR>
<TR>
<TD COLSPAN=2 HEIGHT=2 BGCOLOR="#C0C0C0">
</TD>
</TR>
</TABLE>
<H2>Derived Class Example</H2>
<pre><font color="#404040"><i>#!/usr/bin/perl</i></font>
<font color="#009A00"><i># If using interupts the HiPi::Interrupt module should be used
# before anything else in your script. This is because the
# module loads threads to handle interrupts for pins managed
# by HiPi::Device::GPIO, HiPi::BCM2835 and HiPi::Wiring.
# Loading first reduces your memory footprint and avoids issues
# with modules you may use that are not thread safe.</i></font>
use HiPi::Interrupt;
<font color="#009A00"><i># Some basic modules loaded</i></font>
use 5.14.0;
use strict;
use warnings;
use HiPi::Device::GPIO;
use HiPi::Constant qw( :raspberry );
<font color="#009A00"><i>#-------------------------------------------------------------</i></font>
package MyInterruptHandler;
<font color="#009A00"><i>#-------------------------------------------------------------
# To handle interrupts we create a class deriving from
# HiPi::Interrupt::Handler and override the on_action_....
# methods.
#-------------------------------------------------------------</i></font>
use strict;
use warnings;
use parent qw( HiPi::Interrupt::Handler );
use HiPi::Constant qw( :raspberry );
sub new {
my($class, %params) = @_;
$class->SUPER::new(%params);
}
<font color="#009A00"><i>#----------------------------------------------
# Action Events Handling - these events will be
# called in response to actions in the interrupt
# handling threads. For this example we just
# print the info STDOUT
#------------------------------------------</i></font>
sub on_action_add {
my($self, $msg) = @_;
<font color="#009A00"><i> # we get this message after we have added a pin
# check if( $msg->error ) to see if there was an
# error. When $msg->error is true, error details
# are in $msg->msgtext</i></font>
$self->do_handle_message( $msg );
}
sub on_action_remove {
my($self, $msg) = @_;
<font color="#009A00"><i> # we get this message after we have removed a pin
# check if( $msg->error ) to see if there was an
# error. When $msg->error is true, error details
# are in $msg->msgtext</i></font>
$self->do_handle_message( $msg );
}
sub on_action_interrupt {
my($self, $msg) = @_;
<font color="#009A00"><i> # we get this message after we have detected an interrupt
# check if( $msg->error ) to see if there was an
# error. When $msg->error is true, error details
# are in $msg->msgtext</i></font>
$self->do_handle_message( $msg );
}
sub on_action_error {
my($self, $msg) = @_;
<font color="#009A00"><i> # we get this message after a generic error
# error details are in $msg->msgtext</i></font>
$self->do_handle_message( $msg );
}
sub on_action_continue {
my ($self, $actions) = @_;
<font color="#009A00"><i> # this gets called at a maximum interval of polltimeout
# and is always called after an interrupt is detected and
# handled. If the call is after 1 or more interrupt
# detections $actions will contain the number of interrupts
# detected. If this is called after interrupt detection
# times out then $actions == 0</i></font>
$self->{hbcounter} ++;
unless( $self->{hbcounter} % 100 ) {
say qq(on_action_continue called $self->{hbcounter} times);
}
<font color="#009A00"><i> # exit after 1000 calls</i></font>
$self->stop if $self->{hbcounter} == 1000;
}
sub on_action_start {
my $self = shift;
say 'INTERRUPT POLLING STARTED';
}
sub on_action_stop {
my $self = shift;
say 'INTERRUPT POLLING FINISHED';
}
sub do_handle_message {
my ($self, $msg ) = @_;
say'--------------------------------';
my $output = ( $msg->error ) ? 'ERROR MESSAGE' : uc($msg->action) . ' HANDLED';
say $output;
say qq( action : ) . $msg->action;
say qq( pinid : ) . $msg->pinid;
say qq( error : ) . $msg->error;
say qq( value : ) . $msg->value;
say qq( timestamp : ) . $msg->timestamp;
say qq( msgtext : ) . $msg->msgtext;
say qq( pinclass : ) . $msg->pinclass;
say'--------------------------------';
}
<font color="#009A00"><i># return to default package main
#-------------------------------------------------------------</i></font>
package main;
<font color="#009A00"><i>#-------------------------------------------------------------</i></font>
<font color="#009A00"><i># create our interrupt handler</i></font>
my $handler = MyInterruptHandler->new;
{
<font color="#009A00"><i> # the pins we are going to use</i></font>
my $pinid1 = RPI_PAD1_PIN_13;
my $pinid2 = RPI_PAD1_PIN_11;
my $dev = HiPi::Device::GPIO->new;
<font color="#009A00"><i> # setup a pin as input with a pull up
# resistor and falling edge interrupt
# using HiPi::Device::GPIO</i></font>
$dev->export_pin($pinid1);
my $pin1 = $dev->get_pin($pinid1);
$pin1->mode(RPI_PINMODE_INPT);
$pin1->set_pud(RPI_PUD_OFF);
$pin1->set_pud(RPI_PUD_UP);
$pin1->interrupt( RPI_INT_FALL );
<font color="#009A00"><i> # setup a pin as input with a pull down
# resistor and rising edge interrupt
# using HiPi::Device::GPIO
# use the pin obj returned from
# the export_pin method</i></font>
my $pin2 = $dev->export_pin($pinid2);
$pin2->mode(RPI_PINMODE_INPT);
$pin2->set_pud(RPI_PUD_OFF);
$pin2->set_pud(RPI_PUD_DOWN);
$pin2->interrupt( RPI_INT_RISE );
<font color="#009A00"><i> # add as many pins as we want</i></font>
$handler->add_pin($pin1);
$handler->add_pin($pin2);
}
<font color="#009A00"><i># run the application loop</i></font>
$handler->poll();
1;
</pre></FONT>
<br>
<p>
<br>
<hr>
<br>
<center>
<A HREF="contents.htm"><img align=center src="home.png" BORDER=0 ALT="Contents"></A>
<A HREF="interrupt.htm"><img align=center src="up.png" BORDER=0 ALT="Up"></A>
<A HREF="interrupt_hipi_interrupt_message.htm"><img align=center src="back.png" BORDER=0 ALT="Previous"></A>
<A HREF="interrupt_example_callback.htm"><img align=center src="forward.png" BORDER=0 ALT="Next"></A>
</center>
<HR>
<br>
<center><FONT FACE="Arial, Lucida, Helvetica" size="2" color="#000080">HiPi Modules Copyright © 2013 - 2016 Mark Dootson</font></center>
</BODY></HTML>