File Coverage

blib/lib/Device/Firmata.pm
Criterion Covered Total %
statement 12 25 48.0
branch 0 2 0.0
condition 0 2 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 18 37 48.6


line stmt bran cond sub pod time code
1             package Device::Firmata;
2              
3 1     1   68347 use strict;
  1         3  
  1         30  
4 1     1   6 use warnings;
  1         1  
  1         29  
5              
6 1     1   505 use Device::Firmata::Constants;
  1         3  
  1         59  
7             use Device::Firmata::Base
8 1         6 ISA => 'Device::Firmata::Base',
9             FIRMATA_ATTRIBS => {
10 1     1   466 };
  1         2  
11              
12             =head1 NAME
13              
14             Device::Firmata - module for controlling Firmata devices
15              
16             =head1 DESCRIPTION
17              
18             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.
19              
20             =head1 VERSION
21              
22             Version 0.69
23              
24             =cut
25              
26             our $VERSION = '0.69';
27             our $DEBUG = 0;
28              
29              
30             =head1 SYNOPSIS
31              
32             use strict;
33             use warnings;
34              
35             use Device::Firmata::Constants qw/ :all /;
36             use Device::Firmata;
37              
38             use Time::HiRes 'sleep';
39              
40             $|++;
41              
42             my $led_pin = 13;
43              
44             my $device = Device::Firmata->open('/dev/ttyUSB0') or die "Could not connect to Firmata Server";
45             $device->pin_mode($led_pin=>PIN_OUTPUT);
46             my $iteration = 0;
47             while (1) {
48             my $strobe_state = $iteration++%2;
49             $device->digital_write($led_pin=>$strobe_state);
50             sleep 0.5;
51             }
52              
53             =head1 METHODS
54              
55             =head2 open ( serialPort , [opts] )
56              
57             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.
58              
59             =cut
60              
61             sub open {
62             # --------------------------------------------------
63             # Establish a connection to Arduino via the serial port
64             #
65 0     0 1   my ( $self, $serial_port, $opts ) = @_;
66              
67             # We're going to try and create the device connection first...
68 0           my $package = "Device::Firmata::Platform";
69 0           eval "require $package";
70 0           my $serialio = "Device::Firmata::IO::SerialIO";
71 0           eval "require $serialio";
72              
73 0           my $io = $serialio->open( $serial_port, $opts );
74 0 0         my $platform = $package->attach( $io, $opts ) or die "Could not connect to Firmata Server";
75              
76             # Figure out what platform we're running on
77 0           $platform->probe;
78 0           return $platform;
79             }
80              
81             =head2 listen ( host, port, [opts] )
82              
83             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.
84              
85             =cut
86              
87             sub listen {
88             # --------------------------------------------------
89             # Listen on socket and wait for Arduino to establish a connection
90             #
91 0     0 1   my ( $pkg, $ip, $port, $opts ) = @_;
92              
93 0           my $netio = "Device::Firmata::IO::NetIO";
94 0           eval "require $netio";
95              
96 0   0       return $netio->listen( $ip, $port, $opts ) || die "Could not bind to socket";
97             }
98              
99             =head1 EXAMPLES
100              
101             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.
102              
103             =head1 SEE ALSO
104              
105             L
106              
107             =head1 LICENSE
108              
109             Copyright (C) 2010 Aki Mimoto
110              
111             Copyright (C) 2012 Norbert Truchsess
112              
113             Copyright (C) 2016 Jens B.
114              
115             This is free software; you can redistribute it and/or modify
116             it under the same terms as Perl itself.
117              
118             See L for more information.
119              
120             =cut
121              
122             1;