| 1 | package Lab::Moose::Instrument; | ||||||||||
| 2 | 30 | $Lab::Moose::Instrument::VERSION = '3.931'; | |||||||||
| 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', | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 30 | -also => [@EXPORT_OK]; | |||||||||
| 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 | 31 | ▶ | sub default_connection_options { | ||||||||
|
Subroutine
default_connection_options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 71 | return { | ||||||||||
| 72 | 31 | any => {}, | |||||||||
| 73 | VXI11 => {}, | ||||||||||
| 74 | USB => {}, | ||||||||||
| 75 | LinuxGPIB => {}, | ||||||||||
| 76 | 'VISA::GPIB' => {}, | ||||||||||
| 77 | 'VISA::USB' => {}, | ||||||||||
| 78 | Socket => {}, | ||||||||||
| 79 | Zhinst => {}, | ||||||||||
| 80 | WWW => {}, | ||||||||||
| 81 | }; | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 31 | ▶ | sub _default_connection_options { | ||||||||
|
Subroutine
_default_connection_options: called CC=2 SCAR=7.2
| |||||||||||
| 85 | 31 | my $self = shift; | |||||||||
| 86 | |||||||||||
| 87 | 31 | my $options = $self->default_connection_options(); | |||||||||
| 88 | 31 | $options = $options->{ $self->connection_type() }; | |||||||||
| 89 | 31 | ▶ | if ($options) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | return $options; | |||||||||
| 91 | } | ||||||||||
| 92 | else { | ||||||||||
| 93 | 31 | return {}; | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 35 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||
| 98 | 35 | my $self = shift; | |||||||||
| 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
| |||||||||||
| 102 | 4 | ▶ | if ( $self->has_connection_type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | croak $error_msg ; | |||||||||
| 104 | } | ||||||||||
| 105 | 4 | return; | |||||||||
| 106 | } | ||||||||||
| 107 | 31 | ▶ | if ( not $self->has_connection_type ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 236 | ▶ | sub _trim_pmt { | ||||||||
|
Subroutine
_trim_pmt: called CC=1 SCAR=0.0
| |||||||||||
| 129 | 236 | my ($retval) = pos_validated_list( | |||||||||
| 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 | 1 | ▶ | sub read { | ||||||||
|
Subroutine
read: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 140 | 1 | my $self = shift; | |||||||||
| 141 | 1 | ▶ | if ($self->connection_type ne 'HTTP') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 1 | return _trim_pmt( $self->binary_read(@_) ); | |||||||||
| 143 | } else { | ||||||||||
| 144 | 0 | return $self->binary_read(@_); | |||||||||
| 145 | } | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 235 | ▶ | sub query { | ||||||||
|
Subroutine
query: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 149 | 235 | my $self = shift; | |||||||||
| 150 | 235 | return _trim_pmt( $self->binary_query(@_) ); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | |||||||||||
| 154 | 1306 | ▶ | sub timeout_param { | ||||||||
|
Subroutine
timeout_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 155 | 1306 | return ( timeout => { isa => 'Num', optional => 1 } ); | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | |||||||||||
| 159 | 539 | ▶ | sub read_length_param { | ||||||||
|
Subroutine
read_length_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 160 | 539 | return ( read_length => { isa => 'Int', optional => 1 } ); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | |||||||||||
| 164 | 125 | ▶ | sub channel_param { | ||||||||
|
Subroutine
channel_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 165 | 125 | return ( channel => { isa => 'Int', optional => 1 } ); | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | |||||||||||
| 169 | 36 | ▶ | sub precision_param { | ||||||||
|
Subroutine
precision_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 170 | 36 | return ( precision => | |||||||||
| 171 | { isa => enum( [qw/single double/] ), default => 'single' } ); | ||||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | |||||||||||
| 175 | 533 | ▶ | sub getter_params { | ||||||||
|
Subroutine
getter_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 176 | 533 | return ( timeout_param(), read_length_param() ); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | |||||||||||
| 180 | 754 | ▶ | sub setter_params { | ||||||||
|
Subroutine
setter_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 181 | 754 | return ( timeout_param() ); | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | 1111 | ▶ | sub validated_hash_no_cache { | ||||||||
|
Subroutine
validated_hash_no_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 185 | 1111 | return validated_hash( @_, MX_PARAMS_VALIDATE_NO_CACHE => 1 ); | |||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | |||||||||||
| 189 | 405 | ▶ | sub validated_getter { | ||||||||
|
Subroutine
validated_getter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 190 | 405 | my $args_ref = shift; | |||||||||
| 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 | 537 | ▶ | sub validated_setter { | ||||||||
|
Subroutine
validated_setter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 200 | 537 | my $args_ref = shift; | |||||||||
| 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 | 44 | ▶ | sub validated_no_param_setter { | ||||||||
|
Subroutine
validated_no_param_setter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 212 | 44 | my $args_ref = shift; | |||||||||
| 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 | 125 | ▶ | sub get_default_channel { | ||||||||
|
Subroutine
get_default_channel: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 222 | 125 | my $self = shift; | |||||||||
| 223 | 125 | ▶ | if ( $self->can('instrument_nselect') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 224 | 65 | my $channel = $self->cached_instrument_nselect(); | |||||||||
| 225 | 65 | ▶ | return $channel == 1 ? '' : $channel; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | } | ||||||||||
| 227 | else { | ||||||||||
| 228 | 60 | return ''; | |||||||||
| 229 | } | ||||||||||
| 230 | } | ||||||||||
| 231 | |||||||||||
| 232 | |||||||||||
| 233 | 68 | ▶ | sub validated_channel_getter { | ||||||||
|
Subroutine
validated_channel_getter: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 234 | 68 | my $args_ref = shift; | |||||||||
| 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
| |||||||||||
| 243 | 68 | $channel = $self->get_default_channel(); | |||||||||
| 244 | } | ||||||||||
| 245 | 68 | return ( $self, $channel, %args ); | |||||||||
| 246 | } | ||||||||||
| 247 | |||||||||||
| 248 | |||||||||||
| 249 | 57 | ▶ | sub validated_channel_setter { | ||||||||
|
Subroutine
validated_channel_setter: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 250 | 57 | my $args_ref = shift; | |||||||||
| 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
| |||||||||||
| 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__ | ||||||||||