Class: Shmidi::Button

Inherits:
Control show all
Defined in:
lib/shmidi/controls/button.rb

Constant Summary collapse

CTYPE =
:BUT

Instance Attribute Summary collapse

Attributes inherited from Control

#channel, #note

Attributes included from Base

#_attachments, #_deleted, #_id, #_rev, #version

Instance Method Summary collapse

Methods inherited from Control

#id, #socket

Methods included from Base

#[], #[]=, #clone, #dump, #etag, included, #inspect, #to_hash, #to_s

Constructor Details

#initialize(socket, channel, note, pressed = false) ⇒ Button

Returns a new instance of Button.



8
9
10
11
12
# File 'lib/shmidi/controls/button.rb', line 8

def initialize(socket, channel, note, pressed = false)
  super(socket, channel, note)
  @pressed = pressed
  init
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



6
7
8
# File 'lib/shmidi/controls/button.rb', line 6

def counter
  @counter
end

Instance Method Details

#initObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shmidi/controls/button.rb', line 14

def init
  @counter = 0
  @__on_press = []
  socket.on_event(channel, :on, note) do |event|
    @pressed = true
    @counter += 1 # intentionaly before handlers
    Shmidi.TRACE("#{CTYPE}\t#{id}\tPRESSED\t#{event.value}")
    @__on_press.each { |b| b.call(self) }
  end

  @on_release = []
  socket.on_event(channel, :off, note) do |event|
    @pressed = false
    Shmidi.TRACE("#{CTYPE}\t#{id}\tRELEASE\t#{event.value}")
    @on_release.each { |b| b.call(self) }
    @counter += 1 # intentionaly after handlers
  end
end

#on_press(&block) ⇒ Object



40
41
42
# File 'lib/shmidi/controls/button.rb', line 40

def on_press(&block)
  @__on_press << block
end

#on_release(&block) ⇒ Object



47
48
49
# File 'lib/shmidi/controls/button.rb', line 47

def on_release(&block)
  @on_release << block
end

#pressed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/shmidi/controls/button.rb', line 37

def pressed?
  !!@pressed
end

#released?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/shmidi/controls/button.rb', line 44

def released?
  !@pressed
end

#resetObject



33
34
35
# File 'lib/shmidi/controls/button.rb', line 33

def reset
  @pressed = false
end