Class: RubyBuzz::Button

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

Overview

Handles a single button on the Buzz controllers.

Identified by event code, all between 704 and 723. Also identifiable by pad and name.

Possible names:

  • buzz

  • yellow

  • green

  • orange

  • blue

Initialized in RubyBuzz::Pad.init_mappings

Each button holds an array of events, each of which is a Proc to be called without arguments. These are called when the button is pushed.

Constant Summary collapse

@@buttons =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name, pad) ⇒ Button

Initialize a button

called in RubyBuzz::Pad.init_mappings

Arguments:

  • code - Integer, the evnt code generated by this button (704-723)

  • name - Symbol, the name of the button, for referncing via the Pad object

  • pad - RubyBuzz::Pad, the object this button belongs to



40
41
42
43
44
45
46
# File 'lib/ruby_buzz/button.rb', line 40

def initialize(code, name, pad)
  @code = code
  @name = name
  @pad = pad
  @events = []
  @@buttons << self
end

Instance Attribute Details

#codeObject

events will be an array of lambdas



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

def code
  @code
end

#eventsObject

events will be an array of lambdas



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

def events
  @events
end

#nameObject

events will be an array of lambdas



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

def name
  @name
end

Class Method Details

.find(code) ⇒ Object

Find a button by its event code. Used by trigger_key to find a button when one is pushed.

Arguments:

  • code - Integer, event code to retrieve button by.



56
57
58
# File 'lib/ruby_buzz/button.rb', line 56

def self.find(code)
  @@buttons.detect { |b| b.code == code }
end

.trigger_key(code) ⇒ Object

Find a button and run all it’s events.

Used by RubyBuzz::Device when button is pushed.

Arguments:

  • code - Integer, event code to retrieve button by.



89
90
91
92
# File 'lib/ruby_buzz/button.rb', line 89

def self.trigger_key(code)
  btn = self.find(code)
  btn.trigger_events
end

Instance Method Details

#add_event(proc) ⇒ Object

Add a process to be triggered when this button is pressed.

Arguments:

  • proc - Proc, ruby method to be called, without arguments on button press.



67
68
69
# File 'lib/ruby_buzz/button.rb', line 67

def add_event(proc)
  @events << proc
end

#trigger_eventsObject

Trigger every proc in the @events array.



74
75
76
77
78
# File 'lib/ruby_buzz/button.rb', line 74

def trigger_events
  @events.each do |event|
    event.call
  end
end