feat(GamePad): Implemented mappings

This commit is contained in:
2025-08-04 14:36:35 +02:00
parent 3942b6d8b2
commit 945a78c463
5 changed files with 87 additions and 13 deletions

View File

@@ -1,16 +1,22 @@
package main
import (
"fmt"
"gitlab.com/gomidi/midi/v2"
"github.com/bendahl/uinput"
)
type Mapping interface {
Is(midi.Message) bool
TriggerIfMatch(midi.Message, uinput.Gamepad) error
}
type ButtonMapping struct {
comment string
midiChannel uint8
midiKey uint8
gamepadKey int
}
func (m ButtonMapping) Is(msg midi.Message) bool {
@@ -24,9 +30,36 @@ func (m ButtonMapping) Is(msg midi.Message) bool {
}
}
func (m ButtonMapping) TriggerIfMatch(msg midi.Message, virtGamepad uinput.Gamepad) error {
if m.Is(msg) {
switch msg.Type() {
case midi.NoteOnMsg:
return virtGamepad.ButtonDown(m.gamepadKey)
case midi.NoteOffMsg:
return virtGamepad.ButtonUp(m.gamepadKey)
default:
return fmt.Errorf("Invalid message type triggered ButtonMapping")
}
}
return nil
}
type ControllerAxis int
const (
LeftX ControllerAxis = iota
LeftY
RightX
RightY
)
type ControlMapping struct {
comment string
midiChannel uint8
midiController uint8
axis ControllerAxis
isSigned bool
}
func (m ControlMapping) Is(msg midi.Message) bool {
@@ -38,3 +71,34 @@ func (m ControlMapping) Is(msg midi.Message) bool {
return false
}
}
func (m ControlMapping) TriggerIfMatch(msg midi.Message, virtGamepad uinput.Gamepad) error {
if m.Is(msg) {
var (
valueAbsolute uint8
valueNormalised float32
)
msg.GetControlChange(nil, nil, &valueAbsolute)
// value is 0-127, normalise
valueNormalised = float32(valueAbsolute) / 127
if m.isSigned {
valueNormalised *= 2
valueNormalised -= 1
}
switch m.axis {
case LeftX:
return virtGamepad.LeftStickMoveX(valueNormalised)
case LeftY:
return virtGamepad.LeftStickMoveY(valueNormalised)
case RightX:
return virtGamepad.RightStickMoveX(valueNormalised)
case RightY:
return virtGamepad.RightStickMoveY(valueNormalised)
}
}
return nil
}