Class: Denko::LED::APA102

Inherits:
Object
  • Object
show all
Includes:
SPI::Peripheral
Defined in:
lib/denko/led/apa102.rb

Instance Attribute Summary collapse

Attributes included from SPI::Peripheral

#spi_bit_order, #spi_frequency, #spi_mode

Attributes included from Behaviors::BusPeripheral

#address

Attributes included from Behaviors::Component

#board

Attributes included from Behaviors::Callbacks

#callback_mutex

Attributes included from Behaviors::SinglePin

#mode, #pin

Instance Method Summary collapse

Methods included from SPI::Peripheral

#spi_listen, #spi_read, #spi_stop, #spi_transfer, #spi_write

Methods included from Behaviors::BusPeripheral

#atomically

Methods included from Behaviors::Component

#initialize, #micro_delay

Methods included from Behaviors::State

#initialize, #state

Methods included from Behaviors::Callbacks

#add_callback, #callbacks, #initialize, #pre_callback_filter, #remove_callback, #update

Instance Attribute Details

#bppObject (readonly)

Returns the value of attribute bpp.



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

def bpp
  @bpp
end

#brightnessObject

Returns the value of attribute brightness.



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

def brightness
  @brightness
end

#lengthObject (readonly)

Returns the value of attribute length.



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

def length
  @length
end

Instance Method Details

#[]=(index, array) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/denko/led/apa102.rb', line 55

def []=(index, array)
  # Per-pixel brightness control, as optional 3rd indexed element of array.
  if array[3]
    @buffer[index*bpp+0] = 0b11100000 | array[3]
  end
  
  # APA102 uses BGR ordering.
  @buffer[index*bpp+1] = array[2]
  @buffer[index*bpp+2] = array[1]
  @buffer[index*bpp+3] = array[0]
end

#after_initialize(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/denko/led/apa102.rb', line 13

def after_initialize(options={})
  super(options)

  raise ArgumentError, "no length given for APA102 array" unless options[:length]
  @length = options[:length]

  # Start frame is always 32 0-bits (4 bytes).
  @start_frame = Array.new(4) { 0 }

  # End frame must be at least @length/2 bits long, and 32 bits (4 bytes) minimum.
  end_frame_bytes = (@length / 16.0).ceil
  end_frame_bytes = 4 if end_frame_bytes < 4

  # Use all 0's for end frame instead of 1's. Prevents extra pixel when using partial strip.
  @end_frame = Array.new(end_frame_bytes) { 0 }

  # This is BYTES per pixel, not bits per pixel.
  # 0th byte is per-pixel brightness (PWM applied to all 3 colors).
  @bpp = 4
  @buffer = Array.new(length * bpp) { 0 }

  # Default to max brightness.
  self.brightness = 31
  
  off
end

#all_onObject



67
68
69
70
71
# File 'lib/denko/led/apa102.rb', line 67

def all_on
  self.brightness = 31
  @buffer = @buffer.each_slice(bpp).map { [@masked_brightness,255,255,255] }.flatten
  show
end

#before_initialize(options = {}) ⇒ Object



8
9
10
11
# File 'lib/denko/led/apa102.rb', line 8

def before_initialize(options={})
  options[:pin] = 255
  super(options)
end

#clearObject



78
79
80
# File 'lib/denko/led/apa102.rb', line 78

def clear
  @buffer = @buffer.each_slice(bpp).map { [@masked_brightness,0,0,0] }.flatten
end

#offObject



73
74
75
76
# File 'lib/denko/led/apa102.rb', line 73

def off
  clear
  show
end

#showObject



82
83
84
# File 'lib/denko/led/apa102.rb', line 82

def show
  spi_write(@start_frame + @buffer + @end_frame)
end