Skip to content

Instantly share code, notes, and snippets.

@zwpp
Created May 25, 2016 14:54
Show Gist options
  • Save zwpp/6f29f239c318cef5c1fb8e0808767cbb to your computer and use it in GitHub Desktop.
Save zwpp/6f29f239c318cef5c1fb8e0808767cbb to your computer and use it in GitHub Desktop.
This can send EV3RT application with less effort.
#!/usr/bin/env ruby
EV3MAC = '00:00:00:00:00:00'.freeze # Replace with appropriate MAC Address.
RFCOMM = '/dev/rfcomm0'.freeze
require 'open3'
module Ev3
class << Ev3
def connected?
File.exist?(RFCOMM)
end
def connect
return if connected?
resault, thread = Open3.popen2e("rfcomm -r connect #{RFCOMM} #{EV3MAC}")[1..2]
until connected? # Wait for connect, using polling.
print "Waiting...\r"
raise 'rfcomm: ' + resault.read unless thread.status
sleep 0.1
end
puts "rfcomm started pid: #{thread.pid}"
end
def send_file(filename)
raise 'ev3_not_connected' unless connected?
system("sz \"#{filename}\"", [:out, :in] => [RFCOMM, 'r+'])
end
def send_file_as(srcname, dstname)
raise 'ev3_not_connected' unless connected?
dstpath = "/tmp/#{dstname}"
File.symlink(File.realpath(srcname), dstpath)
system("sz \"#{dstpath}\"", [:out, :in] => [RFCOMM, 'r+'])
File.delete(dstpath)
end
end
end
def ev3_connect
unless Ev3.connected?
puts 'EV3 does not connected.'
puts 'Turn on EV3 and press [Enter].'
gets
Ev3.connect
end
end
# main
case ARGV.size
when 1
ev3_connect
Ev3.send_file(ARGV[0])
when 2
ev3_connect
Ev3.send_file_as(ARGV[0], ARGV[1])
else
puts "usage: \033[1mruby " + __FILE__ + "\033[0m \033[3m\033[4mfilename\033[0m [\033[3m\033[4mdstname\033[0m]"
end
# vim: set expandtab sw=2 :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment