Some contributed examples using ptpcam and the Lua script from ptpCamGui
- To give yourself access to the same functionality as ptpcamgui create the file A/CHDK/LUALIB/lptpgui.lua with the text from below. This is a copy of what ptpcamgui copies to your SD card to enable many of its functions. If you want the latest version, you will find this on your memory card after using ptpcamgui to download files from your camera.
-- lptpgui.lua -- store this file in CHDK/LUALIB
lptpgui = {}
lptpgui.version=120
-- begin -- DCIM Download --
local function getfileson(path,hfile)
local fd,c,s,fpath,stat,i,tc,ts=os.listdir(path),0,0
if fd~=nil and #fd>0 then
for i=1, #fd do
fpath=path .. "/" .. fd[i]
stat=os.stat(fpath)
if stat~=nil then
if stat.is_dir==true then
if fd[i]~="CANONMSC" then
tc,ts=getfileson(fpath,hfile)
c=c+tc
s=s+ts
end
elseif stat.is_file==true then
hfile:write(path .. "/" .. "|" .. fd[i] .. "|" .. os.date("%Y%m%d%H%M%S", stat.mtime) .. "\n")
s=s+(stat.size-1)/4096+1
c=c+1
end
end
end
end
return c,s
end
local function getdcimfiles()
local fn,count,size,tc,ts="A/ptpgui.txt",0,0
local file=io.open(fn,"wb")
if file then
tc,ts=getfileson("A/DCIM",file)
count=count+tc
size=size+ts
file:close()
end
if count>0 then size=(size-1)/256+1 else size=0 end
return count .. "\n" .. fn .. "\n" .. size
end
function lptpgui.dcimdl()
return getdcimfiles()
end
-- end -- DCIM Download --
-- begin -- GetCamMode --
function lptpgui.getcammode()
modemap=require("GEN/modelist")
capmode=require("capmode")
mode=""
for i=1, #modemap do
if capmode.valid(modemap[i]) then mode=mode..modemap[i].."|" end
end
return mode
end
-- end -- GetCamMode --
-- begin -- GetCamInfo --
function lptpgui.getcaminfo()
info=""
info=info..get_vbatt().."|"..get_config_value(8).."|"..get_config_value(9).."|"..get_free_disk_space()
return info
end
-- end -- GetCamInfo --
-- begin -- ExecLuaString --
local function init_defaultvars(tvars)
if type(tvars)=="table" then
for var,val in pairs(tvars) do rawset(_G,var,val) end
end
end
local function gui_print(...)
local str=""
for i=1,select("#",...) do
if str~="" then str=str.." " end
str=str..tostring(select(i,...))
end
write_usb_msg(str)
cam_print(...)
end
function lptpgui.exec_luastring(script,tvars)
init_defaultvars(tvars)
script=string.gsub(script,"\002","\n")
script=string.gsub(script,"\003","]]")
cam_print=print
print=gui_print
func,merr=loadstring(script)
if func==nil then
print(merr)
else
func()
end
end
-- end -- ExecLuaString --
-- begin -- ExecLuaFile --
function lptpgui.exec_luafile(filename,tvars)
init_defaultvars(tvars)
cam_print=print
print=gui_print
func,merr=loadfile(filename)
if func==nil then
print(merr)
else
func()
end
end
-- end -- ExecLuaFile --
return lptpgui
- Note the redefine of print before your script runs in the exec_luafile code above. This allows your print commands to also be seen by your (batch file, perl,...) script running on the PC. The output can be fetched using the ptpcam --chdk="getm" command (see perl script below for an example). This print output stays in memory on your camera until you perform a getm so if you don't plan on reading the messages using getm you may want to add if cam_print==nil then cam_print=print end and use cam_print in your scripts instead to avoid a possible memory over-use issue.
- The following is the help given from the command line of ptpcam and ptpcam --chdk, followed by some examples showing some common tasks you will likely want to do.
C:\ptpcam_V2.0>ptpcam -h
USAGE: ptpcam [OPTION]
Options:
--bus=BUS-NUMBER USB bus number
--dev=DEV-NUMBER USB assigned device number
-r, --reset Reset the device
-l, --list-devices List all PTP devices
-i, --info Show device info
-o, --list-operations List supported operations
-p, --list-properties List all PTP device properties
(e.g. focus mode, focus distance, etc.)
-s, --show-property=NUMBER Display property details (or set its value,
if used in conjunction with --val)
--set-property=NUMBER Set property value (--val required)
--set=PROP-NAME Set property by name (abbreviations allowed)
--val=VALUE Property value (numeric for --set-property and
string or numeric for --set)
--show-all-properties Show all properties values
--show-unknown-properties Show unknown properties values
-L, --list-files List all files
-g, --get-file=HANDLE Get file by given handler
-G, --get-all-files Get all files
--overwrite Force file overwrite while savingto disk
-d, --delete-object=HANDLE Delete object (file) by given handle
-D, --delete-all-files Delete all files form camera
-c, --capture Initiate capture
--nikon-ic, --nic Initiate Nikon Direct Capture (no download!)
--nikon-dc, --ndc Initiate Nikon Direct Capture and download
--loop-capture=N Perform N times capture/get/delete
-f, --force Talk to non PTP devices
-v, --verbose Be verbose (print more debug)
-h, --help Print this help message
--chdk[=command] CHDK mode. Interactive shell unless optional
command is given. Run interactive shell and
press 'h' for a list of commands.
C:\ptpcam_V2.0>
C:\ptpcam_V2.0>ptpcam --chdk
<conn> h
q quit quit program
h help list commands
r reset reconnect to camera
version get CHDK PTP version (ptpcam and camera)
shutdown shutdown camera (soft)
reboot reboot camera
reboot <filename> reboot camera using specified firmware update
reboot-fi2 reboot camera using default firmware update
m memory <address> get byte at address
m memory <address>-<address> get bytes at given range
m memory <address> <num> get num bytes at given address
set <address> <long> set long value at address
c call <address> <arg1> ... call function at address with given arguments
upload <local> <remote> upload local file to camera
download <remote> <local> download file from camera
mode <val> set mode (0=playback,1=record)
lua execute lua code
luar execute "return " and retreive result
script-support show supported script interfaces
script-status show script execution and message status
getm get messages / return values from script
putm <message> send <message> to running script
<conn> #======================= EXAMPLES ===========================
<conn>
<conn> #----- generate the file list -------------------------------
<conn> #the following will call CHDK/LUALIB/lptpgui.lua and will
<conn> #create a file called A/ptpgui.txt on your SD card that contains
<conn> #a complete list of jpg files on your camera including path names
<conn> #and date and time of creation (change false to true if you have gps)
<conn> luar require("lptpgui").dcimdl(false)
script:1
1:ret:'420
A/ptpgui.txt
712'
<conn>
<conn> #----- download the file list, download a file --------------
<conn> #download the file list generated above
<conn> download A/ptpgui.txt fileList.txt
<conn> #now that you have complete path info you can easily download
<conn> #only the image you require
<conn> download A/DCIM/101___07/IMG_0419.JPG img.jpg
<conn>
<conn> #----- upload your CHDK LUA script --------------------------
<conn> #after you tweak your script, you can upload it to your camera
<conn> upload myRevisedScript.lua A/CHDK/SCRIPTS/myRevisedScript.lua
<conn>
<conn> #----- run your CHDK LUA script on your camera --------------
<conn> #put your camera into record mode
<conn> mode 1
<conn> #then run your scipt with params 0,1,2,3
<conn> lua require('lptpgui').exec_luafile([[A/CHDK/SCRIPTs/myRevisedScript.lua]],{0,1,2,3})
- The following (activestate 5.10) perl script will download the last N files from your camera.
#!perl
#---------------------------------------------------------------------------
# Fetches last N files (based on date/time of pic) from camera. (montana123)
#---------------------------------------------------------------------------
my $numFilesToGet=5;
#---------------------------------------------------------------------------
printf("\n Fetching file list. \n");
my @fileListResponse = `ptpcam --chdk="luar require('lptpgui').dcimdl(false)"`;
sleep 2;
my @downLoadResponse = `ptpcam --chdk="download A/ptpgui.txt fileList.txt"`;
sleep 2;
printf("\n Fetching last $numFilesToGet files.\n");
open FH, "<fileList.txt" or die $!; my @fileList=<FH>; close FH;
my $lastFile = $#fileList;
my $firstFile = $lastFile-($numFilesToGet-1);
for ($i=$firstFile;$i<=$lastFile;$i++) {
($path,$name,$datetime,$junk)=split(/\|/,$fileList[$i]);
#printf("$path-$name-$datetime-$junk\n");
$result=`ptpcam --chdk="download $path$name $name"`;
printf (" > $path - $name result= $result\n");
sleep 2;
}
printf("\n --\nAll done.\nHit ENTER to quit."); $response=<stdin>; exit (0);
- The following (activestate 5.10) perl script will (optionally) upload your CHDK LUA script to your camera and (optionally) run it.
#!perl
#---------------------------------------------------------------------------
# Optionally upload/run script to/on camera (montana123)
#---------------------------------------------------------------------------
my $myScript="myRevisedScript.lua";
my $upload=0;
my $run=1;
#---------------------------------------------------------------------------
if ($upload) {
printf("\n Uploading $myScript script to camera. \n");
my @upLoadResponse = `ptpcam --chdk="upload $myScript A/CHDK/SCRIPTS/$myScript"`;
}
if ($run) {
if ($upload) { sleep 3; }
$scriptMessage = `ptpcam --chdk="getm"`; #discard existing messages
sleep 1;
printf("\n Running $myScript script on camera.\n");
my $modeResponse = `ptpcam --chdk="mode 1"`;
sleep 3;
my $scriptResponse = `ptpcam --chdk="lua require('lptpgui').exec_luafile([[A/CHDK/SCRIPTS/$myScript]])"`;
printf(" > launch response= $scriptResponse\n");
my $wait=100;
while ($wait) {
sleep 3;
$scriptStatus = `ptpcam --chdk="script-status"`;
($junk,$run,$msg)=split(/\s+/,$scriptStatus);
printf(" > status: $run - $msg\n");
$wait--; if ($run =~ /no/) {$wait=0;}
}
if ($msg =~ /yes/) {
sleep 1;
$scriptMessage = `ptpcam --chdk="getm"`;
printf("\n Output From Script PRINT command(s):\n\n$scriptMessage\n --\n");
}
sleep 1;
my $modeResponse = `ptpcam --chdk="mode 0"`;
}
printf("\n All done.\n Hit ENTER to quit."); $response=<stdin>; exit(0);