How to write binary files? - LUA Scripting - CHDK Forum

How to write binary files?

  • 19 Replies
  • 16586 Views
*

Offline fudgey

  • *****
  • 1705
  • a570is
How to write binary files?
« on: 28 / January / 2009, 17:50:36 »
Advertisements
How do I write binary files? I wrote this to dump some RAM (don't run it if you can't tell what it does) but it prints clear text numbers instead of bytes:

Code: (lua) [Select]
--[[
@title peek ImgTbl a570 100e
@param a script enable
@default a 0
--]]

-- prevent accidental run by careless people
if a>0 then
  len=peek(0x5e18)
  start=peek(0x5e14)
 
  print ("start: "..start)
  print ("length: "..len)
  print ("press SET to dump")
  print ("press MENU to exit")
  wait_click(0)
  if is_pressed "set" then
    cls()
    words=len/4
    if words*4 < len then
      print("length/4 non-integer, padding")
      words=words+1
    end
    print("dumping "..words.." words")
   
    -- open file
    fout=io.open("A/CHDK/imgtbl.dat", "wb")
    if (fout) then 
      for aye=0,words-1 do
        word=peek(start+4*aye)
        -- word has 4 chars, first char least significant: word = c4c3c2c1
        chr1=bitand(word,0xff)
        chr2=bitshri(bitand(word,0xff00),8)
        chr3=bitshri(bitand(word,0xff0000),16)
        chr4=bitshri(bitand(word,0xff000000),24)
       
        fout:write(chr1)
        fout:write(chr2)
        fout:write(chr3)
        fout:write(chr4)
      end
     
      io.close(fout)
     
      print("done.")
    else
      print("io.open failed")
    end
   
    print ("press a key to exit")
    wait_click(0)
  end
end

From http://www.lua.org/pil/21.2.2.html I kind of thought that would work...

Re: How to write binary files?
« Reply #1 on: 28 / January / 2009, 20:24:48 »
ug, didn't see your url.

have you tried running the example itself?

should help establish if its a problem with your script or lua chdk
« Last Edit: 28 / January / 2009, 20:27:21 by mattkime »

*

Offline reyalp

  • ******
  • 14125
Re: How to write binary files?
« Reply #2 on: 28 / January / 2009, 22:24:28 »
Don't forget what the H stands for.

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: How to write binary files?
« Reply #3 on: 29 / January / 2009, 14:16:21 »

Re: How to write binary files?
« Reply #4 on: 05 / February / 2009, 02:09:06 »
alright, this function seems to work for copying files but its very slow. (about a minute a mb) thats likely because i'm reading and writing 10 bytes at a time. what would be a safe higher value?

Code: [Select]
function disk.copyFile()
local logFile = io.open("A/DCIM/100CANON/IMG_0001.JPG")
local newFile = io.open("A/DCIM/100CANON/new.JPG","w")

while true do
local bytes = logFile:read(10)
if not bytes then break end
newFile:write(bytes)
end
logFile:close()
newFile:close()
end

*

Offline reyalp

  • ******
  • 14125
Re: How to write binary files?
« Reply #5 on: 05 / February / 2009, 02:40:26 »
You should be able to read/write many KB at a time, limited only by available memory.

I'm not sure, but you may run into memory issues doing this, because each read/write creates a new string, which even though it becomes unreferenced in the next loop iteration, won't necessarily be garbage collected. If this does become a problem, you can use collectgarbage to force collection.
Don't forget what the H stands for.

Re: How to write binary files?
« Reply #6 on: 05 / February / 2009, 07:44:20 »
oh, one annoyance - files created by lua don't get their date/time set correctly.

Re: How to write binary files?
« Reply #7 on: 05 / February / 2009, 11:03:28 »
>>You should be able to read/write many KB at a time, limited only by available memory.

reading and writing in 4Kb segments appears to work fine. i found fudgey's excellent memory usage wiki page and it appears that i should be able to write in 100Kb blocks without having memory issues. i'll have to do some tests to find what the optimal size is to produce a balance of low memory usage and high speed.

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: How to write binary files?
« Reply #8 on: 05 / February / 2009, 13:48:12 »
You may find my new free RAM OSD useful for testing this...(or just log free RAM to another file or to a variable in Lua):

http://chdk.setepontos.com/index.php/topic,2831.msg28991.html#msg28991.

100 kiB is a huge amount of RAM for a570is IMO. With blocks that large, if Lua doesn't use the same exact spot for each successive string, I fear it may fragment RAM beyond belief pretty quickly (i.e. if you are creating a generic file mover function, maybe it should have a block size argument if it turns out 100 kiB is significantly faster than 4 kiB).

And a reminder from reyalp's earlier post, may also be useful to you:
mem_use_in_kb=collectgarbage("count")
release some unused Lua RAM (try if mem_use_in_kb goes too high):
collectgarbage("collect")

Re: How to write binary files?
« Reply #9 on: 05 / February / 2009, 16:25:01 »
>>100 kiB is a huge amount of RAM for a570is IMO.

I'm curious what that is based on. From your wiki page on memory it seems that there should be plenty of room to do everything and have more than 100k to spare. Anyway, i'm interested finding a good universal number and i'm willing to take suggestions.

i guess i'll be running some tests tonight to see how block size alters speed.

 

Related Topics


SimplePortal © 2008-2014, SimplePortal