|
| 1 | +"""Android-inspired CameraManager for MicroPythonOS. |
| 2 | +
|
| 3 | +Provides unified access to camera devices (back-facing, front-facing, external). |
| 4 | +Follows module-level singleton pattern (like SensorManager, AudioFlinger). |
| 5 | +
|
| 6 | +Example usage: |
| 7 | + import mpos.camera_manager as CameraManager |
| 8 | +
|
| 9 | + # In board init file: |
| 10 | + CameraManager.add_camera(CameraManager.Camera( |
| 11 | + lens_facing=CameraManager.CameraCharacteristics.LENS_FACING_BACK, |
| 12 | + name="OV5640", |
| 13 | + vendor="OmniVision" |
| 14 | + )) |
| 15 | +
|
| 16 | + # In app: |
| 17 | + cam_list = CameraManager.get_cameras() |
| 18 | + if len(cam_list) > 0: |
| 19 | + print("we have a camera!") |
| 20 | +
|
| 21 | +MIT License |
| 22 | +Copyright (c) 2024 MicroPythonOS contributors |
| 23 | +""" |
| 24 | + |
| 25 | +try: |
| 26 | + import _thread |
| 27 | + _lock = _thread.allocate_lock() |
| 28 | +except ImportError: |
| 29 | + _lock = None |
| 30 | + |
| 31 | + |
| 32 | +# Camera lens facing constants (matching Android Camera2 API) |
| 33 | +class CameraCharacteristics: |
| 34 | + """Camera characteristics and constants.""" |
| 35 | + LENS_FACING_BACK = 0 # Back-facing camera (primary) |
| 36 | + LENS_FACING_FRONT = 1 # Front-facing camera (selfie) |
| 37 | + LENS_FACING_EXTERNAL = 2 # External USB camera |
| 38 | + |
| 39 | + |
| 40 | +class Camera: |
| 41 | + """Camera metadata (lightweight data class, Android-inspired). |
| 42 | + |
| 43 | + Represents a camera device with its characteristics. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self, lens_facing, name=None, vendor=None, version=None): |
| 47 | + """Initialize camera metadata. |
| 48 | +
|
| 49 | + Args: |
| 50 | + lens_facing: Camera orientation (LENS_FACING_BACK, LENS_FACING_FRONT, etc.) |
| 51 | + name: Human-readable camera name (e.g., "OV5640", "Front Camera") |
| 52 | + vendor: Camera vendor/manufacturer (e.g., "OmniVision") |
| 53 | + version: Driver version (default 1) |
| 54 | + """ |
| 55 | + self.lens_facing = lens_facing |
| 56 | + self.name = name or "Camera" |
| 57 | + self.vendor = vendor or "Unknown" |
| 58 | + self.version = version or 1 |
| 59 | + |
| 60 | + def __repr__(self): |
| 61 | + facing_names = { |
| 62 | + CameraCharacteristics.LENS_FACING_BACK: "BACK", |
| 63 | + CameraCharacteristics.LENS_FACING_FRONT: "FRONT", |
| 64 | + CameraCharacteristics.LENS_FACING_EXTERNAL: "EXTERNAL" |
| 65 | + } |
| 66 | + facing_str = facing_names.get(self.lens_facing, f"UNKNOWN({self.lens_facing})") |
| 67 | + return f"Camera({self.name}, facing={facing_str})" |
| 68 | + |
| 69 | + |
| 70 | +# Module state |
| 71 | +_initialized = False |
| 72 | +_cameras = [] # List of Camera objects |
| 73 | + |
| 74 | + |
| 75 | +def init(): |
| 76 | + """Initialize CameraManager. |
| 77 | + |
| 78 | + Returns: |
| 79 | + bool: True if initialized successfully |
| 80 | + """ |
| 81 | + global _initialized |
| 82 | + _initialized = True |
| 83 | + return True |
| 84 | + |
| 85 | + |
| 86 | +def is_available(): |
| 87 | + """Check if CameraManager is initialized. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + bool: True if CameraManager is initialized |
| 91 | + """ |
| 92 | + return _initialized |
| 93 | + |
| 94 | + |
| 95 | +def add_camera(camera): |
| 96 | + """Register a camera device. |
| 97 | +
|
| 98 | + Args: |
| 99 | + camera: Camera object to register |
| 100 | +
|
| 101 | + Returns: |
| 102 | + bool: True if camera added successfully |
| 103 | + """ |
| 104 | + if not isinstance(camera, Camera): |
| 105 | + print(f"[CameraManager] Error: add_camera() requires Camera object, got {type(camera)}") |
| 106 | + return False |
| 107 | + |
| 108 | + if _lock: |
| 109 | + _lock.acquire() |
| 110 | + |
| 111 | + try: |
| 112 | + # Check if camera with same facing already exists |
| 113 | + for existing in _cameras: |
| 114 | + if existing.lens_facing == camera.lens_facing: |
| 115 | + print(f"[CameraManager] Warning: Camera with facing {camera.lens_facing} already registered") |
| 116 | + # Still add it (allow multiple cameras with same facing) |
| 117 | + |
| 118 | + _cameras.append(camera) |
| 119 | + print(f"[CameraManager] Registered camera: {camera}") |
| 120 | + return True |
| 121 | + finally: |
| 122 | + if _lock: |
| 123 | + _lock.release() |
| 124 | + |
| 125 | + |
| 126 | +def get_cameras(): |
| 127 | + """Get list of all registered cameras. |
| 128 | +
|
| 129 | + Returns: |
| 130 | + list: List of Camera objects (copy of internal list) |
| 131 | + """ |
| 132 | + if _lock: |
| 133 | + _lock.acquire() |
| 134 | + |
| 135 | + try: |
| 136 | + return _cameras.copy() if _cameras else [] |
| 137 | + finally: |
| 138 | + if _lock: |
| 139 | + _lock.release() |
| 140 | + |
| 141 | + |
| 142 | +def get_camera_by_facing(lens_facing): |
| 143 | + """Get first camera with specified lens facing. |
| 144 | +
|
| 145 | + Args: |
| 146 | + lens_facing: Camera orientation (LENS_FACING_BACK, LENS_FACING_FRONT, etc.) |
| 147 | +
|
| 148 | + Returns: |
| 149 | + Camera object or None if not found |
| 150 | + """ |
| 151 | + if _lock: |
| 152 | + _lock.acquire() |
| 153 | + |
| 154 | + try: |
| 155 | + for camera in _cameras: |
| 156 | + if camera.lens_facing == lens_facing: |
| 157 | + return camera |
| 158 | + return None |
| 159 | + finally: |
| 160 | + if _lock: |
| 161 | + _lock.release() |
| 162 | + |
| 163 | + |
| 164 | +def has_camera(): |
| 165 | + """Check if any camera is registered. |
| 166 | +
|
| 167 | + Returns: |
| 168 | + bool: True if at least one camera available |
| 169 | + """ |
| 170 | + if _lock: |
| 171 | + _lock.acquire() |
| 172 | + |
| 173 | + try: |
| 174 | + return len(_cameras) > 0 |
| 175 | + finally: |
| 176 | + if _lock: |
| 177 | + _lock.release() |
| 178 | + |
| 179 | + |
| 180 | +def get_camera_count(): |
| 181 | + """Get number of registered cameras. |
| 182 | +
|
| 183 | + Returns: |
| 184 | + int: Number of cameras |
| 185 | + """ |
| 186 | + if _lock: |
| 187 | + _lock.acquire() |
| 188 | + |
| 189 | + try: |
| 190 | + return len(_cameras) |
| 191 | + finally: |
| 192 | + if _lock: |
| 193 | + _lock.release() |
| 194 | + |
| 195 | + |
| 196 | +# Initialize on module load |
| 197 | +init() |
0 commit comments