Created
December 11, 2012 07:00
-
-
Save jhsu/4256440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Blink1 | |
def self.list | |
native(["--list"]).select {|l| l =~ /^id/ } | |
end | |
def self.ids | |
list.map {|info| info[/^id:(\d+)/]; $1 } | |
end | |
def self.all | |
ids.map {|id| new(id) } | |
end | |
attr_reader :id | |
def initialize(id) | |
@id = id | |
end | |
def blink | |
native ["--blink", 2, "-m", "100"] | |
self | |
end | |
def off | |
native ["--off"] | |
self | |
end | |
def color(rgb="255,255,255") | |
native ["--rgb", rgb, "-m", "800"] | |
self | |
end | |
private | |
def native(flags) | |
flags += ["-d",id] | |
self.class.native(flags) | |
self | |
end | |
def self.native(flags) | |
flags = flags.map(&:to_s) | |
output = IO.popen(['blink1-tool', *flags]) | |
output.readlines | |
end | |
end | |
blinks = Blink1.all | |
blinks.each {|b| b.blink } | |
puts "Which blinked first? ([R]ight/[l]eft):" | |
choice = STDIN.gets.chomp | |
if choice =~ /l/i | |
left, right = blinks | |
else | |
right, left = blinks | |
end | |
begin | |
system("stty raw -echo") #=> Raw mode, no echo | |
loop do | |
char = STDIN.getc | |
break if char =~ /q/i | |
Thread.new do | |
case char | |
when /h/ | |
left.color("0,200,0") | |
sleep 0.5 | |
left.off | |
sleep 0.5 | |
left.color("0,200,0") | |
sleep 0.3 | |
left.off | |
when /l/ | |
right.color("0,200,0") | |
sleep 0.5 | |
right.off | |
sleep 0.5 | |
right.color("0,200,0") | |
sleep 0.3 | |
right.off | |
end | |
end | |
end | |
rescue Interrupt | |
ensure | |
blinks.each(&:off) | |
system("stty -raw echo") #=> Reset terminal mode | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment