Class: BlinkStick
- Inherits:
-
Object
- Object
- BlinkStick
- Defined in:
- lib/blinkstick.rb
Constant Summary collapse
- @@VENDOR_ID =
0X20A0
- @@PRODUCT_ID =
0x41E5
Class Method Summary collapse
Instance Method Summary collapse
- #color ⇒ Object
- #color=(value) ⇒ Object
- #description ⇒ Object
- #get_info_block(id) ⇒ Object
- #info_block1 ⇒ Object
- #info_block1=(value) ⇒ Object
- #info_block2 ⇒ Object
- #info_block2=(value) ⇒ Object
- #manufacturer ⇒ Object
- #off ⇒ Object
- #open(device = nil) ⇒ Object
- #random_color ⇒ Object
- #serial ⇒ Object
- #set_color(channel, index, value) ⇒ Object
- #set_colors(channel, data) ⇒ Object
- #set_info_block(id, data) ⇒ Object
Class Method Details
.find_all ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/blinkstick.rb', line 22 def self.find_all @@usb ||= LIBUSB::Context.new result = [] @@usb.devices(:idVendor => @@VENDOR_ID, :idProduct => @@PRODUCT_ID).each { | device | b = BlinkStick.new b.open(device) result.push(b) } result end |
.find_by_serial(serial) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/blinkstick.rb', line 37 def self.find_by_serial(serial) @@usb ||= LIBUSB::Context.new @@usb.devices(:idVendor => @@VENDOR_ID, :idProduct => @@PRODUCT_ID).each { | device | if device.serial_number == serial b = BlinkStick.new b.open(device) return b end } nil end |
Instance Method Details
#color ⇒ Object
55 56 57 58 |
# File 'lib/blinkstick.rb', line 55 def color result = @handle.control_transfer(:bmRequestType => 0x80 | 0x20, :bRequest => 0x1, :wValue => 0x1, :wIndex => 0x0000, :dataIn => 4) Color::RGB.new(result[1].ord, result[2].ord, result[3].ord) end |
#color=(value) ⇒ Object
51 52 53 |
# File 'lib/blinkstick.rb', line 51 def color=(value) @handle.control_transfer(:bmRequestType => 0x20, :bRequest => 0x9, :wValue => 0x1, :wIndex => 0x0000, :dataOut => 1.chr + value.red.to_i.chr + value.green.to_i.chr + value.blue.to_i.chr) end |
#description ⇒ Object
167 168 169 |
# File 'lib/blinkstick.rb', line 167 def description @device.product end |
#get_info_block(id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/blinkstick.rb', line 134 def get_info_block(id) bytes = @handle.control_transfer(:bmRequestType => 0x80 | 0x20, :bRequest => 0x1, :wValue => id + 1, :wIndex => 0x0000, :dataIn => 33) result = "" for i in 1..(bytes.length-1) if i == "\x00" break end result += bytes[i] end result end |
#info_block1 ⇒ Object
171 172 173 |
# File 'lib/blinkstick.rb', line 171 def info_block1 get_info_block(1) end |
#info_block1=(value) ⇒ Object
175 176 177 |
# File 'lib/blinkstick.rb', line 175 def info_block1=(value) set_info_block(1, value) end |
#info_block2 ⇒ Object
179 180 181 |
# File 'lib/blinkstick.rb', line 179 def info_block2 get_info_block(2) end |
#info_block2=(value) ⇒ Object
183 184 185 |
# File 'lib/blinkstick.rb', line 183 def info_block2=(value) set_info_block(2, value) end |
#manufacturer ⇒ Object
163 164 165 |
# File 'lib/blinkstick.rb', line 163 def manufacturer @device.manufacturer end |
#off ⇒ Object
130 131 132 |
# File 'lib/blinkstick.rb', line 130 def off self.color = Color::RGB.new(0, 0, 0) end |
#open(device = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/blinkstick.rb', line 10 def open(device = nil) @@usb ||= LIBUSB::Context.new if (device) @device = device else @device = @@usb.devices(:idVendor => @@VENDOR_ID, :idProduct => @@PRODUCT_ID).first end @handle = @device.open end |
#random_color ⇒ Object
154 155 156 157 |
# File 'lib/blinkstick.rb', line 154 def random_color r = Random.new self.color = Color::RGB.new(r.rand(255), r.rand(255), r.rand(255)) end |
#serial ⇒ Object
159 160 161 |
# File 'lib/blinkstick.rb', line 159 def serial @device.serial_number end |
#set_color(channel, index, value) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/blinkstick.rb', line 60 def set_color(channel, index, value) attempts = 0 while attempts < 5 attempts += 1 begin @handle.control_transfer(:bmRequestType => 0x20, :bRequest => 0x9, :wValue => 0x5, :wIndex => 0x0000, :dataOut => 1.chr + channel.to_i.chr + index.to_i.chr + value.red.to_i.chr + value.green.to_i.chr + value.blue.to_i.chr) break rescue if attempts == 5 raise end end end end |
#set_colors(channel, data) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/blinkstick.rb', line 80 def set_colors(channel, data) report_id = 9 max_leds = 64 if data.size <= 8 * 3 max_leds = 8 report_id = 6 elsif data.size <= 16 * 3 max_leds = 16 report_id = 7 elsif data.size <= 32 * 3 max_leds = 32 report_id = 8 elsif data.size <= 64 * 3 max_leds = 64 report_id = 9 end report = report_id.chr + channel.to_i.chr (0..max_leds * 3 - 1).each do | i | if data.size > i report += data[i].to_i.chr else report += 0.chr end end #Debug code #puts report.unpack('U'*report.length).collect {|x| x.to_s 16}.join(" ") attempts = 0 while attempts < 5 attempts += 1 begin @handle.control_transfer(:bmRequestType => 0x20, :bRequest => 0x9, :wValue => report_id, :wIndex => 0, :dataOut => report) break rescue if attempts == 5 raise end end end end |
#set_info_block(id, data) ⇒ Object
148 149 150 151 152 |
# File 'lib/blinkstick.rb', line 148 def set_info_block(id, data) data = (id + 1).chr + data data = data + 0.chr while data.length < 33 @handle.control_transfer(:bmRequestType => 0x20, :bRequest => 0x9, :wValue => id + 1, :wIndex => 0x0000, :dataOut => data) end |