diff options
author | Christian Pointner <equinox@spreadspace.org> | 2019-02-11 19:44:31 +0100 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2019-02-11 19:44:31 +0100 |
commit | 838fae0460964a734159958fe850ebab2a65dd00 (patch) | |
tree | 13c2f66e9b3aa72e213372db96cd38c791d89c63 /pkg | |
parent | controller leds work now (diff) |
basic handling of midi events
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/controller/controller.go | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index b982c56..ab04fcc 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -25,16 +25,19 @@ package controller import ( "errors" "fmt" + "log" "strings" "github.com/scgolang/midi" ) const ( + BUTTON_ON = byte(0x90) + BUTTON_OFF = byte(0x80) NOTE_OFFSET_BUTTON = byte(0x00) NUM_BUTTONS = 4 - CC_LED = byte(0xB0) + LED_CC = byte(0xB0) NOTE_OFFSET_LED = byte(0x00) NUM_LEDS = 4 LED_OFF = byte(0x00) @@ -75,7 +78,31 @@ func NewController(c Config) (*Controller, error) { return ctrl, nil } +func (c *Controller) handleMidiPacket(p midi.Packet) { + num := p.Data[1] + switch p.Data[0] { + case BUTTON_ON: + log.Printf("controller: button %d pressed", num) + case BUTTON_OFF: + log.Printf("controller: button %d released", num) + default: + // ignore all other events + return + } +} + func (c *Controller) Init() error { + ch, err := c.Dev.Packets() + if err != nil { + return err + } + + go func() { + for { + // TODO: handle Errors (reopen the device!) + c.handleMidiPacket(<-ch) + } + }() return nil } @@ -93,7 +120,7 @@ func (c *Controller) setLed(num byte, value byte) error { return fmt.Errorf("this controller has only %d leds.", NUM_LEDS) } - n, err := c.Dev.Write([]byte{CC_LED, NOTE_OFFSET_LED + num, value}) + n, err := c.Dev.Write([]byte{LED_CC, NOTE_OFFSET_LED + num, value}) if err != nil { // reopen device? return err |