Using the print_screen function - Script Writing - CHDK Forum supplierdeeply

Using the print_screen function

  • 3 Replies
  • 1429 Views
*

Offline lapser

  • *****
  • 1093
Using the print_screen function
« on: 01 / September / 2012, 22:34:12 »
Advertisements
One of the most useful scripting commands is "print", which writes text to the screen.  It is also important to be able to write text to a log file for later viewing. CHDK provides the "print_screen" function to do this:

http://chdk.wikia.com/wiki/CHDK_scripting#print_screen

To turn on logging to a file, you start your program with "print_screen(nnnn)" where nnnn is a number from 0 to 9999. The resulting file will be named "LOG_nnnn.TXT"

Contrary to what it says in the above documentation, if the log file already exists, it is overwritten, and you lose the previous log data each time your run your script. It would be nice to have the option to append to the same file, or automatically write to a new file if the requested file exists.

Lua has file io functions that can do this, but using them adds a lot of complexity to your scripts, plus they don't work with uBasic. Here's a different solution to the problem. Start your script with :
Code: (lua) [Select]
print_screen(os.date("%H%M"))
print(os.date())
Since the file name contains the 4 digit, 24-hour time, there is no chance you will overwrite the file until at least the next day. That leaves time to move the log files to your computer at the end of the day. The 2nd line of code starts off the log file with the full date and time string.

The uBasic function get_time can do the same thing. I'll leave it to someone else to write the uBasic line. Thanks.
« Last Edit: 03 / September / 2012, 19:29:04 by lapser »
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

Re: Using the print_screen function
« Reply #1 on: 01 / September / 2012, 22:42:43 »
Good app note.  Thanks!
Ported :   A1200    SD940   G10    Powershot N    G16

*

Offline lapser

  • *****
  • 1093
Re: Using the print_screen function
« Reply #2 on: 03 / September / 2012, 22:32:30 »
This is how to do the same thing in Basic.
Code: (vb) [Select]
H=100*(get_time 2) + get_time 1
print_screen H
print get_time 4,get_time 3,get_time 5,H
If you just want to open a unique log file (for at least 24 hours as described above) just enter:
Code: (vb) [Select]
print_screen 100*(get_time 2) + get_time 1
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline lapser

  • *****
  • 1093
Re: Using the print_screen function
« Reply #3 on: 30 / September / 2012, 16:24:48 »
You can also rename the print_screen file in the restore() function:

Code: (lua) [Select]
function restore()
  print_screen(2) -- closes LOG_0001.TXT so you can rename it
  os.rename("A/CHDK/LOGS/LOG_0001.TXT","A/CHDK/LOGS/"..os.date("%m%d%H%M")..".LOG")
end

print_screen(1)
print(os.date())
If you don't mind that the entire file name isn't visible in the chdk file browser, you can add a "%y" to the new file name:
  os.date("%y%m%d%H%M")
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos


 

Related Topics