Class: Denko::LED::WS2812

Inherits:
Object
  • Object
show all
Includes:
Behaviors::SinglePin
Defined in:
lib/denko/led/ws2812.rb

Instance Attribute Summary collapse

Attributes included from Behaviors::SinglePin

#mode, #pin

Attributes included from Behaviors::Component

#board

Instance Method Summary collapse

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods included from Behaviors::State

#initialize, #state

Instance Attribute Details

#bppObject (readonly)

Returns the value of attribute bpp.



6
7
8
# File 'lib/denko/led/ws2812.rb', line 6

def bpp
  @bpp
end

#lengthObject (readonly)

Returns the value of attribute length.



6
7
8
# File 'lib/denko/led/ws2812.rb', line 6

def length
  @length
end

Instance Method Details

#[]=(index, array) ⇒ Object



18
19
20
21
22
23
# File 'lib/denko/led/ws2812.rb', line 18

def []=(index, array)
  # Just do GRB order for now.
  buffer[index*bpp]   = array[1]
  buffer[index*bpp+1] = array[0]
  buffer[index*bpp+2] = array[2]
end

#after_initialize(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
# File 'lib/denko/led/ws2812.rb', line 8

def after_initialize(options={})
  super(options)
  raise ArgumentError, "no length given for WS2812 array" unless options[:length]
  @length = options[:length]

  # This is BYTES per pixel, not bits per pixel.
  @bpp = 3
  off
end

#all_onObject



29
30
31
32
# File 'lib/denko/led/ws2812.rb', line 29

def all_on
  buffer.map! { 255 } # should scale with brightness
  show
end

#bufferObject



25
26
27
# File 'lib/denko/led/ws2812.rb', line 25

def buffer
  @buffer ||= Array.new(length * bpp) { 0 }
end

#clearObject



39
40
41
# File 'lib/denko/led/ws2812.rb', line 39

def clear
  buffer.map! { 0 }
end

#offObject



34
35
36
37
# File 'lib/denko/led/ws2812.rb', line 34

def off
  clear
  show
end

#showObject



43
44
45
# File 'lib/denko/led/ws2812.rb', line 43

def show
  board.show_ws2812(self.pin, self.buffer)
end