Completed implementation.

This commit is contained in:
Anna Rose Wiggins 2025-07-15 15:27:49 -04:00
parent 0915ea059a
commit 58abd4cc34
10 changed files with 260 additions and 66 deletions

View file

@ -4,6 +4,7 @@ import (
"time"
"github.com/holoplot/go-evdev"
"github.com/jonboulle/clockwork"
)
// MappingRuleAxisToButton represents a rule that converts an axis input into a (potentially repeating)
@ -16,7 +17,10 @@ type MappingRuleAxisToButton struct {
RepeatRateMax int
nextEvent time.Duration
lastEvent time.Time
pressed bool
repeat bool
pressed bool // "pressed" indicates that we've sent the output button signal, but still need to send the button release
active bool // "active" is true whenever the input is not in a deadzone
clock clockwork.Clock
}
func NewMappingRuleAxisToButton(base MappingRuleBase, input *RuleTargetAxis, output *RuleTargetButton, repeatRateMin, repeatRateMax int) *MappingRuleAxisToButton {
@ -28,7 +32,10 @@ func NewMappingRuleAxisToButton(base MappingRuleBase, input *RuleTargetAxis, out
RepeatRateMax: repeatRateMax,
lastEvent: time.Now(),
nextEvent: NoNextEvent,
repeat: repeatRateMin != 0 && repeatRateMax != 0,
pressed: false,
active: false,
clock: clockwork.NewRealClock(),
}
}
@ -42,13 +49,16 @@ func (rule *MappingRuleAxisToButton) MatchEvent(device RuleTargetDevice, event *
// If we're inside the deadzone, unset the next event
if rule.Input.InDeadZone(event.Value) {
rule.nextEvent = NoNextEvent
rule.active = false
return nil, nil
}
// If we aren't repeating, we trigger the event immediately
// TODO: we aren't using pressed correctly; that should be set *and released* in here...
if rule.RepeatRateMin == 0 || rule.RepeatRateMax == 0 {
rule.nextEvent = time.Millisecond
// We also only set this if active == false, so that only one
// event can be emitted per "active" period
if !rule.repeat && !rule.active {
rule.nextEvent = 0
rule.active = true
return nil, nil
}
@ -56,6 +66,7 @@ func (rule *MappingRuleAxisToButton) MatchEvent(device RuleTargetDevice, event *
strength := 1.0 - rule.Input.GetAxisStrength(event.Value)
rate := int64(LerpInt(rule.RepeatRateMax, rule.RepeatRateMin, strength))
rule.nextEvent = time.Duration(rate * int64(time.Millisecond))
rule.active = true
return nil, nil
}
@ -63,21 +74,30 @@ func (rule *MappingRuleAxisToButton) MatchEvent(device RuleTargetDevice, event *
// TimerEvent returns an event when enough time has passed (compared to the last recorded axis value)
// to emit an event.
func (rule *MappingRuleAxisToButton) TimerEvent() *evdev.InputEvent {
// If we pressed the button last tick, release it
// If we pressed the button last tick, release it before doing anything else
if rule.pressed {
rule.pressed = false
return rule.Output.CreateEvent(0, nil)
}
// This indicates that we should not emit another event
// If we should not emit another event,
// we just update lastEvent for station keeping
if rule.nextEvent == NoNextEvent {
rule.lastEvent = time.Now()
rule.lastEvent = rule.clock.Now()
return nil
}
if time.Now().Compare(rule.lastEvent.Add(rule.nextEvent)) > -1 {
rule.lastEvent = time.Now()
if rule.clock.Now().Compare(rule.lastEvent.Add(rule.nextEvent)) > -1 {
rule.lastEvent = rule.clock.Now()
rule.pressed = true
// The default case here is to leave nextEvent at whatever
// it has been set to by MatchEvent. Since nextEvent is a delta,
// this will naturally cause the repeat to happen
if !rule.repeat {
rule.nextEvent = NoNextEvent
}
return rule.Output.CreateEvent(1, nil)
}