line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package RPi::WiringPi; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
58923
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
45
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
638
|
use parent 'RPi::WiringPi::Util'; |
|
1
|
|
|
|
|
395
|
|
|
1
|
|
|
|
|
10
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use GPSD::Parse; |
9
|
|
|
|
|
|
|
use RPi::ADC::ADS; |
10
|
|
|
|
|
|
|
use RPi::ADC::MCP3008; |
11
|
|
|
|
|
|
|
use RPi::BMP180; |
12
|
|
|
|
|
|
|
use RPi::DAC::MCP4922; |
13
|
|
|
|
|
|
|
use RPi::DigiPot::MCP4XXXX; |
14
|
|
|
|
|
|
|
use RPi::I2C; |
15
|
|
|
|
|
|
|
use RPi::LCD; |
16
|
|
|
|
|
|
|
use RPi::Pin; |
17
|
|
|
|
|
|
|
use RPi::Serial; |
18
|
|
|
|
|
|
|
use RPi::SPI; |
19
|
|
|
|
|
|
|
use RPi::WiringPi::Constant qw(:all); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '2.3619'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $fatal_exit = 1; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
BEGIN { |
26
|
|
|
|
|
|
|
sub _error { |
27
|
|
|
|
|
|
|
my $err = shift; |
28
|
|
|
|
|
|
|
print "\ndie() caught... ". __PACKAGE__ ." is cleaning up\n", |
29
|
|
|
|
|
|
|
RPi::WiringPi::Util::cleanup(); |
30
|
|
|
|
|
|
|
print "\ncleaned up, exiting...\n"; |
31
|
|
|
|
|
|
|
print "\noriginal error: $err\n"; |
32
|
|
|
|
|
|
|
exit if $fatal_exit; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$SIG{__DIE__} = \&_error; |
36
|
|
|
|
|
|
|
$SIG{INT} = \&_error; |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
# core |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new { |
41
|
|
|
|
|
|
|
my ($self, %args) = @_; |
42
|
|
|
|
|
|
|
$self = bless {%args}, $self; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
if (! $ENV{NO_BOARD}){ |
45
|
|
|
|
|
|
|
if (my $scheme = $ENV{RPI_PIN_MODE}){ |
46
|
|
|
|
|
|
|
# this checks if another application has already run |
47
|
|
|
|
|
|
|
# a setup routine |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self->pin_scheme($scheme); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
|
|
|
|
|
|
# we default to gpio mode |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if (! defined $self->{setup}) { |
55
|
|
|
|
|
|
|
$self->SUPER::setup_gpio(); |
56
|
|
|
|
|
|
|
$self->pin_scheme(RPI_MODE_GPIO); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
|
|
|
|
|
|
if ($self->_setup =~ /^w/) { |
60
|
|
|
|
|
|
|
$self->SUPER::setup(); |
61
|
|
|
|
|
|
|
$self->pin_scheme(RPI_MODE_WPI); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
elsif ($self->_setup =~ /^g/) { |
64
|
|
|
|
|
|
|
$self->SUPER::setup_gpio(); |
65
|
|
|
|
|
|
|
$self->pin_scheme(RPI_MODE_GPIO); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
elsif ($self->_setup =~ /^p/) { |
68
|
|
|
|
|
|
|
$self->SUPER::setup_phys(); |
69
|
|
|
|
|
|
|
$self->pin_scheme(RPI_MODE_PHYS); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
elsif ($self->_setup =~ /^W/){ |
72
|
|
|
|
|
|
|
$self->pin_scheme(RPI_MODE_WPI); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
|
|
|
|
|
|
$self->pin_scheme(RPI_MODE_UNINIT); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
# set the env var so we can catch multiple |
80
|
|
|
|
|
|
|
# setup calls properly |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$ENV{RPI_SCHEME} = $self->pin_scheme; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
$self->_fatal_exit; |
85
|
|
|
|
|
|
|
return $self; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
sub adc { |
88
|
|
|
|
|
|
|
my ($self, %args) = @_; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $adc; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
if (defined $args{model} && $args{model} eq 'MCP3008'){ |
93
|
|
|
|
|
|
|
my $pin = $self->pin($args{channel}); |
94
|
|
|
|
|
|
|
$adc = RPi::ADC::MCP3008->new($pin->num); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
|
|
|
|
|
|
# ADSxxxx ADCs don't require any pins |
98
|
|
|
|
|
|
|
$adc = RPi::ADC::ADS->new(%args); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
return $adc; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
sub dac { |
103
|
|
|
|
|
|
|
my ($self, %args) = @_; |
104
|
|
|
|
|
|
|
my $cs_pin = $self->pin($args{cs}); |
105
|
|
|
|
|
|
|
my $shdn_pin = $self->pin($args{shdn}) if defined $args{shdn}; |
106
|
|
|
|
|
|
|
$args{model} = 'MCP4922' if ! defined $args{model}; |
107
|
|
|
|
|
|
|
my $dac = RPi::DAC::MCP4922->new(%args); |
108
|
|
|
|
|
|
|
return $dac; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
sub dpot { |
111
|
|
|
|
|
|
|
my ($self, $cs, $channel) = @_; |
112
|
|
|
|
|
|
|
$self->register($self->pin($cs)); |
113
|
|
|
|
|
|
|
my $dpot = RPi::DigiPot::MCP4XXXX->new($cs, $channel); |
114
|
|
|
|
|
|
|
return $dpot; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
sub gps { |
117
|
|
|
|
|
|
|
my ($self, %args) = @_; |
118
|
|
|
|
|
|
|
my $gps = GPSD::Parse->new(%args); |
119
|
|
|
|
|
|
|
return $gps; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
sub pin { |
122
|
|
|
|
|
|
|
my ($self, $pin_num) = @_; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $pins_in_use = $self->registered_pins; |
125
|
|
|
|
|
|
|
my $gpio = $self->pin_to_gpio($pin_num); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
if (grep {$gpio == $_} @{ $self->registered_pins }){ |
128
|
|
|
|
|
|
|
die "\npin $pin_num is already in use... can't create second object\n"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $pin = RPi::Pin->new($pin_num); |
132
|
|
|
|
|
|
|
$self->register_pin($pin); |
133
|
|
|
|
|
|
|
return $pin; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
sub serial { |
136
|
|
|
|
|
|
|
my ($self, $device, $baud) = @_; |
137
|
|
|
|
|
|
|
return RPi::Serial->new($device, $baud); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
sub i2c { |
140
|
|
|
|
|
|
|
my ($self, $addr, $i2c_device) = @_; |
141
|
|
|
|
|
|
|
return RPi::I2C->new($addr, $i2c_device); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
sub lcd { |
144
|
|
|
|
|
|
|
my ($self, %args) = @_; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
# pre-register all pins so we can clean them up |
147
|
|
|
|
|
|
|
# accordingly upon cleanup |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
for (qw(rs strb d0 d1 d2 d3 d4 d5 d6 d7)){ |
150
|
|
|
|
|
|
|
if (! exists $args{$_} || $args{$_} !~ /^\d+$/){ |
151
|
|
|
|
|
|
|
die "lcd() requires pin configuration within a hash\n"; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
next if $args{$_} == 0; |
154
|
|
|
|
|
|
|
my $pin = $self->pin($args{$_}); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my $lcd = RPi::LCD->new; |
158
|
|
|
|
|
|
|
$lcd->init(%args); |
159
|
|
|
|
|
|
|
return $lcd; |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
sub bmp { |
162
|
|
|
|
|
|
|
my ($self, $base) = @_; |
163
|
|
|
|
|
|
|
return RPi::BMP180->new($base); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
sub hygrometer { |
166
|
|
|
|
|
|
|
my ($self, $pin) = @_; |
167
|
|
|
|
|
|
|
$self->register_pin($pin); |
168
|
|
|
|
|
|
|
my $sensor = RPi::DHT11->new($pin); |
169
|
|
|
|
|
|
|
return $sensor; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
sub spi { |
172
|
|
|
|
|
|
|
my ($self, $chan, $speed) = @_; |
173
|
|
|
|
|
|
|
my $spi = RPi::SPI->new($chan, $speed); |
174
|
|
|
|
|
|
|
return $spi; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
sub shift_register { |
177
|
|
|
|
|
|
|
my ($self, $base, $num_pins, $data, $clk, $latch) = @_; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my @pin_nums; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
for ($data, $clk, $latch){ |
182
|
|
|
|
|
|
|
my $pin = $self->pin($_); |
183
|
|
|
|
|
|
|
push @pin_nums, $pin->num; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
$self->shift_reg_setup($base, $num_pins, @pin_nums); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
# private |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub _fatal_exit { |
191
|
|
|
|
|
|
|
my $self = shift; |
192
|
|
|
|
|
|
|
$self->{fatal_exit} = shift if @_; |
193
|
|
|
|
|
|
|
$fatal_exit = $self->{fatal_exit} if defined $self->{fatal_exit}; |
194
|
|
|
|
|
|
|
return $self->{fatal_exit}; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
sub _setup { |
197
|
|
|
|
|
|
|
return $_[0]->{setup}; |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub _vim{1;}; |
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
__END__ |