Class: HWPing::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hwping/launcher.rb

Constant Summary collapse

DeviceNotFoundError =
Class.new(IOError)
DEVICE =
{
  :vendor_id  => 0x2123,
  :product_id => 0x1010
}
COMMANDS =
{
  :down  => 0x01,
  :up    => 0x02,
  :left  => 0x04,
  :right => 0x08,
  :fire  => 0x10,
  :stop  => 0x20,
  :on    => 0x01,
  :off   => 0x00
}
TARGETS =
{
  :launcher => 0x02,
  :led      => 0x03
}
REQUEST_TYPE =
0x21
REQUEST =
0x09

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ Launcher

Returns a new instance of Launcher.



45
46
47
48
49
50
51
# File 'lib/hwping/launcher.rb', line 45

def initialize(device)
  @device = device
  @handle = @device.open
  @handle.detach_kernel_driver(0) if @handle.kernel_driver_active?(0)
  @x = 0
  @y = 0
end

Class Method Details

.connectObject

Connect to the device



34
35
36
37
38
39
40
41
42
43
# File 'lib/hwping/launcher.rb', line 34

def self.connect
  usb = LIBUSB::Context.new
  launcher = usb.devices(
    :idVendor  => DEVICE[:vendor_id],
    :idProduct => DEVICE[:product_id]
  ).first

  raise DeviceNotFoundError, 'Launcher was not found.' if launcher.nil?
  new(launcher)
end

Instance Method Details

#fireObject

Fire a rocket and wait for the device to reload



74
75
76
77
# File 'lib/hwping/launcher.rb', line 74

def fire
  send! :launcher, :fire
  Kernel.sleep 4.5
end

#led(status) ⇒ Object

Switch the LED on/off



69
70
71
# File 'lib/hwping/launcher.rb', line 69

def led(status)
  send! :led, status
end

#move(direction, duration) ⇒ Object

Not really the best motor control



95
96
97
98
99
100
# File 'lib/hwping/launcher.rb', line 95

def move(direction, duration)
  send! :launcher, direction
  Kernel.sleep duration.to_f / 1000
  send! :launcher, :stop
  update_pos direction, duration
end

#point_and_fire(arr) ⇒ Object

Point at the given location and fire a rocket



54
55
56
57
58
59
60
61
# File 'lib/hwping/launcher.rb', line 54

def point_and_fire(arr)
  led(:on)
  reset
  right arr[0]
  up arr[1]
  fire
  led(:off)
end

#positionObject

Get the relative position



64
65
66
# File 'lib/hwping/launcher.rb', line 64

def position
  [@x, @y]
end

#resetObject

Resets the launcher into the default position



80
81
82
83
84
85
# File 'lib/hwping/launcher.rb', line 80

def reset
  down(1000)
  left(6000) # should be enough
  @x = 0
  @y = 0
end