Class: Neotrellis::Neopixel

Inherits:
Object
  • Object
show all
Defined in:
lib/neotrellis/neopixel.rb

Overview

Neopixel is the driver of the RGB led array on the Neotrellis device.

Examples:

Turn on led #2 in red for 2 seconds with debug output

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E, debug: true)
pixels = Neotrellis::Neopixel.new(seesaw)
pixels.set(1, Neotrellis::Neopixel::RED)
sleep 2
pixels.set(1, Neotrellis::Neopixel::OFF)

Turn on all leds with color #b5f115 for 2 seconds

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E)
pixels = Neotrellis::Neopixel.new(seesaw)
pixels.fill(Neotrellis::Neopixel::Color.new(181, 241, 21))
sleep 2
pixels.off

Display white columns

seesaw = Neotrellis::Seesaw.new(device: "/dev/i2c-1", addr: 0x2E)
pixels = Neotrellis::Neopixel.new(seesaw, autoshow: false)
Neotrellis::Neopixel::DEFAULT_PIXEL_NUMBER.times { |i|
	pixels.set(i, Neotrellis::Neopixel::WHITE) unless i%2 == 0
}
pixels.show

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

Instance Method Summary collapse

Constructor Details

#initialize(seesaw, size: DEFAULT_PIXEL_NUMBER, autoshow: true, brightness: 1.0) ⇒ Neopixel

Initialize a neopixel array driven by a seesaw chip.

Parameters:

  • seesaw (Neotrellis::SeeSaw)

    Seesaw driver

  • size (Integer) (defaults to: DEFAULT_PIXEL_NUMBER)

    Number of leds on the array

  • autoshow (Boolean) (defaults to: true)

    Automatically call ‘show()` after each update

  • brightness (Float) (defaults to: 1.0)

    Brightness of the leds. Must be between 0.0 and 1.0



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

#autoshowObject

Enable autoshow feature. Automatically call ‘show()` after each update.



50
51
52
# File 'lib/neotrellis/neopixel.rb', line 50

def autoshow
  @autoshow
end

#brightnessObject

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.

Parameters:



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_randomObject

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

#offObject

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.

Parameters:

  • pixel (Integer)

    ID of the pixel in the array. Must be between 0 and ‘size`-1

  • color (Neotrellis::Neopixel::Color)

    Color to display by the pixel



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

#showObject

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