Class: IoT::Button

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

Overview

Button - simple tactile / push button

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BinaryReceptor

#high?, #low?

Methods inherited from Receptor

#model_name, #read

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

Parameters:

  • value

    the value to set the attribute measure_pause to.



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


84
85
86
# File 'lib/iot/button.rb', line 84

def long_press?(intervals=@intervals)
  intervals.size == 1 && intervals[0][1] >= @long_click
end

#nameObject



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

def name
  @receptor_name
end

#not_pressed?Boolean

Is button not being pressed?

Returns:

  • (Boolean)


31
32
33
# File 'lib/iot/button.rb', line 31

def not_pressed?
  high?
end

#pressed?Boolean

Is button being pressed?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


69
70
71
# File 'lib/iot/button.rb', line 69

def single_press?(intervals=@intervals)
  intervals.size == 1
end

#timeout?Boolean

Returns:

  • (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?

Returns:

  • (Boolean)


79
80
81
# File 'lib/iot/button.rb', line 79

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

#wait_for_pressObject

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

Returns:

  • (Boolean)


39
40
41
# File 'lib/iot/button.rb', line 39

def was_not_pressed?
  !was_pressed?
end

#was_pressed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/iot/button.rb', line 35

def was_pressed?
  @intervals.size > 0
end