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 ([email protected])
WS2801 user-space driver
Defined Under Namespace
Modules: Effects
Constant Summary collapse
- @@options =
{ :len => 25, :strip => [], :device => "/dev/spidev0.0", :autowrite => true }
Class Method Summary collapse
-
.autowrite(autowrit = nil) ⇒ Object
Set/read current Autowrite option Write after each set (default: true).
-
.device(dev = nil) ⇒ Object
Set/read device.
-
.fade(options = {}) ⇒ Object
Fade pixel to color.
-
.generate ⇒ Object
Generate empty strip array.
-
.get(pixel) ⇒ Object
Get Pixel.
-
.length(len = nil) ⇒ Object
Set/read length of strip.
-
.off ⇒ Object
Set off.
-
.set(options = {}) ⇒ Object
Set pixel to color.
-
.strip(strip = nil) ⇒ Object
Set/read current Strip.
-
.write ⇒ Object
Write colors to the device (this needs root rights).
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 [:autowrite] if autowrit.nil? [: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 [:device] if dev.nil? [: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 = {} WS2801.generate if [:strip].length == 0 [:pixel] = (0..(WS2801.length-1)).to_a if [:pixel].nil? or [:pixel] == :all [:pixel] = [[:pixel]] if [:pixel].is_a? Numeric [:r] = 0 if [:r].nil? [:g] = 0 if [:g].nil? [:b] = 0 if [:b].nil? while true [: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 [:strip][(i*3)] > [:r] [:strip][(i*3)] -= 1 elsif [:strip][(i*3)] < [:r] [:strip][(i*3)] += 1 end if [:strip][(i*3+1)] > [:g] [:strip][(i*3+1)] -= 1 elsif [:strip][(i*3+1)] < [:g] [:strip][(i*3+1)] += 1 end if [:strip][(i*3+2)] > [:b] [:strip][(i*3+2)] -= 1 elsif [:strip][(i*3+2)] < [:b] [:strip][(i*3+2)] -= 1 end end (breakme = true; break) if [:strip][(i*3+2)] == [:b] and [:strip][(i*3+1)] == [:g] and [:strip][(i*3)] == [:r] WS2801.write if [:autowrite] break if breakme sleep([:timeout] || 0.01) end end |
.generate ⇒ Object
Generate empty strip array
Example:
>> WS2801.generate
70 71 72 |
# File 'lib/ws2801.rb', line 70 def self.generate [:strip] = Array.new([: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 [[:strip][pixel*3], [:strip][pixel*3+1], [: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 [:len] if len.nil? [:len] = len end |
.off ⇒ Object
Set off
Example:
>> WS2801.off
181 182 183 184 |
# File 'lib/ws2801.rb', line 181 def self.off WS2801.generate WS2801.write if [: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 = {} WS2801.generate if [:strip].length == 0 [:pixel] = (0..(WS2801.length-1)).to_a if [:pixel].nil? or [:pixel] == :all [:pixel] = [[:pixel]] if [:pixel].is_a? Numeric [:pixel].each do |i| [:strip][(i*3)] = [:r] || 0 [:strip][(i*3)+1] = [:g] || 0 [:strip][(i*3)+2] = [:b] || 0 end WS2801.write if [: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 [:strip] if strip.nil? [:strip] = strip end |
.write ⇒ Object
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 [:strip].nil? [:strip].each_with_index do |s,i| [:strip][i] = 0 if [:strip][i].nil? end File.open([:device], 'w') do |file| file.write([:strip].pack('C*')) end end |