Refactor mapping rules.

This commit is contained in:
Anna Rose Wiggins 2025-07-06 15:46:48 -04:00
parent b9d02e6482
commit 08fc828b46
6 changed files with 176 additions and 165 deletions

View file

@ -0,0 +1,54 @@
package mappingrules
import (
"time"
"github.com/holoplot/go-evdev"
)
// TODO: How are we going to implement this? It needs to operate on a timer...
type MappingRuleProportionalAxis struct {
MappingRuleBase
Input *RuleTargetAxis
Output *RuleTargetButton
Sensitivity int32
LastValue int32
LastEvent time.Time
}
func (rule *MappingRuleProportionalAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) *evdev.InputEvent {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil
}
if device != rule.Input.GetDevice() ||
event.Code != rule.Input.GetCode() {
return nil
}
// set the last value to the normalized input value
rule.LastValue = rule.Input.NormalizeValue(event.Value)
return nil
}
// TimerEvent returns an event when enough time has passed (compared to the last recorded axis value)
// to emit an event.
func (rule *MappingRuleProportionalAxis) TimerEvent() *evdev.InputEvent {
// This is tighter coupling than we'd like, but it will do for now.
// TODO: maybe it would be better to just be more declarative about event types and their inputs and outputs.
if rule.LastValue < rule.Input.AxisStart {
rule.LastEvent = time.Now()
return nil
}
// calculate target time until next event press
// nextEvent := rule.LastEvent + (rule.LastValue)
// TODO: figure out what the condition should be
if false {
// TODO: emit event
rule.LastEvent = time.Now()
}
return nil
}