File Coverage

blib/lib/Device/BlinkStick.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 17 19 89.4


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             # get the first blinkstick found
17             my $device = $bs->first() ;
18             # make it red
19             $first->set_color( 'red') ;
20              
21             sleep( 2) ;
22             # blink red for 5s (5000ms) on and off for 250ms
23             $first->blink( 'red', 5000, 250) ;
24              
25             =head1 DESCRIPTION
26              
27             See Also
28              
29             =head2 left to do from python version
30              
31             def _usb_get_string(self, device, length, index):
32             def get_led_data(self):
33             def data_to_message(self, data):
34             def set_random_color(self):
35             def turn_off(self):
36             def pulse(self, red=0, green=0, blue=0, name=None, hex=None, repeats=1, duration=1000, steps=50):
37             def blink(self, red=0, green=0, blue=0, name=None, hex=None, repeats=1, delay=500):
38             def morph(self, red=0, green=0, blue=0, name=None, hex=None, duration=1000, steps=50):
39             def open_device(self, d):
40              
41             =over 4
42              
43             =cut
44              
45             package Device::BlinkStick ;
46             $Device::BlinkStick::VERSION = '0.2.0';
47 1     1   21293 use 5.014 ;
  1         5  
48 1     1   5 use warnings ;
  1         1  
  1         26  
49 1     1   5 use strict ;
  1         5  
  1         27  
50 1     1   871 use Moo ;
  1         15240  
  1         6  
51 1     1   2181 use Device::USB ;
  0            
  0            
52             use Device::BlinkStick::Stick ;
53              
54             # ----------------------------------------------------------------------------
55              
56             use constant VENDOR_ID => 0x20a0 ;
57             use constant PRODUCT_ID => 0x41e5 ;
58             use constant UPDATE_TIME => 2 ;
59              
60             # ----------------------------------------------------------------------------
61              
62             # mapping of serial IDs to device info
63             has devices => ( is => 'ro', init_arg => 0 ) ;
64             # the first device found
65             has first => ( is => 'ro', init_arg => 0 ) ;
66             has verbose => ( is => 'ro' ) ;
67             has inverse => ( is => 'ro' ) ;
68              
69             # ----------------------------------------------------------------------------
70              
71             =item new
72              
73             =over 4
74              
75             =item devices
76              
77             Get all blinkstick devices available
78              
79             =over 4
80              
81             =item first
82              
83             Get the first blicnk stick device found
84              
85             =over 4
86              
87             =item verbose
88              
89             output some debug as things happen
90              
91             =back
92              
93             =cut
94              
95             sub BUILD
96             {
97             my $self = shift ;
98             my $args = shift ;
99              
100             # find the sticks
101             $self->refresh_devices() ;
102             }
103              
104             # ----------------------------------------------------------------------------
105             # find all the blinkstick devices
106              
107             sub refresh_devices
108             {
109             state $last = 0 ;
110             my $self = shift ;
111              
112             # we don't want to update this too often as it takes ~ 0.4s to run
113             if ( !$last || $last + UPDATE_TIME < time() ) {
114             $last = time() ;
115             my $usb = Device::USB->new() ;
116             my @sticks = $usb->list_devices( VENDOR_ID, PRODUCT_ID ) ;
117              
118             # find all devices
119             if ( scalar(@sticks) ) {
120             delete $self->{first} ;
121             $self->{devices} = {} ;
122             foreach my $dev (@sticks) {
123             my $device = Device::BlinkStick::Stick->new( device => $dev, verbose => $self->verbose(), inverse => $self->inverse() ) ;
124             if ( !$self->{first} ) {
125             $self->{first} = $device ;
126             }
127             # build the mapping of devices
128             $self->{devices}->{ lc($device->serial_number()) } = $device ;
129             }
130             }
131             }
132              
133             return $self->{devices} ;
134             }
135              
136             =back
137              
138             =cut
139              
140             # ----------------------------------------------------------------------------
141             1 ;
142