File Coverage

blib/lib/PINE64/GPIO.pm
Criterion Covered Total %
statement 3 44 6.8
branch 0 14 0.0
condition 0 9 0.0
subroutine 1 6 16.6
pod 5 5 100.0
total 9 78 11.5


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2 1     1   67876 use strict;
  1         2  
  1         571  
3              
4             package PINE64::GPIO;
5              
6             our $VERSION = '0.91';
7              
8             #global vars
9              
10             #array of system gpio pin numbers
11             my @line_nums = (227,226,362,71,233,76,64,65,66,229,230,69,73,80,32,33,72,77,78,79,67,231,68,70,74,75);
12              
13             #gpio index num. maps to line_nums system gpio number
14             my $iox;
15              
16             sub new{
17 0     0 1   my $class = shift;
18 0           my $self = bless {}, $class;
19 0           return $self;
20             }#end new
21              
22             sub gpio_enable{
23 0     0 1   my $ind = $_[1];
24 0           $iox = $line_nums[$ind];
25              
26 0           my $gpiosz = @line_nums;
27              
28 0           my $direction = $_[2];
29              
30             #err chk
31 0 0 0       if($ind < 0 || $ind > $gpiosz){
32 0           print "INVALID GPIO RANGE\n";
33 0           exit;
34             }#end if invalid index
35 0 0 0       if($direction ne 'in' && $direction ne 'out'){
36 0           print "INVALID DIRECTION\n";
37 0           exit;
38             }#end if invalid direction
39            
40             #write gpio pin val to export file
41 0 0         open(EF, ">", "/sys/class/gpio/export") or die $!;
42 0           print EF $iox;
43 0           close(EF);
44              
45             #set direction of gpio pin
46 0 0         open(GF, ">", "/sys/class/gpio/gpio$iox/direction") or die $!;
47 0           print GF $direction;
48 0           close(GF);
49              
50             }#end gpio_enable
51              
52             sub gpio_disable{
53             #takes gpio pin num as arg
54 0     0 1   my $ind = $_[1];
55              
56             #map pin num to system gpio number
57 0           $iox = $line_nums[$ind];
58              
59             #unexport gpio pin
60 0           open(UE, ">", "/sys/class/gpio/unexport");
61 0           print UE $iox;
62 0           close UE;
63              
64             }#end gpio_disable
65              
66             sub gpio_read{
67             #gpio number as arg, returns direction in/out
68 0     0 1   my $ind = $_[1];
69 0           $iox = $line_nums[$ind];
70            
71 0           my $value = '';
72              
73             #reads state of gpio pin
74 0 0         open(GS, "/sys/class/gpio/gpio$iox/value") or die $!;
75 0           while(){ $value = $_; };
  0            
76             #print "$iox val: $value";
77 0           close(GS);
78              
79 0 0 0       if($value == 0 || $value == 1){
80 0           return $value;
81             }#end unless
82             else{
83 0           print "ERROR: Undefined value on GPIO $iox\n";
84 0           exit;
85             }#end else
86             }#end gpio_read
87              
88             sub gpio_write{
89 0     0 1   my $ind = $_[1];
90 0           $iox = $line_nums[$ind];
91              
92 0           my $value = $_[2];
93              
94             #write value to gpio pin
95 0 0         open(GV, ">", "/sys/class/gpio/gpio$iox/value") or die $!;
96 0           print GV $value;
97 0           close(GV);
98             }#end gpio_write
99              
100             1;
101             __END__