-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton_input.rb
45 lines (39 loc) · 1.07 KB
/
button_input.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
require "concurrent-edge"
require_relative "./input_event"
class ButtonInput
LONG_PRESS_DURATION = 1
DEBOUNCE_DELAY = 0.1
def initialize(left_button_pin, right_button_pin)
@inputs = Concurrent::Channel.new(capacity: 1000)
connect(left_button_pin){ |action| add_input_event(action, :left) }
connect(right_button_pin){ |action| add_input_event(action, :right) }
end
def get(block: true)
if block
@inputs.take
else
@inputs.poll
end
end
private
def add_input_event(action, side)
if action == :click
@inputs.put(InputEvent.new(side))
else
@inputs.put(InputEvent.new(side, :undo))
end
end
def connect(button_pin_nr)
pin = UntroubledPiPiper::Pin.new(pin: button_pin_nr, direction: :in, pull: :up)
UntroubledPiPiper.after pin: button_pin_nr, goes: :down do
start_time = Time.now
event = loop do
break :long_press if Time.now - start_time >= LONG_PRESS_DURATION
if pin.read == 1 # button has been released
break :click
end
end
yield event
end
end
end