| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
# ABSTRACT: |
|
3
|
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
Device::BlinkStick |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use 5.10.0 ; |
|
11
|
|
|
|
|
|
|
use strict ; |
|
12
|
|
|
|
|
|
|
use warnings ; |
|
13
|
|
|
|
|
|
|
use Device::BlinkStick; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $bs = Device::BlinkStick->new() ; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# set first LED on all devices to blue |
|
18
|
|
|
|
|
|
|
my $all_devices = $bs->devices() ; |
|
19
|
|
|
|
|
|
|
foreach my $k ( keys %$all_devices) { |
|
20
|
|
|
|
|
|
|
$all->{$k}->set_color( 'blue') ; |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# get the first blinkstick found |
|
24
|
|
|
|
|
|
|
my $device = $bs->first() ; |
|
25
|
|
|
|
|
|
|
# make it red |
|
26
|
|
|
|
|
|
|
$first->led( color => 'red') ; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sleep( 2) ; |
|
29
|
|
|
|
|
|
|
# blink red for 5 times, delaying for 250ms between black and the color |
|
30
|
|
|
|
|
|
|
$first->blink( color => 'red', delay => 250, times => 5) ; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Module to control a number of blinkstick devices L connected via USB. |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
package Device::BlinkStick ; |
|
39
|
|
|
|
|
|
|
$Device::BlinkStick::VERSION = '0.3.0'; |
|
40
|
1
|
|
|
1
|
|
20657
|
use 5.014 ; |
|
|
1
|
|
|
|
|
4
|
|
|
41
|
1
|
|
|
1
|
|
5
|
use warnings ; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
25
|
|
|
42
|
1
|
|
|
1
|
|
4
|
use strict ; |
|
|
1
|
|
|
|
|
11
|
|
|
|
1
|
|
|
|
|
22
|
|
|
43
|
1
|
|
|
1
|
|
15547
|
use Moo ; |
|
|
1
|
|
|
|
|
24116
|
|
|
|
1
|
|
|
|
|
6
|
|
|
44
|
1
|
|
|
1
|
|
2084
|
use Device::USB ; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
use Device::BlinkStick::Stick ; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
use constant VENDOR_ID => 0x20a0 ; |
|
50
|
|
|
|
|
|
|
use constant PRODUCT_ID => 0x41e5 ; |
|
51
|
|
|
|
|
|
|
use constant UPDATE_TIME => 2 ; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# mapping of serial IDs to device info |
|
56
|
|
|
|
|
|
|
has devices => ( is => 'ro', init_arg => 0 ) ; |
|
57
|
|
|
|
|
|
|
# the first device found |
|
58
|
|
|
|
|
|
|
has first => ( is => 'ro', init_arg => 0 ) ; |
|
59
|
|
|
|
|
|
|
has verbose => ( is => 'ro' ) ; |
|
60
|
|
|
|
|
|
|
has inverse => ( is => 'ro' ) ; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 new |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Instantiate a new object, also finds all currently connected devices and populates |
|
67
|
|
|
|
|
|
|
the accessor method variables |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head3 parameters |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=over 4 |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item verbose |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
output some debug as things happen |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=back |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head3 access methods |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=over 4 |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item devices |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Get all blinkstick device L objects available as a hash ref |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $bs = Device::BlinkStick->new() ; |
|
88
|
|
|
|
|
|
|
my $devices = $bs->devices() ; |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item first |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Get the first blink stick device (object L) found |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $bs = Device::BlinkStick->new() ; |
|
95
|
|
|
|
|
|
|
my $device = $bs->first() ; |
|
96
|
|
|
|
|
|
|
# make it red |
|
97
|
|
|
|
|
|
|
$first->led( color => 'red') ; |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub BUILD |
|
104
|
|
|
|
|
|
|
{ |
|
105
|
|
|
|
|
|
|
my $self = shift ; |
|
106
|
|
|
|
|
|
|
my $args = shift ; |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# find the sticks |
|
109
|
|
|
|
|
|
|
$self->refresh_devices() ; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
113
|
|
|
|
|
|
|
# find all the connected blinkstick devices |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 refresh_devices |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Check the USB for any added or removed devices and update our internal list |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Returns all blinkstick device objects available as a hash ref |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
my $bs = Device::BlinkStick->new() ; |
|
122
|
|
|
|
|
|
|
my $current = $bs->refresh_devices() ; |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub refresh_devices |
|
127
|
|
|
|
|
|
|
{ |
|
128
|
|
|
|
|
|
|
state $last = 0 ; |
|
129
|
|
|
|
|
|
|
my $self = shift ; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# we don't want to update this too often as it takes ~ 0.4s to run |
|
132
|
|
|
|
|
|
|
if ( !$last || $last + UPDATE_TIME < time() ) { |
|
133
|
|
|
|
|
|
|
$last = time() ; |
|
134
|
|
|
|
|
|
|
my $usb = Device::USB->new() ; |
|
135
|
|
|
|
|
|
|
my @sticks = $usb->list_devices( VENDOR_ID, PRODUCT_ID ) ; |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# find all devices |
|
138
|
|
|
|
|
|
|
if ( scalar(@sticks) ) { |
|
139
|
|
|
|
|
|
|
delete $self->{first} ; |
|
140
|
|
|
|
|
|
|
$self->{devices} = {} ; |
|
141
|
|
|
|
|
|
|
foreach my $dev (@sticks) { |
|
142
|
|
|
|
|
|
|
my $device = Device::BlinkStick::Stick->new( |
|
143
|
|
|
|
|
|
|
device => $dev, |
|
144
|
|
|
|
|
|
|
verbose => $self->verbose(), |
|
145
|
|
|
|
|
|
|
inverse => $self->inverse() |
|
146
|
|
|
|
|
|
|
) ; |
|
147
|
|
|
|
|
|
|
if ( !$self->{first} ) { |
|
148
|
|
|
|
|
|
|
$self->{first} = $device ; |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
# build the mapping of devices |
|
151
|
|
|
|
|
|
|
$self->{devices}->{ lc( $device->serial_number() ) } |
|
152
|
|
|
|
|
|
|
= $device ; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
return $self->{devices} ; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
161
|
|
|
|
|
|
|
# find a matching device |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 find |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Find a device by name or serial number |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
my $bs = Device::BlinkStick->new() ; |
|
168
|
|
|
|
|
|
|
my $d = $bs->find( 'strip') ; # I have a device I've named strip! |
|
169
|
|
|
|
|
|
|
$d->set_mode( 3) ; |
|
170
|
|
|
|
|
|
|
$d->led( color => 'green') ; # set all LEDs to green |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=over 4 |
|
173
|
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=item name |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
The name or serial number to match |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=back |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Returns undef if fails to match a device |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub find |
|
185
|
|
|
|
|
|
|
{ |
|
186
|
|
|
|
|
|
|
my $self = shift ; |
|
187
|
|
|
|
|
|
|
my ($name) = @_ ; |
|
188
|
|
|
|
|
|
|
my $stick ; |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$name = lc($name) ; |
|
191
|
|
|
|
|
|
|
# check match on serial number |
|
192
|
|
|
|
|
|
|
if ( $self->{devices}->{$name} ) { |
|
193
|
|
|
|
|
|
|
$stick = $self->{devices}->{$name}; |
|
194
|
|
|
|
|
|
|
} else { |
|
195
|
|
|
|
|
|
|
# match against each device name |
|
196
|
|
|
|
|
|
|
foreach my $s ( keys %{ $self->{devices} } ) { |
|
197
|
|
|
|
|
|
|
if ( lc( $self->{devices}->{$s}->device_name ) eq $name ) { |
|
198
|
|
|
|
|
|
|
$stick = $self->{devices}->{$s} ; |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
} |
|
202
|
|
|
|
|
|
|
return $stick ; |
|
203
|
|
|
|
|
|
|
} |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
|
206
|
|
|
|
|
|
|
1 ; |
|
207
|
|
|
|
|
|
|
|