"""
This file contains the python-based equivalent of this MSL sequencer script:

```
#
# I-V-Curve test with parameter specification at startup
#

PARAM start_voltage, "Starting voltage"
PARAM stop_voltage, "Stop voltage"
PARAM num_steps, "Number of steps"

# Calculate step size
num_steps = $num_steps + 1 # add one step for stop_voltage
step_size = ($stop_voltage-$start_voltage) / ($num_steps-1)

# Initialize measurement arrays at startup
ODBCREATE /Equipment/Test/Variables/Voltage, FLOAT, $num_steps
ODBCREATE /Equipment/Test/Variables/Current, FLOAT, $num_steps
ODBCREATE /Equipment/Test/Variables/V, FLOAT

# Erase any previously stored array
ODBSET /Equipment/Test/Variables/Voltage[*], 0
ODBSET /Equipment/Test/Variables/Current[*], 0

v = $start_voltage
ODBSET /Equipment/Test/Variables/V, $v

current = 0

# Turn on Keithley
ODBSET /Equipment/KEITHLEY/Variables/Set State, 1
# Wait to turn on
WAIT SECONDS, 2


# Looping starts at 1
LOOP i, $num_steps
    # Store voltage in array and in variable
    ODBSET /Equipment/Test/Variables/Voltage[$i-1], $v
    ODBSET /Equipment/Test/Variables/V, $v

    # Set voltage and measure
    ODBSET /Equipment/KEITHLEY/Variables/Demand Voltage, $v
    # Wait for measurement to be stored in Current
    WAIT SECONDS, 10
    ODBGET /Equipment/KEITHLEY/Variables/Current, current
    
    # Outputting current to ODB array
    ODBSET /Equipment/Test/Variables/Current[$i-1], $current

    # increment voltage
    v = $v + $step_size
ENDLOOP

# Turn off Keithley
ODBSET /Equipment/KEITHLEY/Variables/Set State, 0

# Wait to turn off
WAIT SECONDS, 1
```

To use this script you will need to copy it to the 
`<experiment_dir>/userfiles/sequencer/` directory.
"""

# I-V-Curve test with parameter specification at startup

# You don't HAVE to import midas.sequencer in your script, but it
# may be useful if you want to use "type annotations" when defining
# your `define_params()`, `sequence()`, `at_exit()` functions.
# The type annotations will help your IDE (VS Code, PyCharm etc) know
# which functions you can call on the `seq` object.
import midas.sequencer

def define_params(seq: midas.sequencer.SequenceClient):
    seq.register_param("start_voltage", "Starting voltage", 0.0)
    seq.register_param("stop_voltage", "Stop voltage", 10.0)
    seq.register_param("num_steps", "Number of steps", 100)

def sequence(seq: midas.sequencer.SequenceClient):
    # Calculate step size
    num_steps = seq.get_param("num_steps") + 1 # add one step for stop_voltage
    step_size = (seq.get_param("stop_voltage")-seq.get_param("start_voltage")) / (num_steps-1)

    # Initialize measurement arrays at startup, and
    # rase any previously stored array
    seq.odb_set("/Equipment/Test/Variables/Voltage", [0.0] * num_steps)
    seq.odb_set("/Equipment/Test/Variables/Current", [0.0] * num_steps)
    seq.odb_set("/Equipment/Test/Variables/V", 0)

    v = seq.get_param("start_voltage")

    current = 0

    # Turn on Keithley
    seq.odb_set("/Equipment/KEITHLEY/Variables/Set State", 1)

    # Wait to turn on
    seq.wait_seconds(2)

    # Looping starts at 0 in python!!!
    for i in seq.range(num_steps):
        # Store voltage in array and in variable
        seq.odb_set(f"/Equipment/Test/Variables/Voltage[{i}]", v)
        seq.odb_set("/Equipment/Test/Variables/V", v)

        # Set voltage and measure
        seq.odb_set("/Equipment/KEITHLEY/Variables/Demand Voltage", v)

        # Wait for measurement to be stored in Current
        seq.wait_seconds(10)
        current = seq.odb_get("/Equipment/KEITHLEY/Variables/Current")
        
        # Outputting current to ODB array
        seq.odb_set(f"/Equipment/Test/Variables/Current[{i}]", current)

        # increment voltage
        v += step_size

    # Turn off Keithley
    seq.odb_set("/Equipment/KEITHLEY/Variables/Set State", 0)

    # Wait to turn off
    seq.wait_seconds(1)