ov5640_pico_simpletest.py
1 # SPDX-FileCopyrightText: Copyright (c) 2023 Lady Ada for Adafruit Industries 2 # 3 # SPDX-License-Identifier: Unlicense 4 5 """Capture an image from the camera and display it as ASCII art. 6 7 This demo is designed to run on the Raspberry Pi Pico, but you can adapt it 8 to other boards by changing the constructors for `bus` and `cam` 9 appropriately. 10 11 The camera is placed in YUV mode, so the top 8 bits of each color 12 value can be treated as "greyscale". 13 14 It's important that you use a terminal program that can interpret 15 "ANSI" escape sequences. The demo uses them to "paint" each frame 16 on top of the previous one, rather than scrolling. 17 18 Remember to take the lens cap off! 19 """ 20 import sys 21 import time 22 import busio 23 import board 24 import digitalio 25 import adafruit_ov5640 26 27 print("construct bus") 28 bus = busio.I2C(board.GP9, board.GP8) 29 print("construct camera") 30 reset = digitalio.DigitalInOut(board.GP10) 31 cam = adafruit_ov5640.OV5640( 32 bus, 33 data_pins=( 34 board.GP12, 35 board.GP13, 36 board.GP14, 37 board.GP15, 38 board.GP16, 39 board.GP17, 40 board.GP18, 41 board.GP19, 42 ), # [16] [org] 43 clock=board.GP11, # [15] [blk] 44 vsync=board.GP7, # [10] [brn] 45 href=board.GP21, # [27/o14] [red] 46 mclk=board.GP20, # [16/o15] 47 shutdown=None, 48 reset=reset, 49 size=adafruit_ov5640.OV5640_SIZE_QQVGA, 50 ) 51 print("print chip id") 52 print(cam.chip_id) 53 54 55 cam.colorspace = adafruit_ov5640.OV5640_COLOR_YUV 56 cam.flip_y = True 57 cam.flip_x = True 58 cam.test_pattern = False 59 60 buf = bytearray(cam.capture_buffer_size) 61 chars = b" .':-+=*%$#" 62 remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)] 63 64 width = cam.width 65 row = bytearray(width) 66 67 print("capturing") 68 cam.capture(buf) 69 print("capture complete") 70 71 sys.stdout.write("\033[2J") 72 while True: 73 cam.capture(buf) 74 for j in range(0, cam.height, 2): 75 sys.stdout.write(f"\033[{j//2}H") 76 for i in range(cam.width): 77 row[i] = remap[buf[2 * (width * j + i)]] 78 sys.stdout.write(row) 79 sys.stdout.write("\033[K") 80 sys.stdout.write("\033[J") 81 time.sleep(0.1)