File Coverage

blib/lib/Device/PCDuino.pm
Criterion Covered Total %
statement 15 48 31.2
branch 0 8 0.0
condition n/a
subroutine 5 12 41.6
pod 5 6 83.3
total 25 74 33.7


line stmt bran cond sub pod time code
1             # Copyright (c) 2014, Timm Murray
2             # All rights reserved.
3             #
4             # Redistribution and use in source and binary forms, with or without
5             # modification, are permitted provided that the following conditions are met:
6             #
7             # * Redistributions of source code must retain the above copyright notice,
8             # this list of conditions and the following disclaimer.
9             #
10             # * Redistributions in binary form must reproduce the above copyright notice,
11             # this list of conditions and the following disclaimer in the documentation
12             # and/or other materials provided with the distribution.
13             #
14             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24             # THE POSSIBILITY OF SUCH DAMAGE.
25             #
26             package Device::PCDuino;
27             $Device::PCDuino::VERSION = '0.001';
28 1     1   835 use v5.14;
  1         4  
  1         47  
29              
30             # ABSTRACT: Control the pcDuino with Perl
31              
32 1     1   5 use base 'Exporter';
  1         2  
  1         136  
33             our @EXPORT_OK = qw{
34             set_input
35             input
36             set_output
37             output
38             input_adc
39             output_pwm
40             };
41             our @EXPORT = @EXPORT_OK;
42              
43 1     1   52 use constant MODE_FILE_PATH => '/sys/devices/virtual/misc/gpio/mode/gpio';
  1         3  
  1         76  
44 1     1   5 use constant PIN_FILE_PATH => '/sys/devices/virtual/misc/gpio/pin/gpio';
  1         2  
  1         48  
45 1     1   5 use constant ADC_PIN_FILE_PATH => '/proc/adc';
  1         2  
  1         614  
46              
47              
48             sub set_input
49             {
50 0     0 1   my ($pin) = @_;
51 0           return _set_pin( $pin, 0 );
52             }
53              
54             sub set_output
55             {
56 0     0 1   my ($pin) = @_;
57 0           return _set_pin( $pin, 1 );
58             }
59              
60             sub input
61             {
62 0     0 1   my ($pin) = @_;
63 0           my $file = PIN_FILE_PATH . $pin;
64 0 0         open( my $in, '<', $file, ) or die "Can't open '$file': $!\n";
65              
66 0           my $input = '';
67 0           read( $in, $input, 4 );
68              
69 0           close $in;
70              
71 0           chomp $input;
72 0           return $input;
73             }
74              
75             sub output
76             {
77 0     0 1   my ($pin, $output) = @_;
78 0           my $file = PIN_FILE_PATH . $pin;
79 0 0         open( my $out, '>', $file, ) or die "Can't open '$file': $!\n";
80 0           print $out "$output";
81 0           close $out;
82 0           return 1;
83             }
84              
85             sub input_adc
86             {
87 0     0 1   my ($pin) = @_;
88 0           my $path = ADC_PIN_FILE_PATH . $pin;
89              
90 0 0         open( my $in, '<', $path ) or die "Can't open '$path': $!\n";
91 0           my $input = <$in>;
92 0           close $in;
93              
94 0           my ($val) = $input =~ /\A [^:]* : ([0-9]+) /x;
95 0           return $val;
96             }
97              
98             sub output_pwm
99             {
100 0     0 0   my ($pin, $output) = @_;
101              
102 0           return 1;
103             }
104              
105             sub _set_pin
106             {
107 0     0     my ($pin, $type) = @_;
108 0           my $file = MODE_FILE_PATH . $pin;
109 0 0         open( my $out, '>', $file, ) or die "Can't open '$file': $!\n";
110 0           print $out "$type";
111 0           close $out;
112 0           return 1;
113             }
114              
115              
116             1;
117             __END__