BlikstickSquare 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ccbf4f5dc0d4354cdc086fd4d79c515a5f4b748
4
+ data.tar.gz: c9bac64237c8c22afb97232d84e39b50d4d54857
5
+ SHA512:
6
+ metadata.gz: 4c51732a40b871094c24d2e33690be3de0b9b14d769e31727c2930f89b1248ce0580e4d69ad2c5e7e85707d0049811ceaa35d5a54134d5631dfa6e6693708724
7
+ data.tar.gz: ce84df74bbe1fd77a261beb600f0be0e9ea5d33e7143a9da50dc0bf4407182ca36eaf77ee2c2ffc9086454bccf2e548481c8cb5c36c35a9cdcfc1ecb1ccc93a3
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.7
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'BlikstickSquare/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "BlikstickSquare"
8
+ spec.version = BlikstickSquare::VERSION
9
+ spec.authors = ["Calò, Ignazio"]
10
+ spec.email = ["ignazioc@gmail.com"]
11
+
12
+ spec.summary = 'Command line client for the Blikstick Square'
13
+ spec.homepage = "https://github.com/ignazioc/blinksticksquare"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+
27
+ spec.add_dependency "libusb"
28
+ spec.add_dependency "color"
29
+ spec.add_dependency "commander"
30
+
31
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in BlikstickSquare.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Calo, Ignazio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # BlikstickSquare
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/BlikstickSquare`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'BlikstickSquare'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install BlikstickSquare
22
+
23
+ ## Usage
24
+ ```bash
25
+ $ blink red # set the whole cube to ff0000
26
+ $ blink green # set the whole cube to 00ff00
27
+ $ blink blue # set the whole cube to 0000ff
28
+ $ blink color aa00bb # set the whole cube to aa00bb
29
+ $ blink off # turn off the cube
30
+
31
+ ```
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/BlikstickSquare.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "BlikstickSquare"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
15
+
16
+ #Example
17
+ #cmd = ::BlikstickSquare::Commander.new()
18
+ #cmd.red()
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/blink ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ require 'BlikstickSquare'
3
+ require 'commander/import'
4
+
5
+ program :name, 'blink'
6
+ program :version, '::BlikstickSquare::BlinkStick::version'
7
+ program :description, 'Command line client for the Blikstick Square'
8
+
9
+ command :info do |c|
10
+ c.summary = %q{Print info}
11
+ c.action do |args, options|
12
+ ::BlikstickSquare::Commander.new().print_info()
13
+ end
14
+ end
15
+
16
+ command :off do |c|
17
+ c.summary = %q{Turn off all the leds}
18
+ c.action do |args, options|
19
+ ::BlikstickSquare::Commander.new().off()
20
+ end
21
+ end
22
+
23
+ command :red do |c|
24
+ c.summary = %q{Set red on all the 8 leds}
25
+ c.action do |args, options|
26
+ ::BlikstickSquare::Commander.new().red()
27
+ end
28
+ end
29
+
30
+ command :green do |c|
31
+ c.summary = %q{Set blue on all the 8 leds}
32
+ c.action do |args, options|
33
+ ::BlikstickSquare::Commander.new().green()
34
+ end
35
+ end
36
+
37
+ command :blue do |c|
38
+ c.summary = %q{Set blue on all the 8 leds}
39
+ c.action do |args, options|
40
+ ::BlikstickSquare::Commander.new().blue()
41
+ end
42
+ end
43
+
44
+ command :color do |c|
45
+ c.summary = %q{Set one custom color on all the 8 leds}
46
+ c.syntax = 'blink color 00ff00'
47
+ c.action do |args, options|
48
+ color = args.first
49
+ unless color
50
+ puts "No color provided"
51
+ return
52
+ end
53
+
54
+ ::BlikstickSquare::Commander.new().set_color(color)
55
+ end
56
+ end
@@ -0,0 +1,50 @@
1
+ require "BlikstickSquare/version"
2
+ require "BlikstickSquare/blinkstick"
3
+
4
+ module BlikstickSquare
5
+ class Commander
6
+
7
+ def first_blinkstick
8
+ BlinkStick.find_all.first
9
+ end
10
+
11
+ def print_info
12
+ b = first_blinkstick()
13
+ puts "Found device:"
14
+ puts " Manufacturer: " + b.manufacturer
15
+ puts " Description: " + b.description
16
+ puts " Serial: " + b.serial
17
+ puts " Current Color: " + b.color.html
18
+ puts " Info Block 1: " + b.info_block1()
19
+ puts " Info Block 2: " + b.info_block2()
20
+ end
21
+
22
+ def off
23
+ set_color("000000")
24
+ end
25
+
26
+ def red
27
+ set_color("ff0000")
28
+ end
29
+
30
+ def green
31
+ set_color("00ff00")
32
+ end
33
+
34
+ def blue
35
+ set_color("0000ff")
36
+ end
37
+
38
+ def set_color(color)
39
+ b = first_blinkstick()
40
+ channel = 0
41
+ (0..7).each { | index |
42
+ b.set_color(channel, index, Color::RGB.from_html(color))
43
+ }
44
+ end
45
+
46
+ def red
47
+ set_color("ff0000")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,192 @@
1
+ require "libusb"
2
+ require "color"
3
+
4
+ class BlinkStick
5
+ @@VENDOR_ID = 0X20A0
6
+ @@PRODUCT_ID = 0x41E5
7
+
8
+ def open(device = nil)
9
+ @@usb ||= LIBUSB::Context.new
10
+
11
+ if (device)
12
+ @device = device
13
+ else
14
+ @device = @@usb.devices(:idVendor => @@VENDOR_ID, :idProduct => @@PRODUCT_ID).first
15
+ end
16
+
17
+ @handle = @device.open
18
+ end
19
+
20
+ def self.find_all
21
+ @@usb ||= LIBUSB::Context.new
22
+
23
+ result = []
24
+
25
+ @@usb.devices(:idVendor => @@VENDOR_ID, :idProduct => @@PRODUCT_ID).each { | device |
26
+ b = BlinkStick.new
27
+ b.open(device)
28
+
29
+ result.push(b)
30
+ }
31
+
32
+ result
33
+ end
34
+
35
+ def self.find_by_serial(serial)
36
+ @@usb ||= LIBUSB::Context.new
37
+
38
+ @@usb.devices(:idVendor => @@VENDOR_ID, :idProduct => @@PRODUCT_ID).each { | device |
39
+ if device.serial_number == serial
40
+ b = BlinkStick.new
41
+ b.open(device)
42
+ return b
43
+ end
44
+ }
45
+
46
+ nil
47
+ end
48
+
49
+ def color=(value)
50
+ for i in 0..8 do
51
+ puts "XXX: #{1.chr + 0.to_i.chr + i.to_i.chr + value.red.to_i.chr + value.green.to_i.chr + value.blue.to_i.chr}"
52
+ @handle.control_transfer(:bmRequestType => 0x20,
53
+ :bRequest => 0x9,
54
+ :wValue => 0x1,
55
+ :wIndex => 0x0000,
56
+ :dataOut => 1.chr + 0.to_i.chr + i.to_i.chr + value.red.to_i.chr + value.green.to_i.chr + value.blue.to_i.chr)
57
+ end
58
+
59
+ end
60
+
61
+ def color
62
+ result = @handle.control_transfer(:bmRequestType => 0x80 | 0x20, :bRequest => 0x1, :wValue => 0x1, :wIndex => 0x0000, :dataIn => 4)
63
+ Color::RGB.new(result[1].ord, result[2].ord, result[3].ord)
64
+ end
65
+
66
+ def set_color(channel, index, value)
67
+ attempts = 0
68
+ while attempts < 5
69
+ attempts += 1
70
+
71
+ begin
72
+ @handle.control_transfer(:bmRequestType => 0x20,
73
+ :bRequest => 0x9,
74
+ :wValue => 0x5,
75
+ :wIndex => 0x0000,
76
+ :dataOut => 1.chr + channel.to_i.chr + index.to_i.chr + value.red.to_i.chr + value.green.to_i.chr + value.blue.to_i.chr)
77
+ break
78
+ rescue
79
+ if attempts == 5
80
+ raise
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ def set_colors(channel, data)
87
+ report_id = 9
88
+ max_leds = 64
89
+
90
+ if data.size <= 8 * 3
91
+ max_leds = 8
92
+ report_id = 6
93
+ elsif data.size <= 16 * 3
94
+ max_leds = 16
95
+ report_id = 7
96
+ elsif data.size <= 32 * 3
97
+ max_leds = 32
98
+ report_id = 8
99
+ elsif data.size <= 64 * 3
100
+ max_leds = 64
101
+ report_id = 9
102
+ end
103
+
104
+ report = report_id.chr + channel.to_i.chr
105
+
106
+ (0..max_leds * 3 - 1).each do | i |
107
+ if data.size > i
108
+ report += data[i].to_i.chr
109
+ else
110
+ report += 0.chr
111
+ end
112
+ end
113
+
114
+ #Debug code
115
+ #puts report.unpack('U'*report.length).collect {|x| x.to_s 16}.join(" ")
116
+
117
+ attempts = 0
118
+ while attempts < 5
119
+ attempts += 1
120
+
121
+ begin
122
+ @handle.control_transfer(:bmRequestType => 0x20,
123
+ :bRequest => 0x9,
124
+ :wValue => report_id,
125
+ :wIndex => 0,
126
+ :dataOut => report)
127
+ break
128
+ rescue
129
+ if attempts == 5
130
+ raise
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ def off
137
+ self.color = Color::RGB.new(0, 0, 50)
138
+ end
139
+
140
+ def get_info_block(id)
141
+ bytes = @handle.control_transfer(:bmRequestType => 0x80 | 0x20, :bRequest => 0x1, :wValue => id + 1, :wIndex => 0x0000, :dataIn => 33)
142
+
143
+ result = ""
144
+ for i in 1..(bytes.length-1)
145
+ if i == "\x00"
146
+ break
147
+ end
148
+ result += bytes[i]
149
+ end
150
+
151
+ result
152
+ end
153
+
154
+ def set_info_block(id, data)
155
+ data = (id + 1).chr + data
156
+ data = data + 0.chr while data.length < 33
157
+ @handle.control_transfer(:bmRequestType => 0x20, :bRequest => 0x9, :wValue => id + 1, :wIndex => 0x0000, :dataOut => data)
158
+ end
159
+
160
+ def random_color
161
+ r = Random.new
162
+ self.color = Color::RGB.new(r.rand(255), r.rand(255), r.rand(255))
163
+ end
164
+
165
+ def serial
166
+ @device.serial_number
167
+ end
168
+
169
+ def manufacturer
170
+ @device.manufacturer
171
+ end
172
+
173
+ def description
174
+ @device.product
175
+ end
176
+
177
+ def info_block1
178
+ get_info_block(1)
179
+ end
180
+
181
+ def info_block1=(value)
182
+ set_info_block(1, value)
183
+ end
184
+
185
+ def info_block2
186
+ get_info_block(2)
187
+ end
188
+
189
+ def info_block2=(value)
190
+ set_info_block(2, value)
191
+ end
192
+ end
@@ -0,0 +1,3 @@
1
+ module BlikstickSquare
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: BlikstickSquare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Calò, Ignazio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-10 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: libusb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: color
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: commander
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - ignazioc@gmail.com
100
+ executables:
101
+ - blink
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - BlikstickSquare.gemspec
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - exe/blink
115
+ - lib/BlikstickSquare.rb
116
+ - lib/BlikstickSquare/blinkstick.rb
117
+ - lib/BlikstickSquare/version.rb
118
+ homepage: https://github.com/ignazioc/blinksticksquare
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.6.8
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Command line client for the Blikstick Square
142
+ test_files: []