File Coverage

blib/lib/Device/WebIO/PCDuino.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 2 0.0
condition n/a
subroutine 4 12 33.3
pod 0 8 0.0
total 16 65 24.6


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             # * Redistributions in binary form must reproduce the above copyright
10             # notice, this list of conditions and the following disclaimer in the
11             # documentation and/or other materials provided with the distribution.
12             #
13             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23             # POSSIBILITY OF SUCH DAMAGE.
24             package Device::WebIO::PCDuino;
25             $Device::WebIO::PCDuino::VERSION = '0.002';
26             # ABSTRACT: Device::WebIO implementation for the pcDuino
27 1     1   558 use v5.12;
  1         4  
  1         33  
28 1     1   488 use Moo;
  1         12701  
  1         7  
29 1     1   1725 use namespace::clean;
  1         9470  
  1         6  
30 1     1   867 use Device::PCDuino ();
  1         676  
  1         406  
31              
32             has 'pin_desc' => (
33             is => 'ro',
34             # TODO this is based on the v3's pin header. Would be nice to have
35             # a configurable option for different boards.
36             default => sub {[qw{
37             SCL SDA AREF GND 13 12 11 10 9 8 7 6 5 4 3 2 1 0
38             IOREF RESET 33V 50V GND GND VIN A0 A1 A2 A3 A4 A5
39             }]},
40             );
41             has '_pin_mode' => (
42             is => 'ro',
43             );
44             has '_output_pin_value' => (
45             is => 'ro',
46             );
47              
48              
49             sub BUILDARGS
50             {
51 0     0 0   my ($class, $args) = @_;
52              
53             #$args->{pwm_bit_resolution} = 8;
54             #$args->{pwm_max_int} = 2 ** $args->{pwm_bit_resolution};
55 0           $args->{input_pin_count} = 18;
56 0           $args->{output_pin_count} = 18;
57 0           $args->{'_pin_mode'} = [ ('IN') x $args->{input_pin_count} ];
58 0           $args->{'_output_pin_value'} = [ (0) x $args->{output_pin_count} ];
59             #$args->{pwm_pin_count} = 1;
60             # TODO
61             # 6 bits for ADC 0 and 1, but 12 for the rest
62             #$args->{adc_bit_resolution} = 12;
63             #$args->{adc_max_int} = 2 ** $args->{adc_bit_resolution};
64             # TODO
65             # 2 Volts for ADC 0 and 1, but 3.3V for the rest
66             #$args->{adc_volt_ref} = 3.3;
67             #$args->{adc_pin_count} = 6;
68              
69 0           return $args;
70             }
71              
72             sub all_desc
73             {
74 0     0 0   my ($self) = @_;
75 0           my $pin_count = $self->input_pin_count;
76             return {
77 0           UART => 0,
78             SPI => 0,
79             I2C => 0,
80             ONEWIRE => 0,
81             GPIO => {
82             map {
83 0           my $function = $self->{'_pin_mode'}[$_];
84 0 0         my $value = $function eq 'IN'
85             ? $self->input_pin( $_ )
86             : $self->_output_pin_value->[$_];
87 0           $_ => {
88             function => $function,
89             value => $value,
90             };
91             } 0 .. ($pin_count - 1)
92             },
93             };
94             }
95              
96              
97             has 'input_pin_count', is => 'ro';
98             with 'Device::WebIO::Device::DigitalInput';
99              
100             sub set_as_input
101             {
102 0     0 0   my ($self, $pin) = @_;
103 0           $self->_pin_mode->[$pin] = 'IN';
104 0           Device::PCDuino::set_input( $pin );
105 0           return 1;
106             }
107              
108             sub input_pin
109             {
110 0     0 0   my ($self, $pin) = @_;
111 0           my $in = Device::PCDuino::input( $pin );
112 0           return $in;
113             }
114              
115             sub is_set_input
116             {
117 0     0 0   my ($self, $pin) = @_;
118 0           return $self->_pin_mode->[$pin] eq 'IN';
119             }
120              
121              
122             has 'output_pin_count', is => 'ro';
123             with 'Device::WebIO::Device::DigitalOutput';
124              
125             sub set_as_output
126             {
127 0     0 0   my ($self, $pin) = @_;
128 0           $self->_pin_mode->[$pin] = 'OUT';
129 0           Device::PCDuino::set_output( $pin );
130 0           return 1;
131             }
132              
133             sub output_pin
134             {
135 0     0 0   my ($self, $pin, $val) = @_;
136 0           $self->_output_pin_value->[$pin] = $val;
137 0           Device::PCDuino::output( $pin, $val );
138 0           return 1;
139             }
140              
141             sub is_set_output
142             {
143 0     0 0   my ($self, $pin) = @_;
144 0           return $self->_pin_mode->[$pin] eq 'OUT';
145             }
146              
147              
148             # TODO PWM
149             #has 'pwm_pin_count', is => 'ro';
150             #has 'pwm_bit_resolution', is => 'ro';
151             #has 'pwm_max_int', is => 'ro';
152             #with 'Device::WebIO::Device::PWM';
153             #
154             #sub pwm_output_int
155             #{
156             #}
157              
158              
159             # TODO ADC
160             #has 'adc_max_int', is => 'ro';
161             #has 'adc_bit_resolution', is => 'ro';
162             #has 'adc_volt_ref', is => 'ro';
163             #has 'adc_pin_count', is => 'ro';
164             #with 'Device::WebIO::Device::ADC';
165             #
166             #sub adc_input_int
167             #{
168             #}
169              
170              
171             # TODO
172             #with 'Device::WebIO::Device::SPI';
173             #with 'Device::WebIO::Device::I2C';
174             #with 'Device::WebIO::Device::Serial';
175             #with 'Device::WebIO::Device::VideoStream';
176              
177             1;
178             __END__