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::Firmata; |
25
|
|
|
|
|
|
|
$Device::WebIO::Firmata::VERSION = '0.001'; |
26
|
|
|
|
|
|
|
# ABSTRACT: Interface between Device::WebIO and Device::Firmata (Arduino) |
27
|
1
|
|
|
1
|
|
6041
|
use v5.12; |
|
1
|
|
|
|
|
189
|
|
|
1
|
|
|
|
|
233
|
|
28
|
1
|
|
|
1
|
|
869
|
use Moo; |
|
1
|
|
|
|
|
18716
|
|
|
1
|
|
|
|
|
6
|
|
29
|
1
|
|
|
1
|
|
2393
|
use namespace::clean; |
|
1
|
|
|
|
|
12611
|
|
|
1
|
|
|
|
|
6
|
|
30
|
1
|
|
|
1
|
|
1047
|
use Device::Firmata (); |
|
1
|
|
|
|
|
11399
|
|
|
1
|
|
|
|
|
24
|
|
31
|
1
|
|
|
1
|
|
9
|
use Device::Firmata::Constants qw{ :all }; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1192
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has '_firmata' => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
has 'input_pin_count' => ( |
37
|
|
|
|
|
|
|
is => 'ro', |
38
|
|
|
|
|
|
|
# Max GPIO pins Firmata supports. Would be nice if it had a way to |
39
|
|
|
|
|
|
|
# detect how many pins are actually on the device. |
40
|
|
|
|
|
|
|
default => sub { 128 }, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
has 'output_pin_count' => ( |
43
|
|
|
|
|
|
|
is => 'ro', |
44
|
|
|
|
|
|
|
default => sub { 128 }, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
has 'pwm_pin_count' => ( |
47
|
|
|
|
|
|
|
is => 'ro', |
48
|
|
|
|
|
|
|
default => sub { 128 }, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
with 'Device::WebIO::Device::DigitalOutput'; |
52
|
|
|
|
|
|
|
with 'Device::WebIO::Device::DigitalInput'; |
53
|
|
|
|
|
|
|
with 'Device::WebIO::Device::PWM'; |
54
|
|
|
|
|
|
|
with 'Device::WebIO::Device::ADC'; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub BUILDARGS |
58
|
|
|
|
|
|
|
{ |
59
|
0
|
|
|
0
|
|
|
my ($class, $args) = @_; |
60
|
0
|
|
|
|
|
|
my $port = delete $args->{port}; |
61
|
|
|
|
|
|
|
|
62
|
0
|
0
|
|
|
|
|
my $dev = Device::Firmata->open( $port ) |
63
|
|
|
|
|
|
|
or die "Could not connect to Firmata Server on '$port'\n"; |
64
|
0
|
|
|
|
|
|
$args->{'_firmata'} = $dev; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $args; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub output_pin |
70
|
|
|
|
|
|
|
{ |
71
|
0
|
|
|
0
|
|
|
my ($self, $pin, $set) = @_; |
72
|
0
|
|
|
|
|
|
$self->_firmata->digital_write( $pin, $set ); |
73
|
0
|
|
|
|
|
|
return 1; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub input_pin |
77
|
|
|
|
|
|
|
{ |
78
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
79
|
0
|
|
|
|
|
|
my $value = $self->_firmata->digital_read( $pin ); |
80
|
0
|
|
|
|
|
|
return $value; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub set_as_output |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
86
|
0
|
|
|
|
|
|
$self->_firmata->pin_mode( $pin, PIN_OUTPUT ); |
87
|
0
|
|
|
|
|
|
return 1; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub set_as_input |
91
|
|
|
|
|
|
|
{ |
92
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
93
|
0
|
|
|
|
|
|
$self->_firmata->pin_mode( $pin, PIN_INPUT ); |
94
|
0
|
|
|
|
|
|
return 1; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub pwm_bit_resolution |
99
|
|
|
|
|
|
|
{ |
100
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
101
|
|
|
|
|
|
|
# Arduino Uno bit resolution |
102
|
0
|
|
|
|
|
|
return 8; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
{ |
106
|
|
|
|
|
|
|
my %did_set_pwm; |
107
|
|
|
|
|
|
|
sub pwm_output_int |
108
|
|
|
|
|
|
|
{ |
109
|
0
|
|
|
0
|
|
|
my ($self, $pin, $value) = @_; |
110
|
0
|
|
|
|
|
|
my $firmata = $self->_firmata; |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
$firmata->pin_mode( $pin, PIN_PWM ) |
113
|
|
|
|
|
|
|
if ! exists $did_set_pwm{$pin}; |
114
|
0
|
|
|
|
|
|
$did_set_pwm{$pin} = 1; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
$firmata->analog_write( $pin, $value ); |
117
|
0
|
|
|
|
|
|
return 1; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub adc_bit_resolution |
122
|
|
|
|
|
|
|
{ |
123
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
124
|
|
|
|
|
|
|
# Arduino Uno bit resolution |
125
|
0
|
|
|
|
|
|
return 10; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub adc_volt_ref |
129
|
|
|
|
|
|
|
{ |
130
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
131
|
|
|
|
|
|
|
# Arduino Uno, except when it's 3.3V. This is a rather large assumption. |
132
|
|
|
|
|
|
|
# TODO fix this |
133
|
0
|
|
|
|
|
|
return 5.0; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub adc_pin_count |
137
|
|
|
|
|
|
|
{ |
138
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
139
|
0
|
|
|
|
|
|
return 128; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
{ |
143
|
|
|
|
|
|
|
my %did_set_adc; |
144
|
|
|
|
|
|
|
sub adc_input_int |
145
|
|
|
|
|
|
|
{ |
146
|
0
|
|
|
0
|
|
|
my ($self, $pin) = @_; |
147
|
0
|
|
|
|
|
|
my $firmata = $self->_firmata; |
148
|
|
|
|
|
|
|
|
149
|
0
|
0
|
|
|
|
|
$firmata->pin_mode( $pin, PIN_ANALOG ) |
150
|
|
|
|
|
|
|
if ! exists $did_set_adc{$pin}; |
151
|
0
|
|
|
|
|
|
$did_set_adc{$pin} = 1; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
my $value = $firmata->analog_write( $pin ); |
154
|
0
|
|
|
|
|
|
return $value; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; |
160
|
|
|
|
|
|
|
__END__ |