Skip to content

Commit 008d553

Browse files
committed
Bring code up to standards
1 parent 60e15dd commit 008d553

File tree

2 files changed

+107
-62
lines changed

2 files changed

+107
-62
lines changed

adafruit_pycamera/__init__.py

+73-43
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,11 @@ class PyCameraBase: # pylint: disable=too-many-instance-attributes,too-many-pub
185185
60 * 10,
186186
60 * 15,
187187
60 * 30,
188-
60 * 60
188+
60 * 60,
189189
)
190190

191-
timelapse_submodes = (
192-
"HiPwr",
193-
"MedPwr",
194-
"LowPwr"
195-
)
196-
191+
timelapse_submodes = ("HiPwr", "MedPwr", "LowPwr")
192+
197193
modes = ("JPEG", "GIF", "GBOY", "STOP", "LAPS")
198194

199195
_INIT_SEQUENCE = (
@@ -213,6 +209,11 @@ def __init__(self) -> None: # pylint: disable=too-many-statements
213209
self._timestamp = time.monotonic()
214210
self._bigbuf = None
215211
self._botbar = None
212+
self._timelapsebar = None
213+
self.timelapse_rate_label = None
214+
self._timelapsestatus = None
215+
self.timelapsestatus_label = None
216+
self.timelapse_submode_label = None
216217
self._camera_device = None
217218
self._display_bus = None
218219
self._effect_label = None
@@ -295,19 +296,19 @@ def make_camera_ui(self):
295296
self._botbar.append(self._mode_label)
296297

297298
self._timelapsebar = displayio.Group(x=0, y=180)
298-
self._timelapse_submode_label = label.Label(
299-
terminalio.FONT, text="SubM", color=0xFFFFFF,x=160, y=10, scale=2
299+
self.timelapse_submode_label = label.Label(
300+
terminalio.FONT, text="SubM", color=0xFFFFFF, x=160, y=10, scale=2
300301
)
301-
self._timelapse_rate_label = label.Label(
302-
terminalio.FONT, text="Time", color=0xFFFFFF,x=90, y=10, scale=2
302+
self.timelapse_rate_label = label.Label(
303+
terminalio.FONT, text="Time", color=0xFFFFFF, x=90, y=10, scale=2
303304
)
304-
self._timelapsestatus_label = label.Label(
305+
self.timelapsestatus_label = label.Label(
305306
terminalio.FONT, text="Status", color=0xFFFFFF, x=0, y=10, scale=2
306307
)
307-
self._timelapsebar.append(self._timelapse_rate_label)
308-
self._timelapsebar.append(self._timelapsestatus_label)
309-
self._timelapsebar.append(self._timelapse_submode_label)
310-
308+
self._timelapsebar.append(self.timelapse_rate_label)
309+
self._timelapsebar.append(self.timelapsestatus_label)
310+
self._timelapsebar.append(self.timelapse_submode_label)
311+
311312
self.splash.append(self._topbar)
312313
self.splash.append(self._botbar)
313314
self.splash.append(self._timelapsebar)
@@ -504,9 +505,9 @@ def select_setting(self, setting_name):
504505
self._res_label.text = self.resolutions[self._resolution]
505506
self._mode_label.color = 0xFFFFFF
506507
self._mode_label.background_color = 0x0
507-
self._timelapse_rate_label.color = 0xFFFFFF
508-
self._timelapse_rate_label.background_color = None
509-
508+
self.timelapse_rate_label.color = 0xFFFFFF
509+
self.timelapse_rate_label.background_color = None
510+
510511
if setting_name == "effect":
511512
self._effect_label.color = 0x0
512513
self._effect_label.background_color = 0xFFFFFF
@@ -529,8 +530,8 @@ def select_setting(self, setting_name):
529530
self._res_label.color = 0x0
530531
self._res_label.background_color = 0xFFFFFF
531532
elif setting_name == "timelapse_rate":
532-
self._timelapse_rate_label.color = 0x0
533-
self._timelapse_rate_label.background_color = 0xFFFFFF
533+
self.timelapse_rate_label.color = 0x0
534+
self.timelapse_rate_label.background_color = 0xFFFFFF
534535
self.display.refresh()
535536

536537
@property
@@ -591,7 +592,6 @@ def resolution(self, res):
591592
self._res_label.text = self.resolutions[res]
592593
self.display.refresh()
593594

594-
595595
@property
596596
def timelapse_rate(self):
597597
"""Get or set the amount of time between timelapse shots"""
@@ -602,23 +602,28 @@ def timelapse_rate(self, setting):
602602
setting = (setting + len(self.timelapse_rates)) % len(self.timelapse_rates)
603603
self._timelapse_rate = setting
604604
if self.timelapse_rates[setting] < 60:
605-
self._timelapse_rate_label.text = "%d S" % self.timelapse_rates[setting]
605+
self.timelapse_rate_label.text = "%d S" % self.timelapse_rates[setting]
606606
else:
607-
self._timelapse_rate_label.text = "%d M" % (self.timelapse_rates[setting] / 60)
607+
self.timelapse_rate_label.text = "%d M" % (
608+
self.timelapse_rates[setting] / 60
609+
)
608610
microcontroller.nvm[_NVM_TIMELAPSE_RATE] = setting
609611
self.display.refresh()
610612

611-
612613
@property
613614
def timelapse_submode(self):
614615
"""Get or set the power mode for timelapsing"""
615616
return self._timelapse_submode
616617

617618
@timelapse_submode.setter
618619
def timelapse_submode(self, setting):
619-
setting = (setting + len(self.timelapse_submodes)) % len(self.timelapse_submodes)
620+
setting = (setting + len(self.timelapse_submodes)) % len(
621+
self.timelapse_submodes
622+
)
620623
self._timelapse_submode = setting
621-
self._timelapse_submode_label.text = self.timelapse_submodes[self._timelapse_submode]
624+
self.timelapse_submode_label.text = self.timelapse_submodes[
625+
self._timelapse_submode
626+
]
622627
microcontroller.nvm[_NVM_TIMELAPSE_SUBMODE] = setting
623628

624629
def init_display(self):
@@ -883,54 +888,79 @@ def led_color(self, new_color):
883888
self.pixels[:] = colors
884889

885890
def get_camera_autosettings(self):
886-
exposure = (self.read_camera_register(0x3500) << 12) + \
887-
(self.read_camera_register(0x3501) << 4) + \
888-
(self.read_camera_register(0x3502) >> 4);
889-
wb = [self.read_camera_register(x) for x in \
890-
(0x3400, 0x3401, 0x3402, 0x3403, 0x3404, 0x3405)]
891-
891+
"""Collect all the settings related to exposure and white balance"""
892+
exposure = (
893+
(self.read_camera_register(0x3500) << 12)
894+
+ (self.read_camera_register(0x3501) << 4)
895+
+ (self.read_camera_register(0x3502) >> 4)
896+
)
897+
white_balance = [
898+
self.read_camera_register(x)
899+
for x in (0x3400, 0x3401, 0x3402, 0x3403, 0x3404, 0x3405)
900+
]
901+
892902
settings = {
893-
'gain': self.read_camera_register(0x350b),
894-
'exposure': exposure,
895-
'wb': wb
896-
}
903+
"gain": self.read_camera_register(0x350B),
904+
"exposure": exposure,
905+
"wb": white_balance,
906+
}
897907
return settings
898908

899909
def set_camera_wb(self, wb_register_values=None):
910+
"""Set the camera white balance.
911+
912+
The argument of `None` selects auto white balance, while
913+
a list of 6 numbers sets a specific white balance.
914+
915+
The numbers can come from the datasheet or from
916+
``get_camera_autosettings()["wb"]``."""
900917
if wb_register_values is None:
901918
# just set to auto balance
902919
self.camera.whitebal = True
903920
return
904-
921+
905922
if len(wb_register_values) != 6:
906923
raise RuntimeError("Please pass in 0x3400~0x3405 inclusive!")
907924

908925
self.write_camera_register(0x3212, 0x03)
909926
self.write_camera_register(0x3406, 0x01)
910927
for i, reg_val in enumerate(wb_register_values):
911-
self.write_camera_register(0x3400+i, reg_val)
928+
self.write_camera_register(0x3400 + i, reg_val)
912929
self.write_camera_register(0x3212, 0x13)
913-
self.write_camera_register(0x3212, 0xa3)
930+
self.write_camera_register(0x3212, 0xA3)
914931

915932
def set_camera_exposure(self, new_exposure=None):
933+
"""Set the camera's exposure values
934+
935+
The argument of `None` selects auto exposure.
936+
937+
Otherwise, the new exposure data should come from
938+
``get_camera_autosettings()["exposure"]."""
916939
if new_exposure is None:
917940
# just set auto expose
918941
self.camera.exposure_ctrl = True
919942
return
920-
exposure_ctrl = False
943+
self.camera.exposure_ctrl = False
921944

922945
self.write_camera_register(0x3500, (new_exposure >> 12) & 0xFF)
923946
self.write_camera_register(0x3501, (new_exposure >> 4) & 0xFF)
924947
self.write_camera_register(0x3502, (new_exposure << 4) & 0xFF)
925-
948+
926949
def set_camera_gain(self, new_gain=None):
950+
"""Set the camera's exposure values
951+
952+
The argument of `None` selects auto gain control.
953+
954+
Otherwise, the new exposure data should come from
955+
``get_camera_autosettings()["gain"]."""
927956
if new_gain is None:
928957
# just set auto expose
929958
self.camera.gain_ctrl = True
930959
return
931960

932961
self.camera.gain_ctrl = False
933-
self.write_camera_register(0x350b, new_gain)
962+
self.write_camera_register(0x350B, new_gain)
963+
934964

935965
class PyCamera(PyCameraBase):
936966
"""Wrapper class for the PyCamera hardware"""

examples/camera/code.py

+34-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# SPDX-License-Identifier: Unlicense
44

55
import time
6-
import math
76
import bitmaptools
87
import displayio
98
import gifio
@@ -14,7 +13,15 @@
1413
pycam = adafruit_pycamera.PyCamera()
1514
# pycam.live_preview_mode()
1615

17-
settings = (None, "resolution", "effect", "mode", "led_level", "led_color", "timelapse_rate")
16+
settings = (
17+
None,
18+
"resolution",
19+
"effect",
20+
"mode",
21+
"led_level",
22+
"led_color",
23+
"timelapse_rate",
24+
)
1825
curr_setting = 0
1926

2027
print("Starting!")
@@ -39,17 +46,23 @@
3946
pycam.blit(last_frame)
4047
elif pycam.mode_text == "LAPS":
4148
if timelapse_remaining is None:
42-
pycam._timelapsestatus_label.text = "STOP"
49+
pycam.timelapsestatus_label.text = "STOP"
4350
else:
4451
timelapse_remaining = timelapse_timestamp - time.time()
45-
pycam._timelapsestatus_label.text = f"{timelapse_remaining}s / "
46-
pycam._timelapse_rate_label.text = pycam._timelapse_rate_label.text
47-
pycam._timelapse_submode_label.text = pycam._timelapse_submode_label.text
52+
pycam.timelapsestatus_label.text = f"{timelapse_remaining}s / "
53+
# Manually updating the label text a second time ensures that the label
54+
# is re-painted over the blitted preview.
55+
pycam.timelapse_rate_label.text = pycam.timelapse_rate_label.text
56+
pycam.timelapse_submode_label.text = pycam.timelapse_submode_label.text
4857

4958
# only in high power mode do we continuously preview
50-
if (timelapse_remaining is None) or (pycam._timelapse_submode_label.text == "HiPwr"):
59+
if (timelapse_remaining is None) or (
60+
pycam.timelapse_submode_label.text == "HiPwr"
61+
):
5162
pycam.blit(pycam.continuous_capture())
52-
if pycam._timelapse_submode_label.text == "LowPwr" and (timelapse_remaining is not None):
63+
if pycam.timelapse_submode_label.text == "LowPwr" and (
64+
timelapse_remaining is not None
65+
):
5366
pycam.display.brightness = 0.05
5467
else:
5568
pycam.display.brightness = 1
@@ -58,7 +71,7 @@
5871
if timelapse_remaining is not None and timelapse_remaining <= 0:
5972
# no matter what, show what was just on the camera
6073
pycam.blit(pycam.continuous_capture())
61-
#pycam.tone(200, 0.1) # uncomment to add a beep when a photo is taken
74+
# pycam.tone(200, 0.1) # uncomment to add a beep when a photo is taken
6275
try:
6376
pycam.display_message("Snap!", color=0x0000FF)
6477
pycam.capture_jpeg()
@@ -71,7 +84,9 @@
7184
pycam.live_preview_mode()
7285
pycam.display.refresh()
7386
pycam.blit(pycam.continuous_capture())
74-
timelapse_timestamp = time.time() + pycam.timelapse_rates[pycam.timelapse_rate] + 1
87+
timelapse_timestamp = (
88+
time.time() + pycam.timelapse_rates[pycam.timelapse_rate] + 1
89+
)
7590
else:
7691
pycam.blit(pycam.continuous_capture())
7792
# print("\t\t", capture_time, blit_time)
@@ -224,21 +239,21 @@
224239
if pycam.ok.fell:
225240
print("OK")
226241
if pycam.mode_text == "LAPS":
227-
if timelapse_remaining is None: # stopped
242+
if timelapse_remaining is None: # stopped
228243
print("Starting timelapse")
229244
timelapse_remaining = pycam.timelapse_rates[pycam.timelapse_rate]
230245
timelapse_timestamp = time.time() + timelapse_remaining + 1
231246
# dont let the camera take over auto-settings
232247
saved_settings = pycam.get_camera_autosettings()
233-
#print(f"Current exposure {saved_settings['exposure']}, gain {saved_settings['gain']}, wb {saved_settings['wb']}")
234-
pycam.set_camera_exposure(saved_settings['exposure'])
235-
pycam.set_camera_gain(saved_settings['gain'])
236-
pycam.set_camera_wb(saved_settings['wb'])
237-
else: # is running, turn off
248+
# print(f"Current exposure {saved_settings=}")
249+
pycam.set_camera_exposure(saved_settings["exposure"])
250+
pycam.set_camera_gain(saved_settings["gain"])
251+
pycam.set_camera_wb(saved_settings["wb"])
252+
else: # is running, turn off
238253
print("Stopping timelapse")
239254

240255
timelapse_remaining = None
241256
pycam.camera.exposure_ctrl = True
242-
pycam.set_camera_gain(None) # go back to autogain
243-
pycam.set_camera_wb(None) # go back to autobalance
244-
pycam.set_camera_exposure(None) # go back to auto shutter
257+
pycam.set_camera_gain(None) # go back to autogain
258+
pycam.set_camera_wb(None) # go back to autobalance
259+
pycam.set_camera_exposure(None) # go back to auto shutter

0 commit comments

Comments
 (0)