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.



4263
4264
4265
# File 'lib/echoes/gui.rb', line 4263

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.



4270
4271
4272
4273
4274
4275
4276
# File 'lib/echoes/gui.rb', line 4270

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



4278
4279
4280
# File 'lib/echoes/gui.rb', line 4278

def reset
  @samples.clear
end