#!/usr/bin/env ruby require "lignite" require "thor" require "fileutils" include Lignite::Bytes # The VM current working directory is /home/root/lms2012/sys # which is not very useful. A better default is /home/root/lms2012/prjs # which is displayed on the 2nd tab of the brick UI. EV3TOOL_HOME = "../prjs".freeze def phandles handles = sc.list_open_handles puts "OPEN HANDLES: ", hexdump(handles) end class Ev3Tool < Thor desc "upload LOCAL_FILENAME [BRICK_FILENAME]", "upload a program or a file" map "ul" => "upload" def upload(local_filename, brick_filename = nil) data = File.read(local_filename, encoding: Encoding::BINARY) unless brick_filename prj = File.basename(local_filename, ".rbf") brick_filename = "#{EV3TOOL_HOME}/#{prj}/#{prj}.rbf" end handle = sc.begin_download(data.bytesize, brick_filename) sc.continue_download(handle, data) end desc "download BRICK_FILENAME [LOCAL_FILENAME]", "download a file" map "dl" => "download" def download(brick_filename, local_filename = nil) local_filename ||= File.basename(brick_filename) fsize, handle, data = sc.begin_upload(4096, brick_filename) File.open(local_filename, "w") do |f| loop do f.write(data) fsize -= data.bytesize break if fsize.zero? handle, data = sc.continue_upload(handle, 4096) end end end desc "list-files DIRNAME", "list DIRNAME in a long format" map "ls-l" => "list-files" def list_files(name) name ||= EV3TOOL_HOME name = "#{EV3TOOL_HOME}/#{name}" unless name.start_with?("/") result = "" fsize, handle, data = sc.list_files(4096, name) loop do result += data fsize -= data.bytesize break if fsize.zero? handle, data = sc.continue_list_files(handle, 4096) end result rescue Lignite::VMError nil end desc "ls DIRNAME", "list DIRNAME in a short format" def ls(name) raw = list_files(name) return nil if raw.nil? raw.lines.map do |l| l = l.chomp next nil if ["./", "../"].include?(l) next l if l.end_with?("/") # skip checksum + space + size + space l[32 + 1 + 8 + 1..-1] end.compact end desc "start NAME", "start a program" def start(name) if name.include?("/") name = "#{EV3TOOL_HOME}/#{name}" unless name.start_with?("/") else name = "#{EV3TOOL_HOME}/#{name}/#{name}.rbf" end unless file_exist?(name) $stderr.puts "File #{name.inspect} not found" exit 1 end slot = Lignite::USER_SLOT no_debug = 0 dc.block do # these are local variables data32 :size data32 :ip file_load_image(slot, name, :size, :ip) program_start(slot, :size, :ip, no_debug) end end desc "stop", "stop a running program" def stop dc.program_stop(Lignite::USER_SLOT) end no_commands do def file_exist?(name) dirname = File.dirname(name) filename = File.basename(name) files = ls(dirname) || [] files.include?(filename) end def assisted_connection Lignite::Connection.create rescue => e fn = Lignite::Connection::Bluetooth.config_filename $stderr.puts <