line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
############################################################################### |
2
|
|
|
|
|
|
|
# Distribution : HiPi Modules for Raspberry Pi |
3
|
|
|
|
|
|
|
# File : lib/HiPi.pm |
4
|
|
|
|
|
|
|
# Description : Pepi module for Raspberry Pi |
5
|
|
|
|
|
|
|
# Copyright : Copyright (c) 2013-2019 Mark Dootson |
6
|
|
|
|
|
|
|
# License : This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
######################################################################################### |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package HiPi; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
############################################################################### |
13
|
5
|
|
|
5
|
|
523315
|
use strict; |
|
5
|
|
|
|
|
47
|
|
|
5
|
|
|
|
|
152
|
|
14
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
153
|
|
15
|
5
|
|
|
5
|
|
2495
|
use parent qw( Exporter ); |
|
5
|
|
|
|
|
1613
|
|
|
5
|
|
|
|
|
26
|
|
16
|
5
|
|
|
5
|
|
3328
|
use HiPi::Constant qw( :hipi ); |
|
5
|
|
|
|
|
44
|
|
|
5
|
|
|
|
|
2357
|
|
17
|
5
|
|
|
5
|
|
91
|
use HiPi::RaspberryPi; |
|
5
|
|
|
|
|
16
|
|
|
5
|
|
|
|
|
230
|
|
18
|
5
|
|
|
5
|
|
37
|
use constant hipi_export_constants(); |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
22
|
|
19
|
5
|
|
|
5
|
|
49
|
use Scalar::Util qw( weaken isweak refaddr ); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
660
|
|
20
|
5
|
|
|
5
|
|
41
|
use Carp; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
3944
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION ='0.82'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT_OK = hipi_export_ok(); |
25
|
|
|
|
|
|
|
our %EXPORT_TAGS = hipi_export_tags(); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $registered_exits = {}; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our $interrupt_verbose = 0; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# who knows what we can catch |
32
|
|
|
|
|
|
|
$SIG{INT} = \&_call_registered_and_exit; |
33
|
|
|
|
|
|
|
$SIG{TERM} = \&_call_registered_and_exit; |
34
|
|
|
|
|
|
|
$SIG{HUP} = \&_call_registered_and_exit; |
35
|
|
|
|
|
|
|
|
36
|
7
|
|
|
7
|
0
|
27
|
sub is_raspberry_pi { return HiPi::RaspberryPi::is_raspberry() ; } |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
0
|
0
|
|
sub alt_func_version { return HiPi::RaspberryPi::alt_func_version() ; } |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub catch_sigpipe { |
41
|
0
|
|
|
0
|
0
|
|
$SIG{PIPE} = \&_call_registered_and_exit; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub twos_compliment { |
45
|
0
|
|
|
0
|
0
|
|
my( $class, $value, $numbytes) = @_; |
46
|
0
|
|
|
|
|
|
my $onescomp = (~$value) & ( 2**(8 * $numbytes) -1 ); |
47
|
0
|
|
|
|
|
|
return $onescomp + 1; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub register_exit_method { |
51
|
0
|
|
|
0
|
0
|
|
my($class, $obj, $method) = @_; |
52
|
0
|
|
|
|
|
|
my $key = refaddr( $obj ); |
53
|
0
|
|
|
|
|
|
$registered_exits->{$key} = [ $obj, $method ]; |
54
|
0
|
|
|
|
|
|
weaken( $registered_exits->{$key}->[0] ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub unregister_exit_method { |
58
|
0
|
|
|
0
|
0
|
|
my($class, $obj) = @_; |
59
|
0
|
|
|
|
|
|
my $key = refaddr( $obj ); |
60
|
0
|
0
|
|
|
|
|
delete($registered_exits->{$key}) if exists($registered_exits->{$key}); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub _call_registered_and_exit { |
64
|
0
|
|
|
0
|
|
|
my $interrupt = shift; |
65
|
0
|
|
|
|
|
|
my $tid = 0; |
66
|
0
|
0
|
|
|
|
|
if( $HiPi::Threads::threads ) { |
67
|
0
|
|
|
|
|
|
$tid = threads->tid(); |
68
|
0
|
0
|
|
|
|
|
HiPi::Threads->signal_handler( $interrupt ) unless( $tid ); # only call in main thread |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
for my $key ( keys %$registered_exits ) { |
72
|
0
|
|
|
|
|
|
my $method = $registered_exits->{$key}->[1]; |
73
|
0
|
0
|
0
|
|
|
|
if( isweak( $registered_exits->{$key}->[0] ) && $registered_exits->{$key}->[0]->can($method) ) { |
74
|
0
|
|
|
|
|
|
$registered_exits->{$key}->[0]->$method(); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
0
|
0
|
|
|
|
|
unless( $tid ) { |
78
|
|
|
|
|
|
|
# only in main thread |
79
|
0
|
0
|
|
|
|
|
if($interrupt_verbose) { |
80
|
0
|
|
|
|
|
|
Carp::confess(qq(\nInterrupt SIG$interrupt)); |
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
|
die qq(\nInterrupt SIG$interrupt); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub call_registered_exit_method { |
88
|
0
|
|
|
0
|
0
|
|
my($class, $instance) = @_; |
89
|
0
|
|
|
|
|
|
my $key = refaddr( $instance ); |
90
|
0
|
0
|
|
|
|
|
if(exists($registered_exits->{$key})) { |
91
|
0
|
|
|
|
|
|
my $method = $registered_exits->{$key}->[1]; |
92
|
0
|
0
|
0
|
|
|
|
if( isweak( $registered_exits->{$key}->[0] ) && $registered_exits->{$key}->[0]->can($method) ) { |
93
|
0
|
|
|
|
|
|
$registered_exits->{$key}->[0]->$method(); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=pod |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=encoding UTF-8 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 NAME |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
HiPi - Modules for Raspberry Pi GPIO |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SYNOPSIS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
use HiPi; |
111
|
|
|
|
|
|
|
.... |
112
|
|
|
|
|
|
|
use HiPi qw( :rpi :i2c :spi :mcp3adc :mcp4dac :mpl3115a2 ); |
113
|
|
|
|
|
|
|
.... |
114
|
|
|
|
|
|
|
use HiPi qw( :mcp23x17 :lcd :hrf69 :openthings :energenie ); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 DESCRIPTION |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
HiPi provides modules for use with the Raspberry Pi GPIO and |
119
|
|
|
|
|
|
|
peripherals. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Documentation and details are available at |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
L |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 AUTHOR |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Mark Dootson, C<< mdootson@cpan.org >>. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 COPYRIGHT |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Copyright (c) 2013 - 2019 Mark Dootson |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
__END__ |