|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023 john park for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2024 Tim Cocks for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +""" simple point-and-shoot camera example, with overly selecting using select button. |
| 6 | +
|
| 7 | +Place all overlay files inside /sd/overlays/ directory. |
| 8 | +""" |
| 9 | +import os |
| 10 | +import time |
| 11 | +import traceback |
| 12 | +import adafruit_pycamera # pylint: disable=import-error |
| 13 | + |
| 14 | + |
| 15 | +pycam = adafruit_pycamera.PyCamera() |
| 16 | +pycam.mode = 0 # only mode 0 (JPEG) will work in this example |
| 17 | + |
| 18 | +# User settings - try changing these: |
| 19 | +pycam.resolution = 1 # 0-12 preset resolutions: |
| 20 | +# 0: 240x240, 1: 320x240, 2: 640x480 |
| 21 | + |
| 22 | +pycam.led_level = 1 # 0-4 preset brightness levels |
| 23 | +pycam.led_color = 0 # 0-7 preset colors: 0: white, 1: green, 2: yellow, 3: red, |
| 24 | +# 4: pink, 5: blue, 6: teal, 7: rainbow |
| 25 | +pycam.effect = 0 # 0-7 preset FX: 0: normal, 1: invert, 2: b&w, 3: red, |
| 26 | +# 4: green, 5: blue, 6: sepia, 7: solarize |
| 27 | + |
| 28 | +print("Overlay example camera ready.") |
| 29 | +pycam.tone(800, 0.1) |
| 30 | +pycam.tone(1200, 0.05) |
| 31 | + |
| 32 | +overlay_files = os.listdir("/sd/overlays/") |
| 33 | +cur_overlay_idx = 0 |
| 34 | + |
| 35 | +pycam.overlay = f"/sd/overlays/{overlay_files[cur_overlay_idx]}" |
| 36 | +pycam.overlay_transparency_color = 0xE007 |
| 37 | + |
| 38 | +overlay_files = os.listdir("/sd/overlays/") |
| 39 | +cur_overlay_idx = 0 |
| 40 | + |
| 41 | +while True: |
| 42 | + pycam.blit(pycam.continuous_capture()) |
| 43 | + pycam.keys_debounce() |
| 44 | + # print(dir(pycam.select)) |
| 45 | + if pycam.select.fell: |
| 46 | + cur_overlay_idx += 1 |
| 47 | + if cur_overlay_idx >= len(overlay_files): |
| 48 | + cur_overlay_idx = 0 |
| 49 | + print(f"changing overlay to {overlay_files[cur_overlay_idx]}") |
| 50 | + pycam.overlay = f"/sd/overlays/{overlay_files[cur_overlay_idx]}" |
| 51 | + |
| 52 | + if pycam.shutter.short_count: |
| 53 | + print("Shutter released") |
| 54 | + pycam.tone(1200, 0.05) |
| 55 | + pycam.tone(1600, 0.05) |
| 56 | + try: |
| 57 | + pycam.display_message("snap", color=0x00DD00) |
| 58 | + pycam.capture_jpeg() |
| 59 | + pycam.display_message("overlay", color=0x00DD00) |
| 60 | + pycam.blit_overlay_into_last_capture() |
| 61 | + pycam.live_preview_mode() |
| 62 | + except TypeError as exception: |
| 63 | + traceback.print_exception(exception) |
| 64 | + pycam.display_message("Failed", color=0xFF0000) |
| 65 | + time.sleep(0.5) |
| 66 | + pycam.live_preview_mode() |
| 67 | + except RuntimeError as exception: |
| 68 | + pycam.display_message("Error\nNo SD Card", color=0xFF0000) |
| 69 | + time.sleep(0.5) |
| 70 | + |
| 71 | + if pycam.card_detect.fell: |
| 72 | + print("SD card removed") |
| 73 | + pycam.unmount_sd_card() |
| 74 | + pycam.display.refresh() |
| 75 | + |
| 76 | + if pycam.card_detect.rose: |
| 77 | + print("SD card inserted") |
| 78 | + pycam.display_message("Mounting\nSD Card", color=0xFFFFFF) |
| 79 | + for _ in range(3): |
| 80 | + try: |
| 81 | + print("Mounting card") |
| 82 | + pycam.mount_sd_card() |
| 83 | + print("Success!") |
| 84 | + break |
| 85 | + except OSError as exception: |
| 86 | + print("Retrying!", exception) |
| 87 | + time.sleep(0.5) |
| 88 | + else: |
| 89 | + pycam.display_message("SD Card\nFailed!", color=0xFF0000) |
| 90 | + time.sleep(0.5) |
| 91 | + pycam.display.refresh() |
0 commit comments