Moose/Instrument/DummySource.pm

filter: Statementstmt 97.9% 95 / 97 Branchbran 88.8% 16 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 8 totaltotal 91.4% 128 / 140 CC 10 SCAR 7.3
CC
10
Cov
97%
get_level
4.0
_remaining_sweep_time
3.0
active
3.0
CRAP 2.1
« Moose/DataFolder.pm ↑ Summary Measurement.pm »
1 package Lab::Moose::Instrument::DummySource;
2 4 $Lab::Moose::Instrument::DummySource::VERSION = '3.931';
3 #ABSTRACT: Dummy YokogawaGS200 source for use with 'Debug' connection
4  
5 4 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use Lab::Moose::Instrument
11 4 ▶ qw/validated_getter validated_setter setter_params/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Lab::Moose::Instrument::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Time::HiRes qw/time sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 extends 'Lab::Moose::Instrument';
19  
20 4 has [
21 qw/
22 max_units_per_second
23 max_units_per_step
24 min_units
25 max_units
26 /
27 ] => ( is => 'ro', isa => 'Num', required => 1 );
28  
29 4 has source_level_timestamp => (
30 is => 'rw',
31 isa => 'Num',
32 init_arg => undef,
33 );
34  
35 4 has verbose => (
36 is => 'ro',
37 isa => 'Bool',
38 default => 1
39 );
40  
41 #
42 # Internal variables for continous sweep
43 #
44  
45 4 has _sweep_start_time => (
46 is => 'rw',
47 isa => 'Lab::Moose::PosNum',
48 );
49  
50 4 has _active => (
51 is => 'rw',
52 isa => 'Bool',
53 default => 0
54 );
55  
56 4 has _sweep_target_level => (
57 is => 'rw',
58 isa => 'Num',
59 );
60  
61 4 has _sweep_start_level => (
62 is => 'rw',
63 isa => 'Num',
64 );
65  
66 4 has _sweep_rate => (
67 is => 'rw',
68 isa => 'Num',
69 );
70  
71 4 with qw(
72 Lab::Moose::Instrument::Common
73 Lab::Moose::Instrument::LinearStepSweep
74 Lab::Moose::Instrument::SCPI::Source::Function
75 Lab::Moose::Instrument::SCPI::Source::Level
76 Lab::Moose::Instrument::SCPI::Source::Range
77 );
78  
79 18 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
80 18 my $self = shift;
81 18 $self->clear();
82 18 $self->cached_source_level(0);
83 18 $self->cached_source_function('VOLT');
84  
85 # $self->cls();
86  
87 # FIXME: check protect params
88 }
89  
90  
91 167 ▶sub set_level {
Subroutine set_level: called CC=1 SCAR=0.0
Pod undocumented
92 167 my ( $self, $value, %args ) = validated_setter(
93 \@_,
94 value => { isa => 'Num' },
95 );
96  
97 167 return $self->linear_step_sweep(
98 to => $value, verbose => $self->verbose,
99 %args
100 );
101 }
102  
103 5 ▶sub cached_level {
Subroutine cached_level: called CC=1 SCAR=0.0
Pod undocumented
104 5 my $self = shift;
105 5 return $self->get_level(@_);
106 }
107  
108 # return negative if sweep is done
109 25 ▶sub _remaining_sweep_time {
Subroutine _remaining_sweep_time: called CC=3 SCAR=11.1
110 25 my $self = shift;
111 25 ▶ if ( not $self->_active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_active) 0 25 25
112 0 croak "no sweep";
113 }
114 25 my $start_time = $self->_sweep_start_time();
115 25 my $rate = $self->_sweep_rate;
116 25 my $target_level = $self->_sweep_target_level;
117 25 my $start_level = $self->_sweep_start_level;
118  
119 25 ▶ my $sgn = $target_level >= $start_level ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$target_level >= $start_level ? : 19 6 25
120 25 my $duration = abs( ( $target_level - $start_level ) / $rate );
121 25 return $duration - ( time() - $start_time );
122 }
123  
124 221 ▶sub get_level {
Subroutine get_level: called CC=4 SCAR=13.9
Pod undocumented
125 221 my ( $self, %args ) = validated_getter( \@_ );
126  
127 221 ▶ if ( $self->_active() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_active) { } 14 207 221
128  
129 # in sweep
130  
131 14 my $remaining_time = $self->_remaining_sweep_time();
132  
133 14 my $start_time = $self->_sweep_start_time();
134 14 my $rate = $self->_sweep_rate;
135 14 my $target_level = $self->_sweep_target_level;
136 14 my $start_level = $self->_sweep_start_level;
137  
138 14 ▶ my $sgn = $target_level >= $start_level ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$target_level >= $start_level ? : 10 4 14
139 14 my $duration = abs( ( $target_level - $start_level ) / $rate );
140 14 ▶ if ( $remaining_time < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_time < 0) { } 3 11 14
141 3 $self->_active(0);
142 3 $self->source_level( value => $target_level );
143 3 return $target_level;
144 }
145 else {
146 11 return $start_level + $sgn * $rate * ( time - $start_time );
147 }
148 }
149 else {
150 207 return $self->cached_source_level();
151 }
152 }
153  
154 5 ▶sub config_sweep {
Subroutine config_sweep: called CC=2 SCAR=7.1
Pod undocumented
155 5 my $self = shift;
156 5 my %args = @_;
157 5 ▶ if ( $self->_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_active) 0 5 5
158 0 croak("config_sweep called while instrument is active");
159 }
160 5 my $target = $args{point};
161 5 my $rate = abs( $args{rate} );
162 5 $self->_sweep_start_level( $self->cached_level );
163 5 $self->_sweep_target_level($target);
164 5 $self->_sweep_rate($rate);
165 }
166  
167 5 ▶sub trg {
Subroutine trg: called CC=1 SCAR=0.0
Pod undocumented
168 5 my $self = shift;
169 5 $self->_active(1);
170 5 $self->_sweep_start_time( time() );
171 }
172  
173 2 ▶sub wait {
Subroutine wait: called CC=2 SCAR=6.9
Pod undocumented
174 2 my $self = shift;
175 2 my $start_time = $self->_sweep_start_time();
176 2 my $rate = $self->_sweep_rate;
177 2 my $target_level = $self->_sweep_target_level;
178 2 my $start_level = $self->_sweep_start_level;
179 2 my $duration = abs( ( $target_level - $start_level ) / $rate );
180  
181 2 ▶ if ( $start_time + $duration > time() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_time + $duration > &Lab::Moose::Instrument::DummySource::time()) 1 1 2
182 1 sleep( $start_time + $duration - time() );
183 }
184  
185 # Sweep done
186 2 $self->_active(0);
187 2 $self->source_level( value => $target_level );
188 }
189  
190 14 ▶sub active {
Subroutine active: called CC=3 SCAR=11.0
Pod undocumented
191 14 my $self = shift;
192 14 ▶ if ( not $self->_active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_active) 3 11 14
193 3 return 0;
194 }
195 11 ▶ if ( $self->_remaining_sweep_time > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_remaining_sweep_time > 0) 10 1 11
196 10 return 1;
197 }
198 1 return 0;
199 }
200  
201 4 __PACKAGE__->meta()->make_immutable();
202  
203 4 1;
204  
205 __END__
« Moose/DataFolder.pm ↑ Summary Measurement.pm »