Class: OpenLighting::DmxController

Inherits:
Object
  • Object
show all
Defined in:
lib/open_lighting/dmx_controller.rb

Overview

The DmxController class is responsible for sending control messages across the DMX bus.

Due to idiosynchricies of the underlying open_lighting subsytem, all devices must receive a control signal each time anything on the bus receives a control signal. The DmxController class is responsible for aggregating this information from the DmxDevice instances and sending it down the bus.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DmxController



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/open_lighting/dmx_controller.rb', line 13

def initialize(options = {})
  @devices = []
  (options[:devices] || []).each {|dev| @devices << dev}
  self.fps = options[:fps] || 40
  self.universe = options[:universe] || 1
  self.cmd = options[:cmd] || "ola_streaming_client -u #{universe}"

  if options[:test]
    self.do_not_sleep = true
    self.connect_test_pipe
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/open_lighting/dmx_controller.rb', line 124

def method_missing(meth, *args, &block)
  meth_without_bang = meth.to_s.gsub(/!\Z/, "").to_sym

  if points.include? meth
    buffer :point => meth
  elsif points.include? meth_without_bang
    # causes controller.center! to convert to controller.instant!(:point => :center)
    instant! :point => meth_without_bang
  elsif capabilities.include? meth
    buffer meth => args
  elsif capabilities.include? meth_without_bang
    instant! meth_without_bang => args.first
  else
    super # You *must* call super if you don't handle the
          # method, otherwise you'll mess up Ruby's method
          # lookup.
  end
end

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def cmd
  @cmd
end

#devicesObject

Returns the value of attribute devices.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def devices
  @devices
end

#do_not_sleepObject

Returns the value of attribute do_not_sleep.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def do_not_sleep
  @do_not_sleep
end

#fpsObject

Returns the value of attribute fps.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def fps
  @fps
end

#read_pipeObject

Returns the value of attribute read_pipe.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def read_pipe
  @read_pipe
end

#universeObject

Returns the value of attribute universe.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def universe
  @universe
end

#write_pipeObject

Returns the value of attribute write_pipe.



12
13
14
# File 'lib/open_lighting/dmx_controller.rb', line 12

def write_pipe
  @write_pipe
end

Instance Method Details

#<<(val) ⇒ Object



34
35
36
37
38
# File 'lib/open_lighting/dmx_controller.rb', line 34

def <<(val)
  val.start_address ||= current_values.count + 1
  val.controller = self
  @devices << val
end

#begin_animation!(options = {}, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/open_lighting/dmx_controller.rb', line 94

def begin_animation!(options = {}, &block)
  previous = current_values
  buffer(options)

  block.call(@devices) if block

  count = ticks(options[:seconds])
  count.times do |i|
    # interpolate previous to current
    write! interpolate(previous, current_values, count, i+1)
    sleep(wait_time) unless self.do_not_sleep
  end
end

#buffer(options = {}) ⇒ Object



44
45
46
# File 'lib/open_lighting/dmx_controller.rb', line 44

def buffer(options = {})
  @devices.each {|device| device.buffer(options.dup)}
end

#capabilitiesObject



116
117
118
# File 'lib/open_lighting/dmx_controller.rb', line 116

def capabilities
  @devices.map{|device| device.capabilities}.flatten.uniq
end

#close!Object



58
59
60
61
# File 'lib/open_lighting/dmx_controller.rb', line 58

def close!
  self.write_pipe.close if self.write_pipe
  self.read_pipe.close  if self.read_pipe
end

#connect_test_pipeObject



26
27
28
# File 'lib/open_lighting/dmx_controller.rb', line 26

def connect_test_pipe
  self.read_pipe, self.write_pipe = IO.pipe
end

#current_valuesObject



73
74
75
76
77
78
79
80
# File 'lib/open_lighting/dmx_controller.rb', line 73

def current_values
  results = []
  @devices.each do |d|
    results[d.start_address, d.start_address+d.capabilities.count] = d.current_values
  end
  # backfill unknown values with zero, in case of gaps due to starting_address errors
  results.map{|i| i.nil? ? 0 : i}.drop(1)
end

#instant!(options = {}) ⇒ Object



63
64
65
66
# File 'lib/open_lighting/dmx_controller.rb', line 63

def instant!(options = {})
  buffer(options)
  write!
end

#interpolate(first, last, total, i) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/open_lighting/dmx_controller.rb', line 108

def interpolate(first, last, total, i)
  results = []
  first.count.times do |j|
    results[j] = (last[j] - first[j])*i.to_f/total + first[j]
  end
  results
end

#pointsObject



120
121
122
# File 'lib/open_lighting/dmx_controller.rb', line 120

def points
  @devices.map{|device| device.points.keys}.flatten.uniq
end

#set(options = {}) ⇒ Object



40
41
42
# File 'lib/open_lighting/dmx_controller.rb', line 40

def set(options = {})
  warn "[DEPRECATION] `set` is deprecated. Use `buffer` instead."
end

#ticks(seconds) ⇒ Object



82
83
84
# File 'lib/open_lighting/dmx_controller.rb', line 82

def ticks(seconds)
  [1, (seconds.to_f * self.fps.to_f).to_i].max
end

#to_dmxObject



68
69
70
71
# File 'lib/open_lighting/dmx_controller.rb', line 68

def to_dmx
  # dmx addresses start at 1, ruby arrays start at zero
  current_values.join ","
end

#transition!(options = {}, &block) ⇒ Object



90
91
92
# File 'lib/open_lighting/dmx_controller.rb', line 90

def transition!(options = {}, &block)
  warn "[DEPRECATION] `transition!` is deprecated. Use `begin_animation!` instead."
end

#wait_timeObject



86
87
88
# File 'lib/open_lighting/dmx_controller.rb', line 86

def wait_time
  1.0 / self.fps.to_f
end

#write!(values = current_values) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/open_lighting/dmx_controller.rb', line 48

def write!(values=current_values)
  self.write_pipe ||= IO.popen(self.cmd, "w")

  # DMX only wants integer inputs
  values.map!{|i| i.to_i}

  self.write_pipe.write "#{values.join ","}\n"
  self.write_pipe.flush
end