Class: Fusuma::Plugin::Detectors::HoldDetector

Inherits:
Detector
  • Object
show all
Defined in:
lib/fusuma/plugin/detectors/hold_detector.rb

Overview

Detect Hold gesture

Constant Summary collapse

SOURCES =
%w[gesture timer].freeze
BUFFER_TYPE =
"gesture"
GESTURE_RECORD_TYPE =
"hold"
BASE_THRESHOLD =
0.7

Instance Attribute Summary

Attributes inherited from Detector

#tag, #type

Instance Method Summary collapse

Methods inherited from Detector

#create_event, #first_time?, #last_time, #sources, tag, type, #watch?

Methods inherited from Base

#config_index, #config_param_types, #config_params, inherited, plugins, #shutdown

Constructor Details

#initialize(*args) ⇒ HoldDetector

Returns a new instance of HoldDetector.



17
18
19
20
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 17

def initialize(*args)
  super
  @timer = Inputs::TimerInput.instance
end

Instance Method Details

#create_oneshot_index(finger:) ⇒ Config::Index

Parameters:

  • finger (Integer)

Returns:



80
81
82
83
84
85
86
87
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 80

def create_oneshot_index(finger:)
  Config::Index.new(
    [
      Config::Index::Key.new(type),
      Config::Index::Key.new(finger.to_i)
    ]
  )
end

#create_repeat_index(finger:, status:) ⇒ Config::Index

Parameters:

  • finger (Integer)

Returns:



91
92
93
94
95
96
97
98
99
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 91

def create_repeat_index(finger:, status:)
  Config::Index.new(
    [
      Config::Index::Key.new(type),
      Config::Index::Key.new(finger.to_i),
      Config::Index::Key.new(status)
    ]
  )
end

#detect(buffers) ⇒ Events::Event, ...

Parameters:

Returns:

  • (Events::Event)

    if event is detected

  • (Array<Events::Event>)

    if hold end event is detected

  • (NilClass)

    if event is NOT detected



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 26

def detect(buffers)
  hold_buffer = find_hold_buffer(buffers)
  return if hold_buffer.empty?

  last_hold = hold_buffer.events.last

  timer_buffer = buffers.find { |b| b.type == "timer" }
  last_timer = timer_buffer.events.last

  finger = hold_buffer.finger
  holding_time = calc_holding_time(hold_events: hold_buffer.events, last_timer: last_timer)

  status = case last_hold.record.status
  when "begin"
    if holding_time.zero?
      "begin"
    else
      "timer"
    end
  when "cancelled"
    "cancelled"
  when "end"
    "end"
  else
    last_record = last_hold.record.status
    raise "Unexpected Status:#{last_record.status} in #{last_record}"
  end

  repeat_index = create_repeat_index(finger: finger, status: status)
  oneshot_index = create_oneshot_index(finger: finger)

  if status == "begin"
    @timeout = nil
    if threshold(index: oneshot_index) < @timer.interval
      @timer.wake_early(Time.now + threshold(index: oneshot_index))
    end
  elsif status == "timer"
    return if @timeout

    return unless enough?(index: oneshot_index, holding_time: holding_time)

    @timeout = holding_time
    return create_event(record: Events::Records::IndexRecord.new(
      index: oneshot_index, trigger: :oneshot
    ))
  end

  create_event(record: Events::Records::IndexRecord.new(
    index: repeat_index, trigger: :repeat
  ))
end