File Coverage

blib/lib/RPi/Pin.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package RPi::Pin;
2              
3 4     4   17304 use strict;
  4         9  
  4         94  
4 4     4   21 use warnings;
  4         8  
  4         88  
5              
6 4     4   1488 use parent 'WiringPi::API';
  4         935  
  4         18  
7              
8             our $VERSION = '2.3604';
9              
10             sub new {
11             my ($class, $pin) = @_;
12              
13             if (! defined $pin || $pin !~ /^\d+$/){
14             die "pin must be an integer\n";
15             }
16              
17             my $self = bless {}, $class;
18              
19             if (! $ENV{NO_BOARD}){
20             if (! defined $ENV{RPI_PIN_MODE}){
21             $ENV{RPI_PIN_MODE} = 1;
22             $self->setup_gpio;
23             }
24             }
25              
26             $self->{pin} = $pin;
27              
28             return $self;
29             }
30             sub mode {
31             my ($self, $mode) = @_;
32              
33             if (! defined $mode){
34             return $self->get_alt($self->num);
35             }
36             if ($mode != 0 && $mode != 1 && $mode != 2 && $mode != 3){
37             die "mode() mode param must be either 0 (input), 1 " .
38             "(output), 2 (PWM output) or 3 (GPIO CLOCK output)\n";
39             }
40              
41             $self->pin_mode($self->num, $mode);
42             }
43             sub mode_alt {
44             my ($self, $alt) = @_;
45              
46             if (! defined $alt){
47             return $self->get_alt($self->num);
48             }
49              
50             $self->pin_mode_alt($self->num, $alt);
51             }
52             sub read {
53             my $state = $_[0]->read_pin($_[0]->num);
54             return $state;
55             }
56             sub write {
57             my ($self, $value) = @_;
58             if ($value != 0 && $value != 1){
59             die "Core::write_pin value must be 0 or 1\n";
60             }
61             $self->write_pin($self->num, $value);
62             }
63             sub pull {
64             my ($self, $direction) = @_;
65              
66             # 0 == down, 1 == up, 2 == off
67              
68             if ($direction != 0 && $direction != 1 && $direction != 2){
69             die "Core::pull_up_down requires either 0, 1 or 2 for direction";
70             }
71              
72             $self->pull_up_down($self->num, $direction);
73             }
74             sub pwm {
75             my ($self, $value) = @_;
76              
77             if ($self->mode != 2 && $self->num == 18){
78             my $num = $self->num;
79             die "\npin $num isn't set to mode 2 (PWM). pwm() can't be set\n";
80             }
81              
82             if ($value > 1023 || $value < 0){
83             die "\npwm() value must be 0-1023\n";
84             }
85              
86             $self->pwm_write($self->num, $value);
87             }
88             sub num {
89             return $_[0]->{pin};
90             }
91             sub set_interrupt {
92             my ($self, $edge, $callback) = @_;
93             WiringPi::API::set_interrupt($self->num, $edge, $callback);
94             }
95             sub interrupt_set {
96             my ($self, $edge, $callback) = @_;
97             $self->set_interrupt($self->num, $edge, $callback);
98             }
99             sub _vim{1;};
100             1;
101             __END__