Module: WS2801

Defined in:
lib/ws2801.rb

Overview

Controller for: RGB Pixel with WS2801 Chip build for Diffused Digital RGB LED Pixels from Adafruits on Raspberry Pi but should work on any device and the same led chip © 2013 Roman Pramberger (roman@pramberger.ch)

WS2801 user-space driver

Defined Under Namespace

Modules: Effects

Constant Summary

@@options =
{
	:len => 25,
	:strip => [],
	:device => "/dev/spidev0.0",
	:autowrite => true
}

Class Method Summary collapse

Class Method Details

.autowrite(autowrit = nil) ⇒ Object

Set/read current Autowrite option Write after each set (default: true)

Example;

>> WS2801.autowrite
>> WS2801.autowrite @newstrip


61
62
63
64
# File 'lib/ws2801.rb', line 61

def self.autowrite autowrit = nil
	return @@options[:autowrite] if autowrit.nil?
	@@options[:autowrite] = autowrit
end

.device(dev = nil) ⇒ Object

Set/read device

Example:

>> WS2801.device("/dev/spidev0.0")
>> WS2801.device
=> "/dev/spidev0.0"

Arguments (or nil):

device: (String)


40
41
42
43
# File 'lib/ws2801.rb', line 40

def self.device dev = nil
	return @@options[:device] if dev.nil?
	@@options[:device] = dev
end

.fade(options = {}) ⇒ Object

Fade pixel to color

Example:

>> WS2801.set { :r => 255, :pixel => [1..10], :timeout => 0.1 }
>> WS2801.set { :g => 128, :pixel => :all }
>> WS2801.set { :r => 40, :g => 255, :b => 200, :pixel => 4 }

Options:

:pixel => []      # array with pixel ids
:timeout => (Float)
:r => (Integer)
:g => (Integer)
:b => (Integer)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ws2801.rb', line 128

def self.fade options = {}
	WS2801.generate if @@options[:strip].length == 0
	options[:pixel] = (0..(WS2801.length-1)).to_a if options[:pixel].nil? or options[:pixel] == :all
	options[:pixel] = [options[:pixel]] if options[:pixel].is_a? Numeric
	options[:r] = 0 if options[:r].nil?
	options[:g] = 0 if options[:g].nil?
	options[:b] = 0 if options[:b].nil?

	while true
		options[:pixel].each do |i|
			#next if @@options[:strip][(i*3+2)] == options[:b] and @@options[:strip][(i*3+1)] == options[:g] and @@options[:strip][(i*3)] == options[:r]
			if @@options[:strip][(i*3)]   > options[:r]
				@@options[:strip][(i*3)]   -= 1
			elsif @@options[:strip][(i*3)]   < options[:r]
				@@options[:strip][(i*3)]   += 1
			end
			if @@options[:strip][(i*3+1)] > options[:g]
				@@options[:strip][(i*3+1)] -= 1 
			elsif @@options[:strip][(i*3+1)] < options[:g]
				@@options[:strip][(i*3+1)] += 1
			end 
			if @@options[:strip][(i*3+2)] > options[:b]
				@@options[:strip][(i*3+2)] -= 1
			elsif @@options[:strip][(i*3+2)] < options[:b]
				@@options[:strip][(i*3+2)] -= 1
			end
		end
		(breakme = true; break) if @@options[:strip][(i*3+2)] == options[:b] and @@options[:strip][(i*3+1)] == options[:g] and 
@@options[:strip][(i*3)] == 
options[:r]
		WS2801.write if @@options[:autowrite]
		break if breakme

		sleep(options[:timeout] || 0.01)
	end
end

.generateObject

Generate empty strip array

Example:

>> WS2801.generate


70
71
72
# File 'lib/ws2801.rb', line 70

def self.generate
	@@options[:strip] = Array.new(@@options[:len]*3+1) { 0 }
end

.get(pixel) ⇒ Object

Get Pixel

Example:

>> WS2801.get 1
=> [255,0,0]

Arguments:

pixel - Pixel id


173
174
175
# File 'lib/ws2801.rb', line 173

def self.get pixel
	[@@options[:strip][pixel*3], @@options[:strip][pixel*3+1], @@options[:strip][pixel*3+2]]
end

.length(len = nil) ⇒ Object

Set/read length of strip

Example:

>> WS2801.length(25)
>> WS2801.length
=> 25

Arguments (or nil):

count: (Integer)


26
27
28
29
# File 'lib/ws2801.rb', line 26

def self.length len = nil
	return @@options[:len] if len.nil?
	@@options[:len] = len
end

.offObject

Set off

Example:

>> WS2801.off


181
182
183
184
# File 'lib/ws2801.rb', line 181

def self.off
	WS2801.generate
	WS2801.write if @@options[:autowrite]
end

.set(options = {}) ⇒ Object

Set pixel to color

Example:

>> WS2801.set { :r => 255, :pixel => [1..10] }
>> WS2801.set { :g => 128, :pixel => :all }
>> WS2801.set { :r => 40, :g => 255, :b => 200, :pixel => 4 }

Options:

:pixel => []      # array with pixel ids
:r => (Integer)
:g => (Integer)
:b => (Integer)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ws2801.rb', line 103

def self.set options = {}
	WS2801.generate if @@options[:strip].length == 0
	options[:pixel] = (0..(WS2801.length-1)).to_a if options[:pixel].nil? or options[:pixel] == :all
	options[:pixel] = [options[:pixel]] if options[:pixel].is_a? Numeric
	options[:pixel].each do |i|
		@@options[:strip][(i*3)]   = options[:r] || 0
		@@options[:strip][(i*3)+1] = options[:g] || 0
		@@options[:strip][(i*3)+2] = options[:b] || 0
	end
	WS2801.write if @@options[:autowrite]
end

.strip(strip = nil) ⇒ Object

Set/read current Strip

Example;

>> WS2801.strip
>> WS2801.strip @newstrip


50
51
52
53
# File 'lib/ws2801.rb', line 50

def self.strip strip = nil
	return @@options[:strip] if strip.nil?
	@@options[:strip] = strip
end

.writeObject

Write colors to the device (this needs root rights)

Example:

>> WS2801.write


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ws2801.rb', line 79

def self.write
	return false if @@options[:strip].nil?

	@@options[:strip].each_with_index do |s,i|
		@@options[:strip][i] = 0 if @@options[:strip][i].nil?
	end

	File.open(@@options[:device], 'w') do |file|
		file.write(@@options[:strip].pack('C*'))
	end
end