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
}.freeze
COMMANDS =
{
  :down  => 0x01,
  :up    => 0x02,
  :left  => 0x04,
  :right => 0x08,
  :fire  => 0x10,
  :stop  => 0x20,
  :on    => 0x01,
  :off   => 0x00
}.freeze
TARGETS =
{
  :launcher => 0x02,
  :led      => 0x03
}.freeze
REQUEST_TYPE =
0x21
REQUEST =
0x09

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device) ⇒ Launcher

Returns a new instance of Launcher.



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

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



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

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

  fail 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



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

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

#led(status) ⇒ Object

Switch the LED on/off



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

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

#move(direction, duration) ⇒ Object

Not really the best motor control



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

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



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

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

#positionObject

Get the relative position



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

def position
  [@x, @y]
end

#resetObject

Resets the launcher into the default position



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

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