class Tinkerforge::BrickletLEDStripV2

Controls up to 2048 RGB(W) LEDs

Constants

CALLBACK_FRAME_STARTED

This callback is triggered directly after a new frame render is started. The parameter is the number of LEDs in that frame.

You should send the data for the next frame directly after this callback was triggered.

For an explanation of the general approach see BrickletLEDStripV2#set_led_values.

Public Class Methods

new(uid, ipcon) click to toggle source

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.

Calls superclass method
# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 107
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_SET_LED_VALUES_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_LED_VALUES_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_FRAME_DURATION] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_FRAME_DURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_SUPPLY_VOLTAGE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CLOCK_FREQUENCY] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CLOCK_FREQUENCY] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CHIP_TYPE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CHIP_TYPE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CHANNEL_MAPPING] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_CHANNEL_MAPPING] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_FRAME_STARTED_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_FRAME_STARTED_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_SPITFP_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_WRITE_FIRMWARE_POINTER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_FIRMWARE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_CHIP_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_RESET] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_UID] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READ_UID] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_FRAME_STARTED] = [10, 'S']

  @ipcon.add_device self
end

Public Instance Methods

get_bootloader_mode() click to toggle source

Returns the current bootloader mode, see BrickletLEDStripV2#set_bootloader_mode.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 338
def get_bootloader_mode
  check_validity

  send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 9, 'C'
end
get_channel_mapping() click to toggle source

Returns the currently used channel mapping as set by BrickletLEDStripV2#set_channel_mapping.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 284
def get_channel_mapping
  check_validity

  send_request FUNCTION_GET_CHANNEL_MAPPING, [], '', 9, 'C'
end
get_chip_temperature() click to toggle source

Returns the temperature as measured inside the microcontroller. The value returned is not the ambient temperature!

The temperature is only proportional to the real temperature and it has bad accuracy. Practically it is only useful as an indicator for temperature changes.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 396
def get_chip_temperature
  check_validity

  send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 10, 's'
end
get_chip_type() click to toggle source

Returns the currently used chip type as set by BrickletLEDStripV2#set_chip_type.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 256
def get_chip_type
  check_validity

  send_request FUNCTION_GET_CHIP_TYPE, [], '', 10, 'S'
end
get_clock_frequency() click to toggle source

Returns the currently used clock frequency as set by BrickletLEDStripV2#set_clock_frequency.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 234
def get_clock_frequency
  check_validity

  send_request FUNCTION_GET_CLOCK_FREQUENCY, [], '', 12, 'L'
end
get_frame_duration() click to toggle source

Returns the frame duration as set by BrickletLEDStripV2#set_frame_duration.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 202
def get_frame_duration
  check_validity

  send_request FUNCTION_GET_FRAME_DURATION, [], '', 10, 'S'
end
get_frame_started_callback_configuration() click to toggle source

Returns the configuration as set by BrickletLEDStripV2#set_frame_started_callback_configuration.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 299
def get_frame_started_callback_configuration
  check_validity

  send_request FUNCTION_GET_FRAME_STARTED_CALLBACK_CONFIGURATION, [], '', 9, '?'
end
get_identity() click to toggle source

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an :ref:`Isolator Bricklet <isolator_bricklet>` is always at position 'z'.

The device identifier numbers can be found :ref:`here <device_identifier>`. |device_identifier_constant|

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 443
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_led_values(index, length) click to toggle source

Returns length RGB(W) values starting from the given index.

If the channel mapping has 3 colors, you will get the data in the sequence RGBRGBRGB… if the channel mapping has 4 colors you will get the data in the sequence RGBWRGBWRGBW… (assuming you start at an index divisible by 3 (RGB) or 4 (RGBW)).

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 510
def get_led_values(index, length)
  value_length = nil # assigned in block
  value_data = nil # assigned in block

  @stream_mutex.synchronize {
    ret = get_led_values_low_level index, length
    value_length = ret[0]
    value_chunk_offset = ret[1]
    value_out_of_sync = value_chunk_offset != 0
    value_data = ret[2]

    while not value_out_of_sync and value_data.length < value_length
      ret = get_led_values_low_level index, length
      value_length = ret[0]
      value_chunk_offset = ret[1]
      value_out_of_sync = value_chunk_offset != value_data.length
      value_data += ret[2]
    end

    if value_out_of_sync # discard remaining stream to bring it back in-sync
      while value_chunk_offset + 60 < value_length
        ret = get_led_values_low_level index, length
        value_length = ret[0]
        value_chunk_offset = ret[1]
      end

      raise StreamOutOfSyncException, 'Value stream is out-of-sync'
    end
  }

  value_data[0, value_length]
end
get_led_values_low_level(index, length) click to toggle source

Returns length RGB(W) values starting from the given index.

If the channel mapping has 3 colors, you will get the data in the sequence RGBRGBRGB… if the channel mapping has 4 colors you will get the data in the sequence RGBWRGBWRGBW… (assuming you start at an index divisible by 3 (RGB) or 4 (RGBW)).

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 181
def get_led_values_low_level(index, length)
  check_validity

  send_request FUNCTION_GET_LED_VALUES_LOW_LEVEL, [index, length], 'S S', 72, 'S S C60'
end
get_spitfp_error_count() click to toggle source

Returns the error count for the communication between Brick and Bricklet.

The errors are divided into

  • ACK checksum errors,

  • message checksum errors,

  • framing errors and

  • overflow errors.

The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 316
def get_spitfp_error_count
  check_validity

  send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 24, 'L L L L'
