Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a LightRGB method to support RGB colors. #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cmd/hellomk2rgb/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"log"
"time"

"github.com/IanLewis/launchpad/mk2"
// "github.com/rakyll/launchpad/mk2"
)

func main() {
pad, err := mk2.Open()
if err != nil {
log.Fatalf("error while openning connection to launchpad: %v", err)
}
defer pad.Close()

// render cycles through the pure red, green, and blue palettes.
var palette int
render := func(i, j int) {
pad.Clear()
// Turn on all the buttons in sequence.
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
c := mk2.Color{}
switch palette {
case 0:
c.R = j + (i * 8)
case 1:
c.G = j + (i * 8)
case 2:
c.B = j + (i * 8)
}
pad.LightRGB(i, j, c)
time.Sleep(20 * time.Millisecond)
}
}
palette = (palette + 1) % 3
}

render(0, 0)
ch := pad.Listen()
for {
hit := <-ch
log.Printf("Button pressed at <x=%d, y=%d>", hit.X, hit.Y)
// Re-render the color palette again.
render(hit.X, hit.Y)
}

}
29 changes: 24 additions & 5 deletions mk2/launchpad.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ type Launchpad struct {
outputStream *portmidi.Stream
}

// Color represents an RGB color. Color values should be [0, 64) for the Launchpad Mk2
type Color struct {
R int
G int
B int
}

// Hit represents physical touches to Launchpad buttons.
type Hit struct {
X int
Expand Down Expand Up @@ -100,19 +107,31 @@ func (l *Launchpad) Read() (hits []Hit, err error) {
return
}

// Light lights the button at x,y with the given red, green, and blue values.
// Light lights the button at x,y with the given palette color.
// x and y are [0, 7]. Color is [0, 128).
// All available colors are documented and visualized at Launchpad's Programmers Guide
// All available colors are documented and visualized in the Launchpad's Programmers Guide
// at https://global.novationmusic.com/sites/default/files/novation/downloads/10529/launchpad-mk2-programmers-reference-guide_0.pdf.
func (l *Launchpad) Light(x, y, color int) error {
// TODO(jbd): Support top row.
led := int64((8-y)*10 + x + 1)
return l.outputStream.WriteShort(0x90, led, int64(color))
}

// Reset turns off all buttons.
func (l *Launchpad) Reset() error {
// Sends a "light all ligts" SysEx command with 0 color.
// LightRGB lights the button at x,y with the given rgb color.
// x and y are [0, 7]. RGB color values are [0, 64).
// This method is documented in the Launchpad's Programmers Guide under the "RGB Mode" heading.
// at https://global.novationmusic.com/sites/default/files/novation/downloads/10529/launchpad-mk2-programmers-reference-guide_0.pdf.
func (l *Launchpad) LightRGB(x, y int, c Color) error {
// This method uses RGB Mode on the Launchpad Mk2.
// RGB colors are set using SysEx extended mode messages.
led := byte((8-y)*10 + x + 1)
return l.outputStream.WriteSysExBytes(portmidi.Time(), []byte{0xf0, 0x00, 0x20, 0x29, 0x02, 0x18, 0x0b, led, byte(c.R), byte(c.G), byte(c.B), 0xf7})
}

// Clear turns off all the LEDs on the Launchpad.
func (l *Launchpad) Clear() error {
// Sends a "light all lights" SysEx command with 0 color.
// This is documented in the Launchpad's Programmers Guide under the "Light all LEDs using SysEx" heading.
return l.outputStream.WriteSysExBytes(portmidi.Time(), []byte{0xf0, 0x00, 0x20, 0x29, 0x02, 0x18, 0x0e, 0x00, 0xf7})
}

Expand Down