From 55b780c4b23c636a97a50cb4838c69607c38230c Mon Sep 17 00:00:00 2001 From: Jens B Date: Fri, 5 Jan 2018 23:00:08 +0100 Subject: [PATCH] support PIN_PULLUP * support Firmata Protocol 2.5 feature PIN_PULLUP --- Changes | 3 +++ lib/Device/Firmata.pm | 4 ++-- lib/Device/Firmata/Constants.pm | 3 +++ lib/Device/Firmata/Platform.pm | 15 ++++++++++++--- lib/Device/Firmata/Protocol.pm | 1 + 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Changes b/Changes index 71f834a..a3d37b5 100644 --- a/Changes +++ b/Changes @@ -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) diff --git a/lib/Device/Firmata.pm b/lib/Device/Firmata.pm index b3185da..5867ba8 100644 --- a/lib/Device/Firmata.pm +++ b/lib/Device/Firmata.pm @@ -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; diff --git a/lib/Device/Firmata/Constants.pm b/lib/Device/Firmata/Constants.pm index f2e0108..beabbfd 100644 --- a/lib/Device/Firmata/Constants.pm +++ b/lib/Device/Firmata/Constants.pm @@ -30,6 +30,7 @@ use constant ( PIN_STEPPER => 8, PIN_ENCODER => 9, PIN_SERIAL => 10, + PIN_PULLUP => 11, PIN_LOW => 0, PIN_HIGH => 1, } @@ -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 => [ @@ -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 => [ diff --git a/lib/Device/Firmata/Platform.pm b/lib/Device/Firmata/Platform.pm index 2e205fb..0ed53ff 100644 --- a/lib/Device/Firmata/Platform.pm +++ b/lib/Device/Firmata/Platform.pm @@ -256,6 +256,7 @@ sub sysex_handle { my @stepperpins; my @encoderpins; my @serialpins; + my @pulluppins; foreach my $pin (keys %$capabilities) { if (defined $capabilities->{$pin}) { @@ -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; @@ -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; }; @@ -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 )); @@ -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; @@ -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; diff --git a/lib/Device/Firmata/Protocol.pm b/lib/Device/Firmata/Protocol.pm index 6ab4814..21803ea 100644 --- a/lib/Device/Firmata/Protocol.pm +++ b/lib/Device/Firmata/Protocol.pm @@ -114,6 +114,7 @@ our $MODENAMES = { 8 => 'STEPPER', 9 => 'ENCODER', 10 => 'SERIAL', + 11 => 'PULLUP', }; =head1 DESCRIPTION