Class: RubyHid::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_hid/observer.rb

Overview

Observer parent class, shared code used by all the controller observer classes:

  • Button

Shared functionality keeps an in-memory array of each controller event which has been referred to.

Direct Known Subclasses

Axis, Button

Constant Summary collapse

EVENTS =

Dummy events hash, this should be set in the individual observer classes.

{}
@@observers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, name) ⇒ Observer

Initialize a control observer

Arguments:

  • code - Integer, the event code generated by this button

  • name - Symbol, the name of the button


43
44
45
46
47
48
# File 'lib/ruby_hid/observer.rb', line 43

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

Instance Attribute Details

#codeObject

events will be an array of lambdas


23
24
25
# File 'lib/ruby_hid/observer.rb', line 23

def code
  @code
end

#eventsObject

events will be an array of lambdas


23
24
25
# File 'lib/ruby_hid/observer.rb', line 23

def events
  @events
end

#nameObject

events will be an array of lambdas


23
24
25
# File 'lib/ruby_hid/observer.rb', line 23

def name
  @name
end

Class Method Details

.buildObject

Create an observer object to observe all of this kind of observer

If this isn't called directly, it will be created as required when find or find_by_name are called.


31
32
33
# File 'lib/ruby_hid/observer.rb', line 31

def self.build
  self::EVENTS.each { |code, name| self.new(code, name) }
end

.find(code) ⇒ Object

Find an observer of this type by its event code. Used by trigger_key to find an observer when its controller is used.

Arguments:

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


59
60
61
62
63
64
65
# File 'lib/ruby_hid/observer.rb', line 59

def self.find(code)
  btn = @@observers.detect { |b| b.code == code }
  if btn.nil?
    btn = self.make_from_code(code)
  end
  btn
end

.find_by_name(name) ⇒ Object

Find an observer of this type by its event code.

Arguments:

  • name - Symbol, a known button name (from BUTTONS)


91
92
93
94
95
96
97
# File 'lib/ruby_hid/observer.rb', line 91

def self.find_by_name(name)
  btn = @@observers.detect { |b| b.name == name }
  if btn.nil?
    btn = self.make_from_name(name)
  end
  btn
end

.make_from_code(code) ⇒ Object

Create an observer object of this type from a known code. used by find when the observer has not been initialised.

Arguments:

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


75
76
77
78
79
80
81
82
# File 'lib/ruby_hid/observer.rb', line 75

def self.make_from_code(code)
  name = EVENTS[code]
  if name
    self.new(code, name)
  else
    nil
  end
end

.make_from_name(name) ⇒ Object

Create an observer of this type from a known name. Used in find_by_name when the observer has not been initialised.

Arguments:

  • name - symbol, a known button name


108
109
110
111
112
113
114
115
# File 'lib/ruby_hid/observer.rb', line 108

def self.make_from_name(name)
  code, btn_name = self::EVENTS.detect { |code, btn_name| btn_name == name }
  if code
    self.new(code, name)
  else
    nil
  end
end

.trigger_event(code, value = nil) ⇒ Object

Find an observer of this type and run all of it's events.

Used by RubyHid::Device when a control change is detected.

Arguments:

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


150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruby_hid/observer.rb', line 150

def self.trigger_event(code, value=nil)
  btn = self.find(code)
  if btn
    btn.trigger_events(value)
  else
    puts unmapped_event_message(code)
  end
rescue => er
  puts "Error in observer #{code} event: #{er.message}"
  puts er.backtrace
end

.unmapped_event_message(code) ⇒ Object


162
163
164
165
166
167
168
169
170
# File 'lib/ruby_hid/observer.rb', line 162

def self.unmapped_event_message(code)
  <<-TEXT
     ==============================================================
     #{self} with event code #{code} has not been mapped.
     Please add it to #{self}::EVENTS with a name.
     ==============================================================

  TEXT
end

Instance Method Details

#add_event(proc) ⇒ Object

Add a process to be triggered when this observers controller is changed.

Arguments:

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


126
127
128
# File 'lib/ruby_hid/observer.rb', line 126

def add_event(proc)
  @events << proc
end

#trigger_events(value = nil) ⇒ Object

Trigger every proc in the @events array.


133
134
135
136
137
# File 'lib/ruby_hid/observer.rb', line 133

def trigger_events(value=nil)
  @events.each do |event|
    event.call(value)
  end
end