ws2801 0.2 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ws2801.rb +54 -15
- metadata +1 -1
data/lib/ws2801.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
#
|
2
2
|
# Controller for: RGB Pixel with WS2801 Chip
|
3
3
|
# build for Diffused Digital RGB LED Pixels from Adafruits on Raspberry Pi
|
4
|
-
#
|
5
|
-
|
6
|
-
#
|
4
|
+
# but should work on any device and the same led chip
|
5
|
+
# (c) 2013 Roman Pramberger (roman@pramberger.ch)
|
6
|
+
#
|
7
|
+
# WS2801 user-space driver
|
8
|
+
#
|
7
9
|
module WS2801
|
8
10
|
@@options = {
|
9
11
|
:len => 25,
|
@@ -89,37 +91,74 @@ module WS2801
|
|
89
91
|
# Set pixel to color
|
90
92
|
#
|
91
93
|
# Example:
|
92
|
-
# >> WS2801.set { :r => 255, :
|
93
|
-
# >> WS2801.set { :g => 128, :
|
94
|
-
# >> WS2801.set { :r => 40, :g => 255, :b => 200, :
|
94
|
+
# >> WS2801.set { :r => 255, :pixel => [1..10] }
|
95
|
+
# >> WS2801.set { :g => 128, :pixel => :all }
|
96
|
+
# >> WS2801.set { :r => 40, :g => 255, :b => 200, :pixel => 4 }
|
95
97
|
#
|
96
98
|
# Options:
|
97
|
-
# :
|
99
|
+
# :pixel => [] # array with pixel ids
|
98
100
|
# :r => (Integer)
|
99
101
|
# :g => (Integer)
|
100
102
|
# :b => (Integer)
|
101
103
|
def self.set options = {}
|
102
104
|
self.generate if @@options[:strip].length == 0
|
103
|
-
options[:
|
104
|
-
options[:
|
105
|
-
options[:
|
105
|
+
options[:pixel] = (0..(self.length-1)).to_a if options[:pixel].nil? or options[:pixel] == :all
|
106
|
+
options[:pixel] = [options[:pixel]] if options[:pixel].is_a? Numeric
|
107
|
+
options[:pixel].each do |i|
|
106
108
|
@@options[:strip][(i*3)] = options[:r] || 0
|
107
109
|
@@options[:strip][(i*3)+1] = options[:g] || 0
|
108
110
|
@@options[:strip][(i*3)+2] = options[:b] || 0
|
109
111
|
end
|
110
|
-
self.write if @@options[:autowrite]
|
112
|
+
self.write if @@options[:autowrite]
|
111
113
|
end
|
112
114
|
|
113
|
-
# Fade to color
|
115
|
+
# Fade pixel to color
|
116
|
+
#
|
117
|
+
# Example:
|
118
|
+
# >> WS2801.set { :r => 255, :pixel => [1..10], :timeout => 0.1 }
|
119
|
+
# >> WS2801.set { :g => 128, :pixel => :all }
|
120
|
+
# >> WS2801.set { :r => 40, :g => 255, :b => 200, :pixel => 4 }
|
114
121
|
#
|
115
|
-
#
|
122
|
+
# Options:
|
123
|
+
# :pixel => [] # array with pixel ids
|
124
|
+
# :timeout => (Float)
|
125
|
+
# :r => (Integer)
|
126
|
+
# :g => (Integer)
|
127
|
+
# :b => (Integer)
|
128
|
+
def self.fade options = {}
|
129
|
+
self.generate if @@options[:strip].length == 0
|
130
|
+
options[:pixel] = (0..(self.length-1)).to_a if options[:pixel].nil? or options[:pixel] == :all
|
131
|
+
options[:pixel] = [options[:pixel]] if options[:pixel].is_a? Numeric
|
132
|
+
options[:r] = 0 if options[:r].nil?
|
133
|
+
options[:g] = 0 if options[:g].nil?
|
134
|
+
options[:b] = 0 if options[:b].nil?
|
135
|
+
|
136
|
+
breakme = 0
|
137
|
+
while true
|
138
|
+
options[:pixel].each do |i|
|
139
|
+
next if @@options[:strip][(i*3+2)] == options[:b] and @@options[:strip][(i*3+1)] == options[:g] and @@options[:strip][(i*3)] == options[:r]
|
140
|
+
@@options[:strip][(i*3)] -= 1 if @@options[:strip][(i*3)] > options[:r]
|
141
|
+
@@options[:strip][(i*3)] += 1 if @@options[:strip][(i*3)] < options[:r]
|
142
|
+
@@options[:strip][(i*3+1)] -= 1 if @@options[:strip][(i*3+1)] > options[:g]
|
143
|
+
@@options[:strip][(i*3+1)] += 1 if @@options[:strip][(i*3+1)] < options[:g]
|
144
|
+
@@options[:strip][(i*3+2)] -= 1 if @@options[:strip][(i*3+2)] > options[:b]
|
145
|
+
@@options[:strip][(i*3+2)] += 1 if @@options[:strip][(i*3+2)] < options[:b]
|
146
|
+
breakme += 1 if @@options[:strip][(i*3+2)] == options[:b] and @@options[:strip][(i*3+1)] == options[:g] and @@options[:strip][(i*3)] == options[:r]
|
147
|
+
end
|
148
|
+
self.write if @@options[:autowrite]
|
149
|
+
break if breakme >= @@options[:len]
|
150
|
+
|
151
|
+
sleep(options[:timeout] || 0.01)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
116
155
|
|
117
|
-
# Set off
|
156
|
+
# Set off
|
118
157
|
#
|
119
158
|
# Example:
|
120
159
|
# >> WS2801.off
|
121
160
|
def self.off
|
122
161
|
self.generate
|
123
|
-
self.write if @@options[:autowrite]
|
162
|
+
self.write if @@options[:autowrite]
|
124
163
|
end
|
125
164
|
end
|