#!/usr/bin/ruby $oxodir="/data/oxocard" $port = "/dev/ttyUSB0" def ex(cmd) puts cmd res = system(cmd) if (res==nil) puts "Could not even run the command #{cmd}. Aborting." exit(-1) end return res end def trycmd(cmd, n=2) n.times { return true if ex(cmd) puts "command returned an error, trying again in 2 sec.." sleep(2) } puts "Executing #{cmd} returned an error. Aborting." exit(-1) end def checkForFile(glob) files = Dir.glob(glob) if files.size==0 puts "No file of pattern #{glob} found. Attempting downloading it..." trycmd("ruby #{$oxodir}/bin/download-images.rb",1) files = Dir.glob(glob) if files.size==0 puts "Sorry, cannot get file of pattern #{glob}, aborting now." exit(-1) end end return File.realpath(files.max_by {|f| File.mtime(f)}) end def flash_tiger() # Flashing MicroPython file=checkForFile("#{$oxodir}/MicroPython*.bin") puts "Using most recent file #{file}" trycmd("esptool.py --port #{$port} erase_flash") trycmd("esptool.py --port #{$port} --chip esp32 write_flash -z 0x1000 #{file}") # TigerJython python modules file=checkForFile("#{$oxodir}/oxocardmodules*.zip") puts "Using most recent file #{file}" zipdir = "#{$oxodir}/oxocardmodules" ex("rm -rf #{zipdir}; mkdir #{zipdir}") trycmd("cd #{zipdir}; unzip #{file}",1) Dir.glob("#{zipdir}/*.py").sort.each{|f| trycmd("ampy -p #{$port} put '#{File.realpath(f)}'",3) } end def flash_blockly() file=checkForFile("#{$oxodir}/blockly_flash*.zip") puts "Using most recent file #{file}" zipdir = "#{$oxodir}/blockly" trycmd("rm -rf #{zipdir}",1) trycmd("cd #{zipdir}; unzip -o #{file}",1) trycmd("esptool.py --port #{$port} erase_flash",2) trycmd("cd #{zipdir}; esptool.py --port #{$port} --chip esp32 write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect 0x1000 bootloader_blockly.bin 0xe000 phy_init_data_blockly.bin 0x10000 OxocardBlockly.bin 0x8000 partitions_blockly.bin",3) end if ARGV.size==0 puts "Usage: ruby flash-image.rb t[iger]|b[lockly]" exit end if ARGV[0]=~/^[Tt]/ flash_tiger() elsif ARGV[0]=~/^[Bb]/ flash_blockly else puts "Usage: ruby flash-image.rb t[iger]|b[lockly]" end