ws2801 0.2 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ws2801.rb +54 -15
  2. 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
- # (c) 2013 Roman Pramberger (roman.pramberger@gmail.com)
5
-
6
- # WS2801 driver
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, :list => [1..10] }
93
- # >> WS2801.set { :g => 128, :list => :all }
94
- # >> WS2801.set { :r => 40, :g => 255, :b => 200, :list => 4 }
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
- # :list => [] # color in pixels in list
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[:list] = (0..(self.length-1)).to_a if options[:list].nil? or options[:list] == :all
104
- options[:list] = [options[:list]] if options[:list].is_a? Numeric
105
- options[:list].each do |i|
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] == true
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
- # Example: WS2801.fade
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 black
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] == true
162
+ self.write if @@options[:autowrite]
124
163
  end
125
164
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ws2801
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '1.0'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: