File Coverage

blib/lib/Lab/Moose/Sweep/Continuous/Time.pm
Criterion Covered Total %
statement 36 49 73.4
branch 9 20 45.0
condition n/a
subroutine 8 9 88.8
pod 0 5 0.0
total 53 83 63.8


line stmt bran cond sub pod time code
1             package Lab::Moose::Sweep::Continuous::Time;
2             $Lab::Moose::Sweep::Continuous::Time::VERSION = '3.900';
3             #ABSTRACT: Time sweep
4              
5 2     2   3088 use v5.20;
  2         10  
6              
7              
8 2     2   15 use Moose;
  2         4  
  2         34  
9 2     2   15172 use Time::HiRes qw/time sleep/;
  2         5  
  2         22  
10 2     2   280 use Carp;
  2         6  
  2         997  
11              
12             extends 'Lab::Moose::Sweep::Continuous';
13              
14             #
15             # Public attributes
16             #
17              
18             has [qw/ +instrument +points +rates/] => ( required => 0 );
19              
20             has duration => ( is => 'ro', isa => 'Lab::Moose::PosNum' );
21              
22             has durations => (
23             is => 'ro',
24             isa => 'ArrayRef[Lab::Moose::PosNum]',
25             traits => ['Array'],
26             handles => {
27             get_duration => 'get', shift_durations => 'shift',
28             num_durations => 'count', durations_array => 'elements',
29             },
30             writer => '_durations'
31             );
32              
33             # TODO: make duration optional => infinite
34              
35             sub BUILD {
36 1     1 0 4 my $self = shift;
37              
38             # Do not mess with intervals/durations attribute arrays. Make copies of them.
39 1         3 my @intervals;
40             my @durations;
41              
42 1 50       35 if ( defined $self->interval ) {
    0          
43 1 50       39 if ( defined $self->intervals ) {
44 0         0 croak "Use either interval or intervals attribute";
45             }
46 1         30 @intervals = ( $self->interval );
47             }
48             elsif ( defined $self->intervals ) {
49 0         0 @intervals = $self->intervals_array;
50             }
51             else {
52             # default interval
53 0         0 @intervals = (0);
54             }
55              
56 1 50       44 if ( defined $self->duration ) {
    0          
57 1 50       36 if ( defined $self->durations ) {
58 0         0 croak "Use either duration or durations attribute";
59             }
60 1         33 @durations = ( $self->duration );
61             }
62             elsif ( defined $self->durations ) {
63 0         0 @durations = $self->durations_array;
64             }
65             else {
66 0         0 croak "Missing mandatory duration or durations argument";
67             }
68              
69 1         42 $self->_intervals( \@intervals );
70 1         41 $self->_durations( \@durations );
71              
72 1 50       48 if ( $self->num_intervals < 1 ) {
73 0         0 croak "need at least one interval";
74             }
75              
76 1 50       40 if ( $self->num_intervals != $self->num_durations ) {
77 0         0 croak "need same number of intervals and durations";
78             }
79             }
80              
81             sub go_to_sweep_start {
82 1     1 0 3 my $self = shift;
83 1         54 $self->reset_index();
84 1         44 $self->reset_points_index();
85 1         44 $self->inc_points_index();
86             }
87              
88             sub start_sweep {
89 1     1 0 3 my $self = shift;
90 1         65 $self->_start_time( time() );
91             }
92              
93             sub sweep_finished {
94 7     7 0 25 my $self = shift;
95              
96 7         267 my $duration = $self->get_duration( $self->points_index - 1 );
97              
98 7 100       322 if ( time() - $self->start_time < $duration ) {
99              
100             # still in duration
101 6         33 return 0;
102             }
103              
104             # duration is finished.
105 1         43 $self->inc_points_index();
106              
107             # Are there more durations?
108 1 50       29 if ( $self->points_index < $self->num_durations() ) {
109 0         0 $self->reset_index();
110 0         0 $self->start_sweep();
111 0         0 return 0;
112             }
113             else {
114 1         6 return 1;
115             }
116             }
117              
118             sub get_value {
119 0     0 0   my $self = shift;
120 0           return time();
121             }
122              
123             __PACKAGE__->meta->make_immutable();
124             1;
125              
126             __END__
127              
128             =pod
129              
130             =encoding UTF-8
131              
132             =head1 NAME
133              
134             Lab::Moose::Sweep::Continuous::Time - Time sweep
135              
136             =head1 VERSION
137              
138             version 3.900
139              
140             =head1 SYNOPSIS
141              
142             use Lab::Moose;
143              
144             my $sweep = sweep(
145             type => 'Continuous::Time',
146             interval => 0.5, # optional, default is 0 (no delay)
147             duration => 60
148             );
149              
150             # Multiple segments with different intervals
151             my $sweep = sweep(
152             type => 'Continuous::Time',
153             # 60s with 0.5s interval followed by 120s with 1s interval
154             durations => [60, 120],
155             intervals => [0.5, 1],
156             );
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
161              
162             Copyright 2018 Simon Reinhardt
163             2020 Andreas K. Huettel, Simon Reinhardt
164              
165              
166             This is free software; you can redistribute it and/or modify it under
167             the same terms as the Perl 5 programming language system itself.
168              
169             =cut