Skip to content

Instantly share code, notes, and snippets.

@Keksbuster
Created February 28, 2012 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Keksbuster/1932933 to your computer and use it in GitHub Desktop.
Save Keksbuster/1932933 to your computer and use it in GitHub Desktop.
Ruby with joystick
require "rubygems"
require "rjoystick"
require "serialport"
#=begin
# example 1
#
#
# ARGV[0] => /dev/input/jsX
#rc-werte
rc_chan = Array.new(8,127) #create 8 channels and set them to 1500 [µs]
invert = 1
last_update = Time.now
k = Rjoystick::Device.new("/dev/input/js0")
puts k.axes
puts k.buttons
puts k.name
puts k.version
puts k.axes_maps
while true
now = Time.now
if((now-last_update)*1000.0 >20.0) #check all 50ms (20Hz) to update signal
puts "#{rc_chan}"
last_update = Time.now
end
e = k.event
if e.type == Rjoystick::Event::JSAXIS
if(e.number==3 || e.number ==1) #invert axis 1 and 3
invert = -1
else
invert = 1
end
rc_chan[e.number] = (128 + (128.000/32768.000)*invert*e.value).to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment