touch

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package touch provides touch input handling and gesture recognition.

Package touch provides zone-aware touch input handling and gesture recognition.

Package touch provides HID touch input reading with velocity-aware smoothing (One-Euro filter).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDeviceDisconnected = errors.New("device disconnected")
)

Common errors

View Source
var NewUSBTouchReader = NewHIDTouchReader

NewUSBTouchReader is an alias for NewHIDTouchReader.

Functions

This section is empty.

Types

type DeviceReader

type DeviceReader interface {
	IsConnected() bool
	ReadTouch(ctx context.Context) ([]Event, error)
}

DeviceReader is an interface for reading touch events from a device. This allows the handler to work with any device implementation without importing the device package (breaking circular dependency).

type Event

type Event struct {
	Button        int           // Button identifier (0-4 for 5 buttons)
	Pressed       bool          // true if pressed, false if released
	Duration      time.Duration // How long the button was held
	SwipeProgress float32       // Live swipe progress (0.0 to 1.0)
	SwipeActive   bool          // true if swipe is in progress (live tracking)
	Velocity      float32       // Swipe velocity in pixels/second (for completed swipes)
	SwipePixels   int           // Signed pixel delta from gesture start (left is negative)
	Timestamp     time.Time     // When this event was captured
	TapX          int           // Display pixel X position of tap (0–639); only valid for Button==0 taps
	SlideX        int           // Net horizontal movement during the press (px, signed); non-zero means finger slid on lift
}

Event represents a touch/button input event from the device.

type HIDTouchReader

type HIDTouchReader struct {
	// contains filtered or unexported fields
}

func NewHIDTouchReader

func NewHIDTouchReader(device TouchDevice, logger *slog.Logger) *HIDTouchReader

NewHIDTouchReader creates a new touch reader.

func (*HIDTouchReader) Read

func (t *HIDTouchReader) Read(ctx context.Context) ([]Event, error)

Read reads and processes touch events from HID input reports

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler processes touch events and dispatches them to zone-aware actions.

func NewHandler

func NewHandler(logger *slog.Logger, dev DeviceReader, zm *zone.Manager) *Handler

NewHandler creates a new touch handler.

func (*Handler) GetLastTouchTime

func (h *Handler) GetLastTouchTime() time.Time

GetLastTouchTime returns the timestamp of the last touch event.

func (*Handler) SetSwipeEnabled

func (h *Handler) SetSwipeEnabled(enabled bool)

SetSwipeEnabled enables or disables swipe gesture recognition.

func (*Handler) SetTapEnabled

func (h *Handler) SetTapEnabled(enabled bool)

SetTapEnabled enables or disables tap gesture recognition.

func (*Handler) Start

func (h *Handler) Start(ctx context.Context) error

Start begins processing touch events.

type SwipeConfig

type SwipeConfig struct {

	// VelocityFastFlick is the velocity threshold for "fast flick" behavior.
	VelocityFastFlick float32

	// VelocityFlick is a higher tier — very strong flick, commit with minimal distance.
	VelocityFlick float32

	// VelocityMedium is the boundary between medium and slow velocity.
	VelocityMedium float32

	// DistanceAutoCancel is the minimum threshold below which swipes always cancel,
	// regardless of velocity. This prevents accidental triggers.
	// Default: 0.15 (15% of screen width ≈ 96px on 640px screen)
	DistanceAutoCancel float32

	// DistanceAutoCommit is the threshold above which swipes always commit,
	// regardless of velocity. Dragging past halfway shows clear intent.
	// Default: 0.50 (50% of screen width ≈ 320px on 640px screen)
	DistanceAutoCommit float32

	// DistanceStandard is the threshold for medium-velocity swipes.
	// This is the "normal" swipe distance for typical gestures.
	// Default: 0.30 (30% of screen width ≈ 192px on 640px screen)
	DistanceStandard float32

	// DistanceFastFlick is the reduced threshold for high-velocity swipes.
	DistanceFastFlick float32

	// DistanceFlick is the threshold for very strong flicks (VelocityFlick tier).
	DistanceFlick float32

	// DistanceSlowDrag is the increased threshold for low-velocity swipes.
	DistanceSlowDrag float32
}

SwipeConfig contains tunable parameters for intelligent swipe gesture detection. These parameters control the multi-heuristic decision making that determines whether a swipe should commit to a page change or cancel back to the current page.

func DefaultSwipeConfig

func DefaultSwipeConfig() SwipeConfig

DefaultSwipeConfig returns a SwipeConfig with sensible defaults based on iOS/Android gesture behavior research and tuned for 640px Nexus screen. Note: Velocity thresholds are calibrated for the smaller screen size - actual swipe velocities on this device range from ~150-400 px/s for typical gestures.

type TouchDevice

type TouchDevice interface {
	ReadTouch(buf []byte, timeoutMs uint) (int, error)
}

TouchDevice is satisfied by any USB handle that can read touch packets.

type ZoneTapDetector

type ZoneTapDetector struct {
	// contains filtered or unexported fields
}

ZoneTapDetector determines which zone was tapped based on touch coordinates.

func NewZoneTapDetector

func NewZoneTapDetector(zones map[string]*zone.Zone) *ZoneTapDetector

NewZoneTapDetector creates a new zone tap detector.

func (*ZoneTapDetector) DetectZone

func (d *ZoneTapDetector) DetectZone(x int) (string, bool)

DetectZone determines which zone was tapped based on X coordinate. Returns the zone ID and true if found, or empty string and false if not found.

func (*ZoneTapDetector) UpdateZones

func (d *ZoneTapDetector) UpdateZones(zones map[string]*zone.Zone)

UpdateZones updates the zone map (called when pages change).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL