Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

device initialization refactoring

  • Loading branch information...
commit e0c44c291d203e4853d11cef9b71a215ca9af988 1 parent e944b23
Thomas Jachmann authored November 15, 2009

Showing 1 changed file with 28 additions and 24 deletions. Show diff stats Hide diff stats

  1. 52  lib/launchpad/device.rb
52  lib/launchpad/device.rb
@@ -12,9 +12,9 @@ class Device
12 12
     
13 13
     # Initializes the launchpad
14 14
     # {
15  
-    #   :device_name  => Name of the MIDI device to use, optional, defaults to Launchpad
16  
-    #   :input        => true/false, whether to use MIDI input for user interaction, optional, defaults to true
17  
-    #   :output       => true/false, whether to use MIDI output for data display, optional, defaults to true
  15
+    #   :device_name      => Name of the MIDI device to use, optional, defaults to Launchpad
  16
+    #   :input            => true/false, whether to use MIDI input for user interaction, optional, defaults to true
  17
+    #   :output           => true/false, whether to use MIDI output for data display, optional, defaults to true
18 18
     # }
19 19
     def initialize(opts = nil)
20 20
       opts = {
@@ -25,26 +25,9 @@ def initialize(opts = nil)
25 25
       
26 26
       Portmidi.start
27 27
       
28  
-      if opts[:input]
29  
-        input_device = Portmidi.input_devices.select {|device| device.name == opts[:device_name]}.first
30  
-        raise NoSuchDeviceError.new("MIDI input device #{opts[:device_name]} doesn't exist") if input_device.nil?
31  
-        begin
32  
-          @input = Portmidi::Input.new(input_device.device_id)
33  
-        rescue RuntimeError => e
34  
-          raise DeviceBusyError.new(e)
35  
-        end
36  
-      end
37  
-      
38  
-      if opts[:output]
39  
-        output_device = Portmidi.output_devices.select {|device| device.name == opts[:device_name]}.first
40  
-        raise NoSuchDeviceError.new("MIDI output device #{opts[:device_name]} doesn't exist") if output_device.nil?
41  
-        begin
42  
-          @output = Portmidi::Output.new(output_device.device_id)
43  
-        rescue RuntimeError => e
44  
-          raise DeviceBusyError.new(e)
45  
-        end
46  
-        reset
47  
-      end
  28
+      @input = device(Portmidi.input_devices, Portmidi::Input, :name => opts[:device_name]) if opts[:input]
  29
+      @output = device(Portmidi.output_devices, Portmidi::Output, :name => opts[:device_name]) if opts[:output]
  30
+      reset if output_enabled?
48 31
     end
49 32
     
50 33
     # Closes the device - nothing can be done with the device afterwards
@@ -57,7 +40,17 @@ def close
57 40
     
58 41
     # Determines whether this device has been closed
59 42
     def closed?
60  
-      @input.nil? && @output.nil?
  43
+      !(input_enabled? || output_enabled?)
  44
+    end
  45
+    
  46
+    # Determines whether this device can be used to read input
  47
+    def input_enabled?
  48
+      !@input.nil?
  49
+    end
  50
+    
  51
+    # Determines whether this device can be used to output data
  52
+    def output_enabled?
  53
+      !@output.nil?
61 54
     end
62 55
     
63 56
     # Resets the launchpad - all settings are reset and all LEDs are switched off
@@ -188,6 +181,17 @@ def read_pending_actions
188 181
     
189 182
     private
190 183
     
  184
+    def device(devices, device_type, opts)
  185
+      id = opts[:id]
  186
+      device = devices.select {|device| device.name == opts[:name]}.first
  187
+      raise NoSuchDeviceError.new("MIDI device #{opts[:name]} doesn't exist") if device.nil?
  188
+      begin
  189
+        device_type.new(device.device_id)
  190
+      rescue RuntimeError => e
  191
+        raise DeviceBusyError.new(e)
  192
+      end
  193
+    end
  194
+    
191 195
     def input
192 196
       raise NoInputAllowedError if @input.nil?
193 197
       @input.read(16)

0 notes on commit e0c44c2

Please sign in to comment.
Something went wrong with that request. Please try again.