Moose/Instrument.pm

filter: Statementstmt 96.4% 108 / 112 Branchbran 61.1% 11 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 30 / 30 Podpod 76.4% 13 / 17 totaltotal 91.5% 162 / 177 SCAR 23.1
CC
10
Cov
92%
BUILD
4.1
get_default_channel
3.0
read
2.1
CRAP 10.1
« Bus.pm ↑ Summary Moose/Instrument/DummySource.pm »
1 package Lab::Moose::Instrument;
2 30 $Lab::Moose::Instrument::VERSION = '3.881';
3 #ABSTRACT: Base class for instrument drivers
4  
5 30 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Moose::Util::TypeConstraints qw(enum duck_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Module::Load 'load';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 30 our @EXPORT_OK = qw(
17 timeout_param
18 read_length_param
19 channel_param
20 precision_param
21 getter_params
22 setter_params
23 validated_getter
24 validated_setter
25 validated_no_param_setter
26 validated_channel_getter
27 validated_channel_setter
28 );
29  
30 # do not make imported functions available as methods.
31 use namespace::autoclean
32  
33 # Need this for Exporter.
34 30 -except => 'import',
35 30 ▶ -also => [@EXPORT_OK];
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 30 has connection_type => (
38 is => 'ro',
39 isa => 'Str',
40 predicate => 'has_connection_type',
41 );
42  
43 has connection_options => (
44 is => 'ro',
45 isa => 'HashRef',
46 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 30 );
48  
49 30 has connection => (
50 is => 'ro',
51 isa => duck_type( [qw/Write Read Query Clear/] ),
52  
53 handles => {
54 write => 'Write',
55 binary_read => 'Read',
56 binary_query => 'Query',
57 clear => 'Clear',
58 },
59 writer => '_connection',
60 predicate => 'has_connection',
61 );
62  
63 30 has endian => (
64 is => 'ro',
65 isa => enum( [qw/native big little/] ),
66 default => 'native',
67 );
68  
69 # Can be subclassed in drivers.
70 sub default_connection_options {
71 return {
72 31 ▶ any => {},
Subroutine default_connection_options: called CC=1 SCAR=0.0
Pod undocumented
73 VXI11 => {},
74 USB => {},
75 LinuxGPIB => {},
76 'VISA::GPIB' => {},
77 'VISA::USB' => {},
78 Socket => {},
79 Zhinst => {},
80 WWW => {},
81 };
82 }
83  
84 sub _default_connection_options {
85 31 ▶ my $self = shift;
Subroutine _default_connection_options: called CC=2 SCAR=7.2
86  
87 31 my $options = $self->default_connection_options();
88 31 $options = $options->{ $self->connection_type() };
89 31 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 0 31 31
90 0 return $options;
91 }
92 else {
93 31 return {};
94 }
95 }
96  
97 sub BUILD {
98 35 ▶ my $self = shift;
Subroutine BUILD: called CC=4 SCAR=14.1
Pod undocumented
99 35 my $error_msg
100 = "Give either ready connection or 'connection_type' argument to instrument constructor.";
101 35 ▶ if ( $self->has_connection ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_connection) 4 31 35
102 4 ▶ if ( $self->has_connection_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_connection_type) 0 4 4
103 0 croak $error_msg ;
104 }
105 4 return;
106 }
107 31 ▶ if ( not $self->has_connection_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_connection_type) 0 31 31
108 0 croak $error_msg;
109 }
110 31 my $connection_type = $self->connection_type();
111 31 $connection_type = "Lab::Moose::Connection::$connection_type";
112  
113 my $connection_options = {
114 31 %{ $self->_default_connection_options() },
115 31 %{ $self->connection_options() }
116 };
117 31 load $connection_type;
118 31 my $connection = $connection_type->new( %{$connection_options} );
119 31 $self->_connection($connection);
120 }
121  
122 30 with 'Lab::Moose::Instrument::Log';
123  
124  
125  
126 30 my $ieee488_2_white_space_character = qr/[\x{00}-\x{09}\x{0b}-\x{20}]/;
127  
128 sub _trim_pmt {
129 236 ▶ my ($retval) = pos_validated_list(
Subroutine _trim_pmt: called CC=1 SCAR=0.0
130 \@_,
131 { isa => 'Str' }
132 );
133  
134 236 $retval =~ s/${ieee488_2_white_space_character}*\n?\Z//;
135  
136 236 return $retval;
137 }
138  
139 sub read {
140 1 ▶ my $self = shift;
Subroutine read: called CC=2 SCAR=7.6
Pod documented
141 1 ▶ if ($self->connection_type ne 'HTTP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connection_type ne 'HTTP') { } 1 0 1
142 1 return _trim_pmt( $self->binary_read(@_) );
143 } else {
144 0 return $self->binary_read(@_);
145 }
146 }
147  
148 sub query {
149 235 ▶ my $self = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod documented
150 235 return _trim_pmt( $self->binary_query(@_) );
151 }
152  
153  
154 sub timeout_param {
155 1306 ▶ return ( timeout => { isa => 'Num', optional => 1 } );
Subroutine timeout_param: called CC=1 SCAR=0.0
Pod documented
156 }
157  
158  
159 sub read_length_param {
160 539 ▶ return ( read_length => { isa => 'Int', optional => 1 } );
Subroutine read_length_param: called CC=1 SCAR=0.0
Pod documented
161 }
162  
163  
164 sub channel_param {
165 125 ▶ return ( channel => { isa => 'Int', optional => 1 } );
Subroutine channel_param: called CC=1 SCAR=0.0
Pod documented
166 }
167  
168  
169 sub precision_param {
170 36 ▶ return ( precision =>
Subroutine precision_param: called CC=1 SCAR=0.0
Pod documented
171 { isa => enum( [qw/single double/] ), default => 'single' } );
172 }
173  
174  
175 sub getter_params {
176 533 ▶ return ( timeout_param(), read_length_param() );
Subroutine getter_params: called CC=1 SCAR=0.0
Pod documented
177 }
178  
179  
180 sub setter_params {
181 754 ▶ return ( timeout_param() );
Subroutine setter_params: called CC=1 SCAR=0.0
Pod documented
182 }
183  
184 sub validated_hash_no_cache {
185 1111 ▶ return validated_hash( @_, MX_PARAMS_VALIDATE_NO_CACHE => 1 );
Subroutine validated_hash_no_cache: called CC=1 SCAR=0.0
Pod undocumented
186 }
187  
188  
189 sub validated_getter {
190 405 ▶ my $args_ref = shift;
Subroutine validated_getter: called CC=1 SCAR=0.0
Pod documented
191 405 my %additional_parameter_spec = @_;
192 405 return validated_hash_no_cache(
193 $args_ref, getter_params(),
194 %additional_parameter_spec
195 );
196 }
197  
198  
199 sub validated_setter {
200 537 ▶ my $args_ref = shift;
Subroutine validated_setter: called CC=1 SCAR=0.0
Pod documented
201 537 my %additional_parameter_spec = @_;
202 537 my ( $self, %args ) = validated_hash_no_cache(
203 $args_ref, setter_params(),
204 value => { isa => 'Str' }, %additional_parameter_spec
205 );
206 537 my $value = delete $args{value};
207 537 return ( $self, $value, %args );
208 }
209  
210  
211 sub validated_no_param_setter {
212 44 ▶ my $args_ref = shift;
Subroutine validated_no_param_setter: called CC=1 SCAR=0.0
Pod documented
213 44 my %additional_parameter_spec = @_;
214 44 my ( $self, %args ) = validated_hash_no_cache(
215 $args_ref, setter_params(),
216 %additional_parameter_spec
217 );
218 44 return ( $self, %args );
219 }
220  
221 sub get_default_channel {
222 125 ▶ my $self = shift;
Subroutine get_default_channel: called CC=3 SCAR=11.0
Pod undocumented
223 125 ▶ if ( $self->can('instrument_nselect') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('instrument_nselect')) { } 65 60 125
224 65 my $channel = $self->cached_instrument_nselect();
225 65 ▶ return $channel == 1 ? '' : $channel;
Branch: 1 branch
BranchTrueFalseTotal
$channel == 1 ? : 65 0 65
226 }
227 else {
228 60 return '';
229 }
230 }
231  
232  
233 sub validated_channel_getter {
234 68 ▶ my $args_ref = shift;
Subroutine validated_channel_getter: called CC=2 SCAR=7.0
Pod documented
235 68 my %additional_parameter_spec = @_;
236 68 my ( $self, %args ) = validated_hash_no_cache(
237 $args_ref, getter_params(),
238 channel_param(), %additional_parameter_spec
239 );
240  
241 68 my $channel = delete $args{channel};
242 68 ▶ if ( not defined $channel ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $channel) 68 0 68
243 68 $channel = $self->get_default_channel();
244 }
245 68 return ( $self, $channel, %args );
246 }
247  
248  
249 sub validated_channel_setter {
250 57 ▶ my $args_ref = shift;
Subroutine validated_channel_setter: called CC=2 SCAR=7.0
Pod documented
251 57 my %additional_parameter_spec = @_;
252 57 my ( $self, %args ) = validated_hash_no_cache(
253 $args_ref, getter_params(), channel_param(),
254 value => { isa => 'Str' },
255 %additional_parameter_spec,
256 );
257 57 my $channel = delete $args{channel};
258 57 ▶ if ( not defined $channel ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $channel) 57 0 57
259 57 $channel = $self->get_default_channel();
260 }
261 57 my $value = delete $args{value};
262 57 return ( $self, $channel, $value, %args );
263 }
264  
265 30 __PACKAGE__->meta->make_immutable();
266  
267 30 1;
268  
269 __END__
270  
271 =pod
272  
273 =encoding UTF-8
274  
275 =head1 NAME
276  
277 Lab::Moose::Instrument - Base class for instrument drivers
278  
279 =head1 VERSION
280  
281 version 3.881
282  
283 =head1 SYNOPSIS
284  
285 A complete device driver based on Lab::Moose::Instrument:
286  
287 package Lab::Moose::Instrument::FooBar;
288 use Moose;
289  
290 use Lab::Moose::Instrument qw/validated_getter validated_setter/;
291  
292 use namespace::autoclean;
293  
294 extends 'Lab::Moose::Instrument';
295  
296 sub get_foo {
297 my ($self, %args) = validated_getter(\@_);
298 return $self->query(command => "Foo?", %args);
299 }
300  
301 sub set_foo {
302 my ($self, $value, %args) = validated_setter(\@_);
303 return $self->write(command => "Foo $value", %args);
304 }
305  
306 __PACKAGE__->meta->make_immutable();
307  
308 =head1 DESCRIPTION
309  
310 The Lab::Moose::Instrument module is a thin wrapper around a connection object.
311 All other Lab::Moose::Instrument::* drivers inherit from this module.
312  
313 =head1 METHODS
314  
315 =head2 new
316  
317 The constructor requires a connection object, which provides
318 C<Read>, C<Write>, C<Query> and C<Clear> methods. You can provide any object,
319 which supports these methods.
320  
321 =head2 write
322  
323 $instrument->write(command => '*RST', timeout => 10);
324  
325 Call the connection's C<Write> method. The timeout parameter is optional.
326  
327 =head2 binary_read
328  
329 my $data = $instrument->binary_read(timeout => 10);
330  
331 Call the connection's C<Read> method. The timeout parameter is optional.
332  
333 =head2 read
334  
335 Like C<binary_read>, but trim trailing whitespace and newline from the result.
336  
337 More precisely, this removes the I<PROGRAM MESSAGE TERMINATOR> (IEEE 488.2
338 section 7.5).
339  
340 =head2 binary_query
341  
342 my $data = $instrument->binary_query(command => '*IDN?', timeout => 10)
343  
344 Call the connection's C<Query> method. The timeout parameter is optional.
345  
346 =head2 query
347  
348 Like C<binary_query>, but trim trailing whitespace and newline from the result.
349  
350 More precisely, this removes the I<PROGRAM MESSAGE TERMINATOR> (IEEE 488.2
351 section 7.5).
352  
353 =head2 clear
354  
355 $instrument->clear();
356  
357 Call the connection's C<Clear> method.
358  
359 =head1 Functions
360  
361 The following functions standardise and simplify the use of
362 L<MooseX::Params::Validate> in instrument drivers. They are only exported on
363 request.
364  
365 =head2 timeout_param
366  
367 Return mandatory validation parameter for timeout.
368  
369 =head2 read_length_param
370  
371 Return mandatory validation parameter for read_length.
372  
373 =head2 channel_param
374  
375 Return optional validation parameter for channel. A given argument has to be an
376 'Int'. The default value is the empty string ''.
377  
378 =head2 precision_param
379  
380 Return optional validation parameter for floating point precision. The
381 parameter has to be either 'single' (default) or 'double'.
382  
383 =head2 getter_params
384  
385 Return list of validation parameters which shell be used in all query
386 operations, eg. timeout, ....
387  
388 =head2 setter_params
389  
390 Return list of validation parameters which shell be used in all write
391 operations, eg. timeout, ....
392  
393 =head2 validated_getter
394  
395 my ($self, %args) = validated_getter(\@_, %additional_parameter_spec);
396  
397 Call C<validated_hash> with the getter_params.
398  
399 =head2 validated_setter
400  
401 my ($self, $value, %args) = validated_setter(\@_, %additional_parameter_spec);
402  
403 Call C<validated_hash> with the C<setter_params> and a mandatory 'value'
404 argument, which must be of 'Str' type.
405  
406 =head2 validated_no_param_setter
407  
408 my ($self, %args) = validated_no_param_setter(\@_, %additional_parameter_spec);
409  
410 Like C<validated_setter> without the 'value' argument.
411  
412 =head2 validated_channel_getter
413  
414 my ($self, $channel, %args) = validated_channel_getter(\@_);
415  
416 Like C<validated_getter> with an additional C<channel_param> argument. If the
417 no channel argument is given, try to call
418 C<$self->cached_instrument_nselect>. If this method is not available, return
419 the empty string for the channel.
420  
421 =head2 validated_channel_setter
422  
423 my ($self, $channel, $value, %args) = validated_channel_setter(\@_);
424  
425 Analog to C<validated_channel_getter>.
426  
427 =head1 COPYRIGHT AND LICENSE
428  
429 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
430  
431 Copyright 2016 Simon Reinhardt
432 2017 Andreas K. Huettel, Simon Reinhardt
433 2018 Simon Reinhardt
434 2020 Andreas K. Huettel, Sam Bingner
435 2021 Fabian Weinelt
436 2022-2023 Mia Schambeck
437  
438  
439 This is free software; you can redistribute it and/or modify it under
440 the same terms as the Perl 5 programming language system itself.
441  
442 =cut
« Bus.pm ↑ Summary Moose/Instrument/DummySource.pm »