Wikia

CHDK Wiki

PTP Extension

Talk0
576pages on
this wiki
Revision as of 01:55, July 28, 2013 by ReyalP (Talk | contribs)

The Picture Transfer Protocol (PTP) facilitates remote operations on a camera connected computer. Typical operations are uploading/downloading photos and remote captures.

Most cameras support some basic operations over PTP, but often only in a limited fashion. For example, downloading of files can be limited to just JPEG files.

The PTP extension for CHDK discussed in this article does not alter the standard operations but adds new CHDK-specific operations instead. This is due to the complexity involved in altering or complete overwriting the standard operations. It also means that special tools are required to access this new functionality.

If you want simple control over the camera without USB a protocol connection (for example, using a micro-controller or RC equipment) see USB Remote.

The CHDK extension was first added to the CHDK trunk in changeset 957, and has been enabled for all cameras since changeset 1116

Related forum thread: [1]

Functionality

An overview of the functionality available through the PTP interface is described below. The specific functionality and usage depends on which client application you use. The canonical reference to the CHDK PTP protocol is core/ptp.h in the CHDK source tree.

File transfer

Files can be uploaded to and downloaded from any part of the file system.

Script execution

The PTP interface allows execution of lua scripts sent over PTP. In general, any scripting function available to normal CHDK scripts can be executed remotely. There are some additional functions specific to PTP, described in Lua/PTP Scripting.

Some of the features available using Lua scripts include

  • Switch to and from record mode
  • Switch recording modes, e.g. Av, Tv, Video
  • Adjust exposure parameters, focus, zoom etc.
  • Shoot pictures or video
  • Manipulate Canon menus and settings with simulated key presses
  • List, create and delete files and directories
  • Return values from script, send and receive arbitrary messages between the camera and PC
  • Shutdown or reboot the camera

Remote display

In CHDK 1.1 support has been added for streaming the contents of the camera LCD display over PTP. (revision 1921 and later)

Debugging features

  • Read and write camera memory
  • call arbitrary camera functions

LibUSB Driver for Windows

All the client applications below require libusb-win32 driver on Windows:

  • It is available for Windows 2000, Windows XP 32 Bit, Windows Vista 32/64 Bit, Windows 7 32/64 Bit.
  • Windows 8 users should read the following guide: How to install an unsigned driver on Windows 8
  • It should be installed as an INF Driver, NOT a Filter Driver.
  • It will most likely break your ability to download photos through Windows built-in Wizard, but you can still download files using the client applications below.

To install libusb-win32 on Windows:

  • Download the latest binary release from here
  • Extract archive
  • Connect camera via USB and turn on in Play mode
  • Let Windows install its built-in driver if not already installed
  • Run \bin\inf-wizard.exe from extracted archive
  • Select your camera from the list of attached USB devices
  • Hit Next and save the resulting INF file to a new folder
  • Hit Install Now...
  • It should appear in Device Manager as libusb-win32 devices > Canon Digital Camera
  • No PC reboot is needed

NOTES

  • The installed INF driver is specific to the selected model. If you use more than one camera model with CHDK PTP clients, you may need to install an INF driver for each one.
  • Depending on the camera, the INF driver installation may be specific to the particular physical USB port. In this case, plugging the camera into a different port will allow you to access the camera using normal windows software. This probably applies to older cameras (vxworks?)
  • You may be able to get the filter driver to work on some OSes (XP) but the INF driver is strongly recommended.

Client applications

As described above, a client program compatible that implements the CHDK protocol is required to access these features

ptpcam

If you are new to ptpcam check out ptpcamgui below. More advanced users may wish to use ptpcam.
Download:
1) download and install libusb (see install instructions above)
2) go here (German CHDK forum topic on the development of ptpcam with CHDK extensions)
3) download ptpcam_Vx.x.zip at the end of the first message.
Note that PTP is simply a protocol for communicating with your camera and is available on many cameras not just Canon. The version of ptpcam available in the forum link has CHDK extensions added to communicate to the CHDK code on your camera. This is a derivative of the opensource ptpcam available at http://libptp.sourceforge.net/ (this source code doesn't include CHDK extensions, also note: no compiled executable is available at the sourceforge link).
If you are comfortable writing batch scripts (or perl/python/... scripts) for your PC, ptpcam can be used to download jpg files and upload lua scripts and execute CHDK lua scripts on your camera all while your camera is carefully lined up on your subject and mounted to a tripod (when, for most Canon cameras, removing the memory card is not possible).
Keep in mind the fact that commands sent to your camera via ptpcam will take more time to complete than it takes for ptpcam to return control to your script. So make sure that after you issue a ptpcam --chdk="blah blah" command, that you have inserted a delay in your (batch file, perl, ...) script running on your PC to give the camera time to finish its business before you go poking the camera again.
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 <code>                         execute lua code
  luar <code>                  execute "return <code>" 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);
   
The source code as well as the original proposal consist of both a patch for the SVN trunk and a patch for the ptpcam program. The latter adds the option --chdk which starts an interactive prompt to communicate with the connected camera. A version of ptpcam is maintained in the chdkde source tree, with builds available from the chdkde forum (download ptpcam_Vx.x.zip). The ptpcam executable may be built for Windows or Linux.

