Class: Tinkerforge::BrickletLEDStrip

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_led_strip.rb

Overview

Controls up to 320 RGB LEDs

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

231
DEVICE_DISPLAY_NAME =

:nodoc:

'LED Strip Bricklet'
CALLBACK_FRAME_RENDERED =

This callback is triggered directly after a new frame is rendered. The parameter is the number of RGB or RGBW 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 BrickletLEDStrip#set_rgb_values.

6
FUNCTION_SET_RGB_VALUES =

:nodoc:

1
FUNCTION_GET_RGB_VALUES =

:nodoc:

2
FUNCTION_SET_FRAME_DURATION =

:nodoc:

3
FUNCTION_GET_FRAME_DURATION =

:nodoc:

4
FUNCTION_GET_SUPPLY_VOLTAGE =

:nodoc:

5
FUNCTION_SET_CLOCK_FREQUENCY =

:nodoc:

7
FUNCTION_GET_CLOCK_FREQUENCY =

:nodoc:

8
FUNCTION_SET_CHIP_TYPE =

:nodoc:

9
FUNCTION_GET_CHIP_TYPE =

:nodoc:

10
FUNCTION_SET_RGBW_VALUES =

:nodoc:

11
FUNCTION_GET_RGBW_VALUES =

:nodoc:

12
FUNCTION_SET_CHANNEL_MAPPING =

:nodoc:

13
FUNCTION_GET_CHANNEL_MAPPING =

:nodoc:

14
FUNCTION_ENABLE_FRAME_RENDERED_CALLBACK =

:nodoc:

15
FUNCTION_DISABLE_FRAME_RENDERED_CALLBACK =

:nodoc:

16
FUNCTION_IS_FRAME_RENDERED_CALLBACK_ENABLED =

:nodoc:

17
FUNCTION_GET_IDENTITY =

:nodoc:

255
CHIP_TYPE_WS2801 =

:nodoc:

2801
CHIP_TYPE_WS2811 =

:nodoc:

2811
CHIP_TYPE_WS2812 =

:nodoc:

2812
CHIP_TYPE_LPD8806 =

:nodoc:

8806
CHIP_TYPE_APA102 =

:nodoc:

102
CHANNEL_MAPPING_RGB =

:nodoc:

6
CHANNEL_MAPPING_RBG =

:nodoc:

9
CHANNEL_MAPPING_BRG =

:nodoc:

33
CHANNEL_MAPPING_BGR =

:nodoc:

36
CHANNEL_MAPPING_GRB =

:nodoc:

18
CHANNEL_MAPPING_GBR =

:nodoc:

24
CHANNEL_MAPPING_RGBW =

:nodoc:

27
CHANNEL_MAPPING_RGWB =

:nodoc:

30
CHANNEL_MAPPING_RBGW =

:nodoc:

39
CHANNEL_MAPPING_RBWG =

:nodoc:

45
CHANNEL_MAPPING_RWGB =

:nodoc:

54
CHANNEL_MAPPING_RWBG =

:nodoc:

57
CHANNEL_MAPPING_GRWB =

:nodoc:

78
CHANNEL_MAPPING_GRBW =

:nodoc:

75
CHANNEL_MAPPING_GBWR =

:nodoc:

108
CHANNEL_MAPPING_GBRW =

:nodoc:

99
CHANNEL_MAPPING_GWBR =

:nodoc:

120
CHANNEL_MAPPING_GWRB =

:nodoc:

114
CHANNEL_MAPPING_BRGW =

:nodoc:

135
CHANNEL_MAPPING_BRWG =

:nodoc:

141
CHANNEL_MAPPING_BGRW =

:nodoc:

147
CHANNEL_MAPPING_BGWR =

:nodoc:

156
CHANNEL_MAPPING_BWRG =

:nodoc:

177
CHANNEL_MAPPING_BWGR =

:nodoc:

180
CHANNEL_MAPPING_WRBG =

:nodoc:

201
CHANNEL_MAPPING_WRGB =

:nodoc:

198
CHANNEL_MAPPING_WGBR =

:nodoc:

216
CHANNEL_MAPPING_WGRB =

:nodoc:

210
CHANNEL_MAPPING_WBGR =

:nodoc:

228
CHANNEL_MAPPING_WBRG =

:nodoc:

225

Constants inherited from Device

Device::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE

Instance Attribute Summary

Attributes inherited from Device

#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #high_level_callbacks, #registered_callbacks, #replaced, #uid

Instance Method Summary collapse

Methods inherited from Device