end
get_status_led_config() click to toggle source

Returns the configuration as set by BrickletLEDStripV2#set_status_led_config

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 384
def get_status_led_config
  check_validity

  send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 9, 'C'
end
get_supply_voltage() click to toggle source

Returns the current supply voltage of the LEDs.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 209
def get_supply_voltage
  check_validity

  send_request FUNCTION_GET_SUPPLY_VOLTAGE, [], '', 10, 'S'
end
read_uid() click to toggle source

Returns the current UID as an integer. Encode as Base58 to get the usual string version.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 427
def read_uid
  check_validity

  send_request FUNCTION_READ_UID, [], '', 12, 'L'
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 544
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
reset() click to toggle source

Calling this function will reset the Bricklet. All configurations will be lost.

After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 408
def reset
  check_validity

  send_request FUNCTION_RESET, [], '', 8, ''
end
set_bootloader_mode(mode) click to toggle source

Sets the bootloader mode and returns the status after the requested mode change was instigated.

You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 331
def set_bootloader_mode(mode)
  check_validity

  send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 9, 'C'
end
set_channel_mapping(mapping) click to toggle source

Sets the channel mapping for the connected LEDs.

If the mapping has 4 colors, the function BrickletLEDStripV2#set_led_values expects 4 values per pixel and if the mapping has 3 colors it expects 3 values per pixel.

The function always expects the order RGB(W). The connected LED driver chips might have their 3 or 4 channels in a different order. For example, the WS2801 chips typically use BGR order, then WS2812 chips typically use GRB order and the APA102 chips typically use WBGR order.

The APA102 chips are special. They have three 8-bit channels for RGB and an additional 5-bit channel for the overall brightness of the RGB LED making them 4-channel chips. Internally the brightness channel is the first channel, therefore one of the Wxyz channel mappings should be used. Then the W channel controls the brightness.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 277
def set_channel_mapping(mapping)
  check_validity

  send_request FUNCTION_SET_CHANNEL_MAPPING, [mapping], 'C', 8, ''
end
set_chip_type(chip) click to toggle source

Sets the type of the LED driver chip. We currently support the chips

  • WS2801,

  • WS2811,

  • WS2812 / SK6812 / NeoPixel RGB,

  • SK6812RGBW / NeoPixel RGBW (Chip Type = WS2812),

  • WS2813 / WS2815 (Chip Type = WS2812)

  • LPD8806 and

  • APA102 / DotStar.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 249
def set_chip_type(chip)
  check_validity

  send_request FUNCTION_SET_CHIP_TYPE, [chip], 'S', 8, ''
end
set_clock_frequency(frequency) click to toggle source

Sets the frequency of the clock.

The Bricklet will choose the nearest achievable frequency, which may be off by a few Hz. You can get the exact frequency that is used by calling BrickletLEDStripV2#get_clock_frequency.

If you have problems with flickering LEDs, they may be bits flipping. You can fix this by either making the connection between the LEDs and the Bricklet shorter or by reducing the frequency.

With a decreasing frequency your maximum frames per second will decrease too.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 227
def set_clock_frequency(frequency)
  check_validity

  send_request FUNCTION_SET_CLOCK_FREQUENCY, [frequency], 'L', 8, ''
end
set_frame_duration(duration) click to toggle source

Sets the frame duration.

Example: If you want to achieve 20 frames per second, you should set the frame duration to 50ms (50ms * 20 = 1 second).

For an explanation of the general approach see BrickletLEDStripV2#set_led_values.

Default value: 100ms (10 frames per second).

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 195
def set_frame_duration(duration)
  check_validity

  send_request FUNCTION_SET_FRAME_DURATION, [duration], 'S', 8, ''
end
set_frame_started_callback_configuration(enable) click to toggle source

Enables/disables the CALLBACK_FRAME_STARTED callback.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 291
def set_frame_started_callback_configuration(enable)
  check_validity

  send_request FUNCTION_SET_FRAME_STARTED_CALLBACK_CONFIGURATION, [enable], '?', 8, ''
end
set_led_values(index, value) click to toggle source

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.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 472
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
set_led_values_low_level(index, value_length, value_chunk_offset, value_chunk_data) click to toggle source

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.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 168
def set_led_values_low_level(index, value_length, value_chunk_offset, value_chunk_data)
  check_validity

  send_request FUNCTION_SET_LED_VALUES_LOW_LEVEL, [index, value_length, value_chunk_offset, value_chunk_data], 'S S S C58', 8, ''
end
set_status_led_config(config) click to toggle source

Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 377
def set_status_led_config(config)
  check_validity

  send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 8, ''
end
set_write_firmware_pointer(pointer) click to toggle source

Sets the firmware pointer for BrickletLEDStripV2#write_firmware. The pointer has to be increased by chunks of size 64. The data is written to flash every 4 chunks (which equals to one page of size 256).

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 350
def set_write_firmware_pointer(pointer)
  check_validity

  send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 8, ''
end
write_firmware(data) click to toggle source

Writes 64 Bytes of firmware at the position as written by BrickletLEDStripV2#set_write_firmware_pointer before. The firmware is written to flash every 4 chunks.

You can only write firmware in bootloader mode.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 364
def write_firmware(data)
  check_validity

  send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 9, 'C'
end
write_uid(uid) click to toggle source

Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.

We recommend that you use Brick Viewer to change the UID.

# File lib/tinkerforge/bricklet_led_strip_v2.rb, line 419
def write_uid(uid)
  check_validity

  send_request FUNCTION_WRITE_UID, [uid], 'L', 8, ''
end