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 74 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 74 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Parent order matters: WiringPi::API is listed first, so under the
7 # default depth-first MRO every WiringPi::API method is found before
8 # anything in Meta, Util or RPi::SysInfo. See the MRO note atop
9 # RPi::WiringPi before adding any method whose name exists in another
10 # parent branch.
11 74 ▶use parent 'WiringPi::API';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 use parent 'RPi::WiringPi::Meta';
13 use parent 'RPi::WiringPi::Util';
14 use parent 'RPi::SysInfo';
15  
16 use Carp qw(croak);
17 use Data::Dumper;
18 use Digest::MD5 qw(md5_hex);
19 use RPi::Const qw(:all);
20  
21 our $VERSION = '3.1802';
22  
23 sub gpio_layout {
24 return $_[0]->SUPER::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 my $led = $self->_led('io');
43 my $path = "/sys/class/leds/$led->{name}";
44  
45 if ($tweak){
46 # Stop disk activity from operating the green LED
47 $self->_led_cmd("echo none | sudo tee $path/trigger");
48 # Turn on the green LED full-time
49 $self->_led_cmd("echo 1 | sudo tee $path/brightness");
50 }
51 else {
52 # Turn off the green LED from being on full-time
53 $self->_led_cmd("echo 0 | sudo tee $path/brightness");
54 # Restore disk activity operating the green LED
55 $self->_led_cmd("echo $led->{trigger} | sudo tee $path/trigger");
56 }
57 }
58 sub pwr_led {
59 my ($self, $tweak) = @_;
60  
61 my $led = $self->_led('pwr');
62 my $path = "/sys/class/leds/$led->{name}";
63  
64 if ($tweak){
65 # Turn off the red power LED
66 $self->_led_cmd("echo 0 | sudo tee $path/brightness");
67 }
68 else {
69 # Restore the red power LED to its default behavior
70 $self->_led_cmd("echo $led->{trigger} | sudo tee $path/trigger");
71 }
72 }
73 sub label {
74 my ($self, $label) = @_;
75 $self->{label} = $label if defined $label;
76 return $self->{label} // '';
77 }
78 sub pin_to_gpio {
79 my ($self, $pin, $scheme) = @_;
80  
81 $scheme = defined $scheme
82 ? $scheme
83 : $self->pin_scheme;
84  
85 if ($scheme == RPI_MODE_WPI){
86 return $self->wpi_to_gpio($pin);
87 }
88 elsif ($scheme == RPI_MODE_GPIO){
89 return $pin;
90 }
91  
92 # Catch-all: an uninitialized (or unrecognized/corrupted) scheme can't
93 # map pins; never fall through to an implicit undef
94  
95 croak "Setup not run; pin mapping scheme not initialized\n";
96 }
97 sub pin_scheme {
98 my ($self, $scheme) = @_;
99  
100 if (defined $scheme){
101 $ENV{RPI_PIN_MODE} = $scheme;
102 }
103
104 return defined $ENV{RPI_PIN_MODE}
105 ? $ENV{RPI_PIN_MODE}
106 : RPI_MODE_UNINIT;
107 }
108 sub pwm_range {
109 my ($self, $range) = @_;
110  
111 if ($> != 0) {
112 die "\nPWM requires your script to be run as the 'root' user (sudo)\n";
113 }
114  
115 if (defined $range){
116 $self->{pwm_range} = $range;
117 $self->pwm_set_range($range);
118 }
119  
120 $self->_pwm_in_use(1);
121  
122 return defined $self->{pwm_range} ? $self->{pwm_range} : PWM_DEFAULT_RANGE;
123 }
124 sub pwm_clock {
125 my ($self, $divisor) = @_;
126  
127 if ($> != 0) {
128 die "\nPWM requires your script to be run as the 'root' user (sudo)\n";
129 }
130  
131 if (defined $divisor) {
132 $self->{pwm_clock} = $divisor;
133 $self->pwm_set_clock($divisor);
134 }
135  
136 $self->_pwm_in_use(1);
137  
138 return defined $self->{pwm_clock} ? $self->{pwm_clock} : PWM_DEFAULT_CLOCK;
139 }
140 sub pwm_mode {
141 my ($self, $mode) = @_;
142  
143 if ($> != 0) {
144 die "\nPWM requires your script to be run as the 'root' user (sudo)\n";
145 }
146  
147 if (defined $mode) {
148 if ($mode == PWM_MODE_MS || $mode == PWM_MODE_BAL) {
149 $self->{pwm_mode} = $mode;
150 $self->pwm_set_mode($mode);
151 }
152 else {
153 croak "pwm_mode() requires either 0 or 1 if a param is sent in\n";
154 }
155  
156 $self->_pwm_in_use(1);
157 }
158  
159 return defined $self->{pwm_mode} ? $self->{pwm_mode} : PWM_DEFAULT_MODE;
160 }
161 sub registered_pins {
162 return $_[0]->_pin_registration;
163 }
164 sub register_pin {
165 my ($self, $pin, $comment) = @_;
166  
167 return if ! $self->_rpi_register_pins || ! $self->_rpi_register;
168  
169 $self->_pin_registration(
170 pin => $pin,
171 alt => $pin->mode_alt,
172 state => $pin->read,
173 mode => $pin->mode,
174 comment => $comment //= '',
175 operation => 'register',
176 requester => $self->uuid,
177 );
178 }
179 sub unregister_pin {
180 my ($self, $pin) = @_;
181  
182 return if ! $self->_rpi_register_pins || ! $self->_rpi_register;
183  
184 $self->_pin_registration(
185 pin => $pin,
186 operation => 'unregister',
187 requester => $self->uuid
188 );
189 }
190 sub unregister_object {
191 my ($self) = @_;
192  
193 return if ! $self->_rpi_register;
194  
195 $self->_meta_txn(sub {
196 my $meta = $self->meta_fetch;
197  
198 delete $meta->{objects}->{$self->uuid};
199 $meta->{object_count} = keys %{ $meta->{objects} };
200  
201 $self->meta_store($meta);
202 });
203 }
204 sub cleanup {
205 my ($self) = @_;
206  
207 # Only the process that created this object may tear it down. A forked child
208 # (eg. background_interrupt(), or any user fork) shares the object but must
209 # not reset pins, mutate the shared meta, or stop interrupts on the parent's
210 # behalf when it exits.
211  
212 return if defined $self->{proc} && $self->{proc} != $$;
213  
214 if ($self->_rpi_register_pins) {
215  
216 $self->_meta_txn(sub {
217 my $meta = $self->meta_fetch;
218  
219 if ($meta->{pwm}{in_use} && $meta->{pwm}{users}{$self->uuid}) {
220 delete $meta->{pwm}{users}{$self->uuid};
221  
222 if (! keys %{ $meta->{pwm}{users} }){
223 WiringPi::API::pwmSetMode(PWM_DEFAULT_MODE);
224 WiringPi::API::pwmSetClock(PWM_DEFAULT_CLOCK);
225 WiringPi::API::pwmSetRange(PWM_DEFAULT_RANGE);
226  
227 $meta->{pwm}->{in_use} = 0;
228 }
229 }
230  
231 for my $pin (keys %{$meta->{pins}}) {
232 if (exists $meta->{pins}->{$pin}{users}{$self->uuid}) {
233 delete $meta->{pins}->{$pin}{users}{$self->uuid};
234  
235 # Reset and drop the pin only when no other object still owns it
236  
237 if (! keys %{ $meta->{pins}->{$pin}{users} }) {
238 $self->_restore_pin_alt($pin, $meta->{pins}->{$pin}{alt});
239 WiringPi::API::digitalWrite($pin, $meta->{pins}->{$pin}{state});
240 delete $meta->{pins}->{$pin};
241 }
242 }
243 }
244  
245 $self->meta_store($meta);
246 });
247 }
248  
249 # Release any armed interrupts: stops the wiringPi ISR threads and closes
250 # the self-pipe so we don't leak the kernel ISR + fds at teardown
251  
252 WiringPi::API::stop_interrupts();
253  
254 # Stop any background workers started via $pi->worker. stop() is idempotent
255 # (a no-op on an already-stopped handle), and the forked-child proc guard
256 # above keeps a child from reaping the parent's workers.
257  
258 for my $worker (@{ $self->{workers} || [] }) {
259 $worker->stop if $worker;
260 }
261 $self->{workers} = [];
262  
263 $self->unregister_object if $self->_rpi_register;
264  
265 # Drop this object's signal-cleanup entries (and restore the original
266 # handlers once the last object is gone)
267  
268 if ($self->_rpi_register && $self->can('_release_signal_handlers')) {
269 $self->_release_signal_handlers;
270 }
271  
272 $self->{clean} = 1;
273 }
274  
275 sub _led {
276 my ($self, $led) = @_;
277  
278 # Pi 5 / RP1 exposes its LEDs as ACT (activity) and PWR (power); earlier
279 # boards use led0 and led1. The default restore trigger differs too
280  
281 my %map = WiringPi::API::pi_rp1_model()
282 ? (
283 io => { name => 'ACT', trigger => 'mmc0' },
284 pwr => { name => 'PWR', trigger => 'none' },
285 )
286 : (
287 io => { name => 'led0', trigger => 'mmc0' },
288 pwr => { name => 'led1', trigger => 'input' },
289 );
290  
291 return $map{$led};
292 }
293 sub _led_cmd {
294 # Runs an LED sysfs write via the shell, surfacing failures instead of
295 # silently no-opping (eg. when sudo isn't available or isn't passwordless)
296  
297 my ($self, $cmd) = @_;
298  
299 if (system("$cmd > /dev/null") != 0) {
300 warn "LED command '$cmd' failed; is sudo configured on this system?\n";
301 }
302 }
303 sub _rpi_register {
304 # Allow defeating the entire registration process (objects and pins)
305 return $_[0]->{rpi_register} // 1;
306 }
307 sub _rpi_register_pins {
308 # Allow defeating the shared memory pin registration
309 return $_[0]->{rpi_register_pins} // 1;
310 }
311 sub _pin_registration {
312 # Manages the registration duties for pins
313  
314 my ($self, %param) = @_;
315  
316 return if ! $self->_rpi_register_pins || ! $self->_rpi_register;
317  
318 my $pin = $param{pin};
319  
320 # The whole transaction runs under _meta_txn so the lock is released even
321 # if a croak (or a die from a pin method) fires mid-section
322  
323 return $self->_meta_txn(sub {
324 my $meta = $self->meta_fetch;
325  
326 if (! defined $pin){
327 my @registered_pins = keys %{ $meta->{pins} };
328 return \@registered_pins;
329 }
330  
331 my $pin_num = $self->pin_to_gpio($pin->num);
332  
333 if ($param{operation} eq 'unregister'){
334 if (! exists $meta->{pins}{$pin_num}{users}{$param{requester}}) {
335 return;
336 }
337 if (exists $meta->{pins}{$pin_num}) {
338 $pin->mode_alt($meta->{pins}{$pin_num}{alt});
339 $pin->write($meta->{pins}{$pin_num}{state});
340 $pin->mode($meta->{pins}{$pin_num}{mode});
341  
342 delete $meta->{pins}{$pin_num};
343  
344 $self->meta_store($meta);
345  
346 return;
347 }
348 }
349  
350 if (! exists $param{state} && ! exists $param{alt}) {
351 croak "_pin_registration() requires both 'alt' and 'state' params\n";
352 }
353  
354 if ($param{operation} eq 'register'){
355 if (exists $meta->{pins}{$pin_num}){
356 croak "pin $pin_num is already in use, can't continue...\n";
357 }
358  
359 $meta->{pins}{$pin_num}{alt} = $param{alt};
360 $meta->{pins}{$pin_num}{state} = $param{state};
361 $meta->{pins}{$pin_num}{mode} = $param{mode};
362 $meta->{pins}{$pin_num}{comment} = $pin->comment;
363 $meta->{pins}{$pin_num}{users}{$param{requester}}++;
364 }
365  
366 my @registered_pins = keys %{ $meta->{pins} };
367  
368 $self->meta_store($meta);
369  
370 return \@registered_pins;
371 });
372 }
373 sub _restore_pin_alt {
374 # Restores a pin to a previously captured alt mode. On the Raspberry Pi 5 /
375 # RP1, get_alt() returns a *mode* enum rather than a classic alt-function
376 # number, so pinModeAlt() can't round-trip the simple cases: INPUT (0) and
377 # OUTPUT (1) must be restored via pinMode(), and "no function" (31, which
378 # pinModeAlt rejects outright) via pinctrl. Real alt functions still
379 # round-trip through pinModeAlt(). Off the RP1, get_alt()'s classic encoding
380 # lines up with pinModeAlt(), so legacy Pi 3/4 behaviour is unchanged.
381  
382 my ($self, $pin_num, $alt) = @_;
383  
384 if (WiringPi::API::pi_rp1_model()) {
385 if ($alt == 0 || $alt == 1) {
386 WiringPi::API::pinMode($pin_num, $alt); # 0 = INPUT, 1 = OUTPUT
387 return;
388 }
389 if ($alt == 31) {
390 # Localize $?/$! so this shell-out (which often runs from cleanup at
391 # program exit) can't clobber the script's own exit status.
392  
393 local ($?, $!);
394  
395 if (system('pinctrl', 'set', $pin_num, 'no') != 0) {
396 warn "Couldn't restore GPIO $pin_num to 'no function' (alt 31) via " .
397 "pinctrl; is pinctrl installed and are you in the 'gpio' group?\n";
398 }
399 return;
400 }
401 }
402  
403 WiringPi::API::pinModeAlt($pin_num, $alt);
404 }
405 sub _pwm_in_use {
406 my $self = shift;
407  
408 return if ! $self->_rpi_register_pins || ! $self->_rpi_register;
409  
410 if ($_[0]){
411 $self->_meta_txn(sub {
412 my $meta = $self->meta_fetch;
413 $meta->{pwm}{in_use} = 1;
414 $meta->{pwm}{users}{$self->uuid} = 1;
415 $self->meta_store($meta);
416 });
417 }
418 }
419  
420 sub _vim{1;};
421  
422 1;
423  
424 __END__
« WiringPi.pm ↑ Summary