blib/lib/Net/Async/ArtNet.pm

filter: Statementstmt 97.0% 33 / 34 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 85.0% 51 / 60 SCAR 22.2
CC
9
Cov
86%
on_recv
5.6
configure
4.0
_add_to_loop
2.1
CRAP 9.2
↑ Summary Build »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2014-2020 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::ArtNet;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.03';
12  
13 2 ▶use base qw( IO::Async::Socket );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 C<Net::Async::ArtNet> - use ArtNet with C<IO::Async>
18  
19 =head1 SYNOPSIS
20  
21 use IO::Async::Loop;
22 use Net::Async::ArtNet;
23  
24 my $loop = IO::Async::Loop->new;
25  
26 $loop->add( Net::Async::ArtNet->new(
27 on_dmx => sub {
28 my $self = shift;
29 my ( $seq, $phy, $universe, $data ) = @_;
30  
31 return unless $phy == 0 and $universe == 0;
32  
33 my $ch10 = $data->[10 - 1]; # DMX channels are 1-indexed
34 print "Channel 10 now set to: $ch10\n";
35 }
36 ) );
37  
38 $loop->run;
39  
40 =head1 DESCRIPTION
41  
42 This object class allows you to use the Art-Net protocol with C<IO::Async>.
43 It receives Art-Net frames containing DMX data.
44  
45 =cut
46  
47 =head1 EVENTS
48  
49 =head2 on_dmx $seq, $phy, $uni, $data
50  
51 A new set of DMX control values has been received. C<$seq> contains the
52 sequence number from the packet, C<$phy> and C<$uni> the physical and universe
53 numbers, and C<$data> will be an ARRAY reference containing up to 512 DMX
54 control values.
55  
56 =cut
57  
58 =head1 PARAMETERS
59  
60 The following named parameters may be passed to C<new> or C<configure>.
61 Additionally, CODE references to set callbacks for events may be passed.
62  
63 =over 8
64  
65 =item family => INT or STRING
66  
67 =item host => INT or STRING
68  
69 =item service => INT or STRING
70  
71 Optional. C<getaddrinfo> parameters to create socket listen for Art-Net
72 packets on.
73  
74 =item port => INT or STRING
75  
76 Synonym for C<service> parameter.
77  
78 =back
79  
80 =cut
81  
82 sub _init
83 {
84 1 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
85 1 $self->SUPER::_init( @_ );
86  
87 1 $self->{service} = 0x1936; # Art-Net
88 }
89  
90 sub configure
91 {
92 2 ▶ my $self = shift;
Subroutine configure: called CC=4 SCAR=13.9
Pod documented
93 2 my %params = @_;
94  
95 2 ▶ $params{service} = delete $params{port} if exists $params{port};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"port"} 1 1 2
96  
97 2 foreach (qw( family host service on_dmx )) {
98 8 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 3 5 8
99 }
100  
101 2 $self->SUPER::configure( %params );
102 }
103  
104 sub on_recv
105 {
106 1 ▶ my $self = shift;
Subroutine on_recv: called CC=5 SCAR=17.3
Pod documented
107 1 my ( $packet ) = @_;
108  
109 1 my ( $magic, $opcode, $verhi, $verlo ) =
110 unpack( "a8 v C C", substr $packet, 0, 12, "" );
111  
112 1 ▶ return unless $magic eq "Art-Net\0";
Branch: 1 branch
BranchTrueFalseTotal
unless $magic eq "Art-Net\0" 0 1 1
113 1 ▶ return unless $verhi == 0 and $verlo == 14;
Branch: 1 branch
BranchTrueFalseTotal
unless $verhi == 0 and $verlo == 14 0 1 1
Condition33%1/3
$verhi == 0 and $verlo == 14
!ll&&!rl&&r
001
MC/DC0%0/2
$verhi == 0 and $verlo == 14
$verhi == 0 $verlo == 14
Truth table33%1/3
A and B
A= $verhi == 0B= $verlo == 14
ABresult
0X0
100
111
114  
115 1 ▶ if( $opcode == 0x5000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 20480) 1 0 1
116 1 my ( $seq, $phy, $universe, $data ) =
117 unpack( "C C v xx a*", $packet );
118 1 $self->maybe_invoke_event( on_dmx => $seq, $phy, $universe, [ unpack "C*", $data ] );
119 }
120 }
121  
122 sub _add_to_loop
123 {
124 1 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=2 SCAR=7.2
125 1 my ( $loop ) = @_;
126  
127 1 ▶ if( !defined $self->read_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->read_handle) 1 0 1
128 return $self->bind(
129 1 ( map { $_, $self->{$_} } qw( family host service ) ),
130 socktype => "dgram",
131 )->get; # Blocking call, but numeric lookup so should be OK
132 }
133  
134 0 $self->SUPER::_add_to_loop( @_ );
135 }
136  
137 =head1 SEE ALSO
138  
139 =over 4
140  
141 =item *
142  
143 L<http://en.wikipedia.org/wiki/Art-Net> - Art-Net - Wikipedia
144  
145 =back
146  
147 =cut
148  
149 =head1 AUTHOR
150  
151 Paul Evans <leonerd@leonerd.org.uk>
152  
153 =cut
154  
155 2 0x55AA;
↑ Summary Build »