import argparse

import dv_processing as dv

parser = argparse.ArgumentParser(
    description='Save a stereo event stream from a pair of time-synchronized iniVation cameras to a file.')

args = parser.parse_args()

# Open the cameras, provide exact names for left and right cameras
leftCamera = dv.io.camera.openSync("DVXplorer_DXA00080")
rightCamera = dv.io.camera.openSync("DAVIS346_00000600")

dv.io.camera.synchronizeAnyTwo(leftCamera, rightCamera)

# Try block to catch user interrupt to stop recording
try:
    # Open a file, pass camera instance for the writer to inspect what data streams have to be recorded
    writer = dv.io.StereoCameraWriter("./stereo.aedat4", leftCamera, rightCamera)

    print("Start recording")
    # While both cameras are physically present
    while leftCamera.isRunning() and rightCamera.isRunning():
        # Read a packet from the camera
        leftEvents = leftCamera.getNextEventBatch()
        # If read was successful
        if leftEvents is not None:
            # Write the packet into the file
            writer.left.writeEvents(leftEvents)

        # Equivalent execution for the right camera
        rightEvents = rightCamera.getNextEventBatch()
        if rightEvents is not None:
            writer.right.writeEvents(rightEvents)
except KeyboardInterrupt:
    pass

print("Ending recording")
