Module: WS2801

Defined in:
lib/ws2801.rb

Overview

WS2801 driver

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


59
60
61
62
# File 'lib/ws2801.rb', line 59

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)


38
39
40
41
# File 'lib/ws2801.rb', line 38

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

.generateObject

Generate empty strip array

Example:

>> WS2801.generate


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

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

.length(len = nil) ⇒ Object

Set/read length of strip

Example:

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

Arguments (or nil):

count: (Integer)


24
25
26
27
# File 'lib/ws2801.rb', line 24

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

.offObject

Set off black

Example:

>> WS2801.off


121
122
123
124
# File 'lib/ws2801.rb', line 121

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

.set(options = {}) ⇒ Object

Set pixel to color

Example:

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

Options:

:list => []      # color in pixels in list
:r => (Integer)
:g => (Integer)
:b => (Integer)


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

def self.set options = {}
	self.generate if @@options[:strip].length == 0
	options[:list] = (0..(self.length-1)).to_a if options[:list].nil? or options[:list] == :all
	options[:list] = [options[:list]] if options[:list].is_a? Numeric
	options[:list].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
	self.write if @@options[:autowrite] == true
end

.strip(strip = nil) ⇒ Object

Set/read current Strip

Example;

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


48
49
50
51
# File 'lib/ws2801.rb', line 48

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


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

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