line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package RPi::WiringPi::Util; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
689
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
8
|
use parent 'WiringPi::API'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
use JSON; |
8
|
|
|
|
|
|
|
use RPi::WiringPi::Constant qw(:all); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '2.3619'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub gpio_layout { |
13
|
|
|
|
|
|
|
return $_[0]->gpio_layout; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
sub pin_to_gpio { |
16
|
|
|
|
|
|
|
my ($self, $pin, $scheme) = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$scheme = defined $scheme |
19
|
|
|
|
|
|
|
? $scheme |
20
|
|
|
|
|
|
|
: $self->pin_scheme; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
if ($scheme == RPI_MODE_WPI){ |
23
|
|
|
|
|
|
|
return $self->wpi_to_gpio($pin); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
elsif ($scheme == RPI_MODE_PHYS){ |
26
|
|
|
|
|
|
|
return $self->phys_to_gpio($pin); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
elsif ($scheme == RPI_MODE_GPIO){ |
29
|
|
|
|
|
|
|
return $pin; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
if ($scheme == RPI_MODE_UNINIT){ |
32
|
|
|
|
|
|
|
die "setup not run; pin mapping scheme not initialized\n"; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
sub pin_map { |
36
|
|
|
|
|
|
|
my ($self, $scheme) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$scheme = $self->pin_scheme if ! defined $scheme; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return {} if $scheme eq RPI_MODE_UNINIT; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
if (defined $self->{pin_map_cache}{$scheme}){ |
43
|
|
|
|
|
|
|
return $self->{pin_map_cache}{$scheme}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my %map; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
for (0..63){ |
49
|
|
|
|
|
|
|
my $pin; |
50
|
|
|
|
|
|
|
if ($scheme == RPI_MODE_WPI) { |
51
|
|
|
|
|
|
|
$pin = $self->phys_to_wpi($_); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ($scheme == RPI_MODE_GPIO){ |
54
|
|
|
|
|
|
|
$pin = $self->phys_to_gpio($_); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ($scheme == RPI_MODE_PHYS){ |
57
|
|
|
|
|
|
|
$pin = $_; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
$map{$_} = $pin; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
$self->{pin_map_cache}{$scheme} = \%map; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return \%map; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
sub pin_scheme { |
66
|
|
|
|
|
|
|
my ($self, $scheme) = @_; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
if (defined $scheme){ |
69
|
|
|
|
|
|
|
$ENV{RPI_PIN_MODE} = $scheme; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
return defined $ENV{RPI_PIN_MODE} |
73
|
|
|
|
|
|
|
? $ENV{RPI_PIN_MODE} |
74
|
|
|
|
|
|
|
: RPI_MODE_UNINIT; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
sub pwm_range { |
77
|
|
|
|
|
|
|
my ($self, $range) = @_; |
78
|
|
|
|
|
|
|
if (defined $range){ |
79
|
|
|
|
|
|
|
$self->{pwm_range} = $range; |
80
|
|
|
|
|
|
|
$self->pwm_set_range($range); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
return defined $self->{pwm_range} ? $self->{pwm_range} : 1023; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
sub export_pin { |
85
|
|
|
|
|
|
|
my ($self, $pin) = @_; |
86
|
|
|
|
|
|
|
system "sudo", "gpio", "export", $self->pin_to_gpio($pin), "in"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
sub unexport_pin { |
89
|
|
|
|
|
|
|
my ($self, $pin) = @_; |
90
|
|
|
|
|
|
|
system "sudo", "gpio", "unexport", $self->pin_to_gpio($pin); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
sub registered_pins { |
93
|
|
|
|
|
|
|
return $_[0]->_pin_registration; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
sub register_pin { |
96
|
|
|
|
|
|
|
my ($self, $pin) = @_; |
97
|
|
|
|
|
|
|
$self->_pin_registration($pin, $pin->mode_alt, $pin->read); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
sub unregister_pin { |
100
|
|
|
|
|
|
|
my ($self, $pin) = @_; |
101
|
|
|
|
|
|
|
$self->_pin_registration($pin); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
sub cleanup{ |
104
|
|
|
|
|
|
|
return if ! $ENV{RPI_PINS}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $pins = decode_json $ENV{RPI_PINS}; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
for my $pin (keys %{ $pins }){ |
109
|
|
|
|
|
|
|
WiringPi::API::pin_mode_alt($pin, $pins->{$pin}{alt}); |
110
|
|
|
|
|
|
|
WiringPi::API::write_pin($pin, $pins->{$pin}{state}); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
delete $ENV{RPI_PINS}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
sub _pin_registration { |
115
|
|
|
|
|
|
|
# manages the registration duties for pins |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my ($self, $pin, $alt, $state) = @_; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $json = $ENV{RPI_PINS}; |
120
|
|
|
|
|
|
|
my $perl = defined $json ? decode_json $json : {}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
if (! defined $pin){ |
123
|
|
|
|
|
|
|
my @registered_pins = keys %{ $perl }; |
124
|
|
|
|
|
|
|
return \@registered_pins; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
if (! defined $alt){ |
128
|
|
|
|
|
|
|
if (defined $perl->{$self->pin_to_gpio($pin->num)}){ |
129
|
|
|
|
|
|
|
$pin->mode_alt($perl->{$pin->num}{alt}); |
130
|
|
|
|
|
|
|
$pin->write($perl->{$pin->num}{state}); |
131
|
|
|
|
|
|
|
delete $perl->{$self->pin_to_gpio($pin->num)}; |
132
|
|
|
|
|
|
|
$ENV{RPI_PINS} = encode_json $perl; |
133
|
|
|
|
|
|
|
return; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
die "_pin_data() requires both \$alt and \$state params\n" |
138
|
|
|
|
|
|
|
if ! defined $state; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
if (exists $perl->{$self->pin_to_gpio($pin->num)}){ |
141
|
|
|
|
|
|
|
my $gpio_pin_num = $self->pin_to_gpio($pin->num); |
142
|
|
|
|
|
|
|
die "pin $gpio_pin_num is already in use, can't continue...\n"; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$perl->{$self->pin_to_gpio($pin->num)}{alt} = $alt; |
146
|
|
|
|
|
|
|
$perl->{$self->pin_to_gpio($pin->num)}{state} = $state; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my @registered_pins = keys %{ $perl }; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$json = encode_json $perl; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$ENV{RPI_PINS} = $json; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
return \@registered_pins; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
sub _vim{1;}; |
157
|
|
|
|
|
|
|
1; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
__END__ |