Class: Rustle::Strip

Inherits:
Object
  • Object
show all
Defined in:
lib/rustle/strip.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, num_leds = 32) ⇒ Strip

Initializes a new strip given a Receiver object and the number of LEDs attached to the strip.

Parameters:

  • receiver (Receiver)
  • num_leds (Fixnum) (defaults to: 32)

    the number of LEDs connected to the strip.

Raises:



26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#num_ledsFixnum (readonly)

Returns the number of LEDs connected to the strip.

Returns:

  • (Fixnum)

    the number of LEDs connected to the strip



19
20
21
# File 'lib/rustle/strip.rb', line 19

def num_leds
  @num_leds
end

#receiverReceiver (readonly)

Returns the receiver associated with the strip.

Returns:

  • (Receiver)

    the receiver associated with the strip



16
17
18
# File 'lib/rustle/strip.rb', line 16

def receiver
  @receiver
end

Instance Method Details

#current_frameFrame

Returns the current frame being displayed by the strip.

Returns:

  • (Frame)

    the current frame being displayed by the strip.



48
49
50
# File 'lib/rustle/strip.rb', line 48

def current_frame
  @queue.first
end

#next_frameFrame

Returns the next frame in the buffer. If the buffer only has one frame, it returns the current frame.

Returns:

  • (Frame)

    the next frame in the buffer. If the buffer only has one frame, it returns the current frame.



37
38
39
# File 'lib/rustle/strip.rb', line 37

def next_frame
  @queue[1] || current_frame
end

#next_frame!Object

Advances the strip to the next frame.



42
43
44
45
# File 'lib/rustle/strip.rb', line 42

def next_frame!
  @receiver.push_frame(next_frame)
  @queue.shift if @queue.length > 1
end

#off!Object

Turns off all LEDs on the strip



61
62
63
64
65
# 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

#queue_frames(frames) ⇒ Object

Queues an array of frames for transitions. Note: this does not cause any physical changes; it merely prepares a list thereof.

Parameters:

  • frames (Array<Frame>)

    an array of frames to load into the queue.



56
57
58
# File 'lib/rustle/strip.rb', line 56

def queue_frames(frames)
  @queue += frames
end

#to(color) ⇒ Object

Changes all LEDs to a particular color

Parameters:



70
71
72
73
74
# File 'lib/rustle/strip.rb', line 70

def to(color)
  sleep 0.0025
  @queue << Frame.new([color] * @num_leds)
  next_frame!
end

#transition(klass, duration, opts = {}) ⇒ Transition

Instantiates and executes a Transition subclass.

Examples:

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)

Parameters:

  • klass (Symbol)

    a symbol representing the name of the class (e.g. for WipeToTransition, the corresponding symbol would be :wipe_to).

  • duration (Fixnum)

    the duration of the transition in milliseconds

  • opts (Hash) (defaults to: {})

    a hash containing transition-specific options

Returns:

  • (Transition)

    an instance of a Transition subclass



90
91
92
93
94
95
96
97
98
# 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