White balance lock#

Camera 1#

Sample code to monitor white balance lock state:

/// Keep reference on MainCamera peripheral to get notified of changes.
private var cameraRef: Ref<MainCamera>?

/// Monitors and prints white balance lock state.
func monitorWhiteBalanceLock(drone: Drone) {
    cameraRef = drone.getPeripheral(Peripherals.mainCamera) { camera in
        // called on main thread when the camera peripheral changes
        if let camera = camera {
            if let whiteBalanceLock = camera.whiteBalanceLock {
                // whether white balance is lockable
                let isLockable = whiteBalanceLock.isLockable
                print("White balance isLockable: \(isLockable)")
                // whether white balance is locked
                let locked = whiteBalanceLock.locked
                print("White balance locked: \(locked)")
                // updating flag
                let updating = whiteBalanceLock.updating
                print("White balance lock updating: \(updating)")
            } else {
                // white balance lock is not available
                print("White balance lock not available")
            }
        }
    }
}

Example of output:

White balance isLockable: Optional(true)
White balance locked: false
White balance lock updating: false

Sample code to lock and unlock white balance:

/// Locks or unlocks white balance.
func lockWhiteBalance(drone: Drone, lock: Bool) {
    if let camera = drone.getPeripheral(Peripherals.mainCamera) {
        camera.whiteBalanceLock?.setLock(lock: lock)
    }
}

Trying to change the lock value when isLockable is not true will have no effect.

Camera 2#

Note: You need to keep a reference on the whiteBalanceLock component to get notified of changes.

Sample code to monitor white balance lock:

/// Keep reference on Camera2WhiteBalanceLock component to get notified of changes.
private var whiteBalanceLockRef: Ref<Camera2WhiteBalanceLock>?

/// Monitors and prints white balance lock.
func monitorWhiteBalanceLock(drone: Drone) {
    if let camera = drone.getPeripheral(Peripherals.mainCamera2) {
        // get whiteBalanceLock component reference and register listener
        whiteBalanceLockRef = camera.getComponent(Camera2Components.whiteBalanceLock) { whiteBalanceLock in
            if let whiteBalanceLock = whiteBalanceLock {
                if !whiteBalanceLock.supportedModes.isEmpty {
                    // mode value is not relevant, there is no supported value
                    print("White balance lock not available")
                } else {
                    if !whiteBalanceLock.supportedModes.contains(.locked) {
                        // white balance cannot be locked at present
                        print("White balance can't be locked")
                    }
                    // get setting value
                    let whiteBalanceLockMode = whiteBalanceLock.mode
                    print("Current white balance lock mode: \(whiteBalanceLockMode)")
                }
            } else {
                // whiteBalanceLock component nil, meaning white balance lock is unavailable
                print("White balance lock not available")
            }
        }
    }
}

Example of output:

Lock is not supported
Not lockable
Current value is: locked

Sample code to lock and unlock white balance:

/// Locks or unlocks white balance.
func setWhiteBalanceLock(drone: Drone, lockMode: Camera2WhiteBalanceLockMode) {
    // retrieve white balance component
    if let camera = drone.getPeripheral(Peripherals.mainCamera2),
       let whiteBalanceLock = camera.getComponent(Camera2Components.whiteBalanceLock),
       whiteBalanceLock.supportedModes.contains(lockMode) {
        // change white balance lock mode
        whiteBalanceLock.mode = lockMode
    }
}