class RubyHid::Device
The main interface for the Buzz controllers. Primarily used to monitor key pushes and trigger events. Keep a single instance of the class:
‘RubyHid::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 RubyHid::Button
class. You can end this worker with `stop_watching`.
Constants
- ALLOWED_EVENT_TYPES
- AXIS_TYPE
- BUTTON_TYPE
Event
types we’re interested in, used to filter out meta-data.1 - button (0, 1) 3 - axis (usually 0 - 255, centred on 128)
- Event
Attributes
The worker is a thread which is watching the device
Public Class Methods
List possible devices from /dev/input/by-id/
Or, list devices containing a string with search_term argument
# File lib/ruby_hid/device.rb, line 47 def Device.list(search_term=nil) if search_term Dir["/dev/input/by-id/*#{search_term}*event-joystick*"] else Dir['/dev/input/by-id/*event-joystick*'] end end
Initialise device, getting event file from /dev/input/by-id/
# File lib/ruby_hid/device.rb, line 80 def initialize(filename=nil, block_size=24) raise NoFileSpecifiedError if filename.nil? @dev = File.open(filename) @block_size = block_size rescue NoFileSpecifiedError, Errno::ENOENT => er puts "Could not find device: are your controllers plugged in?" raise er end
Public Instance Methods
Expose each event to a block of code as it comes in.
# File lib/ruby_hid/device.rb, line 112 def each begin loop do event = read if event next unless ALLOWED_EVENT_TYPES.include?(event.type) yield event end end rescue Errno::ENODEV end end
# File lib/ruby_hid/device.rb, line 37 def force_rights `sudo chmod 777 /sys/class/leds/*/brightness` `sudo chmod 777 /dev/input/event*` end
The format string which RubyHid
uses to decode raw data.
# File lib/ruby_hid/device.rb, line 92 def format @format ||= case @block_size when 16 'llSSl' when 24 'qqSSl' end end
Read a single block.
# File lib/ruby_hid/device.rb, line 104 def read bin = @dev.read @block_size Event.new *bin.unpack(format) end
Start a background worker which scans input file and triggers any events bound to each one.
# File lib/ruby_hid/device.rb, line 129 def start_watching return if @worker @worker = Thread.new do loop do event = read next unless ALLOWED_EVENT_TYPES.include?(event.type) case event.type when BUTTON_TYPE RubyHid::Button.trigger_event(event.code, event.value) when AXIS_TYPE RubyHid::Axis.trigger_event(event.code, event.value) end end end end
Stop the background worker, release it’s resources.
# File lib/ruby_hid/device.rb, line 148 def stop_watching @worker.kill @worker = nil end