diff --git a/config.go b/config.go index a7002ab..62627ab 100644 --- a/config.go +++ b/config.go @@ -6,6 +6,7 @@ import ( "github.com/goccy/go-yaml" "github.com/bendahl/uinput" + "github.com/charmbracelet/log" ) type Config struct { @@ -28,6 +29,7 @@ type MappingConfig struct { Button ButtonName `yaml:"button"` Axis AxisName `yaml:"axis"` IsSigned bool `yaml:"isSigned"` + Deadzone float64 `yaml:"deadzone"` } type MappingType string @@ -116,6 +118,8 @@ func (mc MappingConfig) Construct() (Mapping, error) { return ButtonMapping{}, err } + log.Debug("Parsed button mapping", "comment", mc.Comment, "midiChannel", mc.MidiChannel, "midiKey", mc.MidiKey, "button", button) + return ButtonMapping{mc.Comment, mc.MidiChannel, mc.MidiKey, button}, nil case ControlMappingType: axis, err := mc.Axis.Construct() @@ -123,7 +127,9 @@ func (mc MappingConfig) Construct() (Mapping, error) { return ControlMapping{}, err } - return ControlMapping{mc.Comment, mc.MidiChannel, mc.MidiController, axis, mc.IsSigned}, nil + log.Debug("Parsed control mapping", "comment", mc.Comment, "midiChannel", mc.MidiChannel, "midiController", mc.MidiController, "axis", axis, "isSigned", mc.IsSigned, "deadzone", mc.Deadzone) + + return ControlMapping{mc.Comment, mc.MidiChannel, mc.MidiController, axis, mc.IsSigned, mc.Deadzone}, nil default: return ButtonMapping{}, fmt.Errorf("Invalid mapping type") } diff --git a/config.yaml b/config.yaml index 394180b..a9d3f63 100644 --- a/config.yaml +++ b/config.yaml @@ -39,9 +39,11 @@ controller: midiController: 1 axis: left-x isSigned: true + deadzone: 0.01 - comment: Filter right type: control midiChannel: 2 midiController: 1 axis: right-x isSigned: true + deadzone: 0.01 diff --git a/mapping.go b/mapping.go index 013b312..a5b9ebe 100644 --- a/mapping.go +++ b/mapping.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math" "github.com/charmbracelet/log" "gitlab.com/gomidi/midi/v2" @@ -39,12 +40,12 @@ func (m ButtonMapping) TriggerIfMatch(msg midi.Message, virtGamepad uinput.Gamep switch msg.Type() { case midi.NoteOnMsg: if velocity != 0 { - log.Debugf("%s: Button down", m.comment) + log.Debug(m.comment, "status", "down") return virtGamepad.ButtonDown(m.gamepadKey) } fallthrough // if reached here, velocity is 0 -> NoteOff case midi.NoteOffMsg: - log.Debugf("%s: Button up", m.comment) + log.Debug(m.comment, "status", "up") return virtGamepad.ButtonUp(m.gamepadKey) default: return fmt.Errorf("Invalid message type triggered ButtonMapping") @@ -73,6 +74,7 @@ type ControlMapping struct { midiController uint8 axis ControllerAxis isSigned bool + deadzone float64 } func (m ControlMapping) Is(msg midi.Message) bool { @@ -89,29 +91,33 @@ func (m ControlMapping) TriggerIfMatch(msg midi.Message, virtGamepad uinput.Game if m.Is(msg) { var ( valueAbsolute uint8 - valueNormalised float32 + valueNormalised float64 ) msg.GetControlChange(nil, nil, &valueAbsolute) // value is 0-127, normalise - valueNormalised = float32(valueAbsolute) / 127 + valueNormalised = float64(valueAbsolute) / 127 if m.isSigned { valueNormalised *= 2 valueNormalised -= 1 } - log.Debugf("%s: value %v", m.comment, valueNormalised) + if math.Abs(valueNormalised) < m.deadzone { + valueNormalised = 0 + } + + log.Debug(m.comment, "value", valueNormalised, "deadzone", m.deadzone) switch m.axis { case LeftX: - return virtGamepad.LeftStickMoveX(valueNormalised) + return virtGamepad.LeftStickMoveX(float32(valueNormalised)) case LeftY: - return virtGamepad.LeftStickMoveY(valueNormalised) + return virtGamepad.LeftStickMoveY(float32(valueNormalised)) case RightX: - return virtGamepad.RightStickMoveX(valueNormalised) + return virtGamepad.RightStickMoveX(float32(valueNormalised)) case RightY: - return virtGamepad.RightStickMoveY(valueNormalised) + return virtGamepad.RightStickMoveY(float32(valueNormalised)) } }