Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from halorgium/connections
Split out the connections into their own classes
- Loading branch information
Showing
4 changed files
with
102 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Connections | ||
require 'connections/bluetooth' | ||
require 'connections/usb' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Connections::Bluetooth | ||
def initialize(dev) | ||
@connection_type = "serialport" | ||
@dev = dev | ||
@sp = SerialPort.new(dev, 57600, 8, 1, SerialPort::NONE) | ||
|
||
@sp.flow_control = SerialPort::HARD | ||
@sp.read_timeout = 5000 | ||
$stderr.puts "Cannot connect to #{dev}" if @sp.nil? | ||
end | ||
|
||
def write(msg) | ||
msg = [(msg.size & 255),(msg.size >> 8)] + msg | ||
msg.each do |b| | ||
@sp.putc b | ||
end | ||
end | ||
|
||
def read | ||
len_header = @sp.sysread(2) | ||
@sp.sysread(len_header.unpack("v")[0]) | ||
end | ||
|
||
def close | ||
@sp.close if @sp and not @sp.closed? | ||
end | ||
|
||
def connected? | ||
not @sp.closed? | ||
end | ||
|
||
def type | ||
"Bluetooth" | ||
end | ||
|
||
def info | ||
@dev | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class Connections::Usb | ||
ID_VENDOR_LEGO = 0x0694 | ||
ID_PRODUCT_NXT = 0x0002 | ||
OUT_ENDPOINT = 0x01 | ||
IN_ENDPOINT = 0x82 | ||
TIMEOUT = 1000 | ||
READSIZE = 64 | ||
INTERFACE = 0 | ||
|
||
def initialize(interface) | ||
@interface = interface || INTERFACE | ||
usb = LIBUSB::Context.new | ||
@usb_dev = usb.devices(idVendor: ID_VENDOR_LEGO, idProduct: ID_PRODUCT_NXT).first | ||
$stderr.puts "Cannot find usb device" if @usb_dev.nil? | ||
@usb = @usb_dev.open | ||
@usb.reset_device | ||
@usb.claim_interface(@interface) | ||
end | ||
|
||
def write(msg) | ||
@usb.bulk_transfer(endpoint: OUT_ENDPOINT, dataOut: msg.pack("C*"), timeout: TIMEOUT) | ||
end | ||
|
||
def read | ||
@usb.bulk_transfer(endpoint: IN_ENDPOINT, dataIn: READSIZE, timeout: TIMEOUT) | ||
end | ||
|
||
def close | ||
if @usb | ||
@usb.release_interface(@interface) | ||
@usb.close | ||
@usb_dev = @usb = nil | ||
end | ||
end | ||
|
||
def connected | ||
not not @usb | ||
end | ||
|
||
def type | ||
"USB" | ||
end | ||
|
||
def info | ||
@interface | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters