faderuby 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.
- checksums.yaml +7 -0
- data/.gitignore +24 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +11 -0
- data/faderuby.gemspec +24 -0
- data/lib/faderuby.rb +8 -0
- data/lib/faderuby/client.rb +59 -0
- data/lib/faderuby/display_surface.rb +17 -0
- data/lib/faderuby/pixel.rb +24 -0
- data/lib/faderuby/strip.rb +15 -0
- data/lib/faderuby/version.rb +3 -0
- data/spec/lib/client_spec.rb +37 -0
- data/spec/spec_helper.rb +1 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 23c53f19b60b9058f0a4509ae4f0713a2c4c0edc
|
4
|
+
data.tar.gz: 59a51ab8dc1fe5f98f0588a6e104dc9af4e6898f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1abab48874ca8ccfc0136d6fa835ec875efbc01502dd8578ec4c41e43c4120d956e579b9a766e9e647769970182399e49e5f7ac2fff44964c1c7dfa52dcefef1
|
7
|
+
data.tar.gz: 06e7acd7e1d3518c35cbe75e307120cc7cc91ce709cddb5c81bea3e142805cffa25e50e841ad22d85e18975ab07a0f9cd77e4644e5cb8b78342325d44d4925d8
|
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
bin/*
|
11
|
+
bin
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/reports
|
17
|
+
test/tmp
|
18
|
+
test/version_tmp
|
19
|
+
tmp
|
20
|
+
*.bundle
|
21
|
+
*.so
|
22
|
+
*.o
|
23
|
+
*.a
|
24
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 James Harrison
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# FadeRuby
|
2
|
+
|
3
|
+
Ruby interface for Fadecandy LED control servers using the Open Pixel Control protocol.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
|
7
|
+
* Ruby 2.x, 2.1 or higher recommended - Faderuby does make use of named parameters
|
8
|
+
* Faderuby server (fcserver)
|
9
|
+
* Some cool LEDs
|
10
|
+
* Nothing better to do with your life
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'faderuby'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install faderuby
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
We use a client to talk to the device; you can specify a host and port for the fadecandy server, the defaults are localhost/7890.
|
29
|
+
```ruby
|
30
|
+
client = FadeRuby::Client.new([host], [port])
|
31
|
+
```
|
32
|
+
|
33
|
+
The client can be used to directly write pixel data:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
# Set the first connected pixel on channel 0 to blue
|
37
|
+
client.set_pixels([0,0,255], 0)
|
38
|
+
# Set the first pixel to green and the second pixel to red
|
39
|
+
client.set_pixels([0,255,0,255,0,0], 0)
|
40
|
+
```
|
41
|
+
There are also convenience classes - subclasses of DisplaySurface like Strip can be used to manage collections of Pixels.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# 30 LED strip
|
45
|
+
strip = FadeRuby::Strip.new(30)
|
46
|
+
# Set the whole strip to green
|
47
|
+
strip.set_all(r: 0, g: 255, b: 0)
|
48
|
+
client.write(strip)
|
49
|
+
# Set the first 10 LEDs to blue
|
50
|
+
strip.pixels[0..10].each{|pix| pix.set(r: 0, g: 0, b: 255) }
|
51
|
+
client.write(strip)
|
52
|
+
```
|
53
|
+
|
54
|
+
Fadecandy will do temporal dithering (fades) for you, so you can do smooth fades by simply waiting between commands. The following example smoothly rolls a new colour onto the strip at random:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
next_color = [rand(255), rand(255), rand(255)]
|
58
|
+
while true
|
59
|
+
strip.pixels.each do |pix|
|
60
|
+
pix.set(r: next_color[0], g: next_color[1], b: next_color[2])
|
61
|
+
client.write(strip)
|
62
|
+
sleep 0.2
|
63
|
+
end
|
64
|
+
sleep 0.2
|
65
|
+
next_color = [rand(255), rand(255), rand(255)]
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
1. Fork it ( https://github.com/JamesHarrison/faderuby/fork )
|
72
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
73
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
74
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
75
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
|
5
|
+
# Default directory to look in is `/specs`
|
6
|
+
# Run with `rake spec`
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |task|
|
8
|
+
task.rspec_opts = ['--color', '--format', 'documentation']
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :spec
|
data/faderuby.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'faderuby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "faderuby"
|
8
|
+
spec.version = FadeRuby::VERSION
|
9
|
+
spec.authors = ["James Harrison"]
|
10
|
+
spec.email = ["james@talkunafraid.co.uk"]
|
11
|
+
spec.summary = %q{Fadecandy LED driver client}
|
12
|
+
spec.description = %q{Fadecandy LED driver client using the OpenPixelControl protocol}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/faderuby.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
#
|
4
|
+
# The FadeRuby::Client class handles low-level communication with the server
|
5
|
+
#
|
6
|
+
# @author James Harrison <james@talkunafraid.co.uk>
|
7
|
+
#
|
8
|
+
class FadeRuby::Client
|
9
|
+
|
10
|
+
#
|
11
|
+
# Get a socket to talk to the Fadecandy server with
|
12
|
+
#
|
13
|
+
# @return [TCPSocket] Socket configured to talk to the server with TCP_NODELAY
|
14
|
+
#
|
15
|
+
def socket
|
16
|
+
return @s if @s
|
17
|
+
@s = TCPSocket.new(@host, @port)
|
18
|
+
@s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
19
|
+
return @s
|
20
|
+
end
|
21
|
+
|
22
|
+
# Set up a new client
|
23
|
+
#
|
24
|
+
# @param [String] host Fadecandy server host
|
25
|
+
# @param [Integer] port Fadecandy server port
|
26
|
+
#
|
27
|
+
def initialize(host='localhost', port=7890)
|
28
|
+
@host = host
|
29
|
+
@port = port
|
30
|
+
socket
|
31
|
+
end
|
32
|
+
|
33
|
+
#
|
34
|
+
# Send a new frame of video
|
35
|
+
#
|
36
|
+
# @param [[Integer]] colors Pixel values to set as [r,g,b,r,g,b,r,g,b...]
|
37
|
+
# where r,g,b are three ints between 0 and 65535
|
38
|
+
# @param [Integer] channel Channel to send to, default 0
|
39
|
+
#
|
40
|
+
def set_pixels(colors, channel=0)
|
41
|
+
size = (colors.length * 8)
|
42
|
+
data = colors.pack('C*')
|
43
|
+
header = [channel, 0, data.length].pack('CCS>')
|
44
|
+
socket.send header+data, 0
|
45
|
+
end
|
46
|
+
|
47
|
+
#
|
48
|
+
# Write a display surface to this client
|
49
|
+
#
|
50
|
+
# @param [<type>] disp_surf <description>
|
51
|
+
#
|
52
|
+
# @return [<type>] <description>
|
53
|
+
#
|
54
|
+
def write(disp_surf, channel=0)
|
55
|
+
set_pixels(disp_surf.pixels.map(&:to_a).flatten, channel)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#
|
2
|
+
# DisplaySurface represents a display and is subclassed by geometry specific
|
3
|
+
# classes
|
4
|
+
#
|
5
|
+
# @author James Harrison <james@talkunafraid.co.uk>
|
6
|
+
#
|
7
|
+
class FadeRuby::DisplaySurface
|
8
|
+
def pixels
|
9
|
+
@pixels ||= []
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_all(r: nil, g: nil, b: nil)
|
13
|
+
pixels.each do |pix|
|
14
|
+
pix.set(r: r, g: g, b: b)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class FadeRuby::Pixel
|
2
|
+
attr_reader :r, :g, :b
|
3
|
+
def initialize(r=0,g=0,b=0)
|
4
|
+
@r = r
|
5
|
+
@g = g
|
6
|
+
@b = b
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_a
|
10
|
+
[r,g,b]
|
11
|
+
end
|
12
|
+
|
13
|
+
def set(r: nil, g: nil, b: nil)
|
14
|
+
@r = r if r
|
15
|
+
@g = g if g
|
16
|
+
@b = b if b
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_random
|
20
|
+
@r = rand(255)
|
21
|
+
@g = rand(255-@r)
|
22
|
+
@b = rand(255-@r-@g)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class FadeRuby::Strip < FadeRuby::DisplaySurface
|
2
|
+
|
3
|
+
#
|
4
|
+
# Set up a new strip, giving the length in LEDs
|
5
|
+
#
|
6
|
+
# @param [Fixnum] length number of LEDs
|
7
|
+
#
|
8
|
+
def initialize(length)
|
9
|
+
length.times do
|
10
|
+
pixels.push(FadeRuby::Pixel.new)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe(FadeRuby::Client) do
|
4
|
+
context 'creating a new client' do
|
5
|
+
it 'should open a socket' do
|
6
|
+
expect_any_instance_of(FadeRuby::Client).to receive(:socket)
|
7
|
+
described_class.new
|
8
|
+
end
|
9
|
+
end
|
10
|
+
context 'given a mock socket' do
|
11
|
+
let(:client) { described_class.new }
|
12
|
+
let(:socket_mock) { double('socket') }
|
13
|
+
# Mock the socket
|
14
|
+
before(:each) do
|
15
|
+
allow_any_instance_of(FadeRuby::Client).to receive(:socket).and_return(socket_mock)
|
16
|
+
end
|
17
|
+
describe '#set_pixels' do
|
18
|
+
context 'setting 1 pixel on channel 0 to 128, 128, 128' do
|
19
|
+
after(:each) do
|
20
|
+
client.set_pixels([128,128,128], 0)
|
21
|
+
end
|
22
|
+
it 'should send the right message' do
|
23
|
+
expect(socket_mock).to receive(:send).with([0, 0, 3, 128, 128, 128].pack('CCS>CCC'), 0)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
context 'setting 2 pixels on channel 1 to 256, 128, 0' do
|
27
|
+
after(:each) do
|
28
|
+
client.set_pixels([256, 128, 0, 256, 128, 0], 1)
|
29
|
+
end
|
30
|
+
it 'should send the right message' do
|
31
|
+
expect(socket_mock).to receive(:send).with([1, 0, 6, 256, 128, 0, 256, 128, 0].pack('CCS>CCCCCC'), 0)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'faderuby'
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faderuby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Harrison
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Fadecandy LED driver client using the OpenPixelControl protocol
|
56
|
+
email:
|
57
|
+
- james@talkunafraid.co.uk
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- faderuby.gemspec
|
68
|
+
- lib/faderuby.rb
|
69
|
+
- lib/faderuby/client.rb
|
70
|
+
- lib/faderuby/display_surface.rb
|
71
|
+
- lib/faderuby/pixel.rb
|
72
|
+
- lib/faderuby/strip.rb
|
73
|
+
- lib/faderuby/version.rb
|
74
|
+
- spec/lib/client_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Fadecandy LED driver client
|
100
|
+
test_files:
|
101
|
+
- spec/lib/client_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
has_rdoc:
|