Skip to content

Commit

Permalink
allow saving and setting exposure/shutter, gain and balance so that i…
Browse files Browse the repository at this point in the history
…t can be frozen during timelapse
  • Loading branch information
ladyada committed Jan 28, 2024
1 parent fa81fa0 commit 60e15dd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
49 changes: 49 additions & 0 deletions adafruit_pycamera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,55 @@ def led_color(self, new_color):
else:
self.pixels[:] = colors

def get_camera_autosettings(self):

Check failure on line 885 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring

Check failure on line 885 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring
exposure = (self.read_camera_register(0x3500) << 12) + \
(self.read_camera_register(0x3501) << 4) + \
(self.read_camera_register(0x3502) >> 4);
wb = [self.read_camera_register(x) for x in \
(0x3400, 0x3401, 0x3402, 0x3403, 0x3404, 0x3405)]

Check failure on line 891 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Variable name "wb" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern

Check failure on line 891 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Variable name "wb" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
settings = {
'gain': self.read_camera_register(0x350b),
'exposure': exposure,
'wb': wb
}
return settings

def set_camera_wb(self, wb_register_values=None):
if wb_register_values is None:
# just set to auto balance
self.camera.whitebal = True
return

Check failure on line 903 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring

Check failure on line 903 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring

if len(wb_register_values) != 6:
raise RuntimeError("Please pass in 0x3400~0x3405 inclusive!")

self.write_camera_register(0x3212, 0x03)
self.write_camera_register(0x3406, 0x01)
for i, reg_val in enumerate(wb_register_values):
self.write_camera_register(0x3400+i, reg_val)
self.write_camera_register(0x3212, 0x13)
self.write_camera_register(0x3212, 0xa3)

def set_camera_exposure(self, new_exposure=None):
if new_exposure is None:
# just set auto expose
self.camera.exposure_ctrl = True
return

Check failure on line 919 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring

Check failure on line 919 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring
exposure_ctrl = False

self.write_camera_register(0x3500, (new_exposure >> 12) & 0xFF)
self.write_camera_register(0x3501, (new_exposure >> 4) & 0xFF)
self.write_camera_register(0x3502, (new_exposure << 4) & 0xFF)

Check failure on line 924 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Unused variable 'exposure_ctrl'

Check failure on line 924 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Unused variable 'exposure_ctrl'

def set_camera_gain(self, new_gain=None):
if new_gain is None:
# just set auto expose
self.camera.gain_ctrl = True
return

Check failure on line 930 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring

Check failure on line 930 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Missing function or method docstring

self.camera.gain_ctrl = False
self.write_camera_register(0x350b, new_gain)

class PyCamera(PyCameraBase):
"""Wrapper class for the PyCamera hardware"""
Expand Down
13 changes: 13 additions & 0 deletions examples/camera/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,20 @@
print("OK")
if pycam.mode_text == "LAPS":
if timelapse_remaining is None: # stopped
print("Starting timelapse")
timelapse_remaining = pycam.timelapse_rates[pycam.timelapse_rate]
timelapse_timestamp = time.time() + timelapse_remaining + 1
# dont let the camera take over auto-settings
saved_settings = pycam.get_camera_autosettings()
#print(f"Current exposure {saved_settings['exposure']}, gain {saved_settings['gain']}, wb {saved_settings['wb']}")
pycam.set_camera_exposure(saved_settings['exposure'])
pycam.set_camera_gain(saved_settings['gain'])
pycam.set_camera_wb(saved_settings['wb'])
else: # is running, turn off
print("Stopping timelapse")

timelapse_remaining = None
pycam.camera.exposure_ctrl = True
pycam.set_camera_gain(None) # go back to autogain
pycam.set_camera_wb(None) # go back to autobalance
pycam.set_camera_exposure(None) # go back to auto shutter

0 comments on commit 60e15dd

Please sign in to comment.