Logging date and time to textfile (print_screen) - page 2 - Script Writing - CHDK Forum

Logging date and time to textfile (print_screen)

  • 15 Replies
  • 6082 Views
*

Offline Jucifer

  • *****
  • 251
  • [A710IS]
Re: Logging date and time to textfile (print_screen)
« Reply #10 on: 21 / August / 2008, 03:30:51 »
Advertisements
alrighty then, hacked print_screen a bit further...
print_screen(0) writes to log_0000.txt (or, in ubasic, print_screen 10000)
print_screen(false) disables writing (as it did before) (ubasic: print_screen 0)
(as a side effect of the current implementation, also print_screen(-10000) disables writing... but don't tell anyone ;)


Hm. I think I'll modify it even further in a couple of days...
or weeks...
or not...

Re: Logging date and time to textfile (print_screen)
« Reply #11 on: 21 / August / 2008, 16:08:31 »
Great hack.
Together with the get_time function you now can make the logfile named as e.g. log_MMDD.txt (MM=month, DD=day) like in the following LUA snipped:

--[[
@title Timestamp test
@param p Log (0=No, 1=Yes)
--]]

function date()
   yyyy = (get_time("Y"))
   mm   = (get_time("M"))
   if (tonumber(mm)<10) then
      mm = "0"..mm
   end
   dd   = (get_time("D"))
   if (tonumber(dd)<10) then
      dd = "0"..dd
   end
   yyyymmdd = yyyy .. "-" .. mm .. "-" .. dd
   log_id = mm..dd
end

function time()
   hh = (get_time("h"))
   if (tonumber(hh)<10) then
      hh = "0"..hh
   end
   mi = (get_time("m"))
   if (tonumber(mi)<10) then
      mi = "0"..mi
   end
   ss = (get_time("s"))
   if (tonumber(ss)<10) then
      ss = "0"..ss
   end
   hhmmss = hh .. ":" .. mi .. ":" .. ss
end

function openfile()
   print_screen (log_id)
end

function closefile()
   print_screen (false)
end

-- main

date()
if (p == 0) then
   closefile()
else
   openfile()
end
print ( "log_"..log_id..".txt" )
n = 0
shoot()
n = n + 1
time()
ts = "MD " .. n .. ": " .. yyyymmdd .. "-" .. hhmmss
cls()
print( ts )
sleep( 5000 )
shoot()
n = n + 1
time()
ts = "MD " .. n .. ": " .. yyyymmdd .. "-" .. hhmmss
cls()
print( ts )
closefile()


*

Offline PhyrePhoX

  • *****
  • 2254
  • make RAW not WAR
    • PhyreWorX
Re: Logging date and time to textfile (print_screen)
« Reply #12 on: 02 / September / 2008, 19:25:19 »
added get_time to ubasic. \o/

Re: Logging date and time to textfile (print_screen)
« Reply #13 on: 05 / September / 2008, 16:40:26 »
Thanks PhyrePhoX: works OK.
See the get_time statement used in a similar uBasic snippet:

@title Timestamp test
@param p Log (0=No, 1=Yes)
@default p 1

rem main
gosub "date"
if p = 0 then
   gosub "closefile"
else
   gosub "openfile"
endif
if e<10 then
   print "log_";"0";i;".txt"
else
   print "log_";i;".txt"
endif
n = 0
shoot
gosub "time"
cls
n = n + 1
gosub "printit"
sleep 5000
shoot
gosub "time"
cls
n = n + 1
gosub "printit"
gosub "closefile"
end

:date
rem f=YYYY, e=MM, d=DD
f = get_time(5)
e = get_time(4)
d = get_time(3)
i = e * 100 + d
return

:time
rem h=hh, m=mm, s=ss
h = get_time(2)
m = get_time(1)
s = get_time(0)
return

:printit
rem some zero prefixing for readability...
if e<10 and d>9  and s<10 then
   print "No ";n;": ";f;"-0";e;"-";d;"-";h;":";m;":";"0";s
endif
if e<10 and d>9  and s>9  then
   print "No ";n;": ";f;"-0";e;"-";d;"-";h;":";m;":";s
endif
if e<10 and d<10 and s<10 then
   print "No ";n;": ";f;"-0";e;"-0";d;"-";h;":";m;":";"0";s
endif
if e<10 and d<10 and s>9  then
   print "No ";n;": ";f;"-0";e;"-0";d;"-";h;":";m;":";s
endif
if e>9  and d<10 and s<10 then
   print "No ";n;": ";f;"-";e;"-0";d;"-";h;":";m;":";"0";s
endif
if e>9  and d<10 and s>9  then
   print "No ";n;": ";f;"-";e;"-0";d;"-";h;":";m;":";s
endif
if e>9  and d>9  and s<10 then
   print "No ";n;": ";f;"-";e;"-";d;"-";h;":";m;":";"0";s
endif
if e>9  and d>9  and s>9  then
   print "No ";n;": ";f;"-";e;"-";d;"-";h;":";m;":";s
endif
return

:openfile
print_screen i
return

:closefile
print_screen 0
return


Re: Logging date and time to textfile (print_screen)
« Reply #14 on: 21 / September / 2008, 04:50:51 »
FYI
With the (preliminary) lua iolib and oslib port as from build 520
the LUA snippet can be shortened to:

--[[
@title Timestamp test
@param p Log (0=No, 1=Yes)
--]]

function date()
   yyyymmdd = os.date("%Y-%m-%d")
   log_id = os.date("%m%d")
end

function time()
   hhmmss = os.date("%H:%M:%S")
end

function openfile()
   print_screen (log_id)
end

function closefile()
   print_screen (false)
end

-- main

date()
if (p == 0) then
   closefile()
else
   openfile()
end
print ( "log_"..log_id..".txt" )
n = 0
shoot()
n = n + 1
time()
ts = "MD " .. n .. ": " .. yyyymmdd .. "-" .. hhmmss
cls()
print( ts )
sleep( 5000 )
shoot()
n = n + 1
time()
ts = "MD " .. n .. ": " .. yyyymmdd .. "-" .. hhmmss
cls()
print( ts )
closefile()

*

Offline reyalp

  • ******
  • 12742
Re: Logging date and time to textfile (print_screen)
« Reply #15 on: 21 / September / 2008, 05:18:40 »
You can also use io to do your logging instead of print/print_screen/cls.

Something like
Code: (lua) [Select]
logfile,msg=io:open("A/log"..log_id.."txt","wb")
if not logfile then
 error("couldn't open log file" .. msg)
end
-- ... loop with MD stuff
  logfile:write("MD ",n,":",os.date("%Y-%m-%d %H:%M:%S"),"\n")
-- ... MD loop
logifle:close()
You could also append to an existing log with "ab" instead of "wb"

As mentioned here: Night-time time-lapse I'd suggest using print( "MD ",n,": ", yyyymmdd, "-", hhmmss) instead of .. when it's convenient, especially in long running loops.

Don't forget what the H stands for.

 

Related Topics