class Artoo::Drivers::Joystick

The sdl-joystick driver behaviors

Constants

COMMANDS

Attributes

button_values[R]

Public Instance Methods

currently_pressed?(b) click to toggle source
# File lib/artoo/drivers/joystick.rb, line 40
def currently_pressed?(b)
  button_values[b]
end
handle_buttons() click to toggle source
# File lib/artoo/drivers/joystick.rb, line 71
def handle_buttons
  connection.num_buttons.times {|b|
    currently_pressed = connection.button(b)
    if button_values[b] != currently_pressed
      button_values[b] = currently_pressed
      publish_button(b)
    end
  }
end
handle_joystick() click to toggle source
# File lib/artoo/drivers/joystick.rb, line 52
def handle_joystick
  number_sticks = connection.num_axes / 2
  number_sticks.times {|s|
    x = connection.axis(s * 2)
    y = connection.axis(s * 2 + 1)

    publish_joystick(s, x, y)
  }
end
handle_message_events() click to toggle source
# File lib/artoo/drivers/joystick.rb, line 44
def handle_message_events
  connection.poll
  handle_joystick
  # TODO: handle_trackball
  # TODO: handle_hats
  handle_buttons
end
handle_trackball() click to toggle source
# File lib/artoo/drivers/joystick.rb, line 62
def handle_trackball
  if connection.num_balls == 1
    x, y = connection.ball(0)

    publish(event_topic_name("update"), "trackball", {:x => x, :y => y})
    publish(event_topic_name("trackball"), {:x => x, :y => y})
  end
end
publish_button(b) click to toggle source
# File lib/artoo/drivers/joystick.rb, line 87
def publish_button(b)
  if button_values[b] == 1
    publish(event_topic_name("update"), "button", b)
    publish(event_topic_name("button"), b)
    publish(event_topic_name("button_#{b}"))
  end
end
publish_joystick(s, x, y) click to toggle source
# File lib/artoo/drivers/joystick.rb, line 81
def publish_joystick(s, x, y)
  publish(event_topic_name("update"), "joystick", {:x => x, :y => y, :s => s})
  publish(event_topic_name("joystick"), {:x => x, :y => y, :s => s})
  publish(event_topic_name("joystick_#{s}"), {:x => x, :y => y})
end
start_driver() click to toggle source

Start driver and any required connections

Calls superclass method
# File lib/artoo/drivers/joystick.rb, line 14
def start_driver
  puts os
  case os
  when :linux
    require 'artoo/drivers/linux_binding_map'
  when :macosx
    require 'artoo/drivers/macosx_binding_map'
  else
    # raise error ?
  end

  @button_values = {}

  begin
    every(interval) do
      handle_message_events
    end

    super
  rescue Exception => e
    Logger.error "Error starting SdlJoystick driver!"
    Logger.error e.message
    Logger.error e.backtrace.inspect
  end
end