Class: IoT::Button
- Inherits:
-
BinaryReceptor
- Object
- Receptor
- BinaryReceptor
- IoT::Button
- Defined in:
- lib/iot/button.rb
Overview
Button - simple tactile / push button
Instance Attribute Summary collapse
-
#measure_pause ⇒ Object
writeonly
Sets the attribute measure_pause.
Instance Method Summary collapse
-
#double_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed twice?.
-
#initialize(pin) ⇒ Button
constructor
A new instance of Button.
-
#long_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed for more than @long_click?.
- #name ⇒ Object
-
#not_pressed? ⇒ Boolean
Is button not being pressed?.
-
#pressed? ⇒ Boolean
Is button being pressed?.
- #set_timeout(timeout) ⇒ Object
-
#single_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed once?.
- #timeout? ⇒ Boolean
-
#triple_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed 3 times?.
-
#wait_for_press ⇒ Object
Wait for the 1st pressing of button.
-
#wait_for_presses(attempts = 2, timeout = @timeout) ⇒ Object
Wait for several sequential presses.
- #was_not_pressed? ⇒ Boolean
- #was_pressed? ⇒ Boolean
Methods inherited from BinaryReceptor
Methods inherited from Receptor
Constructor Details
#initialize(pin) ⇒ Button
Returns a new instance of Button.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/iot/button.rb', line 8 def initialize(pin) @receptor_name = 'BUTTON' super(pin) @measure_pause = 0.01 @long_click = 1.0 @short_interval = 0.2 @timeout = 30 @intervals = [] # samples: wait time press time wait time press time # double press: [[1.486804292, 0.090814152], [0.131284967, 0.13125429]] # long press: [[0.910878286, 1.109352219]] end |
Instance Attribute Details
#measure_pause=(value) ⇒ Object (writeonly)
Sets the attribute measure_pause
6 7 8 |
# File 'lib/iot/button.rb', line 6 def measure_pause=(value) @measure_pause = value end |
Instance Method Details
#double_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed twice?
74 75 76 |
# File 'lib/iot/button.rb', line 74 def double_press?(intervals=@intervals) intervals.size == 2 && intervals[1][0] <= @short_interval end |
#long_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed for more than @long_click?
84 85 86 |
# File 'lib/iot/button.rb', line 84 def long_press?(intervals=@intervals) intervals.size == 1 && intervals[0][1] >= @long_click end |
#name ⇒ Object
21 22 23 |
# File 'lib/iot/button.rb', line 21 def name @receptor_name end |
#not_pressed? ⇒ Boolean
Is button not being pressed?
31 32 33 |
# File 'lib/iot/button.rb', line 31 def not_pressed? high? end |
#pressed? ⇒ Boolean
Is button being pressed?
26 27 28 |
# File 'lib/iot/button.rb', line 26 def pressed? low? end |
#set_timeout(timeout) ⇒ Object
88 89 90 |
# File 'lib/iot/button.rb', line 88 def set_timeout(timeout) @timeout = timeout end |
#single_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed once?
69 70 71 |
# File 'lib/iot/button.rb', line 69 def single_press?(intervals=@intervals) intervals.size == 1 end |
#timeout? ⇒ Boolean
92 93 94 95 96 97 98 |
# File 'lib/iot/button.rb', line 92 def timeout? min_wait_time = 60.0*60.0*60.00 @intervals.each do |interval| min_wait_time = [min_wait_time, interval[0]].min end min_wait_time > @timeout end |
#triple_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed 3 times?
79 80 81 |
# File 'lib/iot/button.rb', line 79 def triple_press?(intervals=@intervals) intervals.size == 3 end |
#wait_for_press ⇒ Object
Wait for the 1st pressing of button
44 45 46 |
# File 'lib/iot/button.rb', line 44 def wait_for_press wait_for_presses(1) end |
#wait_for_presses(attempts = 2, timeout = @timeout) ⇒ Object
Wait for several sequential presses
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/iot/button.rb', line 49 def wait_for_presses(attempts=2, timeout=@timeout) @intervals = [] attempts.times do interval = [] timer = Time.now while not_pressed? && (Time.now-timer) < timeout do sleep @measure_pause end interval << Time.now-timer timer = Time.now while pressed? do sleep @measure_pause end interval << Time.now-timer @intervals << interval end @intervals end |
#was_not_pressed? ⇒ Boolean
39 40 41 |
# File 'lib/iot/button.rb', line 39 def was_not_pressed? !was_pressed? end |
#was_pressed? ⇒ Boolean
35 36 37 |
# File 'lib/iot/button.rb', line 35 def was_pressed? @intervals.size > 0 end |