Class: WallE::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/wall_e/components/button.rb

Instance Method Summary collapse

Constructor Details

#initialize(pin, hold_time = 0.7) ⇒ Button

Public: Initialize a Button.

pin - the Pin the Button is attached to. hold_time - the Integer seconds to indicate that the button is being held (default: 0.07).



8
9
10
11
12
13
14
# File 'lib/wall_e/components/button.rb', line 8

def initialize(pin, hold_time = 0.7)
  @pin = pin
  @pin.set_mode(Pin::INPUT)
  @pin.start_reporting
  @hold_time = hold_time
  @pin.on("digital-read-#{@pin.number}", method(:handler))
end

Instance Method Details

#handler(value) ⇒ Object

Internal: The handler for the Pin’s read event.

value - the Integer returned from the read event.

Returns nothing.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wall_e/components/button.rb', line 48

def handler(value)
  if value.zero?
    @up_callback.call if @up_callback
  else
    @last_press = Time.now
    @down_callback.call if @down_callback
  end

  if value.zero? && (Time.now - @last_press) > @hold_time
    @hold_callback.call if @hold_callback
  end
end

#held(&block) ⇒ Object

Public: What to do when the button is held.

block - the block that will be run when the button is held.

Returns nothing.



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

def held(&block)
  @hold_callback = block
end

#pressed(&block) ⇒ Object

Public: What to do when the button is pressed.

block - the block that will run when the button is pressed.

Returns nothing.



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

def pressed(&block)
  @down_callback = block
end

#released(&block) ⇒ Object

Public: What to do when the button is released.

block - the block that will run when the button is released.

Returns nothing.



30
31
32
# File 'lib/wall_e/components/button.rb', line 30

def released(&block)
  @up_callback = block
end