Lets create some lua libraries - page 2 - LUA Scripting - CHDK Forum  

Lets create some lua libraries

  • 14 Replies
  • 11077 Views
*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: Lets create some lua libraries
« Reply #10 on: 01 / February / 2009, 17:04:01 »
Advertisements
And now that I've been reminded of that old thing, I found this function that someone may find useful (I used it in the new unfinished version of f-log.lua to automatically find new log names).

Code: (Lua) [Select]
--[[
    Find the smallest three digit zero-padded number which doesn't match any
    existing file in A/CHDK/LOGS/ when used as a part of a file name of the
    form headerNNN.extension. Return complete filename without path.
   
    Example:
    logfilename = log.find_new_filename("log_","txt")
    will return "log_002.txt" if log_000.txt and log_001.txt already exist.
   
    If all files up to log_999.txt are found, log_999.txt is returned.   
--]]
function log.find_new_filename(header,extension)
  local numstr 
  local filename
 
  for i=0,999 do
    -- zero pad
    if i<10 then
      numstr = "00"..tostring(i)
    elseif i<100 then
      numstr = "0"..tostring(i)
    else
      numstr = tostring(i)
    end
   
    filename = tostring(header) .. numstr .. "." .. tostring(extension)
    local filepath="A/CHDK/LOGS/"..filename
    local r,msg = os.stat(filepath)
    if r==nil then
      return filename
    end
    -- note: long for loop without forced sleep!
  end

  -- no free names found
  return filename

end


Also, it seems that even before I screwed up the entire log library, I had done this modification (extremely likely from reyalp's advice...) to f-log.lua:
Code: (Lua) [Select]
function log.print(...)
  if log.type==1 and log.isopen==true then
    log.file:write(...)
  else
    print(...)
  end
end

and removed line

     io.output(log.file)

from function log.open(name).


Re: Lets create some lua libraries
« Reply #11 on: 02 / February / 2009, 15:54:14 »
fudgey - do you know of a simple method to truncate text files with lua? i'm working on creating scripts intended to run for long durations (months? a year??) without intervention. so far this has meant removing all logging. a much better option would be to truncate logs once they've reached a specific size.

*

Offline fudgey

  • *****
  • 1705
  • a570is
Re: Lets create some lua libraries
« Reply #12 on: 02 / February / 2009, 16:12:55 »
fudgey - do you know of a simple method to truncate text files with lua? i'm working on creating scripts intended to run for long durations (months? a year??) without intervention. so far this has meant removing all logging. a much better option would be to truncate logs once they've reached a specific size.

Exactly what do you mean by truncating? If you want to delete oldest lines from a log (cut stuff away from the start), I'd suggest creating a new log file for each day (for instance) and deleting old files routinely or as soon as space becomes an issue. That log.find_new_filename(header,extension) function I posted above suits this purpose in part.

Your needs may vary, but the longer an experiment, the more precious logs are to me... so I'd instead of deleting old logs, I would tweak my script to keep logs compact, maybe even log in binary (or, going extreme, parts of zlib could be ported to CHDK and added to lua to compress daily logs). You can fit tons of measurement data in the size of a single JPEG, after all.

Re: Lets create some lua libraries
« Reply #13 on: 02 / February / 2009, 16:31:00 »
maybe i'm being a slacker but i don't see much valuable info in my logs should something go wrong. its just too likely that the problem would be something that i haven't taken into consideration.

i can get files off the camera via eye-fi. if the logs are important to me i'll rename them with a .jpg extension so they're transferred to my computer. hm, maybe thats what i should do.



Re: Lets create some lua libraries
« Reply #14 on: 10 / February / 2009, 09:37:47 »
here's an update to lua libs.  any feedback is appreciated.

WARNING - THESE AREN'T VERIFIED STABLE

i'm tracking down some strange and frustrating bugs with my sunset F16.lua script which relies on these.
---

cam.lua
sec_to_tv96(sec)
sec1000_to_tv96(sec1000)
ISO_to_sv96(ISO)
smartShoot(tv,sv,focusDistance)

date.lua
format_nn(n)
timestamp(t)
datestamp()
datetimestamp()

disk.lua
remaining_shots(free_start, shots_taken)
secs_to_disk_full(free_start, tick_start)
deleteFiles(fileList)
copyFile(original, duplicate)
moveFile(original, newPath)
filterImageFiles(searchString)
deleteUntilJpgFree(min,target)
deleteUntilMbFree(mbMin, mbTarget)
getImageDirectories()
getImageList()
getNewImage()
renameNewImage(name)

log.lua
open(name)
close()
print(...)
find_new_filename(header,extension)

util
check_range(current, minimum, maximum)
sleepMinutesVerbose(minutes)
sleepUntilTime(time)
sleepUntilNextPeriodOfAnHour(periodCount)
printAndPause(...)

 

Related Topics