def turn_off():
    answer = True
    write_voltage(0)
    write_current(0)
    while answer is True:
        supplier.send(c, "turn_off", 1)
        time.sleep(timeout_min)
        answer = supplier.send(c, "read_status")
def test_wave():
    def triangular_wave():
        v_min = 5
        v_max = 10
        v_delta = 0.5
        cycles = 3
        seconds_per_cycle = 1
        steps = int((v_max - v_min) // v_delta)
        timeout_step = seconds_per_cycle / (steps * 2)
        voltage = v_min
        for cycle in range(cycles):
            for updown in [1, -1]:
                for step in range(steps):
                    voltage = voltage + updown * v_delta
                    answer = supplier.send(c, "voltage", voltage)
                    time.sleep(max(timeout_min, timeout_step))

    def square_wave():
        v_min = 5
        v_max = 10
        v_delta = v_max - v_min
        cycles = 3
        seconds_per_cycle = 1
        steps = int((v_max - v_min) // v_delta)
        timeout_step = seconds_per_cycle / (steps)
        voltage = v_min
        for cycle in range(cycles):
            for updown in [1, -1]:
                for step in range(steps):
                    voltage = voltage + updown * v_delta
                    answer = supplier.send(c, "voltage", voltage)
                    time.sleep(max(timeout_min, timeout_step))

    # open or reconnect TCP to server
    if not c.is_open():
        if not c.open():
            print("unable to connect to " + SERVER_HOST + ":" +
                  str(SERVER_PORT))

    if c.is_open():

        # read
        command_name = "read_config"
        answer = supplier.send(c, command_name)
        command_name = "read_voltage"
        answer = supplier.send(c, command_name)
        command_name = "read_current"
        answer = supplier.send(c, command_name)

        # write
        turn_on()
        triangular_wave()
        square_wave()
        turn_off()

    exit()
def write_current(value: float):
    max_tries = 10
    accepted_error = 0.01
    corrected_value = value
    for tries in range(max_tries):
        supplier.send(c, "current", corrected_value)
        time.sleep(max(timeout_min, timeout_step))

        current_config = read_current_config()
        if abs(current_config - corrected_value) < accepted_error:
            break

    return current_config
def write_voltage(value: float):
    max_tries = 10
    accepted_error = 0.5
    gain = 1.13868
    corrected_value = value * gain
    for tries in range(max_tries):
        supplier.send(c, "voltage", corrected_value)
        time.sleep(max(timeout_min, timeout_step))

        voltage_config = read_voltage_config()
        if abs(voltage_config - corrected_value) < accepted_error:
            break

    return voltage_config
 def square_wave():
     v_min = 5
     v_max = 10
     v_delta = v_max - v_min
     cycles = 3
     seconds_per_cycle = 1
     steps = int((v_max - v_min) // v_delta)
     timeout_step = seconds_per_cycle / (steps)
     voltage = v_min
     for cycle in range(cycles):
         for updown in [1, -1]:
             for step in range(steps):
                 voltage = voltage + updown * v_delta
                 answer = supplier.send(c, "voltage", voltage)
                 time.sleep(max(timeout_min, timeout_step))
def read_current() -> float:
    return supplier.send(c, "read_current")
def read_voltage() -> float:
    return supplier.send(c, "read_voltage")
def read_current_config() -> float:
    return supplier.send(c, "read_config")[1]
def read_voltage_config() -> float:
    return supplier.send(c, "read_config")[0]