Skip to content

Commit

Permalink
add low power modes: med power turns off the camera preview, low powe…
Browse files Browse the repository at this point in the history
…r also dims the screen
  • Loading branch information
ladyada committed Jan 27, 2024
1 parent ad34c6c commit fa81fa0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
26 changes: 24 additions & 2 deletions adafruit_pycamera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
_NVM_EFFECT = const(2)
_NVM_MODE = const(3)
_NVM_TIMELAPSE_RATE = const(4)
_NVM_TIMELAPSE_SUBMODE = const(5)


class PyCameraBase: # pylint: disable=too-many-instance-attributes,too-many-public-methods
Expand Down Expand Up @@ -186,6 +187,12 @@ class PyCameraBase: # pylint: disable=too-many-instance-attributes,too-many-pub
60 * 30,
60 * 60
)

timelapse_submodes = (
"HiPwr",
"MedPwr",
"LowPwr"
)

modes = ("JPEG", "GIF", "GBOY", "STOP", "LAPS")

Expand Down Expand Up @@ -288,16 +295,18 @@ def make_camera_ui(self):
self._botbar.append(self._mode_label)

self._timelapsebar = displayio.Group(x=0, y=180)

Check failure on line 297 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Attribute '_timelapse_rate_label' defined outside __init__

Check failure on line 297 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Attribute '_timelapse_rate_label' defined outside __init__
self._timelapse_submode_label = label.Label(
terminalio.FONT, text="SubM", color=0xFFFFFF,x=160, y=10, scale=2
)

Check failure on line 300 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Attribute '_timelapsestatus_label' defined outside __init__

Check failure on line 300 in adafruit_pycamera/__init__.py

View workflow job for this annotation

GitHub Actions / test

Attribute '_timelapsestatus_label' defined outside __init__
self._timelapse_rate_label = label.Label(
terminalio.FONT, text="Time", color=0xFFFFFF,x=90, y=10, scale=2
)
self._timelapse_rate_label.background_color = None
self._timelapsestatus_label = label.Label(
terminalio.FONT, text="Status", color=0xFFFFFF, x=0, y=10, scale=2
)
self._timelapsestatus_label.background_color = None
self._timelapsebar.append(self._timelapse_rate_label)
self._timelapsebar.append(self._timelapsestatus_label)
self._timelapsebar.append(self._timelapse_submode_label)

self.splash.append(self._topbar)
self.splash.append(self._botbar)
Expand Down Expand Up @@ -371,6 +380,7 @@ def init_camera(self, init_autofocus=True) -> None:
self.resolution = microcontroller.nvm[_NVM_RESOLUTION]
self.mode = microcontroller.nvm[_NVM_MODE]
self.timelapse_rate = microcontroller.nvm[_NVM_TIMELAPSE_RATE]
self.timelapse_submode = microcontroller.nvm[_NVM_TIMELAPSE_SUBMODE]

if init_autofocus:
self.autofocus_init()
Expand Down Expand Up @@ -599,6 +609,18 @@ def timelapse_rate(self, setting):
self.display.refresh()


@property
def timelapse_submode(self):
"""Get or set the power mode for timelapsing"""
return self._timelapse_submode

@timelapse_submode.setter
def timelapse_submode(self, setting):
setting = (setting + len(self.timelapse_submodes)) % len(self.timelapse_submodes)
self._timelapse_submode = setting
self._timelapse_submode_label.text = self.timelapse_submodes[self._timelapse_submode]
microcontroller.nvm[_NVM_TIMELAPSE_SUBMODE] = setting

def init_display(self):
"""Initialize the TFT display"""
# construct displayio by hand
Expand Down
19 changes: 17 additions & 2 deletions examples/camera/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,26 @@
)
pycam.blit(last_frame)
elif pycam.mode_text == "LAPS":
pycam.blit(pycam.continuous_capture())
pycam._timelapse_rate_label.text = pycam._timelapse_rate_label.text
if timelapse_remaining is None:
pycam._timelapsestatus_label.text = "STOP"
else:
timelapse_remaining = timelapse_timestamp - time.time()
pycam._timelapsestatus_label.text = f"{timelapse_remaining}s / "
pycam._timelapse_rate_label.text = pycam._timelapse_rate_label.text
pycam._timelapse_submode_label.text = pycam._timelapse_submode_label.text

# only in high power mode do we continuously preview
if (timelapse_remaining is None) or (pycam._timelapse_submode_label.text == "HiPwr"):

Check failure on line 50 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

Access to a protected member _timelapsestatus_label of a client class

Check failure on line 50 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

Access to a protected member _timelapsestatus_label of a client class
pycam.blit(pycam.continuous_capture())
if pycam._timelapse_submode_label.text == "LowPwr" and (timelapse_remaining is not None):
pycam.display.brightness = 0.05
else:
pycam.display.brightness = 1
pycam.display.refresh()

if timelapse_remaining is not None and timelapse_remaining <= 0:
# no matter what, show what was just on the camera
pycam.blit(pycam.continuous_capture())
#pycam.tone(200, 0.1) # uncomment to add a beep when a photo is taken
try:
pycam.display_message("Snap!", color=0x0000FF)
Expand All @@ -59,6 +69,8 @@
pycam.display_message("Error\nNo SD Card", color=0xFF0000)
time.sleep(0.5)
pycam.live_preview_mode()
pycam.display.refresh()
pycam.blit(pycam.continuous_capture())
timelapse_timestamp = time.time() + pycam.timelapse_rates[pycam.timelapse_rate] + 1
else:
pycam.blit(pycam.continuous_capture())
Expand Down Expand Up @@ -206,6 +218,9 @@
# pycam.set_resolution(pycam.resolutions[new_res])
if pycam.select.fell:
print("SEL")
if pycam.mode_text == "LAPS":
pycam.timelapse_submode += 1
pycam.display.refresh()
if pycam.ok.fell:
print("OK")
if pycam.mode_text == "LAPS":
Expand Down

0 comments on commit fa81fa0

Please sign in to comment.