Class: Echoes::ShakeDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/echoes/gui.rb

Overview

Detect a “shake to find pointer” gesture from a stream of mouse samples. Looks at the trailing WINDOW seconds of motion: a shake is several quick direction reversals over a meaningful distance. Tuned for casual wrist-shakes; not a substitute for the OS’s own accessibility feature, which kicks in for the visible system cursor regardless of what apps are doing.

Constant Summary collapse

WINDOW =

seconds of history to consider

0.5
MIN_REVS =

direction reversals required

3
MIN_PATH =

cumulative pixels of motion required

150.0

Instance Method Summary collapse

Constructor Details

#initializeShakeDetector

Returns a new instance of ShakeDetector.



2817
2818
2819
# File 'lib/echoes/gui.rb', line 2817

def initialize
  @samples = []
end

Instance Method Details

#observe(t, x, y) ⇒ Object

Add a (time, x, y) sample. Returns true on the call where a shake first crosses the threshold; callers should treat this as edge-triggered and follow up with #reset.



2824
2825
2826
2827
2828
2829
2830
# File 'lib/echoes/gui.rb', line 2824

def observe(t, x, y)
  @samples << [t, x, y]
  cutoff = t - WINDOW
  @samples.shift while @samples.first && @samples.first[0] < cutoff
  return false if @samples.size < 4
  detect
end

#resetObject



2832
2833
2834
# File 'lib/echoes/gui.rb', line 2832

def reset
  @samples.clear
end