Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

(at least) doubled the speed of change_all

  • Loading branch information...
commit 012d623655dd4039cefc20368c16fbc72e2a02a0 1 parent 4facfd3
@thomasjachmann authored
Showing with 46 additions and 6 deletions.
  1. +2 −0  README.rdoc
  2. +39 −2 lib/launchpad/device.rb
  3. +5 −4 test/test_device.rb
View
2  README.rdoc
@@ -72,6 +72,8 @@ For more details, see the examples. examples/color_picker.rb is the most complex
== Changelog
+* (at least) doubled the speed of change_all by not sending each message individually but sending them in one go (as an array)
+
=== v0.1.1
* ability to close device/interaction to free portmidi resources
View
41 lib/launchpad/device.rb
@@ -160,9 +160,11 @@ def change_all(*colors)
# HACK switch off first grid LED to reset rapid LED change pointer
output(Status::ON, 0, 0)
# send colors in slices of 2
+ messages = []
colors.each_slice(2) do |c1, c2|
- output(Status::MULTI, velocity(c1), velocity(c2))
+ messages << message(Status::MULTI, velocity(c1), velocity(c2))
end
+ output_messages(messages)
end
# Switches LEDs marked as flashing on when using custom timer for flashing.
@@ -330,8 +332,22 @@ def input
#
# [Launchpad::NoOutputAllowedError] when output is not enabled
def output(status, data1, data2)
+ output_messages([message(status, data1, data2)])
+ end
+
+ # Writes several messages to the MIDI device.
+ #
+ # Parameters:
+ #
+ # [+messages+] an array of hashes (usually created with message) with:
+ # [<tt>:message</tt>] an array of
+ # MIDI status code,
+ # MIDI data 1 (note),
+ # MIDI data 2 (velocity)
+ # [<tt>:timestamp</tt>] integer indicating the time when the MIDI message was created
+ def output_messages(messages)
raise NoOutputAllowedError if @output.nil?
- @output.write([{:message => [status, data1, data2], :timestamp => 0}])
+ @output.write(messages)
nil
end
@@ -430,6 +446,27 @@ def brightness(brightness)
end
end
+ # Creates a MIDI message.
+ #
+ # Parameters:
+ #
+ # [+status+] MIDI status code
+ # [+data1+] MIDI data 1 (note)
+ # [+data2+] MIDI data 2 (velocity)
+ #
+ # Returns:
+ #
+ # an array with:
+ #
+ # [<tt>:message</tt>] an array of
+ # MIDI status code,
+ # MIDI data 1 (note),
+ # MIDI data 2 (velocity)
+ # [<tt>:timestamp</tt>] integer indicating the time when the MIDI message was created, in this case 0
+ def message(status, data1, data2)
+ {:message => [status, data1, data2], :timestamp => 0}
+ end
+
end
end
View
9 test/test_device.rb
@@ -34,7 +34,9 @@ class TestDevice < Test::Unit::TestCase
}
def expects_output(device, *args)
- device.instance_variable_get('@output').expects(:write).with([{:message => args, :timestamp => 0}])
+ args = [args] unless args.first.is_a?(Array)
+ messages = args.collect {|data| {:message => data, :timestamp => 0}}
+ device.instance_variable_get('@output').expects(:write).with(messages)
end
def stub_input(device, *args)
@@ -416,14 +418,13 @@ def stub_input(device, *args)
should 'fill colors with 0, set grid 0,0 to 0 and flush colors' do
expects_output(@device, 0x90, 0, 0)
- 20.times {|i| expects_output(@device, 0x92, 17, 17)}
- 20.times {|i| expects_output(@device, 0x92, 12, 12)}
+ expects_output(@device, *([[0x92, 17, 17]] * 20 + [[0x92, 12, 12]] * 20))
@device.change_all([5] * 40)
end
should 'cut off exceeding colors, set grid 0,0 to 0 and flush colors' do
expects_output(@device, 0x90, 0, 0)
- 40.times {|i| expects_output(@device, 0x92, 17, 17)}
+ expects_output(@device, *([[0x92, 17, 17]] * 40))
@device.change_all([5] * 100)
end
Please sign in to comment.
Something went wrong with that request. Please try again.