Thanks for the code. I see you have used voltage_range input to select most appropriate range which is OK, but I have made some changes to allow only defined ranges. I also added separate adjustments for channels CH1 and CH2. There were some errors in the code which I also fixed. Note that attr parameter in sfSetYRangeScaleA is used to select high/low gains and the attribute is fixed for selected voltage_range (value of attr is defined in 'hardware.json' file).
def set_voltage_range(SHardware, CH1_voltage_range, CH2_voltage_range):
VOLTAGE_RANGES = [("volt2", 2.0, 0),
("volt1", 1.0, 0),
("mili500", 500e-3, 0),
("mili200", 200e-3, 0),
("mili100", 100e-3, 1),
("mili50", 50e-3, 1),
("mili20", 20e-3, 1),
("mili10", 10e-3, 1)]
# Set CH1 gain and offset
try:
for idx1 in range(0, len(VOLTAGE_RANGES)+1):
if CH1_voltage_range == VOLTAGE_RANGES[idx1][1]:
(CH1_voltage_range_key, CH1_voltage_range, ch1_gain_attribute) = VOLTAGE_RANGES[idx1]
# get the Calibration data for this range
ch1_offset = calibration_data["calibratedNormal"]["offsetsCh0"][CH1_voltage_range_key]
ch1_gain = calibration_data["calibratedNormal"]["gainValueCh0"][CH1_voltage_range_key]
# set the hardware registers
sfSetYPositionA(SHardware, ch1_offset)
sfSetYRangeScaleA(SHardware, ch1_gain_attribute, ch1_gain)
break
except IndexError:
print("Err0: CH1 voltage range '{} V' is not defined.".format(CH1_voltage_range))
# Set CH2 gain and offset
try:
for idx2 in range(0, len(VOLTAGE_RANGES)+1):
if CH2_voltage_range == VOLTAGE_RANGES[idx2][1]:
(CH2_voltage_range_key, CH2_voltage_range, ch2_gain_attribute) = VOLTAGE_RANGES[idx2]
# get the Calibration data for this range
ch2_offset = calibration_data["calibratedNormal"]["offsetsCh1"][CH2_voltage_range_key]
ch2_gain = calibration_data["calibratedNormal"]["gainValueCh1"][CH2_voltage_range_key]
# set the hardware registers
sfSetYPositionB(SHardware, ch2_offset)
sfSetYRangeScaleB(SHardware, ch2_gain_attribute, ch2_gain)
break
except IndexError:
print("Err0: CH2 voltage range '{} V' is not defined.".format(CH2_voltage_range))