Class: Neotrellis::Neopixel
- Inherits:
-
Object
- Object
- Neotrellis::Neopixel
- Defined in:
- lib/neotrellis/neopixel.rb
Overview
Neopixel is the driver of the RGB led array on the Neotrellis device.
Defined Under Namespace
Classes: Color
Constant Summary collapse
- DEFAULT_PIXEL_NUMBER =
Default number of leds on the neotrellis 4x4 keypad
16
- OFF =
Default common colors
Color.new(0, 0, 0)
- RED =
Color.new(255, 0, 0)
- YELLOW =
Color.new(255, 150, 0)
- GREEN =
Color.new(0, 255, 0)
- CYAN =
Color.new(0, 255, 255)
- BLUE =
Color.new(0, 0, 255)
- PURPLE =
Color.new(180, 0, 255)
- WHITE =
Color.new(255, 255, 255)
Instance Attribute Summary collapse
-
#autoshow ⇒ Object
Enable autoshow feature.
-
#brightness ⇒ Object
Get the brightness of the leds.
Instance Method Summary collapse
-
#fill(color) ⇒ Object
Set the same color for all pixels of the array.
-
#fill_random ⇒ Object
Set a different random color for every pixels of the array.
-
#initialize(seesaw, size: DEFAULT_PIXEL_NUMBER, autoshow: true, brightness: 1.0) ⇒ Neopixel
constructor
Initialize a neopixel array driven by a seesaw chip.
-
#off ⇒ Object
Swtich off all pixels of the array.
-
#set(pixel, color) ⇒ Object
Set the color of one pixel.
-
#show ⇒ Object
Render the data already written in the Seesaw buffer.
Constructor Details
#initialize(seesaw, size: DEFAULT_PIXEL_NUMBER, autoshow: true, brightness: 1.0) ⇒ Neopixel
Initialize a neopixel array driven by a seesaw chip.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/neotrellis/neopixel.rb', line 73 def initialize(seesaw, size: DEFAULT_PIXEL_NUMBER, autoshow: true, brightness: 1.0) @seesaw = seesaw @pin = 3 # NeoPixel bus is on SeeSaw's pin 3 @n = size # Number of NeoPixels on the bus @bpp = 3 # 3 bytes per pixel @autoshow = autoshow # Automaticaly display data in buffer @brightness = [[brightness, 0.0].max, 1.0].min # Size of RGB buffer, 2 bytes for Unsigned Int Big Endian buf_length = [@n*@bpp].pack('S>').unpack('C*') @seesaw.write(NEOPIXEL_BASE, NEOPIXEL_PIN, @pin) @seesaw.write(NEOPIXEL_BASE, NEOPIXEL_BUF_LENGTH, *buf_length) end |
Instance Attribute Details
#autoshow ⇒ Object
Enable autoshow feature. Automatically call ‘show()` after each update.
50 51 52 |
# File 'lib/neotrellis/neopixel.rb', line 50 def autoshow @autoshow end |
#brightness ⇒ Object
Get the brightness of the leds.
49 50 51 |
# File 'lib/neotrellis/neopixel.rb', line 49 def brightness @brightness end |
Instance Method Details
#fill(color) ⇒ Object
Set the same color for all pixels of the array. If ‘autoshow` is false nothing will be displayed until you call the `show()` method.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/neotrellis/neopixel.rb', line 119 def fill(color) # Disable auto show while filling the buffer current_autoshow = @autoshow @autoshow=false @n.times do |pixel| set(pixel, color) end @autoshow = current_autoshow show if @autoshow end |
#fill_random ⇒ Object
Set a different random color for every pixels of the array. If ‘autoshow` is false nothing will be displayed until you call the `show()` method.
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/neotrellis/neopixel.rb', line 134 def fill_random() # Disable auto show while filling the buffer current_autoshow = @autoshow @autoshow=false @n.times do |pixel| set(pixel, Color.new(rand(255), rand(255), rand(255))) end @autoshow = current_autoshow show if @autoshow end |
#off ⇒ Object
Swtich off all pixels of the array. If ‘autoshow` is false nothing will be displayed until you call the `show()` method.
149 150 151 |
# File 'lib/neotrellis/neopixel.rb', line 149 def off() fill(OFF) end |
#set(pixel, color) ⇒ Object
Set the color of one pixel. If ‘autoshow` is false nothing will be displayed until you call the `show()` method.
102 103 104 105 106 107 |
# File 'lib/neotrellis/neopixel.rb', line 102 def set(pixel, color) raise "pixel out of range" unless pixel.between?(0, @n-1) @seesaw.write(NEOPIXEL_BASE, NEOPIXEL_BUF, *([pixel*@bpp].pack('S>').unpack('C*')), *color.to_b(brightness)) show if @autoshow end |
#show ⇒ Object
Render the data already written in the Seesaw buffer. If ‘autoshow` is true, this method is called automatically by `set()` and `fill()`
111 112 113 |
# File 'lib/neotrellis/neopixel.rb', line 111 def show @seesaw.write(NEOPIXEL_BASE, NEOPIXEL_SHOW) end |