概要
作成機器
- 作成したのは、USB-IO2.0のポート1の0番からLEDのプラスへ、LEDのマイナスに抵抗を付けてUSB-IO2.0のGNDへ、といった単純な回路です。
- USB-IO2.0
- 白色LED
- 形状:5mm砲弾
- 半角地:60度
- 電圧3.4V
- 電流20mA
- 抵抗100Ω
動作環境
- CentOS release 5.7 (Final)
- ruby 1.9.3dev (2011-09-23 revision 33323) [i686-linux]
- libusb-devel-0.1.12-5.1
- ruby-usb
動作手順
libusb-develの導入
$ sudo yum install libusb-devel
ruby-usbの導入
$ git clone https://github.com/akr/ruby-usb.git
$ cd ruby-usb
$ ruby extconf.rb
$ make
$ sudo make install
プログラムの実行
- 以下のプログラムを作成してroot権限で実行するとUSB-IO2.0のポート1の0番に繋いだLEDが点滅します。
-
|
サンプルコード |
require 'usb'
class USBIO2
def open(&block)
dev = USB.devices.find { |device| device.idVendor == 0x1352 && device.idProduct == 0x0121 }
@interface = dev.interfaces[0].settings[0].bInterfaceNumber
@configuration = dev.configurations[0].bConfigurationValue
dev.open do |handle|
@handle = handle
begin
handle.usb_set_configuration(@configuration)
handle.usb_claim_interface(@interface)
rescue
handle.usb_detach_kernel_driver_np(@interface)
handle.usb_claim_interface(@interface)
end
block.call self
end
end
def inout(data = [])
send_data = [0] * 64
send_data[0] = 0x20
data.each_with_index do |value, index|
send_data[index * 2 + 1] = index + 1
send_data[index * 2 + 2] = value
end
ret = io_send_recv(send_data)
[ret[1], ret[3]]
end
private
def io_send_recv(send_data)
@seq ||= 0
@seq += 1
send_data[63] = @seq
ret = @handle.usb_bulk_write(1, send_data.pack('C*'), 1000);
raise "Send-Len Error #{ret}" if ret != 64
recv_data = ([0] * 64).pack('C*')
20.times do
ret = @handle.usb_bulk_read(1, recv_data, 100)
return recv_data.unpack('C*') if ret == 64 && send_data[63] == recv_data.unpack('C*')[63]
sleep 1
end
raise "Read Data Error"
end
end
USBIO2.new.open do |usb|
p usb.inout([0x01])
end
|
最終更新:2011年12月11日 22:02