1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
#########################################################################################
# Package HiPi::Device::GPIO
# Description: Wrapper for GPIO
# Copyright : Copyright (c) 2013-2025 Mark Dootson
# License : This is free software; you can redistribute it and/or modify it under
# the same terms as the Perl 5 programming language system itself.
#########################################################################################
package HiPi::Device::GPIO;
#########################################################################################
use strict;
use warnings;
use parent qw( HiPi::Device );
use Carp;
use HiPi qw( :rpi );
use HiPi::RaspberryPi;
use HiPi::Device::GPIO::Pin;
use Fcntl;
our $VERSION ='0.93';
my $sysroot = '/sys/class/gpio';
my $pinoffset = HiPi::Device::GPIO::Pin::get_pin_offset();
sub new {
my ($class, %userparams) = @_;
my %params = ();
foreach my $key (sort keys(%userparams)) {
$params{$key} = $userparams{$key};
}
my $self = $class->SUPER::new(%params);
return $self;
}
# Methods are class methods
sub export_pin {
my( $class, $pinno ) = @_;
my $pinroot = $class->_do_export( $pinno );
return HiPi::Device::GPIO::Pin->_open( pinid => $pinno );
}
sub unexport_pin {
my( $class, $pinno ) = @_;
my $syspin = $pinno + $pinoffset;
my $pinroot = qq(${sysroot}/gpio${syspin});
return if !-d $pinroot;
# unexport the pin
system( qq(/bin/echo $syspin > ${sysroot}/unexport) ) and croak qq(failed to unexport pin $pinno : $!);
}
sub unexport_all {
opendir(my $dir, $sysroot) or die qq(failed to open sysfs root : $!);
my @gpios = grep { /gpio\d+$/ } readdir( $dir );
closedir($dir);
for my $gpio ( @gpios ) {
$gpio =~ s/^gpio//;
system( qq(/bin/echo $gpio > ${sysroot}/unexport) );
}
return scalar @gpios;
}
sub pin_status {
my($class, $pinno) = @_;
my $syspin = $pinno + $pinoffset;
my $pinroot = qq(${sysroot}/gpio${syspin});
return (-d $pinroot ) ? DEV_GPIO_PIN_STATUS_EXPORTED : DEV_GPIO_PIN_STATUS_NONE;
}
sub pin_write {
my($class, $gpio, $level) = @_;
my $wval = ( $level ) ? 1 : 0;
my $fh = _open_fh( _get_pin_filepath( $gpio, 'value' ) );
_write_fh( $fh, $wval);
close( $fh );
return $wval;
}
sub pin_read {
my($class, $gpio) = @_;
my $fh = _open_fh( _get_pin_filepath( $gpio, 'value' ) );
my $rval = _read_fh( $fh, 1);
close( $fh );
return $rval;
}
sub set_pin_mode {
my($class, $gpio, $mode, $init ) = @_;
my $inst;
if( $mode == RPI_MODE_OUTPUT ) {
if( $init ) {
$inst = 'high';
} else {
$inst = 'low';
}
} elsif( $mode == RPI_MODE_INPUT ) {
$inst = 'in';
} else {
croak qq(Invalid value for mode : $mode);
}
my $fh = _open_fh( _get_pin_filepath( $gpio, 'direction' ) );
_write_fh( $fh, $inst);
close( $fh );
return $mode;
}
sub get_pin_mode {
my($class, $gpio ) = @_;
my $fh = _open_fh( _get_pin_filepath( $gpio, 'direction' ) );
my $result = _read_fh( $fh, 16);
close($fh);
return ( $result eq 'out' ) ? RPI_MODE_OUTPUT : RPI_MODE_INPUT;
}
sub get_pin_function {
my($class, $gpio) = @_;
require HiPi::GPIO;
return HiPi::GPIO->get_pin_function( $gpio );
}
sub set_pin_pud {
my($class, $gpio , $pud ) = @_;
require HiPi::GPIO;
# we want to force pin export
_get_pin_filepath( $gpio, 'value' );
return HiPi::GPIO->set_pin_pud( $gpio, $pud );
}
sub get_pin_pud {
my($class, $gpio ) = @_;
require HiPi::GPIO;
# we want to force pin export
_get_pin_filepath( $gpio, 'value' );
return HiPi::GPIO->get_pin_pud( $gpio );
}
sub set_pin_activelow {
my($class, $gpio, $alow ) = @_;
$alow = ( $alow ) ? 1 : 0;
my $fh = _open_fh( _get_pin_filepath( $gpio, 'active_low' ) );
_write_fh( $fh, $alow);
close( $fh );
return $alow;
}
sub get_pin_activelow {
my($class, $gpio ) = @_;
my $fh = _open_fh( _get_pin_filepath( $gpio, 'active_low' ) );
my $result = _read_fh( $fh, 1);
close($fh);
return ( $result ) ? 1 : 0;
}
sub get_pin_interrupt_filepath {
my($class, $gpio ) = @_;
my $fpath = _get_pin_filepath( $gpio, 'value' );
return $fpath;
}
sub set_pin_interrupt {
my($class, $gpio, $newedge ) = @_;
$newedge ||= RPI_INT_NONE;
my $stredge = 'none';
if ( $newedge == RPI_INT_AFALL || $newedge == RPI_INT_FALL || $newedge == RPI_INT_LOW ) {
$stredge = 'falling';
} elsif( $newedge == RPI_INT_ARISE || $newedge == RPI_INT_RISE || $newedge == RPI_INT_HIGH ) {
$stredge = 'rising';
} elsif( $newedge == RPI_INT_BOTH ) {
$stredge = 'both';
} else {
$stredge = 'none';
$newedge = RPI_INT_NONE;
}
my $fh = _open_fh( _get_pin_filepath( $gpio, 'edge' ) );
_write_fh( $fh, $stredge );
close( $fh );
return $newedge;
}
sub get_pin_interrupt {
my($class, $gpio ) = @_;
my $fh = _open_fh( _get_pin_filepath( $gpio, 'edge' ) );
my $result = _read_fh( $fh, 16);
close($fh);
my $edge = RPI_INT_NONE;
if($result eq 'rising') {
$edge = RPI_INT_RISE;
} elsif($result eq 'falling') {
$edge = RPI_INT_FALL;
} elsif($result eq 'both') {
$edge = RPI_INT_BOTH;
}
return $edge;
}
sub set_pin_schmitt {
warn q(HiPi::Device::GPIO does not support schmitt actions);
return undef;
}
sub get_pin_schmitt {
warn q(HiPi::Device::GPIO does not support schmitt actions);
return undef;
}
sub set_pin_slew {
warn q(HiPi::Device::GPIO does not support slew actions);
return undef;
}
sub get_pin_slew {
warn q(HiPi::Device::GPIO does not support slew actions);
return undef;
}
sub get_pin_offset {
return $pinoffset;
}
sub _do_export {
my ($class, $pinno ) = @_;
my $syspin = $pinno + $pinoffset;
my $pinroot = qq(${sysroot}/gpio${syspin});
return $pinroot if -d $pinroot;
system(qq(/bin/echo $syspin > ${sysroot}/export)) and croak qq(failed to export pin $pinno : $!);
# We have to wait for the system to export the pin correctly.
# Max 10 seconds
my $checkpath = qq($pinroot/value);
my $counter = 100;
while( $counter ){
last if( -e $checkpath && -w $checkpath );
$class->delay( 100 );
$counter --;
}
unless( $counter ) {
croak qq(failed to export pin $checkpath);
}
return $pinroot;
}
sub _get_pin_filepath {
my( $pinno, $type ) = @_;
my $pinroot = __PACKAGE__->_do_export( $pinno );
my $filepath = qq($pinroot/$type);
if( -e $filepath ) {
return $filepath;
} else {
croak qq(could not find $type file for pin $pinno);
}
}
sub _open_fh {
my $filepath = shift;
my $fh;
sysopen($fh, $filepath, O_RDWR|O_NONBLOCK) or croak qq(failed to open $filepath : $!);
return $fh;
}
sub _read_fh {
my($fh, $bytes) = @_;
my $value;
sysseek($fh,0,0);
defined( sysread($fh, $value, $bytes) ) or croak(qq(Failed to read from filehandle : $!));
chomp $value;
return $value;
}
sub _write_fh {
my($fh, $val) = @_;
sysseek($fh,0,0);
defined( syswrite($fh, $val) ) or croak(qq(Failed to write to filehandle : $!));
}
# Aliases
*HiPi::Device::GPIO::get_pin = \&export_pin;
*HiPi::Device::GPIO::get_pin_level = \&pin_read;
*HiPi::Device::GPIO::set_pin_level = \&pin_write;
1;
__END__
|