import time


def open(my_instrument, mode='ON', collect=False):
    """
    Function to activate and measure the open circuit potential
    (OCP) of the system. The correction will then be active
    on the intrument. Once this funciton is ran, the new OCP
    will be stored in the instrument and unless tuned off,
    the correction will be applied to all the measurements collected after.

    Parameters
    ----------

    my_instrument: pyvisa.resources.usb.USBInstrument
                   Instrument instance obtained throught the Pyvisa package
    mode: str
          Options are ON or OFF.
    collect: bool
             Option to collect  a new measurement for the open correction.
             If collect is set to false, the last measured open correction will
             be used.

    Returns
    -------
    The function will return a print statement confirming the
    task was completed.
    """

    # Clear previous states and resets the instrument settings
    my_instrument.write('*RST; *CLS')

    # Enable the display to update itself when a change is made
    my_instrument.write(':DISP:ENAB')
    time.sleep(3)

    # Configure the instrument to automatically perform continuous
    # measurements
    my_instrument.write(':INIT:CONT')

    # * Switch the instrument triggering function to 'EXT'
    # in order to control the measurements remotely
    my_instrument.write(':TRIG:SOUR EXT')
    time.sleep(5)
    if mode == 'ON':
        my_instrument.write(':CORR:OPEN:STAT ON')
        if collect:
            my_instrument.write(':CORR:OPEN:EXEC')
        else:
            pass

        return print('the correction for the Open Circuit Potential' +
                     ' was successfully collected')
    else:
        my_instrument.write(':CORR:OPEN:STAT OFF')

        return print('the correction for the Open Circuit Potential' +
                     ' was disactivated')