Last active
August 29, 2015 14:24
-
-
Save PogiNate/ce9d7d85fd04eafdc094 to your computer and use it in GitHub Desktop.
A simple little toy to wire the Leap Motion to a Blink(1). Basically I've got two little expensive toys on my desk and I wanted to make them talk to each other.
This file contains hidden or 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/ruby | |
# This is just a simple toy to play with a Leap Motion and a Blink(1). | |
# Obviously requires both of those. | |
# Leap Motion comes from here: https://www.leapmotion.com/ | |
# Blink(1): http://blink1.thingm.com/ | |
# The 'artoo' below is the artoo-leapmotion gem. Install that and it'll pick up all the | |
# other dependencies for you. | |
# You will need Blink1Control running in API server mode for this to work. | |
# I'd call this a work in progress, but I've had my fun. If this is interesting to anyone then | |
# Feel free to play with it more. | |
require 'artoo' | |
require 'yaml' | |
require 'open-uri' | |
require 'uri' | |
connection :leapmotion, :adaptor => :leapmotion, :port => '127.0.0.1:6437' | |
device :leapmotion, :driver => :leapmotion | |
TIMEOUT = 80 | |
@frame_count = 0 | |
@light_time = 0.30 | |
work do | |
on leapmotion, :open => :on_open | |
on leapmotion, :frame => :on_frame | |
on leapmotion, :close => :on_close | |
end | |
def on_open(*args) | |
puts args | |
end | |
def on_frame(*args) | |
if @frame_count == 0 || @frame_count.nil? | |
frame = args[1] | |
@frame_count = 0 | |
if frame.pointables.length > 0 | |
process_frame frame | |
end | |
@frame_count += 1 | |
else | |
@frame_count= (@frame_count>=TIMEOUT) ? 0 : @frame_count + 1 | |
end | |
end | |
# Grabs the coordinates from the frame info. | |
def get_coordinates pointable | |
thisPoint = pointable.tipPosition | |
return thisPoint | |
end | |
# Turns a set of X,Y,Z coordinates into a hex expressed color. | |
# I'm doing a lot of nasty things to my original data to make it fit here. | |
# First off I'm shoving everything into the positive just to make it easier. | |
# I could have handled that differently; like I could have found out exactly how big the | |
# range is and just mapped it so it spread the 255 degrees we have across the negative and positive | |
# but I didn't want to. | |
# So this just takes the coordinates, forces them into the positive and caps anything that tries to go | |
# above 255. | |
def point_to_hex coordinates | |
coordinates.each_index do |i| | |
coordinates[i] = coordinates[i].abs | |
coordinates[i] = (coordinates[i]>255) ? 255 : coordinates[i] | |
end | |
return sprintf("#%02X%02X%02X", coordinates[0], coordinates[1], coordinates[2]) | |
end | |
# Sends the color set to the blink(1) device. | |
def send_to_blink color, led | |
url = "http://localhost:8934/blink1/fadeToRGB?rgb=#{color}&time=#{@light_time}&ledn=#{led}" | |
clean_uri = URI.escape(url) | |
puts open(clean_uri).read | |
end | |
# Takes the first two pointables and sends them to the two leds on the Blink(1). | |
# There may be more pointables in the frame, but we've only got two leds to play with | |
# So we're conveniently ignoring them. | |
def process_frame(frame) | |
frame.pointables.slice(0, 2).each_index do |i| | |
coords = get_coordinates frame.pointables[i] | |
color = point_to_hex coords | |
puts color | |
send_to_blink color, i + 1 | |
end | |
end | |
def on_close(*args) | |
puts args | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment