Mark, I think (according to the Perl docs I read) that syswrite will
NOT work correctly. syswrite is unbuffered, read is buffered. You
need to use the IO::Handle routines exclusively.
Here's a patch that works for me and returns the prior value (or
should, I wanted to post this before I had fully tested ...) It uses
the IO::Handle routines for all the file IO and works for my limited
testing.
Bill
--- a/hipi-latest/lib/HiPi/Device/GPIO/Pin.pm
+++ b/hipi-latest/lib/HiPi/Device/GPIO/Pin.pm
@@ -46,23 +46,25 @@ sub _open {
sub _do_getvalue {
my $self = shift;
- seek($self->valfh,0,0);
+ $self->valfh->seek(0,0);
my $result;
- read($self->valfh, $result, 16);
+ $self->valfh->read($result, 16);
chomp($result);
return $result;
}
sub _do_setvalue {
my ($self, $newval) = @_;
- write($self->valfh, $newval);
+ my $oldval = _do_getvalue($self);
+ $self->valfh->write($newval);
+ return $oldval;
}
sub _do_getmode {
my $self = shift;
- seek($self->dirfh,0,0);
+ $self->dirfh->seek(0,0);
my $result;
- read($self->dirfh, $result, 16);
+ $self->dirfh->read($result, 16);
chomp($result);
return ( $result eq 'out' ) ? RPI_PINMODE_OUTP :
RPI_PINMODE_INPT;
}
@@ -70,16 +72,16 @@ sub _do_getmode {
sub _do_setmode {
my ($self, $newdir) = @_;
if( ($newdir == RPI_PINMODE_OUTP) || ($newdir eq 'out') ) {
- write($self->dirfh, 'out');
+ $self->dirfh->write('out');
} else {
- write($self->dirfh, 'in');
+ $self->dirfh->write('in');
}
return $newdir;
}
sub _reset_value_handle {
my $self = shift;
- close($self->valfh);
+ $self->valfh->close();
my $filepath = $self->pinroot . '/value';
my $newfh = IO::File->new($filepath, O_RDWR, 0) or croak
qq(failed to open $filepath : $!);
$self->valfh($newfh);
@@ -168,8 +170,8 @@ sub active_low {
sub DESTROY {
my $self = shift;
- close($self->valfh);
- close($self->dirfh);
+ $self->valfh->close();
+ $self->dirfh->close();
}
1;