Device-HID

 view release on metacpan or  search on metacpan

lib/Device/HID.pm  view on Meta::CPAN

    return $ret;
}

=item timeout
 
    $dev->timeout = 0.1; # seconds (=100 ms)
    printf "Timeout is %d\n", $dev->timeout;
 
Lvalue subroutine that can be used to set and get the timeout in seconds for C<read_data>. Granularity is 1 millisecond.

Default value is C<undef>, which means wait indefinitely.
 
=cut

sub timeout : lvalue {
        my $self = shift;
    return $self->{timeout};
}


=item write_data
 
    $dev->write_data($reportid, $data)
 
Writes data to the device.
 
Returns actual number of bytes written or C<undef> on error unless C<autodie> is in effect.
 
=cut
 
sub write_data {
    my $self = shift;
    my ( undef, $size ) = @_;
    my $ret = hid_write( $self->{handle}, $_[0], length $_[0]);
    if ($ret == -1) {
        my $msg = "Error in write_data"; # fixme use hid_error!
        $self->{autodie} and croak $msg or carp $msg;
    }
    return $ret;
}

=item autodie
 
    $dev->autodie = 1;
 
Lvalue subroutine that can be used to set whether the module L<Carp/croak>s on failure.

Default value is C<0>.
 
=cut

sub autodie : lvalue {
    my $self = shift;
    return $self->{autodie};
}

=item renew_on_timeout
 
    $dev->renew_on_timeout;
 
Closes HIDAPI handle and opens a new one transparently at C<read_data> timeout and retries reading. When C<read_data> returns successfully the first time, C<renew_on_timeout> is reset and timeout is set to C<undef>, but can be manually adjusted.

For reasons unknown to me, Valve's Steam controller needs a couple of C<hid_open> calls before C<hid_read> manages to read data. None of the prior C<hid_open> calls fail, they just block indefinitely. For devices that ought to report periodically wha...

=cut

sub renew_on_timeout  {
    my $self = shift;
    $self->{renew} = 1;
    defined $self->{timeout} or croak "Thou shalt not call renew_on_timeout when timeout is not in sight.";

    return $self;
}

sub DESTROY {
    my $self = shift;
    hid_close($self->{handle}) if defined $self->{handle};
}

# Usually, no need to call this one directly
sub init {
    hid_init() == 0 or croak "Failed to initialize HIDAPI";
}

sub exit {
    hid_exit()
}

1;
__END__

=back

=head1 TODO

Use C<hid_error> in croak/carp. Wrap the other information retrieval function. Till then, you can use the XSUBs in L<Device::HID::XS>.

=head1 GIT REPOSITORY

L<http://github.com/athreef/Device-HID>

=head1 SEE ALSO

L<Device::HID::XS>

L<Alien::HIDAPI>

The API of this module was modelled after L<Device::FTDI> by Pavel Shaydo.

=head1 AUTHOR

Ahmad Fatoum C<< <athreef@cpan.org> >>, L<http://a3f.at>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2017 Ahmad Fatoum

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



( run in 2.327 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )