line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Device::Firmata; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
70429
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
548
|
use Device::Firmata::Constants; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
54
|
|
7
|
|
|
|
|
|
|
use Device::Firmata::Base |
8
|
1
|
|
|
|
|
6
|
ISA => 'Device::Firmata::Base', |
9
|
|
|
|
|
|
|
FIRMATA_ATTRIBS => { |
10
|
1
|
|
|
1
|
|
511
|
}; |
|
1
|
|
|
|
|
3
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Device::Firmata - A Perl implementation of the Firmata protocol. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
This module allows a computer running Perl to connect to Firmata devices (Arduinos and compatible, including ESP8266), either via serial I/O (RS-232, USB, etc.) or TCP/IP (LAN, WiFi). Protocol details can be found at L. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Version 0.67 |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.67'; |
25
|
|
|
|
|
|
|
our $DEBUG = 0; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use strict; |
31
|
|
|
|
|
|
|
use warnings; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
use Device::Firmata::Constants qw/ :all /; |
34
|
|
|
|
|
|
|
use Device::Firmata; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
use Time::HiRes 'sleep'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$|++; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $led_pin = 13; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $device = Device::Firmata->open('/dev/ttyUSB0') or die "Could not connect to Firmata Server"; |
43
|
|
|
|
|
|
|
$device->pin_mode($led_pin=>PIN_OUTPUT); |
44
|
|
|
|
|
|
|
my $iteration = 0; |
45
|
|
|
|
|
|
|
while (1) { |
46
|
|
|
|
|
|
|
my $strobe_state = $iteration++%2; |
47
|
|
|
|
|
|
|
$device->digital_write($led_pin=>$strobe_state); |
48
|
|
|
|
|
|
|
sleep 0.5; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 METHODS |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head2 open ( serialPort , [opts] ) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Establish a serial connection with a Firmata device. The first parameter is the name of the serial device connected with the Firmata device, e.g. '/dev/ttyUSB0' or 'COM9'. The second parameter is an optional hash of parameters for the serial port. The parameter C is supported and defaults to C<57600>. Returns a L object. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=cut |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub open { |
60
|
|
|
|
|
|
|
# -------------------------------------------------- |
61
|
|
|
|
|
|
|
# Establish a connection to Arduino via the serial port |
62
|
|
|
|
|
|
|
# |
63
|
0
|
|
|
0
|
1
|
|
my ( $self, $serial_port, $opts ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# We're going to try and create the device connection first... |
66
|
0
|
|
|
|
|
|
my $package = "Device::Firmata::Platform"; |
67
|
0
|
|
|
|
|
|
eval "require $package"; |
68
|
0
|
|
|
|
|
|
my $serialio = "Device::Firmata::IO::SerialIO"; |
69
|
0
|
|
|
|
|
|
eval "require $serialio"; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $io = $serialio->open( $serial_port, $opts ); |
72
|
0
|
0
|
|
|
|
|
my $platform = $package->attach( $io, $opts ) or die "Could not connect to Firmata Server"; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Figure out what platform we're running on |
75
|
0
|
|
|
|
|
|
$platform->probe; |
76
|
0
|
|
|
|
|
|
return $platform; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 listen ( host, port, [opts] ) |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Start a TCP server bound to given local address and port for the Arduino to connect to. Returns a L object. An implementation example can be found in file F. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub listen { |
86
|
|
|
|
|
|
|
# -------------------------------------------------- |
87
|
|
|
|
|
|
|
# Listen on socket and wait for Arduino to establish a connection |
88
|
|
|
|
|
|
|
# |
89
|
0
|
|
|
0
|
1
|
|
my ( $pkg, $ip, $port, $opts ) = @_; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $netio = "Device::Firmata::IO::NetIO"; |
92
|
0
|
|
|
|
|
|
eval "require $netio"; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
0
|
|
|
|
return $netio->listen( $ip, $port, $opts ) || die "Could not bind to socket"; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 EXAMPLES |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
In the folder F you will find more than 15 implementation examples for various Firmata I/O operations including digital I/O, PWM, stepper and encoder as well as bus I/O for I2C and 1-Wire. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 SEE ALSO |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
L |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 LICENSE |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Copyright (C) 2010 Aki Mimoto |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Copyright (C) 2012 Norbert Truchsess |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Copyright (C) 2016 Jens B. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify |
114
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
See L for more information. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |