WiringPi.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/Util.pm ↑ Summary WiringPi/Core.pm »
1 package RPi::WiringPi;
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 'RPi::WiringPi::Core';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use GPSD::Parse;
9 use RPi::ADC::ADS;
10 use RPi::ADC::MCP3008;
11 use RPi::BMP180;
12 use Carp qw(croak confess);
13 use Data::Dumper;
14 use RPi::Const qw(:all);
15 use RPi::DAC::MCP4922;
16 use RPi::DigiPot::MCP4XXXX;
17 use RPi::EEPROM::AT24C32;
18 use RPi::GPIOExpander::MCP23017;
19 use RPi::HCSR04;
20 use RPi::I2C;
21 use RPi::LCD;
22 use RPi::OLED::SSD1306::128_64;
23 use RPi::Pin;
24 use RPi::RTC::DS3231;
25 use RPi::Serial;
26 use RPi::SPI;
27 use RPi::StepperMotor;
28  
29 our $VERSION = '2.3633_01';
30  
31 # initialize the IPC shared memory space
32  
33 tie my %shared_pi_info, 'IPC::Shareable', {
34 key => 'rpiw',
35 create => 1
36 };
37  
38 # class variables
39  
40 my $fatal_exit = 1;
41 my %sig_handlers;
42 my $signal_debug = 0;
43  
44 # core
45  
46 sub new {
47 my ($self, %args) = @_;
48 $self = bless {%args}, $self;
49  
50 if (! $ENV{NO_BOARD}){
51 if (my $scheme = $ENV{RPI_PIN_MODE}){
52 # this checks if another application has already run
53 # a setup routine
54  
55 $self->pin_scheme($scheme);
56 }
57 else {
58 # we default to gpio mode
59  
60 if (! defined $self->{setup}) {
61 $self->SUPER::setup_gpio();
62 $self->pin_scheme(RPI_MODE_GPIO);
63 }
64 else {
65 if ($self->_setup =~ /^w/i) {
66 $self->SUPER::setup();
67 $self->pin_scheme(RPI_MODE_WPI);
68 }
69 elsif ($self->_setup =~ /^g/) {
70 $self->SUPER::setup_gpio();
71 $self->pin_scheme(RPI_MODE_GPIO);
72 }
73 elsif ($self->_setup =~ /^p/) {
74 $self->SUPER::setup_phys();
75 $self->pin_scheme(RPI_MODE_PHYS);
76 }
77 else {
78 $self->pin_scheme(RPI_MODE_UNINIT);
79 }
80 }
81 }
82 # set the env var so we can catch multiple
83 # setup calls properly
84  
85 $ENV{RPI_SCHEME} = $self->pin_scheme;
86 }
87  
88 $self->_fatal_exit($args{fatal_exit});
89  
90 $self->{proc} = $$;
91 $self->{uuid} = $self->checksum;
92  
93 $shared_pi_info{objects}->{$self->uuid} = $self->{proc};
94  
95 $self->_generate_signal_handlers;
96  
97 return $self;
98 }
99 sub adc {
100 my ($self, %args) = @_;
101  
102 if (defined $args{model} && $args{model} eq 'MCP3008'){
103 my $pin = $self->pin($args{channel});
104 return RPi::ADC::MCP3008->new($pin->num);
105 }
106 else {
107 # ADSxxxx ADCs don't require any pins
108 return RPi::ADC::ADS->new(%args);
109 }
110 }
111 sub bmp {
112 return RPi::BMP180->new($_[1]);
113 }
114 sub dac {
115 my ($self, %args) = @_;
116 $self->pin($args{cs});
117 $self->pin($args{shdn}) if defined $args{shdn};
118 $args{model} = 'MCP4922' if ! defined $args{model};
119 return RPi::DAC::MCP4922->new(%args);
120 }
121 sub dpot {
122 my ($self, $cs, $channel) = @_;
123 $self->pin($cs);
124 return RPi::DigiPot::MCP4XXXX->new($cs, $channel);
125 }
126 sub eeprom {
127 my ($self, %args) = @_;
128 return RPi::EEPROM::AT24C32->new(%args);
129 }
130 sub expander {
131 my ($self, $addr, $expander) = @_;
132  
133 if (! defined $expander || $expander eq 'MCP23017'){
134 $addr = 0x20 if ! defined $addr;
135 return RPi::GPIOExpander::MCP23017->new($addr);
136 }
137 }
138 sub gps {
139 my ($self, %args) = @_;
140 return GPSD::Parse->new(%args);
141 }
142 sub hcsr04 {
143 my ($self, $t, $e) = @_;
144 $self->pin($_) for ($t, $e);
145 return RPi::HCSR04->new($t, $e);
146 }
147 sub hygrometer {
148 my ($self, $pin) = @_;
149 $self->register_pin($pin);
150 return RPi::DHT11->new($pin);
151 }
152 sub i2c {
153 my ($self, $addr, $i2c_device) = @_;
154 return RPi::I2C->new($addr, $i2c_device);
155 }
156 sub lcd {
157 my ($self, %args) = @_;
158  
159 # pre-register all pins so we can clean them up
160 # accordingly upon cleanup
161  
162 for (qw(rs strb d0 d1 d2 d3 d4 d5 d6 d7)){
163 if (! exists $args{$_} || $args{$_} !~ /^\d+$/){
164 die "lcd() requires pin configuration within a hash\n";
165 }
166 next if $args{$_} == 0;
167 $self->pin($args{$_});
168 }
169  
170 my $lcd = RPi::LCD->new;
171 $lcd->init(%args);
172 return $lcd;
173 }
174 sub oled {
175 my ($self, $model, $i2c_addr, $display_splash_page) = @_;
176  
177 $model //= '128x64';
178 $i2c_addr //= 0x3C;
179  
180 my %models = (
181 '128x64' => 1,
182 '128x32' => 1,
183 '96x16' => 1,
184 );
185  
186 if (! exists $models{$model}){
187 die "oled() requires one of the following models sent in: " .
188 "128x64, 128x32 or 96x16\n";
189 }
190  
191 if ($model eq '128x64'){
192 return RPi::OLED::SSD1306::128_64->new($i2c_addr, $display_splash_page);
193 }
194 }
195 sub pin {
196 my ($self, $pin_num, $comment) = @_;
197  
198 my $gpio = $self->pin_to_gpio($pin_num);
199  
200 if (grep {$gpio == $_} @{ $self->registered_pins }){
201 croak "\npin $pin_num is already in use... can't create second object\n";
202 }
203  
204 my $pin = RPi::Pin->new($pin_num, $comment);
205  
206 $self->register_pin($pin);
207  
208 return $pin;
209 }
210 sub rtc {
211 my ($self, $rtc_addr) = @_;
212 return RPi::RTC::DS3231->new($rtc_addr);
213 }
214 sub serial {
215 my ($self, $device, $baud) = @_;
216 return RPi::Serial->new($device, $baud);
217 }
218 sub servo {
219 my ($self, $pin_num, %config) = @_;
220  
221 if ($> != 0){
222 die "\n\nat this time, servo() requires PWM functionality, and PWM " .
223 "requires your script to be run as the 'root' user (sudo)\n\n";
224 }
225  
226 $config{clock} = exists $config{clock} ? $config{clock} : 192;
227 $config{range} = exists $config{range} ? $config{range} : 2000;
228  
229 $self->_pwm_in_use(1);
230  
231 my $servo = $self->pin($pin_num);
232 $servo->mode(PWM_OUT);
233  
234 $self->pwm_mode(PWM_MODE_MS);
235 $self->pwm_clock($config{clock});
236 $self->pwm_range($config{range});
237  
238 return $servo;
239 }
240 sub shift_register {
241 my ($self, $base, $num_pins, $data, $clk, $latch) = @_;
242  
243 my @pin_nums;
244  
245 for ($data, $clk, $latch){
246 my $pin = $self->pin($_);
247 push @pin_nums, $pin->num;
248 }
249 $self->shift_reg_setup($base, $num_pins, @pin_nums);
250 }
251 sub spi {
252 my ($self, $chan, $speed) = @_;
253 my $spi = RPi::SPI->new($chan, $speed);
254 return $spi;
255 }
256 sub stepper_motor {
257 my ($self, %args) = @_;
258  
259 if (! exists $args{pins}){
260 die "steppermotor() requires an arrayref of pins sent in\n";
261 }
262  
263 if (! exists $args{expander}) {
264 for (@{$args{pins}}) {
265 $self->pin($_);
266 }
267 }
268  
269 return RPi::StepperMotor->new(%args);
270 }
271 sub DESTROY {
272 my ($self) = @_;
273 $self->cleanup;
274 }
275  
276 # private
277  
278 sub _generate_signal_handlers {
279 my $self = shift;
280  
281 if (! %sig_handlers){
282 # set up the signal handler class structure only once
283 $SIG{INT} = \&_class_signal_handler('INT');
284 $SIG{TERM} = \&_class_signal_handler('TERM');
285 $SIG{__DIE__} = sub { _class_signal_handler('__DIE__', @_) };
286 }
287  
288 $sig_handlers{'__DIE__'}{$self->uuid} = sub {
289 my @err = @_;
290 $self->_cleanup_handler('__DIE__', @err)
291 };
292 $sig_handlers{'INT'}{$self->uuid} = sub {
293 $self->_cleanup_handler('INT')
294 };
295 $sig_handlers{'TERM'}{$self->uuid} = sub {
296 $self->_cleanup_handler('TERM')
297 };
298 }
299 sub _class_signal_handler {
300 # populates the class level signal handler structure
301  
302 my $signal = shift;
303  
304 for (keys %{ $sig_handlers{$signal} }){
305 &{ $sig_handlers{$signal}->{$_} }(@_);
306 }
307 }
308 sub _cleanup_handler {
309 # the actual sig handler methods
310  
311 my ($self, $sig, @err) = @_;
312  
313 print "$_\n" for @err;
314  
315 if ($signal_debug){
316 print "running '$sig' handler for: " . $self->uuid .
317 " with fatal_exit = " . $self->_fatal_exit . "\n";
318 }
319  
320 $self->cleanup;
321  
322 if ($self->_fatal_exit){
323 delete $sig_handlers{$sig}{$self->uuid};
324  
325 if (scalar(keys %{ $sig_handlers{$sig} }) == 0){
326 exit;
327 }
328 }
329 }
330 sub _signal_handlers {
331 return \%sig_handlers;
332 }
333 sub _fatal_exit {
334 my ($self, $fatal) = @_;
335 if (defined $fatal){
336 $fatal_exit = $fatal;
337 }
338 $self->{fatal_exit} = $fatal_exit;
339 return $self->{fatal_exit};
340 }
341 sub _pwm_in_use {
342 my $self = shift;
343 $ENV{PWM_IN_USE} = 1 if @_;
344 }
345 sub _setup {
346 return $_[0]->{setup};
347 }
348  
349 sub _vim{};
350 1;
351 __END__
« WiringPi/Util.pm ↑ Summary WiringPi/Core.pm »