#check_validity, #dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletLEDStrip

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 85

def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 3]

  @response_expected[FUNCTION_SET_RGB_VALUES] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_RGB_VALUES] = 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_RGBW_VALUES] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_RGBW_VALUES] = 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_ENABLE_FRAME_RENDERED_CALLBACK] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_DISABLE_FRAME_RENDERED_CALLBACK] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_IS_FRAME_RENDERED_CALLBACK_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

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

  @ipcon.add_device self
end

Instance Method Details

#disable_frame_rendered_callbackObject

Disables the CALLBACK_FRAME_RENDERED callback.

By default the callback is enabled.

.. versionadded

2.0.6$nbsp;(Plugin)



373
374
375
376
377
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 373

def disable_frame_rendered_callback
  check_validity

  send_request FUNCTION_DISABLE_FRAME_RENDERED_CALLBACK, [], '', 8, ''
end

#enable_frame_rendered_callbackObject

Enables the CALLBACK_FRAME_RENDERED callback.

By default the callback is enabled.

.. versionadded

2.0.6$nbsp;(Plugin)



362
363
364
365
366
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 362

def enable_frame_rendered_callback
  check_validity

  send_request FUNCTION_ENABLE_FRAME_RENDERED_CALLBACK, [], '', 8, ''
end

#get_channel_mappingObject

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

.. versionadded

2.0.6$nbsp;(Plugin)



351
352
353
354
355
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 351

def get_channel_mapping
  check_validity

  send_request FUNCTION_GET_CHANNEL_MAPPING, [], '', 9, 'C'
end

#get_chip_typeObject

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

.. versionadded

2.0.2$nbsp;(Plugin)



244
245
246
247
248
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 244

def get_chip_type
  check_validity

  send_request FUNCTION_GET_CHIP_TYPE, [], '', 10, 'S'
end

#get_clock_frequencyObject

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

.. versionadded

2.0.1$nbsp;(Plugin)



219
220
221
222
223
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 219

def get_clock_frequency
  check_validity

  send_request FUNCTION_GET_CLOCK_FREQUENCY, [], '', 12, 'L'
end

#get_frame_durationObject

Returns the frame duration as set by BrickletLEDStrip#set_frame_duration.



180
181
182
183
184
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 180

def get_frame_duration
  check_validity

  send_request FUNCTION_GET_FRAME_DURATION, [], '', 10, 'S'
end

#get_identityObject

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|



398
399
400
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 398

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#get_rgb_values(index, length) ⇒ Object

Returns length R, G and B values starting from the given LED index.

The values are the last values that were set by BrickletLEDStrip#set_rgb_values.



161
162
163
164
165
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 161

def get_rgb_values(index, length)
  check_validity

  send_request FUNCTION_GET_RGB_VALUES, [index, length], 'S C', 56, 'C16 C16 C16'
end

#get_rgbw_values(index, length) ⇒ Object

Returns length RGBW values starting from the given index.

The values are the last values that were set by BrickletLEDStrip#set_rgbw_values.

.. versionadded

2.0.6$nbsp;(Plugin)



315
316
317
318
319
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 315

def get_rgbw_values(index, length)
  check_validity

  send_request FUNCTION_GET_RGBW_VALUES, [index, length], 'S C', 56, 'C12 C12 C12 C12'
end

#get_supply_voltageObject

Returns the current supply voltage of the LEDs.



187
188
189
190
191
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 187

def get_supply_voltage
  check_validity

  send_request FUNCTION_GET_SUPPLY_VOLTAGE, [], '', 10, 'S'
end

#is_frame_rendered_callback_enabledObject

Returns true if the CALLBACK_FRAME_RENDERED callback is enabled, false otherwise.

.. versionadded

2.0.6$nbsp;(Plugin)



382
383
384
385
386
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 382

def is_frame_rendered_callback_enabled
  check_validity

  send_request FUNCTION_IS_FRAME_RENDERED_CALLBACK_ENABLED, [], '', 9, '?'
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



403
404
405
406
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 403

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#set_channel_mapping(mapping) ⇒ Object

Sets the channel mapping for the connected LEDs.

BrickletLEDStrip#set_rgb_values and BrickletLEDStrip#set_rgbw_values take the data in RGB(W) order. But 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, the 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.

If a 3-channel mapping is selected then BrickletLEDStrip#set_rgb_values has to be used. Calling BrickletLEDStrip#set_rgbw_values with a 3-channel mapping will produce incorrect results. Vice-versa if a 4-channel mapping is selected then BrickletLEDStrip#set_rgbw_values has to be used. Calling BrickletLEDStrip#set_rgb_values with a 4-channel mapping will produce incorrect results.

