class Rustle::Strip
Strips¶ ↑
The Strip
class's purpose is to respond to requests to change the color of the physical strip, and send changes to the {Receiver} when appropriate. All Strip
objects also have a buffer, which is simply an array of {Frame} objects. Calling any method which advances to the next frame causes the Strip
to request the Receiver
class to push an update over the serial port.
New strips cannot be instantiated outside of the {Receiver} class. See {Receiver#initialize} for details on how Strips are instantiated.
Attributes
@return [Fixnum] the number of LEDs connected to the strip
@return [Receiver] the receiver associated with the strip
Public Class Methods
Initializes a new strip given a {Receiver} object and the number of LEDs attached to the strip.
@param [Receiver] receiver @param [Fixnum] num_leds
the number of LEDs connected to the strip.
# File lib/rustle/strip.rb, line 26 def initialize(receiver, num_leds = 32) @receiver = receiver @num_leds = num_leds @queue = [] raise ReceiverNotFound unless receiver.is_a? Receiver raise NumLEDsInvalid unless @num_leds && @num_leds > 0 end
Public Instance Methods
@return [Frame] the current frame being displayed by the strip.
# File lib/rustle/strip.rb, line 48 def current_frame @queue.first end
@return [Frame] the next frame in the buffer. If the buffer only has one
frame, it returns the current frame.
# File lib/rustle/strip.rb, line 37 def next_frame @queue[1] || current_frame end
Advances the strip to the next frame.
# File lib/rustle/strip.rb, line 42 def next_frame! @receiver.push_frame(next_frame) @queue.shift if @queue.length > 1 end
Turns off all LEDs on the strip
# File lib/rustle/strip.rb, line 61 def off! sleep 0.0025 @queue << Frame.new([Color.new(0,0,0)] * @num_leds) next_frame! end
Queues an array of frames for transitions. Note: this does not cause any physical changes; it merely prepares a list thereof.
@param [Array<Frame>] frames an array of frames to load into the queue.
# File lib/rustle/strip.rb, line 56 def queue_frames(frames) @queue += frames end
Changes all LEDs to a particular color
@param [Color] color
# File lib/rustle/strip.rb, line 70 def to(color) sleep 0.0025 @queue << Frame.new([color] * @num_leds) next_frame! end
Instantiates and executes a {Transition} subclass.
@example
strip = # [initialized strip...] # Transition the strip to red in 2 seconds using the wipe-to transition strip.transition :wipe_to, 2000, color: Rustle::Color.new(255, 0, 0)
@param [Symbol] klass a symbol representing the name of the class (e.g.
for {WipeToTransition}, the corresponding symbol would be +:wipe_to+).
@param [Fixnum] duration the duration of the transition in milliseconds @param [Hash] opts a hash containing transition-specific options
@return [Transition] an instance of a Transition
subclass
# File lib/rustle/strip.rb, line 90 def transition(klass, duration, opts = {}) # Default to white opts[:color] ||= Color.rgb(255, 255, 255) # :fade_to => FadeToTransition klass = "#{klass.to_s.camelize}Transition".constantize klass.new(self, duration, opts) end