cheeky-dreams 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "flt", ">= 1.3.0"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", ">= 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ flt (1.3.0)
6
+ git (1.2.5)
7
+ jeweler (1.6.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.9.2)
12
+ rcov (0.9.11)
13
+ rspec (2.7.0)
14
+ rspec-core (~> 2.7.0)
15
+ rspec-expectations (~> 2.7.0)
16
+ rspec-mocks (~> 2.7.0)
17
+ rspec-core (2.7.1)
18
+ rspec-expectations (2.7.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.7.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ flt (>= 1.3.0)
28
+ jeweler (~> 1.6.4)
29
+ rcov
30
+ rspec (>= 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 simon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = cheeky-dreams
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to cheeky-dreams
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 simon. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "cheeky-dreams"
18
+ gem.homepage = "http://github.com/simojenki/cheeky-dreams"
19
+ gem.license = "MIT"
20
+ gem.summary = "For controlling dream cheeky usb light"
21
+ gem.description = "For controlling dream cheeky usb light"
22
+ gem.email = "simojenki@gmail.com"
23
+ gem.authors = ["simon"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "cheeky-dreams #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,203 @@
1
+ require 'thread'
2
+ require 'flt'
3
+
4
+ module CheekyDreams
5
+
6
+ def self.rgb r, g, b
7
+ [r, g, b].each { |c| raise "Invalid rgb value #{r}, #{g}, #{b}" if c < 0 || c > 255}
8
+ [r, g, b]
9
+ end
10
+ def rgb r, g, b
11
+ CheekyDreams::rgb(r, g, b)
12
+ end
13
+
14
+ def self.rgb_between a, b, ratio
15
+ [
16
+ position_between(a[0], b[0], ratio),
17
+ position_between(a[1], b[1], ratio),
18
+ position_between(a[2], b[2], ratio),
19
+ ]
20
+ end
21
+
22
+ def self.position_between a, b, ratio
23
+ return b if ratio >= 1.0
24
+ ((b - a) * ratio) + a
25
+ end
26
+
27
+ COLOURS = {
28
+ :red => rgb(255, 0, 0),
29
+ :green => rgb(0, 255, 0),
30
+ :blue => rgb(0, 0, 255),
31
+ :yellow => rgb(255,255,0),
32
+ :aqua => rgb(0,255,255),
33
+ :purple => rgb(255,0,255),
34
+ :grey => rgb(192,192,192),
35
+ :white => rgb(255,255,255)
36
+ }
37
+
38
+ def self.rgb_for colour
39
+ case colour
40
+ when Symbol
41
+ raise "Unknown colour '#{colour}'" unless COLOURS.has_key?(colour)
42
+ COLOURS[colour]
43
+ when Array
44
+ colour
45
+ else
46
+ raise "Unsupported colour type #{colour}"
47
+ end
48
+ end
49
+ def rgb_for colour
50
+ CheekyDreams::rgb_for colour
51
+ end
52
+
53
+ def stdout_driver
54
+ Class.new do
55
+ def go rgb
56
+ puts rgb
57
+ end
58
+ end.new
59
+ end
60
+
61
+ def ansi_driver
62
+ require 'rainbow'
63
+ Class.new do
64
+ def go rgb
65
+ print " ".background(rgb)
66
+ print "\r"
67
+ end
68
+ end.new
69
+ end
70
+
71
+ def cycle colours, freq
72
+ Effect::Cycle.new colours, freq
73
+ end
74
+
75
+ def solid colour
76
+ Effect::Solid.new colour
77
+ end
78
+
79
+ def fade from, to, over_how_long
80
+ Effect::Fade.new from, to, over_how_long
81
+ end
82
+
83
+ def func freq, &block
84
+ Effect::Func.new freq, block
85
+ end
86
+
87
+ module Effect
88
+ class Effect
89
+ include Flt
90
+ include CheekyDreams
91
+ end
92
+
93
+ class Func < Effect
94
+ def initialize freq, block
95
+ @freq, @block, @last_change = freq, block, Time.at(0)
96
+ end
97
+
98
+ def next
99
+ now = Time.now
100
+ if (now - @last_change) >= (DecNum(1)/DecNum(@freq))
101
+ @last_change = now
102
+ @current = rgb_for(@block.yield)
103
+ end
104
+ @current
105
+ end
106
+ end
107
+
108
+ class Solid < Effect
109
+ def initialize colour
110
+ @rgb = CheekyDreams::rgb_for(colour)
111
+ end
112
+ def next
113
+ @rgb
114
+ end
115
+ end
116
+
117
+ class Cycle < Effect
118
+ def initialize colours, freq
119
+ @colours, @freq, @last_change = colours.cycle, freq, Time.at(0)
120
+ end
121
+
122
+ def next
123
+ now = Time.now
124
+ if (now - @last_change) >= (DecNum(1)/DecNum(@freq))
125
+ @last_change = now
126
+ @current = rgb_for(@colours.next)
127
+ end
128
+ @current
129
+ end
130
+ end
131
+
132
+ class Fade < Effect
133
+ def initialize from, to, over_how_long
134
+ @from, @to, @over_how_long = from, to, over_how_long
135
+ end
136
+
137
+ def next
138
+ now = Time.now
139
+ if @started_at == nil
140
+ @started_at = now
141
+ @from
142
+ else
143
+ ratio_done = (now - @started_at) / @over_how_long
144
+ CheekyDreams.rgb_between(rgb_for(@from), rgb_for(@to), ratio_done)
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
150
+
151
+ class Light
152
+
153
+ include CheekyDreams
154
+ include Flt
155
+
156
+ def initialize driver, freq = 5
157
+ @driver = driver
158
+ @lock = Mutex.new
159
+ @effect = nil
160
+ @on = false
161
+ @freq = freq
162
+ end
163
+
164
+ def go colour
165
+ @lock.synchronize {
166
+ case colour
167
+ when Symbol
168
+ @effect = solid(colour)
169
+ when Array
170
+ @effect = solid(colour)
171
+ when Effect::Effect
172
+ @effect = colour
173
+ else
174
+ raise "Im sorry dave, I'm afraid I can't do that. #{colour}"
175
+ end
176
+ }
177
+ end
178
+
179
+ def on
180
+ @on = true
181
+ t = Thread.new do
182
+ last_colour = nil
183
+ while @on
184
+ begin
185
+ @lock.synchronize {
186
+ if @effect
187
+ new_colour = @effect.next
188
+ if new_colour != last_colour
189
+ @driver.go new_colour
190
+ last_colour = new_colour
191
+ end
192
+ end
193
+ }
194
+ rescue => e
195
+ puts e.message
196
+ puts e.backtrace.join("\n")
197
+ end
198
+ sleep (DecNum(1)/DecNum(@freq))
199
+ end
200
+ end
201
+ end
202
+
203
+ end
@@ -0,0 +1,125 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe CheekyDreams do
4
+ describe "position_between" do
5
+ it "should calculate the position" do
6
+ CheekyDreams.position_between(100, 110, 0.5).should == 105
7
+ CheekyDreams.position_between(110, 100, 0.5).should == 105
8
+ CheekyDreams.position_between(100, 0, 0.2).should == 80
9
+ CheekyDreams.position_between(100, 0, 0.25).should == 75
10
+ CheekyDreams.position_between(100, 0, 1).should == 0
11
+ end
12
+
13
+ it "should return the end point when the ration goes above 1" do
14
+ CheekyDreams.position_between(100, 1, 1.01).should == 1
15
+ CheekyDreams.position_between(96, 99, 999).should == 99
16
+ end
17
+ end
18
+
19
+ describe "rgb_between" do
20
+ it "should calculate the position" do
21
+ CheekyDreams.rgb_between([100, 50, 0], [110, 20, 0], 0.1).should == [101, 47, 0]
22
+ end
23
+ end
24
+ end
25
+
26
+ describe Light do
27
+
28
+ include CheekyDreams
29
+
30
+ class StubDriver
31
+
32
+ def initialize
33
+ @lock = Mutex.new
34
+ end
35
+
36
+ def go colour
37
+ @lock.synchronize {
38
+ @colour = colour
39
+ }
40
+ end
41
+
42
+ def should_become expected_colour
43
+ rgb = expected_colour.is_a?(Symbol) ? Light::COLOURS[expected_colour] : expected_colour
44
+ start, match = Time.now, false
45
+ while ((Time.now - start < 1) && !match) do
46
+ @lock.synchronize {
47
+ match = rgb == @colour
48
+ }
49
+ sleep 0.01
50
+ end
51
+ raise "Expected driver to become #{rgb}, and didn't, instead is #{@colour}" unless match
52
+ end
53
+ end
54
+
55
+ describe "changing colour" do
56
+ before :each do
57
+ @driver = StubDriver.new
58
+ @light = Light.new @driver
59
+ @light.on
60
+ end
61
+
62
+ it "should go red" do
63
+ @light.go :red
64
+ @driver.should_become [255,0,0]
65
+ end
66
+
67
+ it "should go green" do
68
+ @light.go :green
69
+ @driver.should_become [0,255,0]
70
+ end
71
+
72
+ it "should go blue" do
73
+ @light.go :blue
74
+ @driver.should_become [0,0,255]
75
+ end
76
+
77
+ it "should blow up if you give it a symbol it doesnt understand" do
78
+ lambda { @light.go :pink_with_polka_dots }.should raise_error "Unknown colour 'pink_with_polka_dots'"
79
+ end
80
+
81
+ it "should be able to go any rgb" do
82
+ @light.go rgb(211, 222, 0)
83
+ @driver.should_become [211, 222, 0]
84
+ end
85
+
86
+ it "should be able to go any rgb as just numbers" do
87
+ @light.go [222, 111, 0]
88
+ @driver.should_become [222, 111, 0]
89
+ end
90
+
91
+ it "should be able to cycle between colours when specified as rgb" do
92
+ @light.go cycle([[255, 255, 255], [200, 200, 200], [100, 100, 100]], 10)
93
+ @driver.should_become [255, 255, 255]
94
+ @driver.should_become [200, 200, 200]
95
+ @driver.should_become [100, 100, 100]
96
+ @driver.should_become [255, 255, 255]
97
+ @driver.should_become [200, 200, 200]
98
+ end
99
+
100
+ it "should be able to cycle between colours when specified as symbols" do
101
+ @light.go cycle([:red, :green, :blue], 10)
102
+ @driver.should_become :red
103
+ @driver.should_become :green
104
+ @driver.should_become :blue
105
+ @driver.should_become :red
106
+ @driver.should_become :green
107
+ end
108
+
109
+ end
110
+ end
111
+
112
+ describe "RGB" do
113
+ include CheekyDreams
114
+
115
+ describe "creating with values out of range" do
116
+ it "should blow up" do
117
+ lambda { rgb(-1, 0, 0) }.should raise_error "Invalid rgb value -1, 0, 0"
118
+ lambda { rgb(256, 0, 0) }.should raise_error "Invalid rgb value 256, 0, 0"
119
+ lambda { rgb(0, -1, 0) }.should raise_error "Invalid rgb value 0, -1, 0"
120
+ lambda { rgb(0, 256, 0) }.should raise_error "Invalid rgb value 0, 256, 0"
121
+ lambda { rgb(0, 0, -1) }.should raise_error "Invalid rgb value 0, 0, -1"
122
+ lambda { rgb(0, 0, 256) }.should raise_error "Invalid rgb value 0, 0, 256"
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'cheeky-dreams'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
data/spec/test.rb ADDED
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'cheeky-dreams'
5
+
6
+ require 'rainbow'
7
+
8
+ include CheekyDreams
9
+
10
+ light = Light.new(ansi_driver, 10)
11
+ light.on
12
+
13
+ puts "colours"
14
+ [:red, :green, :blue, CheekyDreams::rgb(155, 155, 155)].each do |colour|
15
+ light.go colour
16
+ sleep 0.5
17
+ end
18
+
19
+ puts "cycle"
20
+ light.go(cycle(CheekyDreams::COLOURS.keys, 2))
21
+ sleep 5
22
+
23
+ puts "fade"
24
+ light.go(fade(:green, :red, 2))
25
+ sleep 3
26
+ light.go(fade(:red, :green, 2))
27
+ sleep 3
28
+
29
+ puts "fade from current to somewhere"
30
+
31
+ puts "colour on block"
32
+ cycle = [:blue, :purple].cycle
33
+ light.go(func(2) { cycle.next })
34
+ sleep 3
35
+
36
+ puts "fake cpu cycles"
37
+ green = 100
38
+ light.go(func(2) { [0, green+=20, 0] })
39
+ sleep 3
40
+
41
+ puts "done"
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cheeky-dreams
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - simon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: flt
16
+ requirement: &2158506720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2158506720
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &2158503320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.3.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2158503320
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &2158502820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2158502820
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &2158502260 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.4
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2158502260
58
+ - !ruby/object:Gem::Dependency
59
+ name: rcov
60
+ requirement: &2158501660 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2158501660
69
+ description: For controlling dream cheeky usb light
70
+ email: simojenki@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - .rspec
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - lib/cheeky-dreams.rb
86
+ - spec/cheeky-dreams_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/test.rb
89
+ homepage: http://github.com/simojenki/cheeky-dreams
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
103
+ - 0
104
+ hash: 1122224430567929941
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.10
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: For controlling dream cheeky usb light
117
+ test_files: []