1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
class TILT_LED
def initialize
$: << File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
require 'freenect'
check_args
initialize_device
tilt_led_set
cleanup
end
#
# Define usage
#
def script_usage
puts "Usage: ruby tilt_led.rb -l (0-6) -t (-30-30)"
puts "LED Options:"
puts " 0 - Off"
puts " 1 - Green"
puts " 2 - Red"
puts " 3 - Yellow"
puts " 4 - Blink Yellow"
puts " 5 - Blink Green"
puts " 6 - Blink Red/Yellow"
puts "Tilt Options:"
puts " Value must be between -30 and 30 degrees."
exit 1
end
#
# Check arguments. I'm sure there's a better way to do this. What can I saw, I'm a noob.
#
def check_args
if( ARGV.include?("-l") || ARGV.include?("-t") )
if (ARGV.include?("-l"))
@led_option = ARGV[(ARGV.index("-l") + 1 )]
ARGV.delete("-l")
ARGV.delete(@led_option)
if @led_option.to_i < 0 || @led_option.to_i > 6
script_usage
end
end
if (ARGV.include?("-t"))
@tilt_option = ARGV[(ARGV.index("-t") + 1 )]
ARGV.delete("-t")
ARGV.delete(@tilt_option)
if @tilt_option.to_i < -30 || @tilt_option.to_i > 30
script_usage
end
end
else
script_usage
end
end
def initialize_device
@ctx = FFI::MemoryPointer.new(:pointer)
@dev = FFI::MemoryPointer.new(:pointer)
#
# initialize context / @device
#
if (FFI::Freenect.freenect_init(@ctx,nil) != 0)
puts "Error: can't initialize context"
exit 1
end
@ctx = @ctx.read_pointer
if (FFI::Freenect.freenect_open_device(@ctx, @dev, 0) != 0)
puts "Error: can't initialize device"
exit 1
end
@dev = @dev.read_pointer
end
def tilt_led_set
#
# Set tilt and/or led options on the @device
#
puts "Number of devices: #{FFI::Freenect.freenect_num_devices(@ctx)}"
if (not @tilt_option.nil?)
puts "Tilt set to: #{@tilt_option}"
FFI::Freenect.freenect_set_tilt_degs(@dev, @tilt_option.to_f)
end
if (not @led_option.nil?)
puts "LED set to: #{@led_option}"
FFI::Freenect.freenect_set_led(@dev, @led_option.to_i)
end
end
def cleanup
#
# Close the context and @device (for cleanup purposes)
#
FFI::Freenect.freenect_close_device(@dev)
if FFI::Freenect.freenect_shutdown(@ctx) != 0
puts "Error shutting down context"
exit 1
else
puts "Successfully shut down context"
exit 0
end
end
end
run = TILT_LED.new
|