File: Pin.pm

package info (click to toggle)
libhipi-perl 0.94-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,048 kB
  • sloc: perl: 471,918; ansic: 22; makefile: 10
file content (137 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download | duplicates (2)
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
#########################################################################################
# Package        HiPi::Device::GPIO::Pin
# Description:   Pin
# Created        Wed Feb 20 04:37:38 2013
# 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::Pin;

#########################################################################################
use strict;
use warnings;
use parent qw( HiPi::Pin );
use Carp;
use Fcntl;
use HiPi qw( :rpi );
use HiPi::RaspberryPi;

my $pinoffset = _calculate_sysfs_pin_offset();

our $VERSION ='0.93';

__PACKAGE__->create_accessors();

sub get_pin_offset {
    return $pinoffset;
}

sub _open {
    my ($class, %params) = @_;
    defined($params{pinid}) or croak q(pinid not defined in parameters);
    my $syspin = $params{pinid} + $pinoffset;
    my $pinroot = qq(/sys/class/gpio/gpio${syspin});
    croak qq(pin $params{pinid} is not exported) if !-d $pinroot;
        
    my $self = $class->SUPER::_open(%params);
    return $self;
}

sub _do_getvalue {
    my $self = shift;
    return HiPi::Device::GPIO->pin_read( $self->pinid );
}

sub _do_setvalue {
    my( $self, $newval) = @_;
    return HiPi::Device::GPIO->pin_write($self->pinid, $newval );
}

sub _do_getmode {
    my $self = shift;
    return HiPi::Device::GPIO->get_pin_mode($self->pinid );
}

sub _do_setmode {
    my ($self, $newmode) = @_;
    return HiPi::Device::GPIO->set_pin_mode($self->pinid, $newmode );
}

sub _do_getinterrupt {
    my $self = shift;
    return HiPi::Device::GPIO->get_pin_interrupt( $self->pinid );
}

sub _do_setinterrupt {
    my ($self, $newedge) = @_;
    return HiPi::Device::GPIO->set_pin_interrupt( $self->pinid, $newedge );
}

sub _do_get_interrupt_filepath {
    my($self) = @_;
    return HiPi::Device::GPIO->get_pin_interrupt_filepath( $self->pinid );
}

sub _do_get_function_name {
    my($self) = @_;
    return HiPi::Device::GPIO->get_pin_function( $self->pinid );
}

sub _do_setpud {
    my($self, $pudval) = @_;
    return HiPi::Device::GPIO->set_pin_pud($self->pinid, $pudval);
}

sub _do_getpud {
    my($self) = @_;
    return HiPi::Device::GPIO->get_pin_pud($self->pinid);
}

sub _do_setschmitt {
    my( $self, $val ) = @_;
    return HiPi::Device::GPIO->set_pin_schmitt($self->pinid, $val);
}

sub _do_getschmitt {
    my( $self ) = @_;
    return HiPi::Device::GPIO->get_pin_schmitt($self->pinid);
}

sub _do_setslew {
    my( $self, $val ) = @_;
    return HiPi::Device::GPIO->set_pin_slew($self->pinid, $val);    
}

sub _do_getslew {
    my( $self ) = @_;
    return HiPi::Device::GPIO->get_pin_slew($self->pinid);
}

sub _do_activelow {
    my($self, $newval) = @_;
    
    my $result = undef;
    
    if( defined( $newval ) ) {
        $result = HiPi::Device::GPIO->set_pin_activelow($self->pinid, $newval);
    } else {
        $result = HiPi::Device::GPIO->get_pin_activelow($self->pinid);
    }
    
    return $result;
}

sub _calculate_sysfs_pin_offset {
    my $offset = 0;
    {
        my $result = qx(cat /sys/kernel/debug/gpio 2>&1 | grep 'ID_SDA');
        if ( $result && $result =~ /gpio-([0-9]+)/ ) {
            $offset = $1;
        }
    }
    return $offset;
}

1;