Class: Rustle::Frame

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

Overview

Frames

All strip changes are sent by serializing frames and sending them to the receiver. Serialization of a frame is done by serializing all Color objects in the leds array (see Color#serialize), and then appending the char 255 to the end. Serialized frames are, thus, strings of chars, where each char is a single RGB color channel.

It is worth noting that, since 255 is our end-of-frame code, all color data sent as an integer in the range 0..254. It is safe to assume that an approximate 1/255th drop in brightness is not noticeable. There is, however, the option of modifying the sketch to multiply each RGB channel by 1.004 and flooring the value.

Coercion of RGB values into the range 0..254 is handled in Color#serialize.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(leds) ⇒ Frame

Initializes a new frame.

Parameters:

  • leds (Array)

    n array of Color objects, whose length is the same as the number of LEDs connected to the strip.



30
31
32
33
34
# File 'lib/rustle/frame.rb', line 30

def initialize(leds)
  validate_leds(leds)

  @leds = leds
end

Instance Attribute Details

#ledsArray<Color> (readonly)

LEDs in a frame are represented as an array of Color objects. Each Color object corresponds to a given LED. The idea is that any physical LED matches its corresponding Color object.

Returns:

  • (Array<Color>)

    an array of Color objects (LEDs).



24
25
26
# File 'lib/rustle/frame.rb', line 24

def leds
  @leds
end

Instance Method Details

#serializeString

Serializes all LED Color objects (see Color#serialize) and appends the end-of-frame code (255.chr)

Returns:

  • (String)

    the serialized frame, represented as a string of chars.



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

def serialize
  @leds.map(&:serialize).join + 255.chr
end