Skip to content

Commit

Permalink
Merge pull request #27 from jnsbyr/0.63
Browse files Browse the repository at this point in the history
software serial support
  • Loading branch information
jnsbyr authored Jan 20, 2018
2 parents 4423041 + 945fdc3 commit 54ae8e4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Revision history for Device-Firmata

0.63 2016.03.19 - Jens Beyer
supported protocol version detection modified (Protocol)

0.62 2016.02.22 - Jens Beyer
added software serial support (Platform, Protocol)

0.61 2016.01.09 - Jens Beyer
added serial pin support (Platform, Protocol, Constants)
added protocol version query (Platform)
Expand Down
4 changes: 2 additions & 2 deletions lib/Device/Firmata.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Device::Firmata - Perl interface to Firmata for the arduino platform.
=head1 VERSION
Version 0.60
Version 0.63
=cut

our $VERSION = '0.60';
our $VERSION = '0.63';
our $DEBUG = 0;


Expand Down
7 changes: 5 additions & 2 deletions lib/Device/Firmata/Platform.pm
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ sub serial_write {

sub serial_read {
my ( $self, $port, $numbytes ) = @_;
if ($port >= 8) {
$self->{io}->data_write($self->{protocol}->packet_serial_listen( $port ));
}
return $self->{io}->data_write($self->{protocol}->packet_serial_read( $port, 0x00, $numbytes ));
}

Expand All @@ -876,8 +879,8 @@ sub serial_stopreading {
}

sub serial_config {
my ( $self, $port, $baud ) = @_;
return $self->{io}->data_write($self->{protocol}->packet_serial_config( $port, $baud ));
my ( $self, $port, $baud, $rxPin, $txPin ) = @_;
return $self->{io}->data_write($self->{protocol}->packet_serial_config( $port, $baud, $rxPin, $txPin ));
}

=head2 poll
Expand Down
53 changes: 42 additions & 11 deletions lib/Device/Firmata/Protocol.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ our $SERIAL_COMMANDS = {
SERIAL_WRITE => 0x20, # write to serial port
SERIAL_READ => 0x30, # read request to serial port
SERIAL_REPLY => 0x40, # read reply from serial port
SERIAL_LISTEN => 0x70, # start listening on software serial port
};

our $MODENAMES = {
Expand Down Expand Up @@ -435,7 +436,11 @@ sub packet_query_version {
}

sub handle_query_version_response {

my ( $self, $data ) = @_;
return {
major_version => shift @$data,
minor_version => shift @$data,
};
}

sub handle_string_data {
Expand Down Expand Up @@ -1034,18 +1039,44 @@ sub handle_encoder_response {
# * 3 baud (bits 0 - 6)
# * 4 baud (bits 7 - 13)
# * 5 baud (bits 14 - 20) // need to send 3 bytes for baud even if value is < 14 bits
# * 6 rxPin (0-127) [optional] // only set if platform requires RX pin number @TODO
# * 7 txPin (0-127) [optional] // only set if platform requires TX pin number @TODO
# * 6 rxPin (0-127) [optional] // only set if platform requires RX pin number
# * 7 txPin (0-127) [optional] // only set if platform requires TX pin number
# * 6|8 END_SYSEX (0xF7)
# */

sub packet_serial_config {
my ( $self, $port, $baud ) = @_;
my ( $self, $port, $baud, $rxPin, $txPin ) = @_;
if (defined($rxPin) && defined($txPin)) {
return $self->packet_sysex_command( SERIAL_DATA,
$SERIAL_COMMANDS->{SERIAL_CONFIG} | $port,
$baud & 0x7f,
($baud >> 7) & 0x7f,
($baud >> 14) & 0x7f,
$rxPin & 0x7f,
$txPin & 0x7f
);
} else {
return $self->packet_sysex_command( SERIAL_DATA,
$SERIAL_COMMANDS->{SERIAL_CONFIG} | $port,
$baud & 0x7f,
($baud >> 7) & 0x7f,
($baud >> 14) & 0x7f
);
}
}

#/* serial listen
# * -------------------------------
# * 0 START_SYSEX (0xF0)
# * 1 SERIAL_DATA (0x60) // command byte
# * 2 SERIAL_LISTEN (0x70) // OR with port to switch to (0x79 = switch to SW_SERIAL1)
# * 3 END_SYSEX (0xF7)
# */

sub packet_serial_listen {
my ( $self, $port ) = @_;
return $self->packet_sysex_command( SERIAL_DATA,
$SERIAL_COMMANDS->{SERIAL_CONFIG} | $port,
$baud & 0x7f,
($baud >> 7) & 0x7f,
($baud >> 14) & 0x7f
$SERIAL_COMMANDS->{SERIAL_LISTEN} | $port
);
}

Expand Down Expand Up @@ -1261,13 +1292,13 @@ Search list of implemented protocols for identical or next lower version.

sub get_max_supported_protocol_version {
my ( $self, $deviceProtcolVersion ) = @_;
return "" unless (defined($deviceProtcolVersion));
return $deviceProtcolVersion if (defined($COMMANDS->{$deviceProtcolVersion}));
return "V_2_01" unless (defined($deviceProtcolVersion)); # min. supported protocol version if undefined
return $deviceProtcolVersion if (defined($COMMANDS->{$deviceProtcolVersion})); # requested version if known

my $maxSupportedProtocolVersion = undef;
foreach my $protocolVersion (sort keys %{$COMMANDS}) {
if ($protocolVersion lt $deviceProtcolVersion) {
$maxSupportedProtocolVersion = $protocolVersion;
$maxSupportedProtocolVersion = $protocolVersion; # nearest lower version if not known
}
}

Expand Down

0 comments on commit 54ae8e4

Please sign in to comment.