Class: PixelPi::Leds

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pixel_pi/fake_leds.rb,
ext/pixel_pi/leds.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#PixelPi::Leds.new(length, gpio, options = {}) ⇒ Object

Create a new PixelPi::Leds instance that can be used to control a string of NeoPixels from a RaspberryPi. The length of the pixel string must be given as well as the GPIO pin number used to control the string. The remaining options have sensible defaults.

length - the nubmer of leds in the string gpio - the GPIO pin number options - Hash of arguments

:dma        - DMA channel defaults to 5
:frequency  - output frequency defaults to 800,000 Hz
:invert     - defaults to `false`
:brightness - defaults to 255


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pixel_pi/fake_leds.rb', line 35

def initialize( length, gpio, options = {} )
  @leds       = [0] * length
  @gpio       = gpio
  @dma        = options.fetch(:dma, 5)
  @frequency  = options.fetch(:frequency, 800_000)
  @invert     = options.fetch(:invert, false)
  @brightness = options.fetch(:brightness, 255)
  @debug      = options.fetch(:debug, false)

  if @debug
    require "rainbow"
    @debug = "" unless @debug.is_a?(String) && !@debug.empty?
  end
end

Instance Attribute Details

#brightnessObject

Returns the brightness.



260
261
262
# File 'ext/pixel_pi/leds.c', line 260

def brightness
  @brightness
end

#dmaObject (readonly)

Returns the DMA channel used to control the pixels.



220
221
222
# File 'ext/pixel_pi/leds.c', line 220

def dma
  @dma
end

#frequencyObject (readonly)

Returns the output frequency.



232
233
234
# File 'ext/pixel_pi/leds.c', line 232

def frequency
  @frequency
end

#gpioObject (readonly)

Returns the GPIO number used to control the pixels.



208
209
210
# File 'ext/pixel_pi/leds.c', line 208

def gpio
  @gpio
end

#invertObject (readonly)

Returns ‘true` if the invert flag is set and `false` if it is not set.



244
245
246
# File 'ext/pixel_pi/leds.c', line 244

def invert
  @invert
end

Instance Method Details

#[](num) ⇒ Object

Get the 24-bit RGB color value for the LED at position ‘num`.

Returns a 24-bit RGB color value.



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'ext/pixel_pi/leds.c', line 350

static VALUE
pp_leds_get_pixel_color( VALUE self, VALUE num )
{
  ws2811_t *ledstring = pp_leds_struct( self );
  ws2811_channel_t channel = ledstring->channel[0];

  int n = FIX2INT(num);
  if (n < 0 || n >= channel.count) {
    rb_raise( rb_eIndexError, "index %d is outside of LED range: 0...%d", n, channel.count-1 );
  }

  return INT2FIX(channel.leds[n]);
}

#[]=(num) ⇒ Object #[]=(num) ⇒ Object

Set the LED at position ‘num` to the provided 24-bit RGB color value.

Returns the 24-bit RGB color value.



97
98
99
100
101
102
103
# File 'lib/pixel_pi/fake_leds.rb', line 97

def []=( num, value )
  closed!
  if (num < 0 || num >= @leds.length)
    raise IndexError, "index #{num} is outside of LED range: 0...#{@leds.length-1}"
  end
  @leds[num] = to_color(value)
end

#clearObject

Clear the display. This will set all values in the LED buffer to zero, and then update the display. All pixels will be turned off by this method.

Returns this PixelPi::Leds instance.



76
77
78
79
80
# File 'lib/pixel_pi/fake_leds.rb', line 76

def clear
  closed!
  @leds.fill 0
  self
end

#closeObject

Shutdown the NeoPixels connected to the DMA / PWM channel. After this method the current PixelPi::Leds instance will no longer be usable; a new instance will need to be created. This method is automatically invoked when the instance is deallcoated by the Ruby garbage collector. It does not need to be explicitly invoked.

Returns ‘nil`.



89
90
91
92
# File 'lib/pixel_pi/fake_leds.rb', line 89

def close
  $stdout.puts if @debug
  @leds = nil
end

