def set_iso_number(iso_number): """TODO(jordanhuus): add doc string """ ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() ptpSession.SetExposureIndex(iso_number) except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport return iso_number
def set_f_number(f_number): """Changes the current aperture size when using manual mode. TODO(jordanhuus): confirm when this works and doesn't work. Args: param1 (float): TODO(jordanhuus): determine unit of measurement """ ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() ptpSession.SetFNumber(f_number) except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport
def get_exposure_time(): """TODO(jordanhuus): set doc string. """ ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD exposure_time = None try: # Open device session ptpSession.OpenSession() exposure_time = ptpSession.GetExposureTime() except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport return exposure_time
def set_exposure_time(exposure_time): """Changes the current exposure time when using manual mode. TODO(jordanhuus): confirm when this works and doesn't work. Args: param1: (int) in milliseconds on how long the exposure will be open. """ ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() ptpSession.SetExposureTime(exposure_time) except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport
def get_device_details(): """TODO(jordanhuus): add description """ ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD device_info = None try: # Open device session ptpSession.OpenSession() device_info = ptpSession.GetDeviceInfoDict() device_info["Model"] = ptpTransport.device_name except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport return device_info
def begin_timelapse(file, delay): """Simple function to initiate camera capture and store the result into the provided file object.n Note: Callers should expect the image to have saved correctly if an exception was not thrown. Args: param1: file object opened using 'wb' to result in <class '_io.BufferedWriter'> param2: delay between captures. In milliseconds. """ ptpTransport = PtpUsbTransport(PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints(PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() print(ptpSession.GetFormattedDeviceInfoString()) # Initiate timelapse capture events id = 0 while True: ptpSession.InitiateCapture(objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) # Check for new object added after capture 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 # Download newly added object if objectid != None: if file is None: file = open("capture_%i.jpg" % id, "wb") ptpSession.GetObject(objectid, file) file.close() id+=1 ptpSession.DeleteObject(objectid) # Delay between shots time.sleep(delay) except PtpException as e: print("PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId)) except Exception as e: print("An exception occurred: %s" % e) traceback.print_exc() # Close the session del ptpSession del ptpTransport
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_thread(capture_count): # PTP Protocol Prep ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD image_file_name = None camera = Camera.query.get(session_id) try: # Open device session ptpSession.OpenSession() for _ in range(capture_count): ptpSession.InitiateCapture( objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) # Check for new object added after capture 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 # Download newly added object image_file_name = \ "latest_%s.jpg" % datetime.now().strftime( "%Y_%m_%d_%H_%M_%S") save_path = ensure_path_available( os.path.expanduser("~") + "/Documents/optic-nerve/images/") + image_file_name if objectid is not None: with open(save_path, "wb") as file: ptpSession.GetObject(objectid, file) camera.image_file_name = image_file_name db.session.commit() except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport # Set camera state to complete camera.camera_state = Camera.STATE_COMPLETE db.session.commit()
#!/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
#!/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 # Read all the possible fstops fstops = ptpSession.GetDevicePropInfo( PtpValues.StandardProperties.F_NUMBER).Enumeration for fstop in fstops: ptpSession.SetFNumber(fstop) print "Capturing %i" % fstop ptpSession.InitiateCapture( objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) while True: evt = ptpSession.CheckForEvent(None) if evt.eventcode == PtpValues.StandardEvents.OBJECT_ADDED: break
#!/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 print "----------- DEVICE -----------" print "StandardVersion: %i" % deviceInfo.StandardVersion print "VendorExtensionID: %i (%s)" % (deviceInfo.VendorExtensionID, PtpValues.VendorNameById( deviceInfo.VendorExtensionID)) print "VendorExtensionVersion: %i" % deviceInfo.VendorExtensionVersion print "VendorExtensionDesc: %s" % deviceInfo.VendorExtensionDesc print "FunctionalMode: %i (%s)" % (deviceInfo.FunctionalMode, PtpValues.FunctionalModeNameById( deviceInfo.FunctionalMode)) print "OperationsSupported:\n\t%s" % "\n\t".join([ PtpValues.OperationNameById(op, deviceInfo.VendorExtensionID) for op in deviceInfo.OperationsSupported
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import traceback import time import ptp.PtpAbstractTransport import ptp.NikonSupport from ptp.PtpUsbTransport import PtpUsbTransport from ptp.PtpSession import PtpSession, PtpException from ptp import PtpValues ptpTransport = PtpUsbTransport(PtpUsbTransport.findptps()[0]) ptpSession = PtpSession(ptpTransport) # seen d109, d10b, d16a rw_vals = (0xd01f, 0xd045, 0xd109, 0xd10b, 0xd16a) ro_vals = (0xd0c3, 0xd0e1, 0xd0e2, 0xd102, 0xd120, 0xd121, 0xd122, 0xd124, 0xd125) vendorId = PtpValues.Vendors.STANDARD try: ptpSession.OpenSession() deviceInfo = ptpSession.GetDeviceInfo() vendorId = deviceInfo.VendorExtensionID # print "%i" % ptpSession.GetDevicePropValue(0xd126, False, "b") for id in rw_vals: print "RW: %04x: %02x" % ( id, ptpSession.GetDevicePropValue(id, False, "B"))
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
def capture_new_image(delete_from_device=False, download_image=True): """Initiate camera capture and store the result. Note: Callers should expect the image to have saved correctly if an exception was not thrown. Args: param1: file object opened using 'wb' to result in <class '_io.BufferedWriter'>. """ # PTP Protocol Prep ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD try: # Open device session ptpSession.OpenSession() ptpSession.InitiateCapture( objectFormatId=PtpValues.StandardObjectFormats.EXIF_JPEG) # Check for new object added after capture 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 # Download newly added object image_file_name = \ "latest_%s.jpg" % datetime.now().strftime("%Y_%m_%d_%H_%M_%S") if download_image: save_path = ensure_path_available( os.path.expanduser("~") + "/Documents/optic-nerve/images/") + image_file_name if objectid is not None: with open(save_path, "wb") as file: ptpSession.GetObject(objectid, file) ptpSession.DeleteObject(objectid) return image_file_name except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport
def get_f_number_options(): """TODO(jordanhuus): add doc string """ ptpTransport = PtpUsbTransport( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) bulk_in, bulk_out, interrupt_in = \ PtpUsbTransport.retrieve_device_endpoints( PtpUsbTransport.findptps(PtpUsbTransport.USB_CLASS_PTP)) ptpSession = PtpSession(ptpTransport) vendorId = PtpValues.Vendors.STANDARD minimum_f_stop = None maximum_f_stop = None f_stop_type_index = None try: # Open device session ptpSession.OpenSession() # Available f-stop values # Start with the most granular option (1/3rd stops) f_numbers_third_stops = [ 100, 110, 120, 140, 160, 180, 200, 220, 250, 280, 320, 350, 400, 450, 500, 560, 630, 710, 800, 900, 1000, 1100, 1300, 1400, 1600, 1800, 2000, 2200, 2500, 2900, 3200, 3600 ] f_numbers_half_stops = [ 100, 120, 140, 170, 200, 240, 280, 330, 400, 480, 560, 670, 800, 950, 1100, 1300, 1600, 1900, 2200, 2700, 3200 ] f_numbers_full_stops = [ 100, 140, 200, 280, 400, 560, 800, 1100, 1600, 2200, 3200 ] f_stops = [ f_numbers_third_stops, f_numbers_half_stops, f_numbers_full_stops ] # Attempt to find min/max f-stop values for i in range(len(f_stops)): if minimum_f_stop and maximum_f_stop: break # Find min for f_number in f_stops[i]: try: ptpSession.SetFNumber(f_number) if not minimum_f_stop: minimum_f_stop = f_number f_stop_type_index = i break except PtpException as ptpe: if ptpe.args[0] == 8220: continue # Find max for f_number in f_stops[i][::-1]: try: ptpSession.SetFNumber(f_number) if not maximum_f_stop: maximum_f_stop = f_number f_stop_type_index = i break except PtpException as ptpe: if ptpe.args[0] == 8220: continue except PtpException as e: raise PtpException( "PTP Exception: %s" % PtpValues.ResponseNameById(e.responsecode, vendorId), ptpSession, ptpTransport) except Exception as e: raise Exception(e) # Close the session del ptpSession del ptpTransport if f_stop_type_index == 0: f_stop_type = "third_stops" elif f_stop_type_index == 1: f_stop_type = "half_stops" elif f_stop_type_index == 2: f_stop_type = "full_stops" return (f_stop_type, f_stops[f_stop_type_index].index(minimum_f_stop), f_stops[f_stop_type_index].index(maximum_f_stop))