Class: Ws2812::UnicornHAT

Inherits:
Object
  • Object
show all
Defined in:
lib/ws2812/unicornhat.rb

Overview

Provides interface for *Unicorn HAT* (a 8x8 ws2812 matrix by Pimoroni)

This particular class is heavily inspired by the unicornhat.py class from UnicornHat’s repo.

See:

unicornhat

github.com/pimoroni/unicorn-hat

Constant Summary collapse

UHAT_MAP =
[
	[7 ,6 ,5 ,4 ,3 ,2 ,1 ,0 ],
	[8 ,9 ,10,11,12,13,14,15],
	[23,22,21,20,19,18,17,16],
	[24,25,26,27,28,29,30,31],
	[39,38,37,36,35,34,33,32],
	[40,41,42,43,44,45,46,47],
	[55,54,53,52,51,50,49,48],
	[56,57,58,59,60,61,62,63]
]

Instance Method Summary collapse

Constructor Details

#initialize(pin = 18, brightness = 50, options = {}) ⇒ UnicornHAT

Initializes the matrix ws2812 driver at given pin, with initial brightness (0..255)

The options hash can contain various additional options, see Basic class’ description of options for more details.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ws2812/unicornhat.rb', line 28

def initialize(pin = 18, brightness = 50, options = {})
	@hat = Basic.new(64, pin, brightness, options)
	@hat.open

	@rotation = 0
	@pixels = Array.new(8) { |x| Array.new(8) { |y| Color.new(0,0,0) } }
	push_all_pixels
rescue Object
	@hat = nil
	raise
end

Instance Method Details

#[](x, y) ⇒ Object

Return Color of led located at given x, y



119
120
121
# File 'lib/ws2812/unicornhat.rb', line 119

def [](x, y)
	@pixels[x][y]
end

#[]=(x, y, color) ⇒ Object

Set given pixel identified by x, y to color

See set for a method that takes individual r, g, b components.

You still have to call show to make the changes visible.



79
80
81
82
83
# File 'lib/ws2812/unicornhat.rb', line 79

def []=(x, y, color)
	check_coords(x, y)
	@pixels[x][y] = color
	@hat[map_coords(x, y)] = color
end

#brightnessObject

Return brightness used for all pixels

The value is from 0 to 255 and is internally used as a scaler for all colors values that are supplied via []=



113
114
115
# File 'lib/ws2812/unicornhat.rb', line 113

def brightness
	@hat.brightness
end

#brightness=(val) ⇒ Object

Set brightness used for all pixels

The value is from 0 to 255 and is internally used as a scaler for all colors values that are supplied via []=

You still have to call show to make the changes visible.



104
105
106
# File 'lib/ws2812/unicornhat.rb', line 104

def brightness=(val)
	@hat.brightness = val
end

#clear(do_show = true) ⇒ Object

Clears all pixels (sets them to black) and calls show if do_show



144
145
146
147
# File 'lib/ws2812/unicornhat.rb', line 144

def clear(do_show = true)
	set_all(Color.new(0, 0, 0))
	show if do_show
end

#closeObject

Closes (deinitializes) communication with the matrix

Can be called on already closed device just fine



55
56
57
58
59
60
61
# File 'lib/ws2812/unicornhat.rb', line 55

def close
	if @hat
		@hat.close
		@hat = nil
	end
	self
end

#direct=(value) ⇒ Object

Set direct mode. See Ws2812::Basic for explanation.



47
48
49
# File 'lib/ws2812/unicornhat.rb', line 47

def direct=(value)
	@hat.direct = value
end

#direct?Boolean

Query direct mode. See Ws2812::Basic for explanation.

Returns:

  • (Boolean)


41
42
43
# File 'lib/ws2812/unicornhat.rb', line 41

def direct?
	@hat.direct?
end

#push_all_pixelsObject

Pushes all pixels from buffer to the lower level (physical device)

This is internally used when changing rotation but it can be useful when you set several pixels to the same Color instance and then manipulate those pixels’ color all at once.



167
168
169
170
171
172
173
# File 'lib/ws2812/unicornhat.rb', line 167

def push_all_pixels
	0.upto(7) do |x|
		0.upto(7) do |y|
			@hat[map_coords(x, y)] = @pixels[x][y]
		end
	end
end

#rotationObject

Returns current rotation as integer; one of: [0, 90, 180, 270]



125
126
127
# File 'lib/ws2812/unicornhat.rb', line 125

def rotation
	@rotation
end

#rotation=(val) ⇒ Object

Set rotation of the Unicorn HAT to val

Permissible values for rotation are [0, 90, 180, 270] (mod 360).

You still have to call show to make the changes visible.



135
136
137
138
139
140
# File 'lib/ws2812/unicornhat.rb', line 135

def rotation=(val)
	permissible = [0, 90, 180, 270]
	fail ArgumentError, "invalid rotation, permissible: #{permissible.join(', ')}" unless permissible.include?(val % 360)
	@rotation = val % 360
	push_all_pixels
end

#set(x, y, r, g, b) ⇒ Object

Set given pixel identified by x, y to r, g, b

See []= for a method that takes Color instance instead of individual components.

You still have to call show to make the changes visible.



92
93
94
95
# File 'lib/ws2812/unicornhat.rb', line 92

def set(x, y, r, g, b)
	check_coords(x, y)
	self[x, y] = Color.new(r, g, b)
end

#set_all(color) ⇒ Object

Sets all pixels to color

You still have to call show to make the changes visible.



153
154
155
156
157
158
159
# File 'lib/ws2812/unicornhat.rb', line 153

def set_all(color)
	0.upto(7) do |x|
		0.upto(7) do |y|
			self[x, y] = color
		end
	end
end

#showObject

Apply all changes since last show

This method renders all changes (brightness, pixels, rotation) done to the strand since last show



68
69
70
# File 'lib/ws2812/unicornhat.rb', line 68

def show
	@hat.show
end