WiringPi/Core.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« WiringPi.pm ↑ Summary
1 package RPi::WiringPi::Core;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use parent 'WiringPi::API';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use parent 'RPi::WiringPi::Util';
8 use parent 'RPi::SysInfo';
9  
10 use Carp qw(croak);
11 use Data::Dumper;
12 use Digest::MD5 qw(md5_hex);
13 use IPC::Shareable;
14 use RPi::Const qw(:all);
15  
16 our $VERSION = '2.3633_01';
17  
18 tie my %shared_pi_info, 'IPC::Shareable', {
19 key => 'rpiw',
20 create => 1,
21 };
22  
23 sub gpio_layout {
24 return $_[0]->gpio_layout;
25 }
26 sub identify {
27 my ($self, $time) = @_;
28  
29 $time //= 5;
30  
31 $self->io_led(1);
32 $self->pwr_led(1);
33  
34 sleep $time;
35  
36 $self->io_led(0);
37 $self->pwr_led(0);
38 }
39 sub io_led {
40 my ($self, $tweak) = @_;
41  
42 if ($tweak){
43 # stop disk activity from operating the green LED
44 `echo none | sudo tee /sys/class/leds/led0/trigger`;
45 # turn on the green LED full-time
46 `echo 1 | sudo tee /sys/class/leds/led0/brightness`;
47 }
48 else {
49 # turn off the green LED from being on full-time
50 `echo 0 | sudo tee /sys/class/leds/led0/brightness`;
51 # start disk activity operating the green LED
52 `echo mmc0 | sudo tee /sys/class/leds/led0/trigger`;
53 }
54 }
55 sub label {
56 my ($self, $label) = @_;
57 $self->{label} = $label if defined $label;
58 return $self->{label} // '';
59 }
60 sub pin_to_gpio {
61 my ($self, $pin, $scheme) = @_;
62  
63 $scheme = defined $scheme
64 ? $scheme
65 : $self->pin_scheme;
66  
67 if ($scheme == RPI_MODE_WPI){
68 return $self->wpi_to_gpio($pin);
69 }
70 elsif ($scheme == RPI_MODE_PHYS){
71 return $self->phys_to_gpio($pin);
72 }
73 elsif ($scheme == RPI_MODE_GPIO){
74 return $pin;
75 }
76 if ($scheme == RPI_MODE_UNINIT){
77 croak "setup not run; pin mapping scheme not initialized\n";
78 }
79 }
80 sub pin_scheme {
81 my ($self, $scheme) = @_;
82  
83 if (defined $scheme){
84 $ENV{RPI_PIN_MODE} = $scheme;
85 }
86
87 return defined $ENV{RPI_PIN_MODE}
88 ? $ENV{RPI_PIN_MODE}
89 : RPI_MODE_UNINIT;
90 }
91 sub pwr_led {
92 my ($self, $tweak) = @_;
93  
94 if ($tweak){
95 # turn off the red power LED
96 `echo 0 | sudo tee /sys/class/leds/led1/brightness`;
97 }
98 else {
99 # low power input to operate the red power LED
100 `echo input | sudo tee /sys/class/leds/led1/trigger`;
101 }
102 }
103 sub pwm_range {
104 my ($self, $range) = @_;
105 if (defined $range){
106 $self->{pwm_range} = $range;
107 $self->pwm_set_range($range);
108 }
109 #FIXME: add const
110 return defined $self->{pwm_range} ? $self->{pwm_range} : 1023;
111 }
112 sub pwm_clock {
113 my ($self, $divisor) = @_;
114 if (defined $divisor){
115 $self->{pwm_clock} = $divisor;
116 $self->pwm_set_clock($divisor);
117 }
118 return defined $self->{pwm_clock} ? $self->{pwm_clock} : PWM_DEFAULT_CLOCK;
119 }
120 sub pwm_mode {
121 my ($self, $mode) = @_;
122 if (defined $mode && ($mode == 0 || $mode == PWM_DEFAULT_MODE)){
123 $self->{pwm_mode} = $mode;
124 $self->pwm_set_mode($mode);
125 }
126 else {
127 croak "pwm_mode() requires either 0 or 1 if a param is sent in\n";
128 }
129 return defined $self->{pwm_mode} ? $self->{pwm_mode} : 1;
130 }
131 sub export_pin {
132 my ($self, $pin) = @_;
133 system "sudo", "gpio", "export", $self->pin_to_gpio($pin), "in";
134 }
135 sub unexport_pin {
136 my ($self, $pin) = @_;
137 system "sudo", "gpio", "unexport", $self->pin_to_gpio($pin);
138 }
139 sub registered_pins {
140 return $_[0]->_pin_registration;
141 }
142 sub register_pin {
143 my ($self, $pin, $comment) = @_;
144  
145 $self->_pin_registration(
146 pin => $pin,
147 alt => $pin->mode_alt,
148 state => $pin->read,
149 mode => $pin->mode,
150 comment => $comment //= '',
151 operation => 'register',
152 requester => $self->uuid
153 );
154 }
155 sub unregister_pin {
156 my ($self, $pin) = @_;
157 $self->_pin_registration(
158 pin => $pin,
159 operation => 'unregister',
160 requester => $self->uuid
161 );
162 }
163 sub cleanup{
164 my ($self) = @_;
165  
166 if ($ENV{PWM_IN_USE}){
167 WiringPi::API::pwm_set_mode(PWM_DEFAULT_MODE);
168 WiringPi::API::pwm_set_clock(PWM_DEFAULT_CLOCK);
169 WiringPi::API::pwm_set_range(PWM_DEFAULT_RANGE);
170 }
171  
172 for my $pin (keys %{ $shared_pi_info{pins} }){
173 if (exists $shared_pi_info{pins}->{$pin}{users}{$self->uuid}){
174 WiringPi::API::pin_mode_alt($pin, $shared_pi_info{pins}->{$pin}{alt});
175 WiringPi::API::write_pin($pin, $shared_pi_info{pins}->{$pin}{state});
176 if ($shared_pi_info{pins}->{$pin}{mode} < 4){
177 WiringPi::API::pin_mode($pin, $shared_pi_info{pins}->{$pin}{mode});
178 }
179 delete $shared_pi_info{pins}->{$pin};
180 }
181 }
182  
183 delete $shared_pi_info{objects}->{$self->uuid};
184 }
185 sub _pin_registration {
186 # manages the registration duties for pins
187  
188 my ($self, %param) = @_;
189  
190 my $pin = $param{pin};
191  
192 if (! defined $pin){
193 my @registered_pins = keys %{ $shared_pi_info{pins} };
194 return \@registered_pins;
195 }
196  
197 my $pin_num = $self->pin_to_gpio($pin->num);
198  
199 if ($param{operation} eq 'unregister'){
200  
201 if (! $shared_pi_info{pins}->{$pin_num}{users}{$param{requester}} eq $self->uuid){
202 return;
203 }
204 if (exists $shared_pi_info{pins}->{$pin_num}){
205 $pin->mode_alt($shared_pi_info{pins}->{$pin_num}{alt});
206 $pin->write($shared_pi_info{pins}->{$pin_num}{state});
207 $pin->mode($shared_pi_info{pins}->{$pin_num}{mode});
208 delete $shared_pi_info{pins}->{$pin_num};
209 return;
210 }
211 }
212  
213 if (! exists $param{state} && ! exists $param{alt}) {
214 croak "_pin_registration() requires both 'alt' and 'state' params\n";
215 }
216  
217 if ($param{operation} eq 'register'){
218 if (exists $shared_pi_info{pins}->{$pin_num}){
219 croak "pin $pin_num is already in use, can't continue...\n";
220 }
221 $shared_pi_info{pins}->{$pin_num}{alt} = $param{alt};
222 $shared_pi_info{pins}->{$pin_num}{state} = $param{state};
223 $shared_pi_info{pins}->{$pin_num}{mode} = $param{mode};
224 $shared_pi_info{pins}->{$pin_num}{comment} = $pin->comment;
225 $shared_pi_info{pins}->{$pin_num}{users}{$param{requester}}++;
226 }
227  
228 my @registered_pins = keys %{ $shared_pi_info{pins} };
229  
230 return \@registered_pins;
231 }
232 sub _vim{1;};
233 1;
234  
235 __END__
« WiringPi.pm ↑ Summary