ws2801 0.1.2 → 0.2

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 +65 -56
  2. metadata +6 -5
@@ -5,9 +5,12 @@
5
5
 
6
6
  # WS2801 driver
7
7
  module WS2801
8
- @@len = 25
9
- @@strip = []
10
- @@device = "/dev/spidev0.0"
8
+ @@options = {
9
+ :len => 25,
10
+ :strip => [],
11
+ :device => "/dev/spidev0.0",
12
+ :autowrite => true
13
+ }
11
14
 
12
15
  # Set/read length of strip
13
16
  #
@@ -19,8 +22,8 @@ module WS2801
19
22
  # Arguments (or nil):
20
23
  # count: (Integer)
21
24
  def self.length len = nil
22
- return @@len if len.nil?
23
- @@len = len
25
+ return @@options[:len] if len.nil?
26
+ @@options[:len] = len
24
27
  end
25
28
 
26
29
  # Set/read device
@@ -33,84 +36,90 @@ module WS2801
33
36
  # Arguments (or nil):
34
37
  # device: (String)
35
38
  def self.device dev = nil
36
- return @@device if dev.nil?
37
- @@device = dev
39
+ return @@options[:device] if dev.nil?
40
+ @@options[:device] = dev
38
41
  end
39
42
 
40
- # Generate empty strip array
43
+ # Set/read current Strip
41
44
  #
42
- # Example:
43
- # >> WS2801.gen
44
- def self.gen
45
- @@strip = Array.new(@@len*3+1)
45
+ # Example;
46
+ # >> WS2801.strip
47
+ # >> WS2801.strip @newstrip
48
+ def self.strip strip = nil
49
+ return @@options[:strip] if strip.nil?
50
+ @@options[:strip] = strip
46
51
  end
47
52
 
48
- # Return current Strip
53
+ # Set/read current Autowrite option
54
+ # Write after each set (default: true)
49
55
  #
50
56
  # Example;
51
- # >> WS2801.strip
52
- def self.strip
53
- return @@strip
57
+ # >> WS2801.autowrite
58
+ # >> WS2801.autowrite @newstrip
59
+ def self.autowrite autowrit = nil
60
+ return @@options[:autowrite] if autowrit.nil?
61
+ @@options[:autowrite] = autowrit
54
62
  end
55
63
 
56
- # Write stripinfo to device (if not empty)
57
- # this needs root rights
64
+ # Generate empty strip array
65
+ #
66
+ # Example:
67
+ # >> WS2801.generate
68
+ def self.generate
69
+ @@options[:strip] = Array.new(@@options[:len]*3+1) { 0 }
70
+ end
71
+
72
+ # Write colors to the device
73
+ # (this needs root rights)
58
74
  #
59
75
  # Example:
60
76
  # >> WS2801.write
61
77
  def self.write
62
- return false if @@strip.nil?
78
+ return false if @@options[:strip].nil?
63
79
 
64
- @@strip.each_with_index do |s,i|
65
- @@strip[i] = 0 if @@strip[i].nil?
80
+ @@options[:strip].each_with_index do |s,i|
81
+ @@options[:strip][i] = 0 if @@options[:strip][i].nil?
66
82
  end
67
83
 
68
- File.open(@@device, 'w') do |file|
69
- file.write(@@strip.pack('C*'))
84
+ File.open(@@options[:device], 'w') do |file|
85
+ file.write(@@options[:strip].pack('C*'))
70
86
  end
71
87
  end
72
88
 
73
- # Put pixel X to
89
+ # Set pixel to color
74
90
  #
75
91
  # Example:
76
- # >> WS2801.put 3, 120, 255, 120
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 }
77
95
  #
78
- # Arguments:
79
- # pixel (Integer)
80
- # red (Integer)
81
- # green (Integer)
82
- # blue (Integer)
83
- def self.put pixel, red, green, blue
84
- return false if @@strip.nil?
85
- @@strip[(pixel*3)] = red
86
- @@strip[(pixel*3)+1] = green
87
- @@strip[(pixel*3)+2] = blue
96
+ # Options:
97
+ # :list => [] # color in pixels in list
98
+ # :r => (Integer)
99
+ # :g => (Integer)
100
+ # :b => (Integer)
101
+ def self.set options = {}
102
+ 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|
106
+ @@options[:strip][(i*3)] = options[:r] || 0
107
+ @@options[:strip][(i*3)+1] = options[:g] || 0
108
+ @@options[:strip][(i*3)+2] = options[:b] || 0
109
+ end
110
+ self.write if @@options[:autowrite] == true
88
111
  end
89
112
 
90
- # Fill all pixel
113
+ # Fade to color
91
114
  #
92
- # Example:
93
- # >> WS2801.fill 120, 255, 120
94
- #
95
- # Arguments:
96
- # red (Integer)
97
- # green (Integer)
98
- # blue (Integer)
99
- def self.fill red, green, blue
100
- self.gen if @@strip.length == 0
101
- ((@@strip.size-1)/3).times do |i|
102
- @@strip[(i*3)] = red
103
- @@strip[(i*3)+1] = green
104
- @@strip[(i*3)+2] = blue
105
- end
106
- end
115
+ # Example: WS2801.fade
107
116
 
108
- # Reset pixel to black
117
+ # Set off black
109
118
  #
110
119
  # Example:
111
- # >> WS2801.reset
112
- def self.reset
113
- self.gen
114
- self.write
120
+ # >> WS2801.off
121
+ def self.off
122
+ self.generate
123
+ self.write if @@options[:autowrite] == true
115
124
  end
116
125
  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.1.2
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,14 +11,15 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-01-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Controlling ws2801 chip from Ruby on Raspberry PI (RGB LED Stripes/Pixel)
15
- email: roman DOT pramberger AT gmail DOT com
14
+ description: Controlling ws2801 chip from Ruby on Raspberry PI or similar (RGB LED
15
+ Stripes/Pixel)
16
+ email: roman@pramberger.ch
16
17
  executables: []
17
18
  extensions: []
18
19
  extra_rdoc_files: []
19
20
  files:
20
21
  - lib/ws2801.rb
21
- homepage: https://rubygems.org/gems/ws2801
22
+ homepage: https://github.com/b1nary/ws2801
22
23
  licenses: []
23
24
  post_install_message:
24
25
  rdoc_options: []
@@ -41,5 +42,5 @@ rubyforge_project:
41
42
  rubygems_version: 1.8.23
42
43
  signing_key:
43
44
  specification_version: 3
44
- summary: Controlling ws2801 chip from Ruby on Raspberry PI (RGB LED Stripes/Pixel)
45
+ summary: Controlling ws2801 chip from Ruby on Raspberry PI or similar (RGB LED Stripes/Pixel)
45
46
  test_files: []