Class: Artoo::Drivers::Joystick

Inherits:
Driver
  • Object
show all
Defined in:
lib/artoo/drivers/joystick.rb

Overview

The sdl-joystick driver behaviors

Direct Known Subclasses

Ps3, Xbox360

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#button_valuesObject (readonly)

Returns the value of attribute button_values.



7
8
9
# File 'lib/artoo/drivers/joystick.rb', line 7

def button_values
  @button_values
end

Instance Method Details

#handle_buttonsObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/artoo/drivers/joystick.rb', line 53

def handle_buttons
  connection.num_buttons.times {|b|
    currently_pressed = connection.button(b)
    if button_values[b] != currently_pressed
      button_values[b] = currently_pressed
      if currently_pressed == 1
        publish_button(b)
      end
    end
  }
end

#handle_joystickObject



34
35
36
37
38
39
40
41
42
# File 'lib/artoo/drivers/joystick.rb', line 34

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_eventsObject



26
27
28
29
30
31
32
# File 'lib/artoo/drivers/joystick.rb', line 26

def handle_message_events
  connection.poll
  handle_joystick
  # TODO: handle_trackball
  # TODO: handle_hats
  handle_buttons
end

#handle_trackballObject



44
45
46
47
48
49
50
51
# File 'lib/artoo/drivers/joystick.rb', line 44

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) ⇒ Object



71
72
73
74
75
# File 'lib/artoo/drivers/joystick.rb', line 71

def publish_button(b)
  publish(event_topic_name("update"), "button", b)
  publish(event_topic_name("button"), b)
  publish(event_topic_name("button_#{b}"))
end

#publish_joystick(s, x, y) ⇒ Object



65
66
67
68
69
# File 'lib/artoo/drivers/joystick.rb', line 65

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_driverObject

Start driver and any required connections



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/artoo/drivers/joystick.rb', line 10

def start_driver
  @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