Class: Rustle::Strip
- Inherits:
-
Object
- Object
- Rustle::Strip
- 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
-
#num_leds ⇒ Fixnum
readonly
The number of LEDs connected to the strip.
-
#receiver ⇒ Receiver
readonly
The receiver associated with the strip.
Instance Method Summary collapse
-
#current_frame ⇒ Frame
The current frame being displayed by the strip.
-
#initialize(receiver, num_leds = 32) ⇒ Strip
constructor
Initializes a new strip given a Receiver object and the number of LEDs attached to the strip.
-
#next_frame ⇒ Frame
The next frame in the buffer.
-
#next_frame! ⇒ Object
Advances the strip to the next frame.
-
#off! ⇒ Object
Turns off all LEDs on the strip.
-
#queue_frames(frames) ⇒ Object
Queues an array of frames for transitions.
-
#to(color) ⇒ Object
Changes all LEDs to a particular color.
-
#transition(klass, duration, opts = {}) ⇒ Transition
Instantiates and executes a Transition subclass.
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.
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_leds ⇒ Fixnum (readonly)
Returns the number of LEDs connected to the strip.
19 20 21 |
# File 'lib/rustle/strip.rb', line 19 def num_leds @num_leds end |
#receiver ⇒ Receiver (readonly)
Returns the receiver associated with the strip.
16 17 18 |
# File 'lib/rustle/strip.rb', line 16 def receiver @receiver end |
Instance Method Details
#current_frame ⇒ Frame
Returns 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_frame ⇒ Frame
Returns 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.
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
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.
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 |