Class: TuxDroid::Tux

Inherits:
Object
  • Object
show all
Includes:
USBCodes
Defined in:
lib/tuxdroid.rb

Overview

Tux Interface Class

Constant Summary

Constants included from USBCodes

USBCodes::FIRMWARE_REV, USBCodes::INTERFACE, USBCodes::PRODUCT_ID, USBCodes::READ_ENDPOINT, USBCodes::READ_TIMEOUT, USBCodes::RECV_LENGTH, USBCodes::SEND_LENGTH, USBCodes::VENDOR_ID, USBCodes::WRITE_ENDPOINT, USBCodes::WRITE_TIMEOUT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTux

Creates a new interface between ruby and the TuxDroid



22
23
24
25
26
# File 'lib/tuxdroid.rb', line 22

def initialize
  @tux_device = self.class.get_usb_device()
  @tux_handle = self.class.get_usb_interface(@tux_device)
  @usb_status = :connected
end

Instance Attribute Details

#usb_statusObject (readonly)

Returns the value of attribute usb_status.



19
20
21
# File 'lib/tuxdroid.rb', line 19

def usb_status
  @usb_status
end

Instance Method Details

#eyes(cmd, options = {}) ⇒ Object

Accepts:

  • :open => opens eyes

  • :closes => closes eyes

  • :blink => toggles eyes (specify option “:times => n” for n amount of toggles)

  • :stop => stops current eye motions



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tuxdroid.rb', line 55

def eyes(cmd, options = {})
  validate_within(options, :times => 0..255)
  case cmd
    when :open
      send_command(Actions::OPEN_EYES)
    when :close
      send_command(Actions::BLINK_EYES, 1)
    when :blink
      send_command(Actions::BLINK_EYES, options[:times] || 1)
    when :stop
      send_command(Actions::STOP_EYES)
    else
      raise UnknownTuxDroidActionError.new("Command: #{cmd.inspect} Options: #{options.inspect}")
  end    
end

#leds(cmd, options = {}) ⇒ Object

Accepts:

  • :on => light up both eyes

  • :off => turn off both eyes

  • :left_on => light up left eye

  • :left_off => turn off left eye

  • :right_on => light up right eye

  • :right_off => turn off right eye

  • :blink => blink both eyes, options are :times => n (n amount of blinks), :delay => n (n x 4ms delay between blinks)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tuxdroid.rb', line 115

def leds(cmd, options = {})
  validate_within(options, :times => 0..255, :delay => 0..255)
  case cmd
    when :on
      send_command(Actions::LED_ON)
    when :off
      send_command(Actions::LED_OFF)
    when :left_on
      send_command(Actions::LED_LEFT_ON)
    when :left_off
      send_command(Actions::LED_LEFT_OFF)
    when :right_on
      send_command(Actions::LED_RIGHT_ON)
    when :right_off
      send_command(Actions::LED_RIGHT_OFF)
    when :blink
      send_command(Actions::LED_BLINK, options[:times] || 1, options[:delay] || 20)
    else
      raise UnknownTuxDroidActionError.new("Command: #{cmd.inspect} Options: #{options.inspect}")
  end        
end

#mouth(cmd, options = {}) ⇒ Object

Accepts:

  • :open => opens mouth

  • :closes => closes mouth

  • :move => toggles mouth (specify option “:times => n” for n amount of toggles)



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tuxdroid.rb', line 36

def mouth(cmd, options = {})
  validate_within(options, :times => 0..255)
  case cmd
    when :open
      send_command(Actions::OPEN_MOUTH)
    when :close
      send_command(Actions::CLOSE_MOUTH)
    when :move
      send_command(Actions::MOVE_MOUTH, options[:times] || 1)
    else
      raise UnknownTuxDroidActionError.new("Command: #{cmd.inspect} Options: #{options.inspect}")
  end
end

#spin(cmd, options = {}) ⇒ Object

Accepts:

  • :left => spins left roughly 90 degrees, options are :times => n (n amount of times), :speed => (1..5)

  • :right => spins right roughly 90 degrees, options are :times => n (n amount of times), :speed => (1..5)

  • :stop => stops current spinning motion



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tuxdroid.rb', line 93

def spin(cmd, options = {})
  validate_within(options, :times => 0..255, :speed => 1..5)
  case cmd
    when :left
      send_command(Actions::SPIN_LEFT, options[:times] || 1, options[:speed] || 3)
    when :right
      send_command(Actions::SPIN_RIGHT, options[:times] || 1, options[:speed] || 3)
    when :stop
      send_command(Actions::STOP_SPIN)
    else
      raise UnknownTuxDroidActionError.new("Command: #{cmd.inspect} Options: #{options.inspect}")
  end        
end

#wings(cmd, options = {}) ⇒ Object

Accepts:

  • :toggle => toggles wings, options are :speed => (1..5)

  • :reset => runs wing reset routine (flaps twice)

  • :flap => toggles wings, options are :times => n (n amount of times), :speed => (1..5)



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tuxdroid.rb', line 75

def wings(cmd, options = {})
  validate_within(options, :times => 0..255, :speed => 1..5)
  case cmd
    when :toggle
      send_command(Actions::WAVE_WINGS, 1, options[:speed] || 3)
    when :reset
      send_command(Actions::RESET_WINGS)
    when :flap
      send_command(Actions::WAVE_WINGS, options[:times] || 1, options[:speed] || 3)
    else
      raise UnknownTuxDroidActionError.new("Command: #{cmd.inspect} Options: #{options.inspect}")
  end        
end