blib/lib/Time/Slideshow.pm

filter: Statementstmt 91.6% 55 / 60 Branchbran 70.0% 7 / 10 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 85.7% 12 / 14 Podpod 90.9% 10 / 11 totaltotal 83.0% 93 / 112 CC 12 SCAR 11.2
CC
12
Cov
85%
new
5.2
current_slide_index
5.0
gcd
3.0
CRAP 3.1
↑ Summary
1 package Time::Slideshow;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 $VERSION= '0.04';
6  
7 =head1 NAME
8  
9 Time::Slideshow - simple stateless slideshow with a fixed set of images
10  
11 =head1 SYNOPSIS
12  
13 my $slideshow= Time::Slideshow->new(
14 starttime => 0,
15 slides => [ 'picture1.jpg', 'picture2.jpg' ],
16 shuffle => 0, # pseudo-rng
17 duration => 45, # show each slide for 45 seconds
18 );
19 my $current_slide= $slideshow->current_slide; # picture1.jpg
20 my $next_slide= $slideshow->next_slide; # picture2.jpg
21 sleep $slideshow->seconds_to_next_slide;
22  
23 =head1 OVERVIEW
24  
25 This module abstracts the inner workings of a slideshow, selecting the
26 next slide to display and calculating the time until the next picture. It
27 is possible to use this object in an asynchronous manner across machines
28 as the complete slide sequence is precalculated from the wallclock time.
29  
30 The module has no methods to advance the slideshow forwards or backwards
31 other than through the passage of time.
32  
33 =head1 METHODS
34  
35 =head2 C<< Time::Slideshow->new %options >>
36  
37 my $slideshow= Time::Slideshow->new(
38 slides => [ glob '~/backgrounds/*.jpg' ],
39 duration => 300, # five minutes per image
40 shuffle => 1, # permute the order of images
41 )
42  
43 Creates a new slideshow object and returns it. The options are
44  
45 =over 4
46  
47 =item B<slides>
48  
49 Array reference to the slides. These can be filenames
50 or URLs or whatever else helps your program to find and display
51 the appropriate image.
52  
53 =item B<duration>
54  
55 The time how long every image is displayed, in seconds. The default
56 value is 45.
57  
58 =item B<shuffle>
59  
60 If you want the order of the images to be shuffled, pass
61 a true value to this option. See the below discussion
62 on what different orders you can expect from shuffling.
63  
64 =item B<starttime>
65  
66 If you want to set up a different offset of the start time,
67 pass this option with the epoch value of the start time. Usually,
68 you want to leave this parameter at its default of 0.
69  
70 =back
71  
72 =cut
73  
74 19 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
75 19 my( $class, %options )= @_;
76 19 ▶ $options{ starttime }||= 0;
Condition50%1/2
$options{"starttime"} ||= 0
l!l
019
MC/DC0%0/1
$options{"starttime"} ||= 0
$options{"starttime"}
Truth table50%1/2
A
A= $options{"starttime"}
Aresult
11
00
77 19 ▶ $options{ shuffle }||= 0;
Condition100%2/2
$options{"shuffle"} ||= 0
l!l
172
MC/DC100%1/1
$options{"shuffle"} ||= 0
$options{"shuffle"}
Truth table100%2/2
A
A= $options{"shuffle"}
Aresult
11
00
78 19 ▶ $options{ duration }||= 45;
Condition50%1/2
$options{"duration"} ||= 45
l!l
190
MC/DC0%0/1
$options{"duration"} ||= 45
$options{"duration"}
Truth table50%1/2
A
A= $options{"duration"}
Aresult
11
00
79 19 ▶ $options{ slides }||= [];
Condition50%1/2
$options{"slides"} ||= []
l!l
190
MC/DC0%0/1
$options{"slides"} ||= []
$options{"slides"}
Truth table50%1/2
A
A= $options{"slides"}
Aresult
11
00
80  
81 19 my $self= bless \%options, $class;
82  
83 19 $self
84 }
85  
86 0 ▶sub starttime { $_[0]->{ starttime }};
Subroutine starttime: not called CC=1 SCAR=6.9
Pod documented
87 2639 ▶sub duration { $_[0]->{ duration }};
Subroutine duration: called CC=1 SCAR=0.0
Pod documented
88 1030 ▶sub slides { $_[0]->{ slides }};
Subroutine slides: called CC=1 SCAR=0.0
Pod documented
89  
90 =head2 C<< $show->add_slide( @slides ) >>
91  
92 $show->add_slide( glob '~/new_backgrounds/*' );
93  
94 If you want to add slides after object
95 creation, you can use this method.
96  
97 =cut
98  
99 0 ▶sub add_slide {
Subroutine add_slide: not called CC=1 SCAR=6.9
Pod documented
100 0 my( $self, @slides )= @_;
101 0 push @{ $self->slides }, @slides
102 };
103  
104 =head2 C<< $show->current_slide >>
105  
106 print "Now displaying ", $show->current_slide, "\n";
107  
108 Returns the name of the slide that is currently displayed.
109  
110 This method is the central method for your application to
111 get the filename or URL of the image that your application
112 needs to display.
113  
114 =cut
115  
116 8 ▶sub current_slide {
Subroutine current_slide: called CC=1 SCAR=0.0
Pod documented
117 8 my( $self, $time )= @_;
118 8 $self->slide_at( $self->current_slide_index( $time ))
119 }
120  
121 =head2 C<< $show->next_slide >>
122  
123 print "Next up is ", $show->next_slide, "\n";
124  
125 Returns the name of the slide that will be displayed after the
126 current slide.
127  
128 You can use this method to preload the image data
129 for the upcoming slide.
130  
131 =cut
132  
133 2 ▶sub next_slide {
Subroutine next_slide: called CC=1 SCAR=0.0
Pod documented
134 2 my( $self, $time )= @_;
135 2 $self->slide_at( $self->current_slide_index( $time ) +1 )
136 }
137  
138 =head2 C<< $show->seconds_to_next_slide >>
139  
140 my $wait= $show->seconds_to_next_slide;
141 sleep $wait;
142  
143 Returns the time remaining to the next slide
144 transition.
145  
146 You can use this method to pause your program or perform
147 other tasks until the next image
148 needs to be displayed.
149  
150 =cut
151  
152 3 ▶sub seconds_to_next_slide {
Subroutine seconds_to_next_slide: called CC=2 SCAR=7.4
Pod documented
153 3 my( $self, $time )= @_;
154 3 ▶ if( ! defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $time) 0 3 3
155 0 $time= Time::HiRes::time;
156 };
157 3 $time -= $self->{ starttime };
158 #warn sprintf "At %d, window %d, in %d", $time, $self->duration, $self->duration - ($time % $self->duration);
159 3 $self->duration - ($time % $self->duration)
160 };
161  
162 =head2 C<< $show->current_slide_index >>
163  
164 my $current_slide_index= $show->current_slide_index;
165  
166 This returns the index of the slide that is currently displayed.
167  
168 Most likely, you want to use C<< $show->current_slide >> instead.
169  
170 =cut
171  
172 874 ▶sub current_slide_index {
Subroutine current_slide_index: called CC=5 SCAR=16.2
Pod documented
173 874 my( $self, $time )= @_;
174 874 ▶ $time = time
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 874 874
175 unless defined $time;
176 874 $time -= $self->{ starttime };
177  
178 874 my $items= 0+@{ $self->slides };
179 874 ▶ return undef unless $items;
Branch: 1 branch
BranchTrueFalseTotal
unless $items 0 874 874
180  
181 874 my $index= int (($time % ($self->duration * $items)) / $self->duration);
182  
183 874 ▶ if( $self->{ shuffle } and $items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"shuffle"} and $items > 1) 869 5 874
Condition66%2/3
$self->{"shuffle"} and $items > 1
!ll&&!rl&&r
50869
MC/DC50%1/2
$self->{"shuffle"} and $items > 1
$self->{"shuffle"} $items > 1
Truth table66%2/3
A and B
A= $self->{"shuffle"}B= $items > 1
ABresult
0X0
100
111
184 # Apply a permutation
185 # We don't want permutation 0 and 1 as 0 and 1 are identical
186 # Step is the distance between items
187 # Incr is the shift we need to apply per round
188 # 0 1 2 3 4 5
189 # Shuffle 1: Step = 2
190 # 0 1 2
191 # 3 4 5
192 # Shuffle 2: Step = 3
193 # 0 1
194 # 2 3
195 # 4 5
196 #
197 # For 10 items, Permutation 8 (Step size 3)
198 # 0 1 2 3 4 5 6 7 8 9
199 # 0 1 2 3
200 # 4 5 6
201 # 7 8 9
202  
203 869 my $permutation= (int ($time / ($self->duration * $items)) % ($items-2));
204 869 my $step= $permutation;
205 869 $step++; # Increment between 1 and $items-1
206  
207 869 my $gcd= gcd( $items, $step );
208 869 my $round_size= int( $items / $gcd );
209  
210 # The round counts from $round_size towards 1 to shuffle the order of loops a bit
211 869 my $round= $round_size - int($index / $round_size) +1;
212  
213 869 my $old_index= $index;
214 869 $index= ((($old_index *$step) % $items) + $round ) % $items;
215  
216 #warn sprintf "Old Index % 2d Step: % 2d Round: % 2d Round size: % 2d Index: % 2d GCD: % 2d\n",
217 # $old_index, $step, $round, $round_size, $index, $gcd;
218 };
219  
220 874 $index
221 };
222  
223 =head2 C<< $show->slide_at $index >>
224  
225 my $slide_count= @{ $show->slides };
226 for my $slide ( 0..$slide_count-1 ) {
227 print "Slide $slide: ", $show->slide_at( $slide ), "\n";
228 };
229  
230 Returns the name of the slide at the given index.
231  
232 =cut
233  
234 10 ▶sub slide_at {
Subroutine slide_at: called CC=1 SCAR=0.0
Pod documented
235 10 my( $self, $index )= @_;
236 10 $index %= 0+@{ $self->slides };
237 10 $self->slides->[ $index ]
238 }
239  
240 # Some math utilities to find the gcd, for the "random" shuffles
241 869 ▶sub gcd {
Subroutine gcd: called CC=3 SCAR=11.0
Pod undocumented
242 869 my( $gcd )= shift;
243 869 for my $d ( @_ ) {
244 869 my $div= $d;
245 869 ▶ while( $div ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($div) 1485 869 2354
246 1485 ($gcd, $div) = ($div, $gcd % $div);
247 };
248 };
249 869 $gcd
250 }
251  
252 2 1;
253  
254 __END__
↑ Summary