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 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_GET_IDENTITY =

:nodoc:

255
CHIP_TYPE_WS2801 =

:nodoc:

2801
CHIP_TYPE_WS2811 =

:nodoc:

2811
CHIP_TYPE_WS2812 =

:nodoc:

2812

Constants inherited from Device

Device::RESPONSE_EXPECTED_ALWAYS_FALSE, 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, #registered_callbacks, #uid

Instance Method Summary collapse

Methods inherited from Device

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 44

def initialize(uid, ipcon)
  super uid, ipcon

  @api_version = [2, 0, 2]

  @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[CALLBACK_FRAME_RENDERED] = RESPONSE_EXPECTED_ALWAYS_FALSE
  @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_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_FRAME_RENDERED] = 'S'
end

Instance Method Details

#get_chip_typeObject

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

.. versionadded

2.0.2$nbsp;(Plugin)



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

def get_chip_type
  send_request(FUNCTION_GET_CHIP_TYPE, [], '', 2, 'S')
end

#get_clock_frequencyObject

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

.. versionadded

2.0.1$nbsp;(Plugin)



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

def get_clock_frequency
  send_request(FUNCTION_GET_CLOCK_FREQUENCY, [], '', 4, 'L')
end

#get_frame_durationObject

Returns the frame duration in ms as set by BrickletLEDStrip#set_frame_duration.



127
128
129
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 127

def get_frame_duration
  send_request(FUNCTION_GET_FRAME_DURATION, [], '', 2, '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’ or ‘d’.

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



199
200
201
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 199

def get_identity
  send_request(FUNCTION_GET_IDENTITY, [], '', 25, 'Z8 Z8 k C3 C3 S')
end

#get_rgb_values(index, length) ⇒ Object

Returns rgb values with the given length starting from the given index.

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



110
111
112
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 110

def get_rgb_values(index, length)
  send_request(FUNCTION_GET_RGB_VALUES, [index, length], 'S C', 48, 'C16 C16 C16')
end

#get_supply_voltageObject

Returns the current supply voltage of the LEDs. The voltage is given in mV.



132
133
134
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 132

def get_supply_voltage
  send_request(FUNCTION_GET_SUPPLY_VOLTAGE, [], '', 2, 'S')
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



204
205
206
207
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 204

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

#set_chip_type(chip) ⇒ Object

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

  • WS2801 (chip = 2801),

  • WS2811 (chip = 2811) and

  • WS2812 (chip = 2812).

The WS2812 is sometimes also called “NeoPixel”, a name coined by Adafruit.

The default value is WS2801 (chip = 2801).

.. versionadded

2.0.2$nbsp;(Plugin)



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

def set_chip_type(chip)
  send_request(FUNCTION_SET_CHIP_TYPE, [chip], 'S', 0, '')
end

#set_clock_frequency(frequency) ⇒ Object

Sets the frequency of the clock in Hz. The range is 10000Hz (10kHz) up to 2000000Hz (2MHz).

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.

The default value is 1.66MHz.

.. note

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

.. versionadded

2.0.1$nbsp;(Plugin)



156
157
158
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 156

def set_clock_frequency(frequency)
  send_request(FUNCTION_SET_CLOCK_FREQUENCY, [frequency], 'L', 0, '')
end

#set_frame_duration(duration) ⇒ Object

Sets the frame duration in ms.

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.

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



122
123
124
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 122

def set_frame_duration(duration)
  send_request(FUNCTION_SET_FRAME_DURATION, [duration], 'S', 0, '')
end

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

Sets the rgb values for the LEDs with the given length starting from index.

The maximum length is 16, the index goes from 0 to 319 and the rgb values have 8 bits each.

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.



102
103
104
# File 'lib/tinkerforge/bricklet_led_strip.rb', line 102

def set_rgb_values(index, length, r, g, b)
  send_request(FUNCTION_SET_RGB_VALUES, [index, length, r, g, b], 'S C C16 C16 C16', 0, '')
end