Class: Button

Inherits:
BinaryReceptor show all
Defined in:
lib/button.rb

Overview

Button - simple push button

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BinaryReceptor

#high?, #low?

Methods inherited from Receptor

#model_name, #name

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

Parameters:

  • value

    the value to set the attribute measure_pause to.



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


22
23
24
# File 'lib/button.rb', line 22

def not_pressed?
  high?
end

#pressed?Boolean

Is button pressed?

Returns:

  • (Boolean)


17
18
19
# File 'lib/button.rb', line 17

def pressed?
  low?
end

#single_press?(intervals = @intervals) ⇒ Boolean

Was the button pressed once?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


62
63
64
# File 'lib/button.rb', line 62

def triple_press?(intervals=@intervals)
  intervals.size == 3
end

#wait_for_pressObject

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