Class: LittleWire::WS2811

Inherits:
Object
  • Object
show all
Defined in:
lib/littlewire/ws2811.rb

Overview

Output colours to ws2812 strips and other 800khz ws2811 led devices To use, simply set colours in the ‘colors’ array property as css color strings or Colorist::Color objects, then call #output(pin) to send it on it’s way

Note that this requires firmware v1.2 and there is a maximum of 64 lights in the firmware at the time of writing. You can connect 64 leds to each of the digital pins on the LittleWire or Digispark device, and this enables you a total of 64 * 4 = 256 lights! Neato! Remember a USB port cannot supply enough current to power 256 lights at full brightness. Power usage is roughly (20ma * color_channel) per light. So 64 lights all lit full brightness white consumes 20*3*64 = 3.84 amps! Wow that’s a lot of light for one little wire! And you can have four of those!

Constant Summary collapse

ColorTransformer =
{
  rgb: ->(input) {
    input # passthrough
  },
  grb: ->(input) {
    Colorist::Color.from_rgb(input.g, input.r, input.b)
  },
  bgr: ->(input) {
    Colorist::Color.from_rgb(input.b, input.r, input.g)
  },
  gbr: ->(input) {
    Colorist::Color.from_rgb(input.g, input.b, input.r)
  },
  rbg: ->(input) {
    Colorist::Color.from_rgb(input.r, input.b, input.g)
  },
  greyscale: ->(input) {
    grey = (input.r + input.g + input.b) / 3 # average the colours
    Colorist::Color.from_rgb(grey, grey, grey)
  },
  # lookup chart
  florapixel_v1: :rbg,
  florapixels_v1: :florapixel_v1,
  ws2812: :rgb,
  florapixel_v2: :ws2812,
  florapixels_v2: :florapixel_v2,
  grayscale: :greyscale, # Woo English!
  white: :greyscale,
}
ChannelSize =

LittleWire can store 64 values

64

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wire, default_pin = false) ⇒ WS2811

:nodoc:



49
50
51
52
53
54
55
# File 'lib/littlewire/ws2811.rb', line 49

def initialize wire, default_pin = false # :nodoc:
  @wire = wire
  @pin = default_pin
  @colors = []
  @wiring = :rgb
  @wiring_map = ColorTransformer[:rgb]
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors.



15
16
17
# File 'lib/littlewire/ws2811.rb', line 15

def colors
  @colors
end

#pinObject

Returns the value of attribute pin.



16
17
18
# File 'lib/littlewire/ws2811.rb', line 16

def pin
  @pin
end

#wiringObject

Returns the value of attribute wiring.



17
18
19
# File 'lib/littlewire/ws2811.rb', line 17

def wiring
  @wiring
end

Instance Method Details

#black!Object

Set the whole strip to be black! This can be nice at the start of your program, because the strip starts out being whatever colours it was when it was powered up, which can be random - this makes sure everything is black, at least up to the max 64 LEDs littlewire supports per channel



118
119
120
# File 'lib/littlewire/ws2811.rb', line 118

def black!
  send(['black'] * ChannelSize)
end

#output(pin = nil) ⇒ Object

send colours to strip, optionally specifying a pin if not specified via

littlewire.ws2811(pin).output


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/littlewire/ws2811.rb', line 90

def output pin = nil
  colors_buffer = @colors.map { |i| @wiring_map[i.is_a?(Colorist::Color) ? i : i.to_color] }
  output_pin = @wire.get_pin(LittleWire::DigitalPinMap, pin || @pin)
  raise "Must specify output pin for ws2811 strip" unless output_pin.is_a? Integer
  
  until colors_buffer.empty?
    
    if colors_buffer.length > 1
      preload(colors_buffer.shift)
      
    elsif colors_buffer.length == 1
      write(colors_buffer.shift, output_pin)
      
    end
  end
end

#send(*colors) ⇒ Object Also known as: set

Set strip to an array of colours, automatically outputting them to the strip immediately



108
109
110
111
# File 'lib/littlewire/ws2811.rb', line 108

def send *colors
  @colors = colors.flatten
  output
end