class URIx::URIx

Attributes

claimed[R]
pin_modes[R]
pin_states[R]
usb[RW]

Public Class Methods

new() click to toggle source

Creates a new URIx interface.

# File lib/urix/urix.rb, line 14
def initialize
        @usb = LIBUSB::Context.new
        @pin_states = 0x0
        @pin_modes = 0x0
        @claimed = false
        
        set_pin_mode( PTT_PIN, :output )
end

Public Instance Methods

claim_interface() click to toggle source

Claim the USB interface for this program. Must be called to begin using the interface.

# File lib/urix/urix.rb, line 26
def claim_interface
        devices = usb.devices(:idVendor => VENDOR_ID, :idProduct => PRODUCT_ID)

        unless devices.first then
                return
        end

        @device = devices.first
        @handle = @device.open
        
        @handle.detach_kernel_driver(HID_INTERFACE)
        @handle.claim_interface( HID_INTERFACE )
end
close_interface() click to toggle source

Closes the interface and frees it to be used by something else. Important to call at the end of program.

# File lib/urix/urix.rb, line 43
def close_interface
        @handle.release_interface( HID_INTERFACE )
        @handle.attach_kernel_driver(HID_INTERFACE)
        @handle.close
end
set_output(pin, state) click to toggle source

Sets the state of a GPIO pin.

@param pin [Integer] ID of GPIO pin. @param state [Boolean] State to set pin to. True == :high

# File lib/urix/urix.rb, line 54
def set_output pin, state
        state = false if state == :low
        state = true  if state == :high

        if ( @pin_states >> ( pin - 1 )).odd? && ( state == false ) or
           ( @pin_states >> ( pin - 1 )).even? && ( state == true ) then

                mask = 0 + ( 1 << ( pin - 1 ))
                @pin_states ^= mask
        end
        
        write_output
end
set_pin_mode(pin, mode) click to toggle source

Sets the mode of a GPIO pin to input or output.

@param pin [Integer] ID of GPIO pin. @param mode [Symbol] :input or :output

# File lib/urix/urix.rb, line 73
def set_pin_mode pin, mode
        if ( @pin_modes >> ( pin - 1 )).odd?  && ( mode == :input  ) or
           ( @pin_modes >> ( pin - 1 )).even? && ( mode == :output ) then

                mask = 0 + ( 1 << ( pin - 1 ))
                @pin_modes ^= mask
        end
end
set_ptt(state) click to toggle source

Shortcut for `set_output( PTT_PIN, state )`

@param state [Boolean] State to set PTT pin to. True == :high

# File lib/urix/urix.rb, line 86
def set_ptt state
        set_output PTT_PIN, state
end

Private Instance Methods

write_output() click to toggle source
# File lib/urix/urix.rb, line 93
def write_output
        type  = LIBUSB::ENDPOINT_OUT
        type += LIBUSB::REQUEST_TYPE_CLASS
        type += LIBUSB::RECIPIENT_INTERFACE

        request = HID_REPORT_SET

        value = 0 + ( HID_RT_OUTPUT << 8 )

        index = HID_INTERFACE

        dout  = 0.chr
        dout += @pin_states.chr
        dout += @pin_modes.chr
        dout += 0.chr

        @handle.control_transfer(
                :bmRequestType => type,
                :bRequest => request,
                :wValue => value,
                :wIndex => index,
                :dataOut => dout
        )
end