| 1 | |||
| 2 | # ABSTRACT: | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Device::BlinkStick::Stick | ||
| 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->led( color => 'red') ; | ||
| 20 | |||
| 21 | sleep( 2) ; | ||
| 22 | # blink red for 5 times, delaying for 250ms between black and the color | ||
| 23 | $first->blink( color => 'red', delay => 250, times => 5) ; | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | Object holding information and control about a specic blinkstick device, the control | ||
| 28 | of a single device is done in L<Device::BlinkStick::Stick> | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | package Device::BlinkStick::Stick ; | ||
| 33 | $Device::BlinkStick::Stick::VERSION = '0.4.0'; | ||
| 34 | use 5.014 ; | ||
| 35 | use warnings ; | ||
| 36 | use strict ; | ||
| 37 | use Moo ; | ||
| 38 | use Data::Hexdumper ; | ||
| 39 | use Time::HiRes qw(usleep) ; | ||
| 40 | use WebColors ; | ||
| 41 | |||
| 42 | use Data::Printer ; | ||
| 43 | |||
| 44 | # ---------------------------------------------------------------------------- | ||
| 45 | |||
| 46 | use constant DEFAULT_USB_TIMEOUT => 1000 ; | ||
| 47 | use constant EMULATE_LEDS => 16 ; | ||
| 48 | use constant EMULATE_DELAY_USECS => 500 ; | ||
| 49 | use constant DEFAULT_BLINK_TIME => 150 ; | ||
| 50 | |||
| 51 | # ---------------------------------------------------------------------------- | ||
| 52 | |||
| 53 | has device => ( is => 'ro' ) ; | ||
| 54 | has verbose => ( is => 'rw' ) ; | ||
| 55 | has inverse => ( is => 'rw' ) ; | ||
| 56 | |||
| 57 | has serial_number => ( is => 'ro', init_arg => 0 ) ; | ||
| 58 | has device_name => ( is => 'ro', init_arg => 0 ) ; | ||
| 59 | has _leds => ( is => 'ro', init_arg => 0 ) ; | ||
| 60 | has type => ( is => 'ro', init_arg => 0 ) ; | ||
| 61 | has brightness => ( | ||
| 62 | is => 'rw', | ||
| 63 | init_arg => 0, | ||
| 64 | isa => sub { | ||
| 65 | if ( $_[0] > 100 ) { | ||
| 66 | print STDERR "Brightness too high\n" ; | ||
| 67 | $_[0] = 100 ; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | ) ; | ||
| 71 | |||
| 72 | # ---------------------------------------------------------------------------- | ||
| 73 | |||
| 74 | =head2 new | ||
| 75 | |||
| 76 | Create an instance of a blink stick device, requires an open USB file device | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item verbose | ||
| 81 | |||
| 82 | output some debug as things happen | ||
| 83 | |||
| 84 | =item serial_number | ||
| 85 | |||
| 86 | optionally find this stick | ||
| 87 | |||
| 88 | =item device_name | ||
| 89 | |||
| 90 | optionally find this stick, if serial_number is also defined that will over-ride | ||
| 91 | I am working on the premise that the device name is stored in info_block_1 | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub BUILD | ||
| 98 | { | ||
| 99 | my $self = shift ; | ||
| 100 | my $args = shift ; | ||
| 101 | |||
| 102 | $args->{device}->open() ; | ||
| 103 | $self->{verbose} = $args->{verbose} ; | ||
| 104 | $self->{serial_number} = $self->{device}->serial_number() || "" ; | ||
| 105 | # get the rest of the info | ||
| 106 | $self->{device_name} = $self->get_device_name() || "" ; | ||
| 107 | $self->{_leds} = $self->get_leds() ; | ||
| 108 | $self->{_mode} = $self->get_mode() ; | ||
| 109 | $self->{type} | ||
| 110 | = ( $self->{serial_number} =~ /-1\.[0-9]$/ ) | ||
| 111 | ? "original" | ||
| 112 | : "pro" ; | ||
| 113 | } | ||
| 114 | |||
| 115 | # ---------------------------------------------------------------------------- | ||
| 116 | |||
| 117 | =head2 info | ||
| 118 | |||
| 119 | get a hash of info about the device | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub info | ||
| 124 | { | ||
| 125 | my $self = shift ; | ||
| 126 | |||
| 127 | my $colorname ; | ||
| 128 | my @c = $self->get_color() ; | ||
| 129 | $colorname = rgb_to_colorname( @c ) ; | ||
| 130 | if ( !$colorname ) { | ||
| 131 | $colorname = sprintf( "#%02X%02X%02X", @c ) ; | ||
| 132 | } | ||
| 133 | |||
| 134 | return { | ||
| 135 | device => sprintf( "%04X:%04X", | ||
| 136 | $self->{device}->idVendor(), | ||
| 137 | $self->{device}->idProduct() ), | ||
| 138 | manufacturer => $self->{device}->manufacturer(), | ||
| 139 | product => $self->{device}->product(), | ||
| 140 | serial_number => $self->{serial_number}, | ||
| 141 | device_name => $self->{device_name}, | ||
| 142 | access_token => $self->get_access_token() || "", | ||
| 143 | leds => $self->{_leds}, | ||
| 144 | mode => defined $self->{_mode} ? $self->{_mode} : 0, | ||
| 145 | color => [ $self->get_color() ], | ||
| 146 | colorname => $colorname, | ||
| 147 | type => $self->type, | ||
| 148 | } ; | ||
| 149 | } | ||
| 150 | |||
| 151 | # ---------------------------------------------------------------------------- | ||
| 152 | # usb read sets the 0x80 flag | ||
| 153 | # read the usb | ||
| 154 | |||
| 155 | sub _usb_read | ||
| 156 | { | ||
| 157 | my $self = shift ; | ||
| 158 | my ( $b_request, $w_value, $w_index, $w_length, $timeout ) = @_ ; | ||
| 159 | $timeout ||= DEFAULT_USB_TIMEOUT ; # we will not allow indefinite timeouts | ||
| 160 | |||
| 161 | my $bytes = ' ' x $w_length ; | ||
| 162 | my $bm_requesttype | ||
| 163 | = 0x80 | 0x20 ; # 0x80 is LIBUSB_ENDPOINT_IN ie read data in | ||
| 164 | # 0x20 is LIBUSB_REQUEST_TYPE_CLASS (0x01 << 5) | ||
| 165 | my $count | ||
| 166 | = $self->{device} | ||
| 167 | ->control_msg( $bm_requesttype, $b_request, $w_value, $w_index, | ||
| 168 | $bytes, $w_length, $timeout ) ; | ||
| 169 | |||
| 170 | if ( $self->verbose ) { | ||
| 171 | say STDERR "USB READ $b_request-$w_value-$w_index ($w_length)\n" | ||
| 172 | . hexdump( data => $bytes, suppress_warnings => 1 ) | ||
| 173 | . "read $count bytes" ; | ||
| 174 | } | ||
| 175 | |||
| 176 | if ( $count != $w_length && $self->verbose ) { | ||
| 177 | if ( $count < 0 ) { | ||
| 178 | warn | ||
| 179 | "Error reading USB device _usb_read( $b_request, $w_value, $w_index, $w_length)" | ||
| 180 | ; | ||
| 181 | } else { | ||
| 182 | warn | ||
| 183 | "unknown error reading USB _usb_read( $b_request, $w_value, $w_index, $w_length)" | ||
| 184 | ; | ||
| 185 | } | ||
| 186 | |||
| 187 | # if we did not read it all or read nothing, clear it | ||
| 188 | $bytes = undef ; | ||
| 189 | } | ||
| 190 | |||
| 191 | return $bytes ; | ||
| 192 | } | ||
| 193 | |||
| 194 | # ---------------------------------------------------------------------------- | ||
| 195 | # consistent way to write | ||
| 196 | |||
| 197 | sub _usb_write | ||
| 198 | { | ||
| 199 | my $self = shift ; | ||
| 200 | my ( $b_request, $w_value, $w_index, $bytes, $w_length, $timeout ) = @_ ; | ||
| 201 | |||
| 202 | $w_length //= length($bytes) ; | ||
| 203 | $timeout ||= DEFAULT_USB_TIMEOUT ; # we will not allow indefinite timeouts | ||
| 204 | |||
| 205 | # 0x00 is LIBUSB_ENDPOINT_OUT ie write data out | ||
| 206 | # 0x20 is LIBUSB_REQUEST_TYPE_CLASS (0x01 << 5) | ||
| 207 | my $bm_requesttype = 0x20 ; | ||
| 208 | my $count | ||
| 209 | = $self->{device} | ||
| 210 | ->control_msg( $bm_requesttype, $b_request, $w_value, $w_index, | ||
| 211 | $bytes, $w_length, $timeout ) ; | ||
| 212 | |||
| 213 | if ( $self->verbose ) { | ||
| 214 | say STDERR "USB WRITE $b_request-$w_value-$w_index\n" | ||
| 215 | . hexdump( data => $bytes, suppress_warnings => 1 ) | ||
| 216 | . "wrote $count bytes" ; | ||
| 217 | } | ||
| 218 | my $status = 0 ; | ||
| 219 | if ( $count != $w_length && $self->verbose ) { | ||
| 220 | if ( $count < 0 ) { | ||
| 221 | warn "Error writing to BlinkStick" ; | ||
| 222 | } else { | ||
| 223 | warn "Unknown error writing to BlinkStick" ; | ||
| 224 | } | ||
| 225 | } else { | ||
| 226 | $status = 1 ; | ||
| 227 | } | ||
| 228 | $status ; | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | # ---------------------------------------------------------------------------- | ||
| 233 | # read one of the info blocks | ||
| 234 | |||
| 235 | sub _get_info_block | ||
| 236 | { | ||
| 237 | my $self = shift ; | ||
| 238 | my ($id) = @_ ; | ||
| 239 | |||
| 240 | print STDERR "_get_info_block $id " if ( $self->verbose ) ; | ||
| 241 | my $bytes = $self->_usb_read( 0x1, $id + 1, 0, 33 ) ; | ||
| 242 | |||
| 243 | if ($bytes) { # first byte may be the id number, followed by the string | ||
| 244 | my ( $ignore, $newbytes ) = unpack( "CZ[32]", $bytes ) ; | ||
| 245 | $bytes = $newbytes ; | ||
| 246 | } | ||
| 247 | $bytes ; | ||
| 248 | } | ||
| 249 | |||
| 250 | # ---------------------------------------------------------------------------- | ||
| 251 | |||
| 252 | =head2 get_device_name | ||
| 253 | |||
| 254 | get the contents of info_block_1, usually the device name? | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub get_device_name | ||
| 259 | { | ||
| 260 | my $self = shift ; | ||
| 261 | |||
| 262 | $self->_get_info_block(1) ; | ||
| 263 | } | ||
| 264 | |||
| 265 | # ---------------------------------------------------------------------------- | ||
| 266 | |||
| 267 | =head2 get_access_token | ||
| 268 | |||
| 269 | get the contents of get_info_block_2, maybe the acccess token for the online service? | ||
| 270 | unused for perl | ||
| 271 | |||
| 272 | =cut | ||
| 273 | |||
| 274 | sub get_access_token | ||
| 275 | { | ||
| 276 | my $self = shift ; | ||
| 277 | |||
| 278 | $self->_get_info_block(2) ; | ||
| 279 | } | ||
| 280 | |||
| 281 | # ---------------------------------------------------------------------------- | ||
| 282 | # write into the info block | ||
| 283 | # id - block number | ||
| 284 | # str, the string to write, will get trimmed to 32 bytes | ||
| 285 | |||
| 286 | sub _set_info_block | ||
| 287 | { | ||
| 288 | my $self = shift ; | ||
| 289 | my ( $id, $str ) = @_ ; | ||
| 290 | |||
| 291 | $str //= '' ; # empty if thats what they want | ||
| 292 | $str = substr( $str, 0, 32 ) if ( length($str) > 32 ) ; | ||
| 293 | |||
| 294 | # packup the string, the first byte seems to be the id number or something | ||
| 295 | my $block = pack( "CZ[32]", $id + 1, $str ) ; | ||
| 296 | print STDERR "_set_info_block $id " if ( $self->verbose ) ; | ||
| 297 | $self->_usb_write( 0x9, $id + 1, 0, $block ) ; | ||
| 298 | } | ||
| 299 | |||
| 300 | # ---------------------------------------------------------------------------- | ||
| 301 | |||
| 302 | =head2 set_device_name | ||
| 303 | |||
| 304 | =over 4 | ||
| 305 | |||
| 306 | =item str | ||
| 307 | |||
| 308 | string to write into info_block_1, the device name | ||
| 309 | |||
| 310 | This will get trimmed to 32 characters | ||
| 311 | |||
| 312 | $stick->set_device_name( 'strip') ; | ||
| 313 | |||
| 314 | =back | ||
| 315 | |||
| 316 | returns true/false depending if the string was set | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 | sub set_device_name | ||
| 321 | { | ||
| 322 | my $self = shift ; | ||
| 323 | my $str = shift ; | ||
| 324 | |||
| 325 | $self->_set_info_block( 1, $str ) ; | ||
| 326 | } | ||
| 327 | |||
| 328 | # ---------------------------------------------------------------------------- | ||
| 329 | |||
| 330 | =head2 set_access_token | ||
| 331 | |||
| 332 | =over 4 | ||
| 333 | |||
| 334 | =item str | ||
| 335 | |||
| 336 | string to write into info_block_2, the online access token. Not used by perl | ||
| 337 | |||
| 338 | This will get trimmed to 32 characters | ||
| 339 | |||
| 340 | $stick->set_access_token( 'abc123dead') ; | ||
| 341 | |||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | returns true/false depending if the string was set | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | sub set_access_token | ||
| 350 | { | ||
| 351 | my $self = shift ; | ||
| 352 | my $str = shift ; | ||
| 353 | |||
| 354 | $self->_set_info_block( 2, $str ) ; | ||
| 355 | } | ||
| 356 | |||
| 357 | # ---------------------------------------------------------------------------- | ||
| 358 | |||
| 359 | =head2 get_mode | ||
| 360 | |||
| 361 | returns the display mode | ||
| 362 | |||
| 363 | 0 normal | ||
| 364 | 1 inverse | ||
| 365 | 2 WS2812 | ||
| 366 | 3 WS2812 mirror - all leds the same | ||
| 367 | |||
| 368 | my $mode = $stick->get_mode() ; | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | sub get_mode | ||
| 373 | { | ||
| 374 | my $self = shift ; | ||
| 375 | |||
| 376 | print STDERR "_get_mode " if ( $self->verbose ) ; | ||
| 377 | my $bytes = $self->_usb_read( 0x1, 4, 0, 2 ) ; | ||
| 378 | |||
| 379 | if ($bytes) { # first byte may be the id number, followed by the string | ||
| 380 | my ( $ignore, $newbytes ) = unpack( "CC", $bytes ) ; | ||
| 381 | $bytes = $newbytes ; | ||
| 382 | } | ||
| 383 | $bytes ; | ||
| 384 | } | ||
| 385 | |||
| 386 | # ---------------------------------------------------------------------------- | ||
| 387 | |||
| 388 | =head2 set_mode | ||
| 389 | |||
| 390 | Set the display mode | ||
| 391 | |||
| 392 | =over 4 | ||
| 393 | |||
| 394 | =item mode | ||
| 395 | |||
| 396 | mode to set | ||
| 397 | |||
| 398 | 0 normal | ||
| 399 | 1 inverse | ||
| 400 | 2 WS2812 | ||
| 401 | 3 WS2812 mirror - all leds the same | ||
| 402 | |||
| 403 | $stick->set_mode( 3) ; | ||
| 404 | |||
| 405 | =back | ||
| 406 | |||
| 407 | returns true/false depending if the mode was set | ||
| 408 | |||
| 409 | =cut | ||
| 410 | |||
| 411 | sub set_mode | ||
| 412 | { | ||
| 413 | my $self = shift ; | ||
| 414 | my $mode = shift ; | ||
| 415 | |||
| 416 | if ( $mode < 0 || $mode > 3 ) { | ||
| 417 | warn "Invalid mode" ; | ||
| 418 | return 0 ; | ||
| 419 | } | ||
| 420 | |||
| 421 | # mode 3 is emulated | ||
| 422 | if ( $mode < 3 ) { | ||
| 423 | my $block = pack( "CC", 4, $mode ) ; | ||
| 424 | print STDERR "set_mode " if ( $self->verbose ) ; | ||
| 425 | $self->_usb_write( 0x9, 4, 0, $block ) ; | ||
| 426 | } | ||
| 427 | $self->{_mode} = $mode ; | ||
| 428 | } | ||
| 429 | |||
| 430 | # ---------------------------------------------------------------------------- | ||
| 431 | |||
| 432 | =head2 get_color | ||
| 433 | |||
| 434 | returns the color as rgb from a channel and index | ||
| 435 | |||
| 436 | my $c = $stick->get_color( 0, 0 ) ; | ||
| 437 | |||
| 438 | =cut | ||
| 439 | |||
| 440 | sub get_color | ||
| 441 | { | ||
| 442 | my $self = shift ; | ||
| 443 | my ( $channel, $index ) = @_ ; | ||
| 444 | |||
| 445 | $channel //= 0 ; | ||
| 446 | $index //= 0 ; | ||
| 447 | if ( !$self->{_type} || $self->{_type} ne 'pro' ) { | ||
| 448 | $channel = 0 ; | ||
| 449 | $index = 0 ; | ||
| 450 | } | ||
| 451 | |||
| 452 | print STDERR "get_color " if ( $self->verbose ) ; | ||
| 453 | my $bytes = $self->_usb_read( 0x1, 0x0001, 0, 4 ) ; | ||
| 454 | my ( $r, $g, $b ) ; | ||
| 455 | |||
| 456 | if ($bytes) { # first byte may be the id number, followed by the string | ||
| 457 | my $ignore ; | ||
| 458 | ( $ignore, $r, $g, $b ) = unpack( "CCCC", $bytes ) ; | ||
| 459 | if ( $self->inverse ) { | ||
| 460 | ( $r, $g, $b ) = ( 255 - $r, 255 - $g, 255 - $b ) ; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | ( $r, $g, $b ) ; | ||
| 464 | } | ||
| 465 | |||
| 466 | # ---------------------------------------------------------------------------- | ||
| 467 | |||
| 468 | sub _apply_brightness | ||
| 469 | { | ||
| 470 | my $self = shift ; | ||
| 471 | my ($c) = @_ ; | ||
| 472 | |||
| 473 | return int( ( $c * $self->brightness ) / 100 ) ; | ||
| 474 | } | ||
| 475 | |||
| 476 | # ---------------------------------------------------------------------------- | ||
| 477 | |||
| 478 | =head2 set_color | ||
| 479 | |||
| 480 | set the rgb color for a single pixel blinkstick, uses brightness | ||
| 481 | |||
| 482 | =head3 Parameters | ||
| 483 | |||
| 484 | =over 4 | ||
| 485 | |||
| 486 | =item r | ||
| 487 | |||
| 488 | red part 0..255 | ||
| 489 | |||
| 490 | =item g | ||
| 491 | |||
| 492 | green part 0..255 | ||
| 493 | |||
| 494 | =item b | ||
| 495 | |||
| 496 | blue part 0..255 | ||
| 497 | |||
| 498 | =item channel | ||
| 499 | |||
| 500 | 64 LED block to use | ||
| 501 | |||
| 502 | =item index | ||
| 503 | |||
| 504 | LED in block to use | ||
| 505 | |||
| 506 | =back | ||
| 507 | |||
| 508 | $stick->set_color( 255, 0, 255, 0, 0) ; | ||
| 509 | |||
| 510 | This should not be used directly, rather use the B<led> method instead | ||
| 511 | |||
| 512 | returns true/false depending if the mode was set | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | sub set_color | ||
| 517 | { | ||
| 518 | my $self = shift ; | ||
| 519 | my ( $r, $g, $b, $channel, $index ) = @_ ; | ||
| 520 | |||
| 521 | # make sure values are in range | ||
| 522 | $r //= 0 ; | ||
| 523 | $g //= 0 ; | ||
| 524 | $b //= 0 ; | ||
| 525 | ( $r, $g, $b ) = ( $r & 255, $g & 255, $b & 255 ) ; | ||
| 526 | |||
| 527 | if ( $self->inverse ) { | ||
| 528 | ( $r, $g, $b ) = ( 255 - $r, 255 - $g, 255 - $b ) ; | ||
| 529 | } | ||
| 530 | |||
| 531 | if ( defined $self->{brightness} ) { | ||
| 532 | ( $r, $g, $b ) = ( | ||
| 533 | $self->_apply_brightness($r), | ||
| 534 | $self->_apply_brightness($g), | ||
| 535 | $self->_apply_brightness($b), | ||
| 536 | ) ; | ||
| 537 | } | ||
| 538 | |||
| 539 | $channel //= 0 ; | ||
| 540 | $index //= 0 ; | ||
| 541 | |||
| 542 | print STDERR "set_color " if ( $self->verbose ) ; | ||
| 543 | |||
| 544 | if ( $self->type && $self->type eq 'pro' ) { | ||
| 545 | # to write to a multi pixel device | ||
| 546 | # emulate the write to all, by actually writing to all | ||
| 547 | if ( $self->{_mode} == 3 ) { | ||
| 548 | for ( my $i = 0; $i < EMULATE_LEDS; $i++ ) { | ||
| 549 | my $block = pack( "CCCCCC", 5, $channel, $i, $r, $g, $b ) ; | ||
| 550 | $self->_usb_write( 0x9, 0x0005, 0, $block, length($block) ) ; | ||
| 551 | usleep(EMULATE_DELAY_USECS) ; | ||
| 552 | } | ||
| 553 | } else { | ||
| 554 | my $block = pack( "CCCCCC", 5, $channel, $index, $r, $g, $b ) ; | ||
| 555 | $self->_usb_write( 0x9, 0x0005, 0, $block, length($block) ) ; | ||
| 556 | } | ||
| 557 | } else { | ||
| 558 | my $block = pack( "CCCC", 0, $r, $g, $b ) ; | ||
| 559 | $self->_usb_write( 0x9, 0x0001, 0, $block, length($block) ) ; | ||
| 560 | } | ||
| 561 | } | ||
| 562 | |||
| 563 | # ---------------------------------------------------------------------------- | ||
| 564 | |||
| 565 | =head2 led | ||
| 566 | |||
| 567 | set the color of a single pixel blinkstick, uses brightness | ||
| 568 | |||
| 569 | =head3 Parameters | ||
| 570 | |||
| 571 | =over 4 | ||
| 572 | |||
| 573 | =item color | ||
| 574 | |||
| 575 | Color is either a colorname from L<WebColors> or a hex triplet of the form FF00AA | ||
| 576 | |||
| 577 | =item channel | ||
| 578 | |||
| 579 | 64 LED block to use | ||
| 580 | |||
| 581 | =item index | ||
| 582 | |||
| 583 | LED in block to use | ||
| 584 | |||
| 585 | =back | ||
| 586 | |||
| 587 | $stick->led( color => 'green200', index => 0, channel => 0) ; | ||
| 588 | |||
| 589 | returns true/false depending if the mode was set | ||
| 590 | |||
| 591 | =cut | ||
| 592 | |||
| 593 | sub led | ||
| 594 | { | ||
| 595 | my $self = shift ; | ||
| 596 | my $params = @_ % 2 ? shift : {@_} ; | ||
| 597 | |||
| 598 | if ( ref($params) ne 'HASH' ) { | ||
| 599 | warn "led accepts a hash or a hashref of parameters" ; | ||
| 600 | return 0 ; | ||
| 601 | } | ||
| 602 | my $r = $params->{color} || 'black' ; | ||
| 603 | my ( $g, $b ) ; | ||
| 604 | if ( $r !~ /^\d+$/ ) { | ||
| 605 | $r = lc($r) ; | ||
| 606 | if ( $r eq 'off' ) { | ||
| 607 | ( $r, $g, $b ) = ( 0, 0, 0 ) ; | ||
| 608 | } elsif ( $r =~ /(random|rand|rnd)/i ) { | ||
| 609 | ( $r, $g, $b ) | ||
| 610 | = ( int( rand(255) ), int( rand(255) ), int( rand(255) ) ) ; | ||
| 611 | } else { | ||
| 612 | # try to determine what it should be | ||
| 613 | ( $r, $g, $b ) = to_rgb($r) ; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | $self->set_color( $r, $g, $b, $params->{channel}, $params->{index} ) ; | ||
| 618 | } | ||
| 619 | |||
| 620 | # ---------------------------------------------------------------------------- | ||
| 621 | |||
| 622 | =head2 blink | ||
| 623 | |||
| 624 | blink a single pixel on and off, uses brightness | ||
| 625 | |||
| 626 | =head3 Parameters | ||
| 627 | |||
| 628 | =over 4 | ||
| 629 | |||
| 630 | =item color | ||
| 631 | |||
| 632 | Color is either a colorname from L<WebColors> or a hex triplet FF00AA | ||
| 633 | |||
| 634 | =item channel | ||
| 635 | |||
| 636 | 64 LED block to use | ||
| 637 | |||
| 638 | =item index | ||
| 639 | |||
| 640 | LED in block to use | ||
| 641 | |||
| 642 | =item times | ||
| 643 | |||
| 644 | Number of times to blink on and off | ||
| 645 | |||
| 646 | =item delay | ||
| 647 | |||
| 648 | Pause period between blinks, defaults to DEFAULT_BLINK_TIME which is 150ms | ||
| 649 | |||
| 650 | =back | ||
| 651 | |||
| 652 | $stick->blink( color => 'green200', index => 0, channel => 0, times => 5) ; | ||
| 653 | |||
| 654 | =cut | ||
| 655 | |||
| 656 | sub blink | ||
| 657 | { | ||
| 658 | my $self = shift ; | ||
| 659 | my $params = @_ % 2 ? shift : {@_} ; | ||
| 660 | |||
| 661 | if ( ref($params) ne 'HASH' ) { | ||
| 662 | warn "blink accepts a hash or a hashref of parameters" ; | ||
| 663 | return 0 ; | ||
| 664 | } | ||
| 665 | |||
| 666 | $params->{delay} ||= DEFAULT_BLINK_TIME ; | ||
| 667 | |||
| 668 | for ( my $i = 0; $i < $params->{times}; $i++ ) { | ||
| 669 | $self->led( | ||
| 670 | color => 'black', | ||
| 671 | channel => $params->{channel}, | ||
| 672 | index => $params->{index} | ||
| 673 | ) ; | ||
| 674 | usleep( $params->{delay} * 1000 ) ; | ||
| 675 | $self->led( | ||
| 676 | color => $params->{color}, | ||
| 677 | channel => $params->{channel}, | ||
| 678 | index => $params->{index} | ||
| 679 | ) ; | ||
| 680 | usleep( $params->{delay} * 1000 ) ; | ||
| 681 | } | ||
| 682 | } | ||
| 683 | |||
| 684 | # ---------------------------------------------------------------------------- | ||
| 685 | |||
| 686 | =head2 get_leds | ||
| 687 | |||
| 688 | returns the number of leds on the device | ||
| 689 | |||
| 690 | =cut | ||
| 691 | |||
| 692 | sub get_leds | ||
| 693 | { | ||
| 694 | my $self = shift ; | ||
| 695 | my $leds ; | ||
| 696 | |||
| 697 | print STDERR "leds " if ( $self->verbose ) ; | ||
| 698 | my $bytes = $self->_usb_read( 0x1, 0x81, 0, 2 ) ; | ||
| 699 | if ($bytes) { | ||
| 700 | my ( $ignore, $l ) = unpack( "CC", $bytes ) ; | ||
| 701 | $leds = $l ; | ||
| 702 | } | ||
| 703 | |||
| 704 | return $leds || "unknown" ; | ||
| 705 | } | ||
| 706 | |||
| 707 | # ---------------------------------------------------------------------------- | ||
| 708 | |||
| 709 | =head2 set_leds | ||
| 710 | |||
| 711 | set the number of leds connected to a device | ||
| 712 | |||
| 713 | =over 4 | ||
| 714 | |||
| 715 | =item count | ||
| 716 | |||
| 717 | 0..64 | ||
| 718 | |||
| 719 | =back | ||
| 720 | |||
| 721 | returns true/false depending if the led count was set | ||
| 722 | |||
| 723 | =cut | ||
| 724 | |||
| 725 | sub set_leds | ||
| 726 | { | ||
| 727 | my $self = shift ; | ||
| 728 | my ($count) = @_ ; | ||
| 729 | |||
| 730 | # make sure values are in range | ||
| 731 | print STDERR "set_leds $count " if ( $self->verbose ) ; | ||
| 732 | |||
| 733 | my $block = pack( "CC", 0x81, $count ) ; | ||
| 734 | $self->_usb_write( 0x9, 0x81, 0, $block, length($block) ) ; | ||
| 735 | } | ||
| 736 | |||
| 737 | |||
| 738 | # ---------------------------------------------------------------------------- | ||
| 739 | 1 ; | ||
| 740 |