Skip to content

Instantly share code, notes, and snippets.

@nakajima
Last active December 16, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakajima/5351809 to your computer and use it in GitHub Desktop.
Save nakajima/5351809 to your computer and use it in GitHub Desktop.
Draw crap on your Novation Launchpad. You'll need the unimidi gem. You know where to get it.
# USAGE
#
# Make sure your launchpad is plugged in.
#
# $ ruby launchpad-art.rb
#
# Select your launchpad from the list of midi controllers, then start pushing
# buttons on your launchpad.
require "unimidi"
OUTPUT = UniMIDI::Output.open(0)
class Event
def initialize(input)
@input = input
end
def inspect
"<Event status=#{status} note=#{note} velocity=#{velocity}>"
end
def status
data[0]
end
def note
data[1]
end
def velocity
data[2]
end
def button
Button.new(note)
end
private
def data
@input[:data]
end
end
class Button
YELLOW = 62
RED_LOW = 13
RED_HIGH = 15
AMBER_LOW = 29
AMBER_HIGH = 63
GREEN_LOW = 28
GREEN_HIGH = 60
def self.map
@map ||= {}
end
def initialize(note, output = OUTPUT)
@color = 0
@note = note
@output = output
end
def on(color=GREEN_HIGH)
Button.map[@note] = color
@output.puts(0x90, @note, color)
end
def on?
Button.map[@note]
end
def off
Button.map.delete(@note)
@output.puts(0x80, @note, 100)
end
def off?
not on?
end
def toggle
color = colors.at colors.index(Button.map[@note]) + 1
if color
on(color)
else
off
end
end
def colors
[nil, RED_HIGH, AMBER_HIGH, GREEN_HIGH]
end
end
@input = UniMIDI::Input.gets
@input.open do |input|
$stdout.puts "send some MIDI to your input now..."
loop do
m = input.gets
m.each do |m|
event = Event.new(m)
$stdout.puts event.inspect
if event.velocity > 0
event.button.toggle
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment