|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 Tim Cocks for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" simple point-and-shoot camera example. With NTP and internal RTC to |
| 5 | + add timestamp to photo filenames. Must install adafruit_ntp library! |
| 6 | + Example code assumes WIFI credentials are properly setup and web workflow |
| 7 | + enabled in settings.toml. If not, you'll need to add code to manually connect |
| 8 | + to your network.""" |
| 9 | + |
| 10 | +import time |
| 11 | +import wifi |
| 12 | +import socketpool |
| 13 | +import rtc |
| 14 | +import adafruit_ntp |
| 15 | +import adafruit_pycamera # pylint: disable=import-error |
| 16 | + |
| 17 | +pool = socketpool.SocketPool(wifi.radio) |
| 18 | +ntp = adafruit_ntp.NTP(pool, tz_offset=0) |
| 19 | +rtc.RTC().datetime = ntp.datetime |
| 20 | + |
| 21 | +pycam = adafruit_pycamera.PyCamera() |
| 22 | +pycam.mode = 0 # only mode 0 (JPEG) will work in this example |
| 23 | + |
| 24 | +# User settings - try changing these: |
| 25 | +pycam.resolution = 2 # 0-12 preset resolutions: |
| 26 | +# 0: 240x240, 1: 320x240, 2: 640x480, 3: 800x600, 4: 1024x768, |
| 27 | +# 5: 1280x720, 6: 1280x1024, 7: 1600x1200, 8: 1920x1080, 9: 2048x1536, |
| 28 | +# 10: 2560x1440, 11: 2560x1600, 12: 2560x1920 |
| 29 | +pycam.led_level = 1 # 0-4 preset brightness levels |
| 30 | +pycam.led_color = 0 # 0-7 preset colors: 0: white, 1: green, 2: yellow, 3: red, |
| 31 | +# 4: pink, 5: blue, 6: teal, 7: rainbow |
| 32 | +pycam.effect = 0 # 0-7 preset FX: 0: normal, 1: invert, 2: b&w, 3: red, |
| 33 | +# 4: green, 5: blue, 6: sepia, 7: solarize |
| 34 | + |
| 35 | +print("Simple camera ready.") |
| 36 | +pycam.tone(800, 0.1) |
| 37 | +pycam.tone(1200, 0.05) |
| 38 | + |
| 39 | +while True: |
| 40 | + pycam.blit(pycam.continuous_capture()) |
| 41 | + pycam.keys_debounce() |
| 42 | + |
| 43 | + if pycam.shutter.short_count: |
| 44 | + print("Shutter released") |
| 45 | + pycam.tone(1200, 0.05) |
| 46 | + pycam.tone(1600, 0.05) |
| 47 | + try: |
| 48 | + pycam.display_message("snap", color=0x00DD00) |
| 49 | + timestamp = "img_{}-{}-{}_{:02}-{:02}-{:02}_".format( |
| 50 | + time.localtime().tm_year, |
| 51 | + time.localtime().tm_mon, |
| 52 | + time.localtime().tm_mday, |
| 53 | + time.localtime().tm_hour, |
| 54 | + time.localtime().tm_min, |
| 55 | + time.localtime().tm_sec, |
| 56 | + ) |
| 57 | + pycam.capture_jpeg(filename_prefix=timestamp) |
| 58 | + pycam.live_preview_mode() |
| 59 | + except TypeError as exception: |
| 60 | + pycam.display_message("Failed", color=0xFF0000) |
| 61 | + time.sleep(0.5) |
| 62 | + pycam.live_preview_mode() |
| 63 | + except RuntimeError as exception: |
| 64 | + pycam.display_message("Error\nNo SD Card", color=0xFF0000) |
| 65 | + time.sleep(0.5) |
| 66 | + |
| 67 | + if pycam.card_detect.fell: |
| 68 | + print("SD card removed") |
| 69 | + pycam.unmount_sd_card() |
| 70 | + pycam.display.refresh() |
| 71 | + |
| 72 | + if pycam.card_detect.rose: |
| 73 | + print("SD card inserted") |
| 74 | + pycam.display_message("Mounting\nSD Card", color=0xFFFFFF) |
| 75 | + for _ in range(3): |
| 76 | + try: |
| 77 | + print("Mounting card") |
| 78 | + pycam.mount_sd_card() |
| 79 | + print("Success!") |
| 80 | + break |
| 81 | + except OSError as exception: |
| 82 | + print("Retrying!", exception) |
| 83 | + time.sleep(0.5) |
| 84 | + else: |
| 85 | + pycam.display_message("SD Card\nFailed!", color=0xFF0000) |
| 86 | + time.sleep(0.5) |
| 87 | + pycam.display.refresh() |
0 commit comments