Move libraries to lib/ and PDF to docs/
This commit is contained in:
90
lib/xtouch/output.go
Normal file
90
lib/xtouch/output.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package xtouch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
"gitlab.com/gomidi/midi/v2/drivers"
|
||||
)
|
||||
|
||||
type LCDColor uint8
|
||||
|
||||
const (
|
||||
ColorBlack LCDColor = 0
|
||||
ColorRed LCDColor = 1
|
||||
ColorGreen LCDColor = 2
|
||||
ColorYellow LCDColor = 3
|
||||
ColorBlue LCDColor = 4
|
||||
ColorMagenta LCDColor = 5
|
||||
ColorCyan LCDColor = 6
|
||||
ColorWhite LCDColor = 7
|
||||
)
|
||||
|
||||
type LEDState uint8
|
||||
|
||||
const (
|
||||
LEDOff LEDState = 0
|
||||
LEDFlash LEDState = 64
|
||||
LEDOn LEDState = 127
|
||||
)
|
||||
|
||||
type Output struct {
|
||||
send func(msg midi.Message) error
|
||||
DeviceID uint8
|
||||
}
|
||||
|
||||
func NewOutput(port drivers.Out, deviceID uint8) (*Output, error) {
|
||||
send, err := midi.SendTo(port)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open output port: %w", err)
|
||||
}
|
||||
return &Output{send: send, DeviceID: deviceID}, nil
|
||||
}
|
||||
|
||||
func (o *Output) SetFader(fader uint8, value uint8) error {
|
||||
cc := CCFaderFirst + fader
|
||||
if fader == 8 {
|
||||
cc = CCFaderMain
|
||||
}
|
||||
return o.send(midi.ControlChange(0, cc, value))
|
||||
}
|
||||
|
||||
func (o *Output) SetButtonLED(button uint8, state LEDState) error {
|
||||
return o.send(midi.NoteOn(0, button, uint8(state)))
|
||||
}
|
||||
|
||||
func (o *Output) SetEncoderRing(encoder uint8, value uint8) error {
|
||||
return o.send(midi.ControlChange(0, CCEncoderFirst+encoder, value))
|
||||
}
|
||||
|
||||
func (o *Output) SetMeter(channel uint8, value uint8) error {
|
||||
return o.send(midi.ControlChange(0, CCMeterFirst+channel, value))
|
||||
}
|
||||
|
||||
func (o *Output) SetLCD(lcd uint8, color LCDColor, invertUpper bool, invertLower bool, upper string, lower string) error {
|
||||
cc := uint8(color)
|
||||
if invertUpper {
|
||||
cc |= 0x10
|
||||
}
|
||||
if invertLower {
|
||||
cc |= 0x20
|
||||
}
|
||||
|
||||
upper = padOrTruncate(upper, 7)
|
||||
lower = padOrTruncate(lower, 7)
|
||||
|
||||
data := []byte{0x00, 0x20, 0x32, o.DeviceID, 0x4C, lcd, cc}
|
||||
data = append(data, []byte(upper)...)
|
||||
data = append(data, []byte(lower)...)
|
||||
return o.send(midi.SysEx(data))
|
||||
}
|
||||
|
||||
func padOrTruncate(s string, n int) string {
|
||||
if len(s) > n {
|
||||
return s[:n]
|
||||
}
|
||||
for len(s) < n {
|
||||
s += " "
|
||||
}
|
||||
return s
|
||||
}
|
||||
230
lib/xtouch/xtouch.go
Normal file
230
lib/xtouch/xtouch.go
Normal file
@@ -0,0 +1,230 @@
|
||||
package xtouch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
"gitlab.com/gomidi/midi/v2/drivers"
|
||||
)
|
||||
|
||||
const (
|
||||
DeviceIDXTouch = 0x14
|
||||
DeviceIDExtender = 0x15
|
||||
)
|
||||
|
||||
const (
|
||||
CCFootController = 4
|
||||
CCFootSwitch1 = 64
|
||||
CCFootSwitch2 = 67
|
||||
CCFaderFirst = 70
|
||||
CCFaderLast = 77
|
||||
CCFaderMain = 78
|
||||
CCEncoderFirst = 80
|
||||
CCEncoderLast = 87
|
||||
CCJogWheel = 88
|
||||
CCMeterFirst = 90
|
||||
CCMeterLast = 97
|
||||
)
|
||||
|
||||
const (
|
||||
NoteButtonFirst = 0
|
||||
NoteButtonLast = 103
|
||||
NoteFaderTouchFirst = 110
|
||||
NoteFaderTouchLast = 117
|
||||
NoteFaderTouchMain = 118
|
||||
)
|
||||
|
||||
type Event interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type ButtonEvent struct {
|
||||
Button uint8
|
||||
Pressed bool
|
||||
}
|
||||
|
||||
func (e ButtonEvent) String() string {
|
||||
action := "released"
|
||||
if e.Pressed {
|
||||
action = "pressed"
|
||||
}
|
||||
return fmt.Sprintf("Button %d %s", e.Button, action)
|
||||
}
|
||||
|
||||
type FaderEvent struct {
|
||||
Fader uint8
|
||||
Value uint8
|
||||
}
|
||||
|
||||
func (e FaderEvent) String() string {
|
||||
label := fmt.Sprintf("Fader %d", e.Fader)
|
||||
if e.Fader == 8 {
|
||||
label = "Fader main"
|
||||
}
|
||||
return fmt.Sprintf("%s = %d", label, e.Value)
|
||||
}
|
||||
|
||||
type FaderTouchEvent struct {
|
||||
Fader uint8
|
||||
Touched bool
|
||||
}
|
||||
|
||||
func (e FaderTouchEvent) String() string {
|
||||
label := fmt.Sprintf("Fader %d", e.Fader)
|
||||
if e.Fader == 8 {
|
||||
label = "Fader main"
|
||||
}
|
||||
action := "released"
|
||||
if e.Touched {
|
||||
action = "touched"
|
||||
}
|
||||
return fmt.Sprintf("%s %s", label, action)
|
||||
}
|
||||
|
||||
type EncoderAbsoluteEvent struct {
|
||||
Encoder uint8
|
||||
Value uint8
|
||||
}
|
||||
|
||||
func (e EncoderAbsoluteEvent) String() string {
|
||||
return fmt.Sprintf("Encoder %d = %d", e.Encoder, e.Value)
|
||||
}
|
||||
|
||||
type EncoderRelativeEvent struct {
|
||||
Encoder uint8
|
||||
Delta int
|
||||
}
|
||||
|
||||
func (e EncoderRelativeEvent) String() string {
|
||||
return fmt.Sprintf("Encoder %d %+d", e.Encoder, e.Delta)
|
||||
}
|
||||
|
||||
type JogWheelEvent struct {
|
||||
Clockwise bool
|
||||
}
|
||||
|
||||
func (e JogWheelEvent) String() string {
|
||||
if e.Clockwise {
|
||||
return "Jog wheel CW"
|
||||
}
|
||||
return "Jog wheel CCW"
|
||||
}
|
||||
|
||||
type FootControllerEvent struct {
|
||||
Value uint8
|
||||
}
|
||||
|
||||
func (e FootControllerEvent) String() string {
|
||||
return fmt.Sprintf("Foot controller = %d", e.Value)
|
||||
}
|
||||
|
||||
type FootSwitchEvent struct {
|
||||
Switch uint8
|
||||
Pressed bool
|
||||
}
|
||||
|
||||
func (e FootSwitchEvent) String() string {
|
||||
action := "released"
|
||||
if e.Pressed {
|
||||
action = "pressed"
|
||||
}
|
||||
return fmt.Sprintf("Foot switch %d %s", e.Switch, action)
|
||||
}
|
||||
|
||||
func FindInPort(substr string) (drivers.In, error) {
|
||||
lower := strings.ToLower(substr)
|
||||
for _, port := range midi.GetInPorts() {
|
||||
if strings.Contains(strings.ToLower(port.String()), lower) {
|
||||
return port, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no MIDI input port matching %q", substr)
|
||||
}
|
||||
|
||||
func FindOutPort(substr string) (drivers.Out, error) {
|
||||
lower := strings.ToLower(substr)
|
||||
for _, port := range midi.GetOutPorts() {
|
||||
if strings.Contains(strings.ToLower(port.String()), lower) {
|
||||
return port, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no MIDI output port matching %q", substr)
|
||||
}
|
||||
|
||||
type EncoderMode int
|
||||
|
||||
const (
|
||||
EncoderAbsolute EncoderMode = iota
|
||||
EncoderRelative
|
||||
)
|
||||
|
||||
type Decoder struct {
|
||||
EncoderMode EncoderMode
|
||||
}
|
||||
|
||||
func (d *Decoder) Decode(msg midi.Message) Event {
|
||||
switch {
|
||||
case msg.Is(midi.NoteOnMsg):
|
||||
var channel, key, velocity uint8
|
||||
msg.GetNoteOn(&channel, &key, &velocity)
|
||||
return decodeNoteOn(key, velocity)
|
||||
|
||||
case msg.Is(midi.NoteOffMsg):
|
||||
var channel, key, velocity uint8
|
||||
msg.GetNoteOff(&channel, &key, &velocity)
|
||||
return decodeNoteOn(key, 0)
|
||||
|
||||
case msg.Is(midi.ControlChangeMsg):
|
||||
var channel, controller, value uint8
|
||||
msg.GetControlChange(&channel, &controller, &value)
|
||||
return d.decodeCC(controller, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeNoteOn(key, velocity uint8) Event {
|
||||
pressed := velocity > 0
|
||||
|
||||
if key >= NoteButtonFirst && key <= NoteButtonLast {
|
||||
return ButtonEvent{Button: key, Pressed: pressed}
|
||||
}
|
||||
if key >= NoteFaderTouchFirst && key <= NoteFaderTouchLast {
|
||||
return FaderTouchEvent{Fader: key - NoteFaderTouchFirst, Touched: pressed}
|
||||
}
|
||||
if key == NoteFaderTouchMain {
|
||||
return FaderTouchEvent{Fader: 8, Touched: pressed}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Decoder) decodeCC(controller, value uint8) Event {
|
||||
switch {
|
||||
case controller >= CCFaderFirst && controller <= CCFaderLast:
|
||||
return FaderEvent{Fader: controller - CCFaderFirst, Value: value}
|
||||
case controller == CCFaderMain:
|
||||
return FaderEvent{Fader: 8, Value: value}
|
||||
case controller >= CCEncoderFirst && controller <= CCEncoderLast:
|
||||
enc := controller - CCEncoderFirst
|
||||
if d.EncoderMode == EncoderRelative {
|
||||
delta := 0
|
||||
switch value {
|
||||
case 65:
|
||||
delta = 1
|
||||
case 1:
|
||||
delta = -1
|
||||
}
|
||||
return EncoderRelativeEvent{Encoder: enc, Delta: delta}
|
||||
}
|
||||
return EncoderAbsoluteEvent{Encoder: enc, Value: value}
|
||||
case controller == CCJogWheel:
|
||||
return JogWheelEvent{Clockwise: value == 65}
|
||||
case controller == CCFootController:
|
||||
return FootControllerEvent{Value: value}
|
||||
case controller == CCFootSwitch1:
|
||||
return FootSwitchEvent{Switch: 1, Pressed: value > 0}
|
||||
case controller == CCFootSwitch2:
|
||||
return FootSwitchEvent{Switch: 2, Pressed: value > 0}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user