File Coverage

blib/lib/Device/Chip.pm
Criterion Covered Total %
statement 32 41 78.0
branch 3 4 75.0
condition 2 2 100.0
subroutine 8 9 88.8
pod 2 2 100.0
total 47 58 81.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2015-2023 -- leonerd@leonerd.org.uk
5              
6 9     9   621737 use v5.26;
  9         41  
7 9     9   63 use warnings;
  9         15  
  9         659  
8 9     9   820 use Object::Pad 0.800;
  9         11932  
  9         449  
9              
10             package Device::Chip 0.26;
11             class Device::Chip :repr(HASH);
12              
13 9     9   4074 use Carp;
  9         25  
  9         874  
14              
15 9     9   2757 use Future::AsyncAwait 0.38; # async method
  9         110663  
  9         66  
16              
17             =head1 NAME
18              
19             C - an abstraction of a hardware chip IO driver
20              
21             =head1 DESCRIPTION
22              
23             =over 2
24              
25             B: this document is currently under heavy development. Details will be
26             added, changed, and evolved as it progresses. Be warned that currently
27             anything may be changed from one version to the next.
28              
29             =back
30              
31             This package describes an interface that classes can use to implement a driver
32             to talk to a specific hardware chip or module. An instance implementing this
33             interface would communicate with the actual hardware device via some instance
34             of the related interface, L.
35              
36             The documentation in this file is aimed primarily at users of C
37             subclasses. For more information on authoring such a module, see instead
38             L.
39              
40             =head2 USING A CHIP DRIVER
41              
42             To actually use a chip driver to talk to a specific piece of hardware that is
43             connected to the computer, an adapter must be supplied. This will be an
44             instance of some class that satisfies the L interface.
45             The chip driver will use this adapter instance to access the underlying
46             hardware port used to electrically connect to the chip and communicate with
47             it. This is supplied by invoking the L method. For example:
48              
49             my $chip = Device::Chip::MAX7219->new;
50              
51             my $adapter = Device::Chip::Adapter::FTDI->new;
52              
53             await $chip->mount( $adapter );
54              
55             =cut
56              
57             =head1 CONSTRUCTOR
58              
59             =cut
60              
61             =head2 new
62              
63             $chip = Device::Chip->new;
64              
65             Returns a new instance of a chip driver object.
66              
67             =cut
68              
69             =head1 METHODS
70              
71             The following methods documented in an C expression return L
72             instances.
73              
74             This allows them to easily be used as a simple synchronous method by using the
75             trailing L call. Alternatively, if the underlying adapter allows a
76             fully asynchronous mode of operation, they can be combined in the usual ways
77             for futures to provide more asynchronous use of the device.
78              
79             =cut
80              
81             field $_adapter;
82             method adapter
83             {
84             return $_adapter //
85             croak "This chip has not yet been mounted on an adapter";
86             }
87              
88             field $_protocol;
89             method protocol
90             {
91             return $_protocol //
92             croak "This chip has not yet been connected to a protocol";
93             }
94              
95             =head2 mount
96              
97             $chip = await $chip->mount( $adapter, %params );
98              
99             Supplies the chip driver with the means to actually communicate with the
100             connected device, via some electrical interface connected to the computer.
101              
102             The parameters given in C<%params> will vary depending on the specific chip in
103             question, and should be documented there.
104              
105             =cut
106              
107 2     2 1 24 async method mount ( $adapter, %params )
  2         8  
  2         20  
  2         5  
  2         4  
108 2         6 {
109 2         5 $_adapter = $adapter;
110              
111 2         16 my $pname = $self->PROTOCOL;
112              
113 2         12 $_protocol = await $_adapter->make_protocol( $pname );
114              
115 2 50       593 my $code = $self->can( "${pname}_options" ) or
116             return $self;
117              
118 0         0 await $self->protocol->configure(
119             $self->$code( %params )
120             );
121              
122 0         0 return $self;
123             }
124              
125             # Perl 5.36+ can distinguish this; but this still works fine on older perls
126 9     9   8099 use constant true => (1 == 1);
  9         46  
  9         5849  
127              
128             sub _parse_options ( $, $str )
129 4     4   12 {
  4         7  
  4         8  
130 4 100 100     48 return map { m/^([^=]+)=(.*)$/ ? ( $1 => $2 ) : ( $_ => true ) }
  4         36  
131             split m/,/, $str // "";
132              
133             }
134              
135             =head2 mount_from_paramstr
136              
137             $chip = await $chip->mount_from_paramstr( $adapter, $paramstr );
138              
139             A variant of L that parses its options from the given string. This
140             string should be a comma-separated list of parameters, where each is given as
141             a name and value separated by equals sign. If there is no equals sign, the
142             value is implied as true, as a convenience for parameters that are simple
143             boolean flags.
144              
145             =cut
146              
147 0     0 1   async method mount_from_paramstr ( $adapter, $paramstr )
  0            
  0            
  0            
  0            
148 0           {
149 0           await $self->mount( $adapter, $self->_parse_options( $paramstr ) );
150             }
151              
152             =head1 AUTHOR
153              
154             Paul Evans
155              
156             =cut
157              
158             0x55AA;