Method: Tinkerforge::BrickletLEDStripV2#set_led_values

Defined in:
lib/tinkerforge/bricklet_led_strip_v2.rb

#set_led_values(index, value) ⇒ Object

Sets the RGB(W) values for the LEDs starting from index. You can set at most 2048 RGB values or 1536 RGBW values (6144 byte each).

To make the colors show correctly you need to configure the chip type (see BrickletLEDStripV2#set_chip_type) and a channel mapping (see BrickletLEDStripV2#set_channel_mapping) according to the connected LEDs.

If the channel mapping has 3 colors, you need to give the data in the sequence RGBRGBRGB… if the channel mapping has 4 colors you need to give data in the sequence RGBWRGBWRGBW…

The data is double buffered and the colors will be transfered to the LEDs when the next frame duration ends (see BrickletLEDStripV2#set_frame_duration).

Generic approach:

  • Set the frame duration to a value that represents the number of frames per second you want to achieve.

  • Set all of the LED colors for one frame.

  • Wait for the CALLBACK_FRAME_STARTED callback.

  • Set all of the LED colors for next frame.

  • Wait for the CALLBACK_FRAME_STARTED callback.

  • And so on.

This approach ensures that you can change the LED colors with a fixed frame rate.



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/tinkerforge/bricklet_led_strip_v2.rb', line 473

def set_led_values(index, value)
  if value.length > 65535
    raise ArgumentError, 'Value can be at most 65535 items long'
  end

  value_length = value.length
  value_chunk_offset = 0

  if value_length == 0
    value_chunk_data = [0] * 58
    ret = set_led_values_low_level index, value_length, value_chunk_offset, value_chunk_data
  else
    ret = nil # assigned in block

    @stream_mutex.synchronize {
      while value_chunk_offset < value_length
        value_chunk_data = value[value_chunk_offset, 58]

        if value_chunk_data.length < 58
          value_chunk_data += [0] * (58 - value_chunk_data.length)
        end

        ret = set_led_values_low_level index, value_length, value_chunk_offset, value_chunk_data
        value_chunk_offset += 58
      end
    }
  end

  ret
end