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)
-
+ (Object) autowrite(autowrit = nil)
Set/read current Autowrite option Write after each set (default: true).
-
+ (Object) device(dev = nil)
Set/read device.
-
+ (Object) generate
Generate empty strip array.
-
+ (Object) length(len = nil)
Set/read length of strip.
-
+ (Object) off
Set off black.
-
+ (Object) set(options = {})
Set pixel to color.
-
+ (Object) strip(strip = nil)
Set/read current Strip.
-
+ (Object) write
Write colors to the device (this needs root rights).
Class Method Details
+ (Object) autowrite(autowrit = nil)
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 |
+ (Object) device(dev = nil)
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 |
+ (Object) generate
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 |
+ (Object) length(len = nil)
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 |
+ (Object) off
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 |
+ (Object) set(options = {})
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 = {} self.generate if @@options[:strip].length == 0 [:list] = (0..(self.length-1)).to_a if [:list].nil? or [:list] == :all [:list] = [[:list]] if [:list].is_a? Numeric [:list].each do |i| @@options[:strip][(i*3)] = [:r] || 0 @@options[:strip][(i*3)+1] = [:g] || 0 @@options[:strip][(i*3)+2] = [:b] || 0 end self.write if @@options[:autowrite] == true end |
+ (Object) strip(strip = nil)
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 |
+ (Object) write
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 |