Class: Rustle::Strip

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

Overview

Strips

The Strip class’ 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.



26
27
28
29
30
# File 'lib/rustle/strip.rb', line 26

def initialize(receiver, num_leds = 32)
  @receiver = receiver
  @num_leds = num_leds
  @queue = []
end

Instance Attribute Details

#num_ledsFixnum (readonly)



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

def num_leds
  @num_leds
end

#receiverReceiver (readonly)



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

def receiver
  @receiver
end

Instance Method Details

#current_frameFrame



45
46
47
# File 'lib/rustle/strip.rb', line 45

def current_frame
  @queue.first
end

#next_frameFrame



34
35
36
# File 'lib/rustle/strip.rb', line 34

def next_frame
  @queue[1] || current_frame
end

#next_frame!Object

Advances the strip to the next frame.



39
40
41
42
# File 'lib/rustle/strip.rb', line 39

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

#off!Object

Turns off all LEDs on the strip



58
59
60
61
# File 'lib/rustle/strip.rb', line 58

def off!
  @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.



53
54
55
# File 'lib/rustle/strip.rb', line 53

def queue_frames(frames)
  @queue += frames
end

#to(color) ⇒ Object

Changes all LEDs to a particular color



66
67
68
69
# File 'lib/rustle/strip.rb', line 66

def to(color)
  @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)


85
86
87
88
89
90
91
92
93
# File 'lib/rustle/strip.rb', line 85

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).frames
end