.. versionadded

2.0.6$nbsp;(Plugin)



342
343
344
345
346
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 342

def set_channel_mapping(mapping)
  check_validity

  send_request FUNCTION_SET_CHANNEL_MAPPING, [mapping], 'C', 8, ''
end

#set_chip_type(chip) ⇒ Object

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),

  • LPD8806 and

  • APA102 / DotStar.

.. versionadded

2.0.2$nbsp;(Plugin)



235
236
237
238
239
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 235

def set_chip_type(chip)
  check_validity

  send_request FUNCTION_SET_CHIP_TYPE, [chip], 'S', 8, ''
end

#set_clock_frequency(frequency) ⇒ Object

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 BrickletLEDStrip#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.

.. note

The frequency in firmware version 2.0.0 is fixed at 2MHz.

.. versionadded

2.0.1$nbsp;(Plugin)



210
211
212
213
214
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 210

def set_clock_frequency(frequency)
  check_validity

  send_request FUNCTION_SET_CLOCK_FREQUENCY, [frequency], 'L', 8, ''
end

#set_frame_duration(duration) ⇒ Object

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 BrickletLEDStrip#set_rgb_values.



173
174
175
176
177
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 173

def set_frame_duration(duration)
  check_validity

  send_request FUNCTION_SET_FRAME_DURATION, [duration], 'S', 8, ''
end

#set_rgb_values(index, length, r, g, b) ⇒ Object

Sets length RGB values for the LEDs starting from index.

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

Example: If you set

  • index to 5,

  • length to 3,

  • r to [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

  • g to [0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] and

  • b to [0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

the LED with index 5 will be red, 6 will be green and 7 will be blue.

.. note

Depending on the LED circuitry colors can be permuted.

The colors will be transfered to actual LEDs when the next frame duration ends, see BrickletLEDStrip#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_RENDERED callback.

  • Set all of the LED colors for next frame.

  • Wait for the CALLBACK_FRAME_RENDERED callback.

  • and so on.

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

The actual number of controllable LEDs depends on the number of free Bricklet ports. See :ref:‘here <led_strip_bricklet_ram_constraints>` for more information. A call of BrickletLEDStrip#set_rgb_values with index + length above the bounds is ignored completely.



151
152
153
154
155
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 151

def set_rgb_values(index, length, r, g, b)
  check_validity

  send_request FUNCTION_SET_RGB_VALUES, [index, length, r, g, b], 'S C C16 C16 C16', 8, ''
end

#set_rgbw_values(index, length, r, g, b, w) ⇒ Object

Sets length RGBW values for the LEDs starting from index.

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

The maximum length is 12, the index goes from 0 to 239 and the rgbw values have 8 bits each.

Example: If you set

  • index to 5,

  • length to 4,

  • r to [255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

  • g to [0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

  • b to [0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0] and

  • w to [0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0]

the LED with index 5 will be red, 6 will be green, 7 will be blue and 8 will be white.

.. note

Depending on the LED circuitry colors can be permuted.

The colors will be transfered to actual LEDs when the next frame duration ends, see BrickletLEDStrip#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_RENDERED callback.

  • Set all of the LED colors for next frame.

  • Wait for the CALLBACK_FRAME_RENDERED callback.

  • and so on.

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

The actual number of controllable LEDs depends on the number of free Bricklet ports. See :ref:‘here <led_strip_bricklet_ram_constraints>` for more information. A call of BrickletLEDStrip#set_rgbw_values with index + length above the bounds is ignored completely.

The LPD8806 LED driver chips have 7-bit channels for RGB. Internally the LED Strip Bricklets divides the 8-bit values set using this function by 2 to make them 7-bit. Therefore, you can just use the normal value range (0-255) for LPD8806 LEDs.

The brightness channel of the APA102 LED driver chips has 5-bit. Internally the LED Strip Bricklets divides the 8-bit values set using this function by 8 to make them 5-bit. Therefore, you can just use the normal value range (0-255) for the brightness channel of APA102 LEDs.

.. versionadded

2.0.6$nbsp;(Plugin)



304
305
306
307
308
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 304

def set_rgbw_values(index, length, r, g, b, w)
  check_validity

  send_request FUNCTION_SET_RGBW_VALUES, [index, length, r, g, b, w], 'S C C12 C12 C12 C12', 8, ''
end