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_THERESHOLD =
0.7

Instance Method Summary collapse

Methods inherited from Detector

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

Methods inherited from Base

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

Methods included from CustomProcess

#fork

Instance Method Details

#create_oneshot_index(finger:) ⇒ Config::Index

Parameters:

  • finger (Integer)

Returns:



72
73
74
75
76
77
78
79
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 72

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:



83
84
85
86
87
88
89
90
91
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 83

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



20
21
22
23
24
25
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
# File 'lib/fusuma/plugin/detectors/hold_detector.rb', line 20

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

  hold_events = hold_buffer.events

  timer_buffer = buffers.find { |b| b.type == 'timer' }
  timer_events = timer_buffer.events

  finger = hold_buffer.finger
  holding_time = calc_holding_time(hold_events: hold_events, timer_events: timer_events)

  @timeout ||= nil
  status = case hold_events.last.record.status
           when 'begin'
             if holding_time.zero?
               'begin'
             else
               'timer'
             end
           when 'cancelled'
             'cancelled'
           when 'end'
             'end'
           else
             last_record = hold_events.last.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)

  @timeout = nil if status == 'begin'

  if 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