diff options
-rw-r--r-- | cmd/dolmetschctl/dolmetschctl.go | 7 | ||||
-rw-r--r-- | pkg/controller/controller.go | 31 |
2 files changed, 32 insertions, 6 deletions
diff --git a/cmd/dolmetschctl/dolmetschctl.go b/cmd/dolmetschctl/dolmetschctl.go index 0c03285..6055896 100644 --- a/cmd/dolmetschctl/dolmetschctl.go +++ b/cmd/dolmetschctl/dolmetschctl.go @@ -25,6 +25,7 @@ package main import ( "log" "os" + "time" "spreadspace.org/dolmetschctl/pkg/controller" ) @@ -46,10 +47,8 @@ func main() { log.Printf("controller successfully initialized!") - if err = c.LedToggle(1); err != nil { - log.Printf("%v", err) - os.Exit(1) + for { + time.Sleep(1 * time.Second) } - log.Printf("exiting.") } 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 |