Skip to content

Commit

Permalink
Merge pull request #25 from jnsbyr/master
Browse files Browse the repository at this point in the history
support Firmata 2.5+ feature "pin mode pullup"
  • Loading branch information
jnsbyr committed Jan 20, 2018
2 parents 54ae8e4 + 55b780c commit 018e3b1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Device-Firmata

0.64 2018.01.03 - Jens Beyer
support Firmata protocol version 2.5 feature PIN_PULLUP (Constants, Platform, Protocol)

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

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.63
Version 0.64
=cut

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


Expand Down
3 changes: 3 additions & 0 deletions lib/Device/Firmata/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use constant (
PIN_STEPPER => 8,
PIN_ENCODER => 9,
PIN_SERIAL => 10,
PIN_PULLUP => 11,
PIN_LOW => 0,
PIN_HIGH => 1,
}
Expand Down Expand Up @@ -295,6 +296,7 @@ use constant (
ONEWIRE => 0x07, # pin configured for 1-Wire commuication
STEPPER => 0x08, # pin configured for stepper motor
SERIAL => 0x0A, # pin configured for serial port
PULLUP => 0x0B, # digital pin in digitalInput mode with pullup

# Deprecated entries
deprecated => [
Expand Down Expand Up @@ -355,6 +357,7 @@ use constant (
STEPPER => 0x08, # pin configured for stepper motor
ENCODER => 0x09, # pin configured for rotary-encoders
SERIAL => 0x0A, # pin configured for serial port
PULLUP => 0x0B, # digital pin in digitalInput mode with pullup

# Deprecated entries
deprecated => [
Expand Down
15 changes: 12 additions & 3 deletions lib/Device/Firmata/Platform.pm
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ sub sysex_handle {
my @stepperpins;
my @encoderpins;
my @serialpins;
my @pulluppins;

foreach my $pin (keys %$capabilities) {
if (defined $capabilities->{$pin}) {
Expand Down Expand Up @@ -298,6 +299,9 @@ sub sysex_handle {
push @serialpins, $pin;
$self->{metadata}{serial_resolutions}{$pin} = $capabilities->{$pin}->{PIN_SERIAL+0}->{resolution};
}
if ($capabilities->{$pin}->{PIN_PULLUP+0}) {
push @pulluppins, $pin;
}
}
}
$self->{metadata}{input_pins} = \@inputpins;
Expand All @@ -311,6 +315,7 @@ sub sysex_handle {
$self->{metadata}{stepper_pins} = \@stepperpins;
$self->{metadata}{encoder_pins} = \@encoderpins;
$self->{metadata}{serial_pins} = \@serialpins;
$self->{metadata}{pullup_pins} = \@pulluppins;
last;
};

Expand Down Expand Up @@ -455,7 +460,7 @@ sub pin_mode {

PIN_MODE_HANDLER: {

( $mode == PIN_INPUT ) and do {
( $mode == PIN_INPUT or $mode == PIN_PULLUP ) and do {
my $port_number = $pin >> 3;
$self->{io}->data_write($self->{protocol}->message_prepare( SET_PIN_MODE => 0, $pin, $mode ));
$self->{io}->data_write($self->{protocol}->message_prepare( REPORT_DIGITAL => $port_number, 1 ));
Expand All @@ -479,13 +484,17 @@ sub pin_mode {
Analogous to the digitalWrite function on the
arduino
Deprecation Warning:
Writing to pin with mode "INPUT" is only supported for backward compatibility
to switch pullup on and off. Use sub pin_mode with $mode=PIN_PULLUP instead.
=cut

sub digital_write {

# --------------------------------------------------
my ( $self, $pin, $state ) = @_;
die "pin '".$pin."' is not configured for mode 'INPUT' or 'OUTPUT'" unless ($self->is_configured_mode($pin,PIN_OUTPUT) or $self->is_configured_mode($pin,PIN_INPUT));
die "pin '".$pin."' is not configured for mode 'INPUT', 'PULLUP' or 'OUTPUT'" unless ($self->is_configured_mode($pin,PIN_OUTPUT) or $self->is_configured_mode($pin,PIN_INPUT) or $self->is_configured_mode($pin,PIN_PULLUP));
my $port_number = $pin >> 3;

my $pin_offset = $pin % 8;
Expand Down Expand Up @@ -514,7 +523,7 @@ sub digital_read {

# --------------------------------------------------
my ( $self, $pin ) = @_;
die "pin '".$pin."' is not configured for mode 'INPUT'" unless $self->is_configured_mode($pin,PIN_INPUT);
die "pin '".$pin."' is not configured for mode 'INPUT' or 'PULLUP'" unless ($self->is_configured_mode($pin,PIN_INPUT) or $self->is_configured_mode($pin,PIN_PULLUP));
my $port_number = $pin >> 3;
my $pin_offset = $pin % 8;
my $pin_mask = 1 << $pin_offset;
Expand Down
1 change: 1 addition & 0 deletions lib/Device/Firmata/Protocol.pm
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ our $MODENAMES = {
8 => 'STEPPER',
9 => 'ENCODER',
10 => 'SERIAL',
11 => 'PULLUP',
};

=head1 DESCRIPTION
Expand Down

0 comments on commit 018e3b1

Please sign in to comment.