Class: Button
- Inherits:
-
BinaryReceptor
- Object
- Receptor
- BinaryReceptor
- Button
- Defined in:
- lib/button.rb
Overview
Button - simple 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?.
-
#not_pressed? ⇒ Boolean
Is button not pressed?.
-
#pressed? ⇒ Boolean
Is button pressed?.
-
#single_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed once?.
-
#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) ⇒ Object
Wait for several sequential presses.
Methods inherited from BinaryReceptor
Methods inherited from Receptor
Constructor Details
#initialize(pin) ⇒ Button
Returns a new instance of Button.
7 8 9 10 11 12 13 14 |
# File 'lib/button.rb', line 7 def initialize(pin) receptor_name = 'BUTTON' super(pin) @measure_pause = 0.01 @long_click = 1.0 @short_interval = 0.2 @intervals = [] end |
Instance Attribute Details
#measure_pause=(value) ⇒ Object (writeonly)
Sets the attribute measure_pause
5 6 7 |
# File 'lib/button.rb', line 5 def measure_pause=(value) @measure_pause = value end |
Instance Method Details
#double_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed twice?
57 58 59 |
# File 'lib/button.rb', line 57 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?
67 68 69 |
# File 'lib/button.rb', line 67 def long_press?(intervals=@intervals) intervals.size == 1 && intervals[0][1] >= @long_click end |
#not_pressed? ⇒ Boolean
Is button not pressed?
22 23 24 |
# File 'lib/button.rb', line 22 def not_pressed? high? end |
#pressed? ⇒ Boolean
Is button pressed?
17 18 19 |
# File 'lib/button.rb', line 17 def pressed? low? end |
#single_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed once?
52 53 54 |
# File 'lib/button.rb', line 52 def single_press?(intervals=@intervals) intervals.size == 1 end |
#triple_press?(intervals = @intervals) ⇒ Boolean
Was the button pressed 3 times?
62 63 64 |
# File 'lib/button.rb', line 62 def triple_press?(intervals=@intervals) intervals.size == 3 end |
#wait_for_press ⇒ Object
Wait for the 1st pressing of button
27 28 29 |
# File 'lib/button.rb', line 27 def wait_for_press wait_for_presses(1) end |
#wait_for_presses(attempts = 2) ⇒ Object
Wait for several sequential presses
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/button.rb', line 32 def wait_for_presses(attempts=2) @intervals = [] attempts.times do interval = [] timer = Time.now while not_pressed? 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 |