Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blink1-devel
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: ruby
env: CODECLIMATE_REPO_TOKEN=ea524e4acf0abd2a5d396cc239710de238314e5bfe3af472baca56a88f7b7ec1
env: CODECLIMATE_REPO_TOKEN=ea524e4acf0abd2a5d396cc239710de238314e5bfe3af472baca56a88f7b7ec1 SPEC_OPTS='--format documentation'
rvm:
- "1.9.3"
- rbx-19mode
- "2.0.0"
- "2.1.0"
before_install:
- sudo apt-get install -qq gcc-avr avr-libc
- sudo apt-get install -qq libusb-1.0-0-dev
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ source "http://rubygems.org"
gemspec

if RUBY_VERSION >= '1.9.3'
gem "codeclimate-test-reporter", :group => :test, :require => nil
gem "codeclimate-test-reporter", group: :test, require: nil
end
6 changes: 2 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ require 'bundler/gem_tasks'
require 'rdoc/task'
require 'rspec/core/rake_task'

task :default => :spec
task :spec => :build
task default: :spec
task spec: :build

task :build do
Dir.chdir('ext/blink1') do
Expand All @@ -18,12 +18,10 @@ end
RSpec::Core::RakeTask.new

RDoc::Task.new do |rdoc|

rdoc.rdoc_files.include("README.rdoc", "lib/**/*.rb", "ext/blink1/blink1.c")
rdoc.generator = 'bootstrap'
rdoc.main = "README.rdoc"
rdoc.rdoc_dir = 'html'
rdoc.title = 'rb-blink1'
rdoc.options << '--line-numbers'

end
73 changes: 39 additions & 34 deletions lib/blink1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ class Blink1
# <span class="name">new</span> <span class="arguments">( {Fixnum} id )</span>
# <span class="name">new</span> <span class="arguments">( {Boolean} auto_open )</span>
# <span class="name">new</span> <span class="arguments">( {String} serial_id )</span>
# <span class="name">new</span> <span class="arguments">( :path => <em>device_path</em> )</span>
# <span class="name">new</span> <span class="arguments">( :serial => <em>serial_id</em> )</span>
# <span class="name">new</span> <span class="arguments">( path: <em>device_path</em> )</span>
# <span class="name">new</span> <span class="arguments">( serial: <em>serial_id</em> )</span>
#
# Returns new instance of +Blink1+
#
def initialize option = nil
def initialize(option = nil)
case option
when Fixnum
open_by_id(option)
when Hash
path = option[:path] || option["path"]
serial = option[:serial] || option["serial"]
path = option[:path]
serial = option[:serial]

if path
open_by_path(path)
elsif serial
Expand All @@ -37,7 +38,8 @@ def initialize option = nil
else
open if option == true
end
@millis ||= 300

@millis ||= 300
@delay_millis ||= 500
end

Expand All @@ -46,19 +48,20 @@ def initialize option = nil
# <span class="name">open</span> <span class="arguments">( {Fixnum} id ) { |blink1| }</span>
# <span class="name">open</span> <span class="arguments">( {Boolean} autoopen ) { |blink1| }</span>
# <span class="name">open</span> <span class="arguments">( {String} serial_id ) { |blink1| }</span>
# <span class="name">open</span> <span class="arguments">( :path => <em>device_path</em> ) { |blink1| }</span>
# <span class="name">open</span> <span class="arguments">( :serial => <em>serial_id</em> ) { |blink1| }</span>
# <span class="name">open</span> <span class="arguments">( path: <em>device_path</em> ) { |blink1| }</span>
# <span class="name">open</span> <span class="arguments">( serial: <em>serial_id</em> ) { |blink1| }</span>
#
# If block given, yieds new instance of +Blink1+.
# If block given, yields new instance of +Blink1+.
#
# If not, returns new +Blink1+
#
def self.open option = nil, &block
b = self.new(option)
def self.open(option = nil, &block)
b = new(option)
b.open if option.nil?

