Class: SurfaceMaster::Interaction

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/surface_master/interaction.rb

Overview

Base class for event-based drivers. Sub-classes should extend the constructor, and implement respond_to_action, etc.

Direct Known Subclasses

Launchpad::Interaction, Orbit::Interaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, #logger=

Constructor Details

#initialize(opts = nil) ⇒ Interaction



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/surface_master/interaction.rb', line 9

def initialize(opts = nil)
  opts ||= {}

  self.logger = opts[:logger]
  logger.debug "Initializing #{self.class}##{object_id} with #{opts.inspect}"

  @device       = opts[:device] || @device_class.new(opts.merge(input: true,
                                                                output: true,
                                                                logger: opts[:logger]))
  @latency      = (opts[:latency] || 0.001).to_f.abs
  @active       = false
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



7
8
9
# File 'lib/surface_master/interaction.rb', line 7

def active
  @active
end

#deviceObject (readonly)

Returns the value of attribute device.



7
8
9
# File 'lib/surface_master/interaction.rb', line 7

def device
  @device
end

Instance Method Details

#change(opts) ⇒ Object



22
# File 'lib/surface_master/interaction.rb', line 22

def change(opts); @device.change(opts); end

#changes(opts) ⇒ Object



23
# File 'lib/surface_master/interaction.rb', line 23

def changes(opts); @device.changes(opts); end

#closeObject



25
26
27
28
29
# File 'lib/surface_master/interaction.rb', line 25

def close
  logger.debug "Closing #{self.class}##{object_id}"
  stop
  @device.close
end

#closed?Boolean



31
# File 'lib/surface_master/interaction.rb', line 31

def closed?; @device.closed?; end

#no_response_to(types = nil, state = :both, opts = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/surface_master/interaction.rb', line 62

def no_response_to(types = nil, state = :both, opts = nil)
  logger.debug "Removing response to #{types.inspect} for state #{state.inspect}"
  types   = Array(types)
  opts  ||= {}
  expand_states(state).each do |st|
    clear_responses_for_state!(types, opts, st)
  end
  nil
end

#respond_to(type, state, opts = nil) ⇒ Object



72
73
74
# File 'lib/surface_master/interaction.rb', line 72

def respond_to(type, state, opts = nil)
  respond_to_action((opts || {}).merge(type: type, state: state))
end

#response_to(types = :all, state = :both, opts = nil, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/surface_master/interaction.rb', line 50

def response_to(types = :all, state = :both, opts = nil, &block)
  logger.debug "Setting response to #{types.inspect} for state #{state.inspect} with"\
    " #{opts.inspect}"
  types   = Array(types)
  opts  ||= {}
  no_response_to(types, state, opts) if opts[:exclusive] == true
  expand_states(state).each do |st|
    add_response_for_state!(types, opts, st, block)
  end
  nil
end

#startObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/surface_master/interaction.rb', line 33

def start
  logger.debug "Starting #{self.class}##{object_id}"

  @active = true
  guard_input_and_reset_at_end! do
    while @active
      @device.read.each { |action| respond_to_action(action) }
      sleep @latency if @latency && @latency > 0.0
    end
  end
end

#stopObject



45
46
47
48
# File 'lib/surface_master/interaction.rb', line 45

def stop
  logger.debug "Stopping #{self.class}##{object_id}"
  @active = false
end