예제 #1
0
파일: Capture.py 프로젝트: capaulson/pyptp
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import traceback
from ptp.PtpUsbTransport import PtpUsbTransport
from ptp.PtpSession import PtpSession, PtpException
from ptp import PtpValues

ptpTransport = PtpUsbTransport(PtpUsbTransport.findptps()[0])
ptpSession = PtpSession(ptpTransport)

vendorId = PtpValues.Vendors.STANDARD
try:
    ptpSession.OpenSession()
    deviceInfo = ptpSession.GetDeviceInfo()
    vendorId = deviceInfo.VendorExtensionID

    id = 0
    while True:
        ptpSession.InitiateCapture(
            objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG)

        objectid = None
        while True:
            evt = ptpSession.CheckForEvent(None)
            if evt == None:
                raise Exception("Capture did not complete")
            if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED:
                objectid = evt.params[0]
                break
예제 #2
0
class CHDKPtpCapture:
    def __init__(self):
        self.autofocuslocked = False

    def connect(self):
        """Connects to the first available ptp device, ptpSession is used to access most ptp commands"""
        ptps = PtpUsbTransport.findptps()
        print ptps
        self.ptpTransport = PtpUsbTransport(ptps[0])
        self.ptpSession = PtpSession(self.ptpTransport)

        self.vendorId = PtpValues.Vendors.STANDARD
        self.ptpSession.OpenSession()

        self.deviceInfo = self.ptpSession.GetDeviceInfo()
        print "ser: " + self.deviceInfo.SerialNumber
        self.vendorId = self.deviceInfo.VendorExtensionID
        print "model: " + self.deviceInfo.Model

        return True

    def capture(self):
        """Captures an image and returns it's objectid"""
        self.activateShootingMode()
        lua_script = "shoot()"
        logging.debug("sending script")
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)
        logging.debug("script finished")
        # below couple of lines in while loop taken from Capture.py of pyptp
        objectid = None
        while True:
            evt = self.ptpSession.CheckForEvent(None)
            if evt == None:
                raise Exception("Capture did not complete")
            if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED:
                objectid = evt.params[0]
                break
        return objectid

    def activateShootingMode(self):
        """Activates shooting mode on the camera, allowing the camera to shoot while under usb control"""
        lua_script = """
        switch_mode_usb(1)
        rec,vid,mode=get_mode()
        while rec == false do
            rec,vid,mode=get_mode()
        end
        """
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)

    def disableFlash(self):
        self.activateShootingMode()
        lua_script = "set_prop(143,2) -- turns flash off on SX200IS"
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)

    def enableAutoFlash(self):
        self.activateShootingMode()
        lua_script = "set_prop(143,1) -- turns flash to auto on SX200IS"
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)

    def lockAutofocus(self):
        """Prevents the camera from autofocusing"""
        self.activateShootingMode()
        lua_script = "set_aflock(1)"
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)
        self.autofocuslocked = True

    def unlockAutofocus(self):
        """Allows the camera to autofocus"""
        self.activateShootingMode()
        lua_script = "set_aflock(0)"
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)
        self.autofocuslocked = False

    def autofocus(self):
        """Attempts to autofocus the camera"""
        self.activateShootingMode()
        lua_script = "press('shoot_half')"
        executeScript(self.ptpSession,
                      lua_script,
                      CHDKPtpValues.ScriptingLanguage.LUA,
                      wait=True)
        time.sleep(
            2
        )  # we must wait for a short time for the autofocus to complete, otherwise if we lockAutofocus right afterwards we can cause the program to lock up

    def downloadAndSaveObject(self, objectid, savepath):
        file = open(savepath, "w")
        self.ptpSession.GetObject(objectid, file)
        file.close()

    def deleteObject(self, objectid):
        self.ptpSession.DeleteObject(objectid)

    def getAutofocusLocked(self):
        return self.autofocuslocked