if block
begin
b.instance_eval &block
b.instance_eval(&block)
ensure
b.close
end
Expand All @@ -70,24 +73,24 @@ def self.open option = nil, &block
#
# Blink with RGB value for +times+.
#
def blink r, g, b, times
def blink(r, g, b, times)
times.times do
self.fade_to_rgb(millis, r, g, b)
fade_to_rgb(millis, r, g, b)
self.class.sleep(delay_millis)
self.fade_to_rgb(millis, 0, 0, 0)
fade_to_rgb(millis, 0, 0, 0)
self.class.sleep(delay_millis)
end
end

#
# Flash random color for +times+.
#
def random times
def random(times)
times.times do
r = rand(0xff)
g = rand(0xff)
b = rand(0xff)
self.fade_to_rgb(millis, r, g, b)
fade_to_rgb(millis, r, g, b)
self.class.sleep(delay_millis)
end
end
Expand All @@ -96,50 +99,52 @@ def random times
# Turn LED white.
#
def on
self.fade_to_rgb(millis, 0xff, 0xff, 0xff)
fade_to_rgb(millis, 0xff, 0xff, 0xff)
end

#
# Turn LED off.
#
def off
self.fade_to_rgb(millis, 0, 0, 0)
fade_to_rgb(millis, 0, 0, 0)
end

#
# Alias for +read_pattern_line+.
#
def [] index
self.read_pattern_line(index)
def [](index)
read_pattern_line(index)
end

#
# Write pattern line with hash with key +fade_millis+, +r+, +g+, +b+.
#
def []= index, prop
fade_millis = prop[:fade_millis] || prop['fade_millis']
r = prop[:r] || prop['r']
g = prop[:g] || prop['g']
b = prop[:b] || prop['b']
self.write_pattern_line(index, fade_millis, r, g, b)
def []=(index, prop)
fade_millis = prop[:fade_millis]
r = prop[:r]
g = prop[:g]
b = prop[:b]

write_pattern_line(index, fade_millis, r, g, b)
end

#
# Returns array of hash with keys +:id+, +:serial+, +:path+
#
def self.list
count = enumerate_vid_pid(vendor_id, product_id)
i = 0
devs = []
while i < count do
i = 0
devs = []

while i < count
devs << {
:id => i,
:serial => cached_serial(i),
:path => cached_path(i)
id: i,
serial: cached_serial(i),
path: cached_path(i)
}
i += 1
end

devs
end

end
4 changes: 2 additions & 2 deletions rb-blink1.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec'
s.add_development_dependency 'spork'

s.files = `git ls-files`.split("\n").reject{|f| f =~ /^(\..+|Gemfile.*|Guardfile|)$/}
s.extensions = ["ext/blink1/extconf.rb"]
s.files = `git ls-files`.split("\n").reject{|f| f =~ /^(\..+|Gemfile.*|Guardfile|)$/}
s.extensions = ["ext/blink1/extconf.rb"]
s.require_paths = ["lib", "ext"]
end

30 changes: 22 additions & 8 deletions spec/blink1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@

describe Blink1 do

context 'native extention methods' do
describe 'native extention methods' do

it 'returns vendor_id' do
Blink1.vendor_id.should eql(10168)
describe 'vendor_id' do
subject { Blink1.vendor_id }
it { should be 10168 }
end

it 'returns product_id' do
Blink1.product_id.should eql(493)
describe 'product_id' do
subject { Blink1.product_id }
it { should be 493 }
end

end

context 'class methods', :device => true do
context 'class methods', device: true do

it 'returns list' do
Blink1.list.is_a?(Array).should be_true
describe 'list' do
subject { Blink1.list }
it { should be_a_kind_of Array }
end

describe 'random' do
subject {
ret = nil
Blink1.open do|b1|
ret = b1.random 20
end
ret
}
it { should be_a_kind_of Fixnum }
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

RSpec.configure do |config|
if ENV['CI']
config.filter_run_excluding :device => true
config.filter_run_excluding device: true
end
end

Expand Down