File Coverage

blib/lib/Net/Async/ArtNet.pm
Criterion Covered Total %
statement 30 31 96.7
branch 8 12 66.6
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 48 55 87.2


line stmt bran cond sub pod time code
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-2025 -- leonerd@leonerd.org.uk
5              
6             package Net::Async::ArtNet 0.04;
7              
8 2     2   763487 use v5.14;
  2         7  
9 2     2   12 use warnings;
  2         18  
  2         163  
10              
11 2     2   21 use base qw( IO::Async::Socket );
  2         5  
  2         1396  
12              
13             =head1 NAME
14              
15             C - use Art-Net with C
16              
17             =head1 SYNOPSIS
18              
19             use IO::Async::Loop;
20             use Net::Async::ArtNet;
21              
22             my $loop = IO::Async::Loop->new;
23              
24             $loop->add( Net::Async::ArtNet->new(
25             on_dmx => sub {
26             my $self = shift;
27             my ( $seq, $phy, $universe, $data ) = @_;
28              
29             return unless $phy == 0 and $universe == 0;
30              
31             my $ch10 = $data->[10 - 1]; # DMX channels are 1-indexed
32             print "Channel 10 now set to: $ch10\n";
33             }
34             ) );
35              
36             $loop->run;
37              
38             =head1 DESCRIPTION
39              
40             This object class allows you to use the Art-Net protocol with C.
41             It receives Art-Net frames containing DMX data.
42              
43             =cut
44              
45             =head1 EVENTS
46              
47             =head2 on_dmx $seq, $phy, $uni, $data
48              
49             A new set of DMX control values has been received. C<$seq> contains the
50             sequence number from the packet, C<$phy> and C<$uni> the physical and universe
51             numbers, and C<$data> will be an ARRAY reference containing up to 512 DMX
52             control values.
53              
54             =cut
55              
56             =head1 PARAMETERS
57              
58             The following named parameters may be passed to C or C.
59             Additionally, CODE references to set callbacks for events may be passed.
60              
61             =over 8
62              
63             =item family => INT or STRING
64              
65             =item host => INT or STRING
66              
67             =item service => INT or STRING
68              
69             Optional. C parameters to create socket listen for Art-Net
70             packets on.
71              
72             =item port => INT or STRING
73              
74             Synonym for C parameter.
75              
76             =back
77              
78             =cut
79              
80             sub _init
81             {
82 1     1   292050 my $self = shift;
83 1         11 $self->SUPER::_init( @_ );
84              
85 1         21 $self->{service} = 0x1936; # Art-Net
86             }
87              
88             sub configure
89             {
90 2     2 1 66674 my $self = shift;
91 2         10 my %params = @_;
92              
93 2 100       13 $params{service} = delete $params{port} if exists $params{port};
94              
95 2         8 foreach (qw( family host service on_dmx )) {
96 8 100       30 $self->{$_} = delete $params{$_} if exists $params{$_};
97             }
98              
99 2         14 $self->SUPER::configure( %params );
100             }
101              
102             sub on_recv
103             {
104 1     1 1 4847 my $self = shift;
105 1         3 my ( $packet ) = @_;
106              
107 1         10 my ( $magic, $opcode, $verhi, $verlo ) =
108             unpack( "a8 v C C", substr $packet, 0, 12, "" );
109              
110 1 50       6 return unless $magic eq "Art-Net\0";
111 1 50 33     12 return unless $verhi == 0 and $verlo == 14;
112              
113 1 50       5 if( $opcode == 0x5000 ) {
114 1         5 my ( $seq, $phy, $universe, $data ) =
115             unpack( "C C v xx a*", $packet );
116 1         12 $self->maybe_invoke_event( on_dmx => $seq, $phy, $universe, [ unpack "C*", $data ] );
117             }
118             }
119              
120             sub _add_to_loop
121             {
122 1     1   210 my $self = shift;
123 1         3 my ( $loop ) = @_;
124              
125 1 50       9 if( !defined $self->read_handle ) {
126             return $self->bind(
127 1         10 ( map { $_, $self->{$_} } qw( family host service ) ),
  3         17  
128             socktype => "dgram",
129             )->get; # Blocking call, but numeric lookup so should be OK
130             }
131              
132 0           $self->SUPER::_add_to_loop( @_ );
133             }
134              
135             =head1 SEE ALSO
136              
137             =over 4
138              
139             =item *
140              
141             L - Art-Net - Wikipedia
142              
143             =back
144              
145             =cut
146              
147             =head1 AUTHOR
148              
149             Paul Evans
150              
151             =cut
152              
153             0x55AA;