Device-Chip

 view release on metacpan or  search on metacpan

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

The following methods documented in an C<await> expression return L<Future>
instances.

This allows them to easily be used as a simple synchronous method by using the
trailing L<Future/get> call. Alternatively, if the underlying adapter allows a
fully asynchronous mode of operation, they can be combined in the usual ways
for futures to provide more asynchronous use of the device.

=cut

field $_adapter;
method adapter
{
   return $_adapter //
      croak "This chip has not yet been mounted on an adapter";
}

field $_protocol;
method protocol
{
   return $_protocol //
      croak "This chip has not yet been connected to a protocol";
}

=head2 mount

   $chip = await $chip->mount( $adapter, %params );

Supplies the chip driver with the means to actually communicate with the
connected device, via some electrical interface connected to the computer.

The parameters given in C<%params> will vary depending on the specific chip in
question, and should be documented there.

=cut

async method mount ( $adapter, %params )
{
   $_adapter = $adapter;

   my $pname = $self->PROTOCOL;

   $_protocol = await $_adapter->make_protocol( $pname );

   my $code = $self->can( "${pname}_options" ) or
      return $self;

   await $self->protocol->configure(
      $self->$code( %params )
   );

   return $self;
}

# Perl 5.36+ can distinguish this; but this still works fine on older perls
use constant true => (1 == 1);

sub _parse_options ( $, $str )
{
   return map { m/^([^=]+)=(.*)$/ ? ( $1 => $2 ) : ( $_ => true ) }
          split m/,/, $str // "";

}

=head2 mount_from_paramstr

   $chip = await $chip->mount_from_paramstr( $adapter, $paramstr );

A variant of L</mount> that parses its options from the given string. This
string should be a comma-separated list of parameters, where each is given as
a name and value separated by equals sign. If there is no equals sign, the
value is implied as true, as a convenience for parameters that are simple
boolean flags.

=cut

async method mount_from_paramstr ( $adapter, $paramstr )
{
   await $self->mount( $adapter, $self->_parse_options( $paramstr ) );
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;



( run in 1.550 second using v1.01-cache-2.11-cpan-71847e10f99 )