ptpCamGui

ptpCamGui is a graphical front end for ptpcam. Builds are available from the chdkde forum It's also part of CHDK Shell.
A forum thread including descriptions can you find here.
The source code for the frontend and the DLL is available on the ptpCamGui assembla project.
ptpCamGui is only available for Windows.
English translation of instructions for using can be found here. Note that despite coming from a German site, both English and German languages are supported in the same .exe and are set automatically (based on your Windows installation language setting).
First time users note that the big red camera icon is used both to enter camera mode and to shoot (when radio buttons to right select "Photo").

chdkptp

An under development client. Considered pre-alpha because functionality and APIs are subject to change. However, the snapshot builds are usable for most common operations.
CHDK 1.1 remote display is supported.
>Source and builds can be found on the chdkptp assembla project. A thread in the chdk forum is available for discussion.
chdkptp provides a lua API to the CHDK protocol on the PC. A CLI and optional GUI based on IUP are implemented in Lua. This allows easy customization, and enables end users to quickly implement complex interactions with code on the camera.
chdkptp is compatible with windows and linux.

chdkcam (historical)

CHDKCAM is a Windows program that provides a graphical interface to the camera and also allows live streaming of the camera's screen. The CHDKCAM binaries should also be compatible with the adapted ptpcam from the original source.
CHDKCAM is only compatible with early development versions of the protocol, it is not compatible with the current CHDK or CHDKDE builds.

Usage notes

Windows

In order to get CHDKCAM or ptpcam to work properly you might need to disable the "Canon Camera Access Library" service (or, if that doesn't work, the "WIA" service).

Linux

Make sure you've got access to USB; in some cases this means switching to root (or changing permissions).

Development details

Also see the original documentation and the forum thread.

FAQ

  • Download/upload fails with code 0x2ff or the camera crashes when you try to transfer a file
Perhaps you didn't correctly specify the file on the camera. Always use the full path and start with a capitalised 'A'. For example: "download A/DCIM/100CANON/IMG_1234.jpg test.jpg"
note this mainly applies to ptpcam, chdkptp corrects the names for you.
  • The canon firmware does not recognize key presses (real or from script) when PTP is connected
There are two known workarounds:
  • Switch to record mode using switch_mode_usb script function. Keys will still be enabled if you switch back to play.
  • On many DryOS cameras, post_levent_to_ui(4484) will also unlock the keys.


Adding support for another camera/firmware version (historical)

note the following is for historical reference only. PTP is implemented in all complete CHDK ports now. As with CHDK itself it is usually a good idea to base your efforts on the sources of a similar camera. The essential steps to get any CHDK PTP support are as follows.

  • Find the add_ptp_handler function for your firmware.
  • Make sure init_chdk_ptp is started as a task.

This should enable you to connect with ptpcam --chdk and download/upload files, read/write memory and execute remote functions as well as Lua code.

Next you probably want to find a way to switch the camera to record mode (and back to playback mode). How to do this depends on the camera, so you probably need to check your reference source for this.

Finally you might want to add specific functions like those needed for CHDKCAM.

Functionality Comparison (historical)

The various variants (original source, CHDKCAM, proposal for addition to trunk) of the CHDK PTP extension support the following functionality.

current trunk original CHDKCAM proposal
file upload/download x x x x
memory read/write x x x x
remote function calls x x x x
mode switching L x x
Lua code execution x x x x
retrieval of props and params L x x
camera shutdown/reboot L x
directory reading L x
script output retrieval L1 x
video settings x
versioning x x

L=functionality available using Lua

1 script error messages are returned and scripts can send arbitrary data, but console output is not returned.

Note that the proposal has much less extensions as the idea is to keep CHDK PTP as small as possible and provide the rest of the functionality (e.g. mode switching, shutdown/reboot) via scripting.

Availability (historical versions)

The proposal version, with some minor modifications, has been included in CHDK trunk since changeset 957

At mweerden's github one can find the original sources as well as a follow-up attempt at a minimal version for inclusion in the SVN trunk. At ewavr's site you can find his CHDKCAM program and binaries for various firmwares.

Below is an overview of the currently supported firmwares. Here "cc" means there is a CHDKCAM version, "mg" it's supported in mweerden's code, "(b)" that there is a binary, "(s)" that it's in the source and "(p)" that there is a patch.

A480 1.00b cc (b)
A540 1.00b cc (p)
A570 1.00e mg (bp)
1.01a mg (bp)
A590 1.00e cc (b)
1.01b cc (b)
A610 1.00d cc (b)
1.00e cc (b)
1.00f cc (b)
A650 1.00d SVN (p)
A710 1.00a cc (b)
A720 1.00c cc (b)
G11 1.00j stubs
IXUS700IS/SD500 1.01a cc (b)
IXUS870IS/SD880 1.01a mg (s)
SX1 2.01a cc (b)
SX10 1.01a cc (b)
1.02b cc (b)
SX200IS 1.00c cc (b)

Around Wikia's network

Random Wiki