Class: Frankenpins::Button

Inherits:
Object
  • Object
show all
Includes:
Unobservable::Support
Defined in:
lib/frankenpins/button.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Button

Returns a new instance of Button.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/frankenpins/button.rb', line 11

def initialize(options)
  options[:pull] = :up unless options.has_key?(:pull)

  @pin = Frankenpins::Pin.new(options)
  @pin.watch do |pin|
    if pin.value == 0
      raise_event :pressed
    elsif pin.value == 1
      raise_event :released
    end

    # Always fire a changed event
    raise_event :changed
  end
end

Instance Method Details

#when(event_name, &block) ⇒ Object



27
28
29
# File 'lib/frankenpins/button.rb', line 27

def when(event_name, &block)
  send(event_name).register(&block)
end