#fill(color) ⇒ Object #fill(color, start[, length]) ⇒ Object #fill(color, range) ⇒ Object #fill {|index| ... } ⇒ Object #fill(start[, length]) {|index| ... } ⇒ Object #fill(range) {|index| ... } ⇒ Object

Set the selected LEDs to the given ‘color`. The `color` msut be given as a 24-bit RGB value. You can also supply a block that receives an LED index and returns a 24-bit RGB color.

Examples:

leds.fill( 0x00FF00 )
leds.fill( 0xFF0000, 2, 2 )
leds.fill( 0x0000FF, (4...8) )
leds.fill { |i| 256 << i }

Returns this PixelPi::Leds instance.

Overloads:

  • #fill {|index| ... } ⇒ Object

    Yields:

    • (index)
  • #fill(start[, length]) {|index| ... } ⇒ Object

    Yields:

    • (index)
  • #fill(range) {|index| ... } ⇒ Object

    Yields:

    • (index)


172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/pixel_pi/fake_leds.rb', line 172

def fill( *args )
  closed!
  if block_given?
    @leds.fill do |ii|
      value = yield(ii)
      to_color(value)
    end
  else
    value = to_color(args.shift)
    @leds.fill(value, *args)
  end
  self
end

#lengthObject

Returns the number of pixels in the LED string.



196
197
198
199
200
201
# File 'ext/pixel_pi/leds.c', line 196

static VALUE
pp_leds_length_get( VALUE self )
{
  ws2811_t *ledstring = pp_leds_struct( self );
  return INT2FIX(ledstring->channel[0].count);
}

#replace(ary) ⇒ Object

Replace the LED colors with the 24-bit RGB color values found in the ‘ary`. If the `ary` is longer than the LED string then the extra color values will be ignored. If the `ary` is shorter than the LED string then only the LEDS up to `ary.length` will be changed.

You must call ‘show` for the new colors to be displayed.

Returns this PixelPi::Leds instance.



133
134
135
136
137
138
139
# File 'lib/pixel_pi/fake_leds.rb', line 133

def replace( ary )
  closed!
  @leds.length.times do |ii|
    @leds[ii] = Integer(ary[ii])
  end
  self
end

#reverseObject

Reverse the order of the LED colors.

Returns this PixelPi::Leds instance.



144
145
146
147
148
# File 'lib/pixel_pi/fake_leds.rb', line 144

def reverse
  closed!
  @leds.reverse!
  self
end

#rotate(count = 1) ⇒ Object

Rotates the LED colors in place so that the color at ‘count` comes first. If `count` is negative then it rotates in the opposite direction, starting from the end of the LEDs where -1 is the last LED.

Returns this PixelPi::Leds instance.



155
156
157
158
159
# File 'lib/pixel_pi/fake_leds.rb', line 155

def rotate( *args )
  closed!
  @leds.rotate!(*args)
  self
end

#set_pixel(num, color) ⇒ Object #set_pixel(num, red, green, blue) ⇒ Object

Set the LED at position ‘num` to the given color. The color can be a single 24-bit RGB `color` value or three separate color values - one for `red`, one for `green`, and one for `blue`.

Returns this PixelPi::Leds instance.



110
111
112
113
# File 'lib/pixel_pi/fake_leds.rb', line 110

def set_pixel( num, *args )
  self[num] = to_color(*args)
  self
end

#showObject

Update the display with the data from the LED buffer.

Returns this PixelPi::Leds instance.



65
66
67
68
69
70
71
72
# File 'lib/pixel_pi/fake_leds.rb', line 65

def show
  closed!
  if @debug
    ary = @leds.map { |value| Rainbow(@debug).color(*to_rgb(value)) }
    $stdout.print "\r#{ary.join}"
  end
  self
end

#to_aObject

Takes the current list of 24-bit RGB values stored in the LED strings and returns them as an Array. These colors might not be actively displayed; it all depends if ‘show` has been called on the PixelPi::Leds instance.

Returns an Array of 24-bit RGB values.



120
121
122
123
# File 'lib/pixel_pi/fake_leds.rb', line 120

def to_a
  closed!
  @leds.dup
end