Class: HIDAPI::SetupTaskHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hidapi/setup_task_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vendor_id, product_id, simple_name, interface) ⇒ SetupTaskHelper

Returns a new instance of SetupTaskHelper


10
11
12
13
14
15
16
17
# File 'lib/hidapi/setup_task_helper.rb', line 10

def initialize(vendor_id, product_id, simple_name, interface)
  @vendor_id = interpret_id(vendor_id)
  @product_id = interpret_id(product_id)
  @simple_name = simple_name.to_s.strip.gsub(' ', '_').gsub(/[^A-Za-z0-9_\-]/, '')
  @interface = interface.to_s.strip.to_i
  @temp_dir = Dir.mktmpdir('hidapi_setup')
  ObjectSpace.define_finalizer(self, ->{ FileUtils.rm_rf(@temp_dir) })
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface


8
9
10
# File 'lib/hidapi/setup_task_helper.rb', line 8

def interface
  @interface
end

#product_idObject (readonly)

Returns the value of attribute product_id


8
9
10
# File 'lib/hidapi/setup_task_helper.rb', line 8

def product_id
  @product_id
end

#simple_nameObject (readonly)

Returns the value of attribute simple_name


8
9
10
# File 'lib/hidapi/setup_task_helper.rb', line 8

def simple_name
  @simple_name
end

#vendor_idObject (readonly)

Returns the value of attribute vendor_id


8
9
10
# File 'lib/hidapi/setup_task_helper.rb', line 8

def vendor_id
  @vendor_id
end

Instance Method Details

#runObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hidapi/setup_task_helper.rb', line 19

def run
  if valid_options?
    if operating_system == :osx
      run_osx
    elsif operating_system == :linux
      run_linux
    elsif operating_system == :windows
      run_windows
    else
      puts "Your operating system was detected as '#{operating_system}', but I don't have a setup routine for that."
      false
    end
  else
    usage
  end
end

#usageObject


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hidapi/setup_task_helper.rb', line 40

def usage()
  puts <<-USAGE
Usage:  bundle exec rake setup_hid_device[vendor_id,product_id,simple_name,interface]
-OR-    #{File.basename(__FILE__)} vendor_id product_id simple_name interface
  vendor_id should be the 16-bit identifier assigned to the device vendor in hex format.
  product_id should be the 16-bit identifier assigned by the manufacturer in hex format.
  simple_name should be a fairly short name for the device.
  interface is optional and defaults to 0.

  ie - bundle exec rake setup_hid_device[04d8,c002,pico-lcd-graphic]
  ie - #{File.basename(__FILE__)} 04d8 c002 pico-lcd-graphic
USAGE
  false
end

#valid_options?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/hidapi/setup_task_helper.rb', line 36

def valid_options?
  vendor_id != 0 && product_id != 0 && simple_name != ''
end