File Coverage

blib/lib/RPi/WiringPi.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package RPi::WiringPi;
2              
3 1     1   94333 use strict;
  1         3  
  1         42  
4 1     1   9 use warnings;
  1         3  
  1         47  
5              
6 1     1   650 use parent 'RPi::WiringPi::Util';
  1         404  
  1         9  
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::HCSR04;
15             use RPi::I2C;
16             use RPi::LCD;
17             use RPi::Pin;
18             use RPi::Serial;
19             use RPi::SPI;
20             use RPi::WiringPi::Constant qw(:all);
21              
22             our $VERSION = '2.3620';
23              
24             my $fatal_exit = 1;
25              
26             BEGIN {
27             sub _error {
28             my $err = shift;
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              
39             # core
40              
41             sub servo {
42             my ($self, $pin_num) = @_;
43              
44             if ($> != 0){
45             die "\n\nat this time, servo() requires PWM functionality, and PWM " .
46             "requires your script to be run as the 'root' user (sudo)\n\n";
47             }
48              
49             $self->_pwm_in_use(1);
50              
51             my $servo = $self->pin($pin_num);
52             $servo->mode(PWM_OUT);
53            
54             $self->pwm_mode(PWM_MODE_MS);
55             $self->pwm_clock(192);
56             $self->pwm_range(2000);
57              
58             return $servo;
59             }
60             sub _pwm_in_use {
61             my $self = shift;
62             $ENV{PWM_IN_USE} = 1 if @_;
63             return $self->{pwm_in_use};
64             }
65             sub new {
66             my ($self, %args) = @_;
67             $self = bless {%args}, $self;
68              
69             if (! $ENV{NO_BOARD}){
70             if (my $scheme = $ENV{RPI_PIN_MODE}){
71             # this checks if another application has already run
72             # a setup routine
73              
74             $self->pin_scheme($scheme);
75             }
76             else {
77             # we default to gpio mode
78              
79             if (! defined $self->{setup}) {
80             $self->SUPER::setup_gpio();
81             $self->pin_scheme(RPI_MODE_GPIO);
82             }
83             else {
84             if ($self->_setup =~ /^w/) {
85             $self->SUPER::setup();
86             $self->pin_scheme(RPI_MODE_WPI);
87             }
88             elsif ($self->_setup =~ /^g/) {
89             $self->SUPER::setup_gpio();
90             $self->pin_scheme(RPI_MODE_GPIO);
91             }
92             elsif ($self->_setup =~ /^p/) {
93             $self->SUPER::setup_phys();
94             $self->pin_scheme(RPI_MODE_PHYS);
95             }
96             elsif ($self->_setup =~ /^W/){
97             $self->pin_scheme(RPI_MODE_WPI);
98             }
99             else {
100             $self->pin_scheme(RPI_MODE_UNINIT);
101             }
102             }
103             }
104             # set the env var so we can catch multiple
105             # setup calls properly
106              
107             $ENV{RPI_SCHEME} = $self->pin_scheme;
108             }
109             $self->_fatal_exit;
110             return $self;
111             }
112             sub adc {
113             my ($self, %args) = @_;
114              
115             my $adc;
116              
117             if (defined $args{model} && $args{model} eq 'MCP3008'){
118             my $pin = $self->pin($args{channel});
119             $adc = RPi::ADC::MCP3008->new($pin->num);
120             }
121             else {
122             # ADSxxxx ADCs don't require any pins
123             $adc = RPi::ADC::ADS->new(%args);
124             }
125             return $adc;
126             }
127             sub bmp {
128             my ($self, $base) = @_;
129             return RPi::BMP180->new($base);
130             }
131             sub dac {
132             my ($self, %args) = @_;
133             # we don't use the pin objects; we generate them simply for registration
134             my $cs_pin = $self->pin($args{cs});
135             my $shdn_pin = $self->pin($args{shdn}) if defined $args{shdn};
136             $args{model} = 'MCP4922' if ! defined $args{model};
137             my $dac = RPi::DAC::MCP4922->new(%args);
138             return $dac;
139             }
140             sub dpot {
141             my ($self, $cs, $channel) = @_;
142             # we don't use the pin objects; we generate them simply for registration
143             my $cs_pin = $self->pin($cs);
144             my $dpot = RPi::DigiPot::MCP4XXXX->new($cs, $channel);
145             return $dpot;
146             }
147             sub gps {
148             my ($self, %args) = @_;
149             my $gps = GPSD::Parse->new(%args);
150             return $gps;
151             }
152             sub hcsr04 {
153             my ($self, $t, $e) = @_;
154             # we don't use the pin objects; we generate them simply for registration
155             my $trig_pin = $self->pin($t);
156             my $echo_pin = $self->pin($e);
157             return RPi::HCSR04->new($t, $e);
158             }
159             sub hygrometer {
160             my ($self, $pin) = @_;
161             $self->register_pin($pin);
162             my $sensor = RPi::DHT11->new($pin);
163             return $sensor;
164             }
165             sub i2c {
166             my ($self, $addr, $i2c_device) = @_;
167             return RPi::I2C->new($addr, $i2c_device);
168             }
169             sub lcd {
170             my ($self, %args) = @_;
171              
172             # pre-register all pins so we can clean them up
173             # accordingly upon cleanup
174              
175             for (qw(rs strb d0 d1 d2 d3 d4 d5 d6 d7)){
176             if (! exists $args{$_} || $args{$_} !~ /^\d+$/){
177             die "lcd() requires pin configuration within a hash\n";
178             }
179             next if $args{$_} == 0;
180             my $pin = $self->pin($args{$_});
181             }
182              
183             my $lcd = RPi::LCD->new;
184             $lcd->init(%args);
185             return $lcd;
186             }
187             sub pin {
188             my ($self, $pin_num) = @_;
189              
190             # we don't use the pin objects; we generate them simply for registration
191              
192             my $pins_in_use = $self->registered_pins;
193             my $gpio = $self->pin_to_gpio($pin_num);
194              
195             if (grep {$gpio == $_} @{ $self->registered_pins }){
196             die "\npin $pin_num is already in use... can't create second object\n";
197             }
198              
199             my $pin = RPi::Pin->new($pin_num);
200             $self->register_pin($pin);
201             return $pin;
202             }
203             sub serial {
204             my ($self, $device, $baud) = @_;
205             return RPi::Serial->new($device, $baud);
206             }
207             sub shift_register {
208             my ($self, $base, $num_pins, $data, $clk, $latch) = @_;
209              
210             my @pin_nums;
211              
212             for ($data, $clk, $latch){
213             my $pin = $self->pin($_);
214             push @pin_nums, $pin->num;
215             }
216             $self->shift_reg_setup($base, $num_pins, @pin_nums);
217             }
218             sub spi {
219             my ($self, $chan, $speed) = @_;
220             my $spi = RPi::SPI->new($chan, $speed);
221             return $spi;
222             }
223              
224             # private
225              
226             sub _fatal_exit {
227             my $self = shift;
228             $self->{fatal_exit} = shift if @_;
229             $fatal_exit = $self->{fatal_exit} if defined $self->{fatal_exit};
230             return $self->{fatal_exit};
231             }
232             sub _setup {
233             return $_[0]->{setup};
234             }
235              
236             sub _vim{1;};
237             1;
238             __END__