class Rustle::Frame

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 +floor+ing the value.

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

Attributes

leds[R]

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.

@return [Array<Color>] an array of {Color} objects (LEDs).

Public Class Methods

new(leds) click to toggle source

Initializes a new frame.

@param [Array] leds n array of {Color} objects, whose length is the same

as the number of LEDs connected to the strip.
# File lib/rustle/frame.rb, line 30
def initialize(leds)
  validate_leds(leds)

  @leds = leds
end

Public Instance Methods

serialize() click to toggle source

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

@return [String] the serialized frame, represented as a string of chars.

# File lib/rustle/frame.rb, line 40
def serialize
  @leds.map(&:serialize).join + 255.chr
end

Private Instance Methods

validate_leds(arr) click to toggle source
# File lib/rustle/frame.rb, line 46
def validate_leds(arr)
  raise InvalidLEDArray unless arr.all? { |l| l.is_a? Color }
end