| 1 | #!/usr/bin/perl -w | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package PINE64::GPIO; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.92'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | #global vars | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #array of system gpio pin numbers | ||||||||||||||||||||||||||||
| 11 | 1 | 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 | 1 | my $iox; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub new{ | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 0 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 19 | 0 | return $self; | |||||||||||||||||||||||||||
| 20 | }#end new | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub gpio_enable{ | ||||||||||||||||||||||||||
|
Subroutine
gpio_enable: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | 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 | ▶ | if($ind < 0 || $ind > $gpiosz){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ind < 0 or $ind > $gpiosz
MC/DC0%0/2
$ind < 0 or $ind > $gpiosz
$ind < 0
$ind > $gpiosz
Truth table0%0/3
A or B
A= $ind < 0B= $ind > $gpiosz
| |||||||||||||||||||||||||||||
| 32 | 0 | print "INVALID GPIO RANGE\n"; | |||||||||||||||||||||||||||
| 33 | 0 | exit; | |||||||||||||||||||||||||||
| 34 | }#end if invalid index | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if($direction ne 'in' && $direction ne 'out'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$direction ne "in" and $direction ne "out"
MC/DC0%0/2
$direction ne "in" and $direction ne "out"
$direction ne "in"
$direction ne "out"
Truth table0%0/3
A and B
A= $direction ne "in"B= $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 | ▶ | open(EF, ">", "/sys/class/gpio/export") or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | print EF $iox; | |||||||||||||||||||||||||||
| 43 | 0 | close(EF); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #set direction of gpio pin | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | open(GF, ">", "/sys/class/gpio/gpio$iox/direction") or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | print GF $direction; | |||||||||||||||||||||||||||
| 48 | 0 | close(GF); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | }#end gpio_enable | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub gpio_disable{ | ||||||||||||||||||||||||||
|
Subroutine
gpio_disable: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | #takes gpio pin num as arg | ||||||||||||||||||||||||||||
| 54 | 0 | 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 | 0 | ▶ | sub gpio_read{ | ||||||||||||||||||||||||||
|
Subroutine
gpio_read: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | #gpio number as arg, returns direction in/out | ||||||||||||||||||||||||||||
| 68 | 0 | my $ind = $_[1]; | |||||||||||||||||||||||||||
| 69 | 0 | $iox = $line_nums[$ind]; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $value = ''; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #reads state of gpio pin | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | open(GS, "/sys/class/gpio/gpio$iox/value") or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | while(<GS>){ $value = $_; }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | #print "$iox val: $value"; | ||||||||||||||||||||||||||||
| 77 | 0 | close(GS); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if($value == 0 || $value == 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$value == 0 or $value == 1
MC/DC0%0/2
$value == 0 or $value == 1
$value == 0
$value == 1
Truth table0%0/3
A or B
A= $value == 0B= $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 | 0 | ▶ | sub gpio_write{ | ||||||||||||||||||||||||||
|
Subroutine
gpio_write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 0 | my $ind = $_[1]; | |||||||||||||||||||||||||||
| 90 | 0 | $iox = $line_nums[$ind]; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my $value = $_[2]; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #write value to gpio pin | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | open(GV, ">", "/sys/class/gpio/gpio$iox/value") or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | print GV $value; | |||||||||||||||||||||||||||
| 97 | 0 | close(GV); | |||||||||||||||||||||||||||
| 98 | }#end gpio_write | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | 1; | |||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||