class RubyBuzz::Device

The main interface for the Buzz controllers. Primarily used to monitor key pushes and trigger events. Keep a single instance of the class:

‘RubyBuzz::Device.new`

The ‘each` method exposes events directly as they come in

‘device.each { |event| puts event }`

The ‘start_watching` method starts a background job which runs the events bound to each button via the RubyBuzz::Button class. You can end this worker with `stop_watching`.

Constants

DEFAULT_FILE_NAME
Event

Attributes

worker[RW]

The worker is a thread which is watching the device

Public Class Methods

new(filename=nil) click to toggle source

Initialise device, getting event file from /dev/input/by-id/

You can override this to a different event, but ruby-buzz only supports a single usb controller.

# File lib/ruby_buzz/device.rb, line 49
def initialize(filename=nil)
  @dev = File.open(filename || DEFAULT_FILE_NAME)
  @block_size = 24
rescue Errno::ENOENT => er
  puts "Could not find device: are your controllers plugged in?"
  raise er
end

Public Instance Methods

each() { |event| ... } click to toggle source

Expose each event to a block of code as it comes in.

# File lib/ruby_buzz/device.rb, line 75
def each
  begin
    loop do
      event = read
      next unless event.type == 1
      next unless event.value == 1
      yield event
    end
  rescue Errno::ENODEV
  end
end
format() click to toggle source

The format for each 24 bit data chunk taken from the event stream.

# File lib/ruby_buzz/device.rb, line 60
def format
  'qqSSl'
end
read() click to toggle source

Read a single 24-bit block.

# File lib/ruby_buzz/device.rb, line 67
def read
  bin = @dev.read @block_size
  Event.new *bin.unpack(format)
end
start_watching() click to toggle source

Start a background worker which scans input file and triggers any events bound to each one.

# File lib/ruby_buzz/device.rb, line 91
def start_watching
  return if @worker
  @worker = Thread.new do
    loop do
      event = read
      next unless event.type == 1
      next unless event.value == 1
      RubyBuzz::Button.trigger_key(event.code)
    end
  end
end
stop_watching() click to toggle source

Stop the background worker, release it’s resources.

# File lib/ruby_buzz/device.rb, line 106
def stop_watching
  @worker.kill
  @worker = nil
end