blib/lib/Net/Async/ArtNet.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 60.0% 6 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 83.9% 47 / 56 SCAR 21.1
CC
8
Cov
84%
on_recv
5.6
configure
3.0
_add_to_loop
2.1
CRAP 8.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.02';
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 port => INT
66  
67 Optional. Port number to listen for Art-Net packets on.
68  
69 =back
70  
71 =cut
72  
73 sub _init
74 {
75 1 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
76 1 $self->SUPER::_init( @_ );
77  
78 1 $self->{port} = 0x1936; # Art-Net
79 }
80  
81 sub configure
82 {
83 2 ▶ my $self = shift;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
84 2 my %params = @_;
85  
86 2 foreach (qw( port on_dmx )) {
87 4 ▶ $self->{$_} = delete $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 2 2 4
88 }
89  
90 2 $self->SUPER::configure( %params );
91 }
92  
93 sub on_recv
94 {
95 1 ▶ my $self = shift;
Subroutine on_recv: called CC=5 SCAR=17.3
Pod documented
96 1 my ( $packet ) = @_;
97  
98 1 my ( $magic, $opcode, $verhi, $verlo ) =
99 unpack( "a8 v C C", substr $packet, 0, 12, "" );
100  
101 1 ▶ return unless $magic eq "Art-Net\0";
Branch: 1 branch
BranchTrueFalseTotal
unless $magic eq "Art-Net\0" 0 1 1
102 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
103  
104 1 ▶ if( $opcode == 0x5000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode == 20480) 1 0 1
105 1 my ( $seq, $phy, $universe, $data ) =
106 unpack( "C C v xx a*", $packet );
107 1 $self->maybe_invoke_event( on_dmx => $seq, $phy, $universe, [ unpack "C*", $data ] );
108 }
109 }
110  
111 sub _add_to_loop
112 {
113 1 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=2 SCAR=7.4
114 1 my ( $loop ) = @_;
115  
116 1 ▶ if( !defined $self->read_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->read_handle) 1 0 1
117 return $self->bind(
118 service => $self->{port},
119 1 socktype => "dgram",
120 )->get; # Blocking call, but numeric lookup so should be OK
121 }
122  
123 0 $self->SUPER::_add_to_loop( @_ );
124 }
125  
126 =head1 SEE ALSO
127  
128 =over 4
129  
130 =item *
131  
132 L<http://en.wikipedia.org/wiki/Art-Net> - Art-Net - Wikipedia
133  
134 =back
135  
136 =cut
137  
138 =head1 AUTHOR
139  
140 Paul Evans <leonerd@leonerd.org.uk>
141  
142 =cut
143  
144 2 0x55AA;
↑ Summary Build »