# -*- coding: utf-8 -*- """Module allowing to command an attenuator""" import os import ssh config = { #Name of the attenuator used by the GPIB driver "name":"Attenuator", #List of the buttons with their attenuations "buttons":{1:1, 2:2, 3:4, 4:4, 5:10, 6:20, 7:40, 8:0}, #User name to connect to the host where the GPIB is installed "user":"spidcom", #Host where the GPIB is installed "host":"blovac"} #List of the possible attenuations, computed from the config, #with the corresponding arguments passed to gpib_control attenuation_arguments = None def update_config(new_config): """Update the configuration""" global config config = new_config init() def init(): """Compute the possible attenuations of the attenuator""" global attenuation_arguments attenuation_arguments = get_commands(config["buttons"]) def attenuation_range_db(): attenuations = attenuation_arguments.keys() return (min(attenuations), max(attenuations)) def set_attenuation(attenuation): """Set the attenuation in dB""" if attenuation_arguments is None: init() command = "gpib_control " + config["name"] + " " + \ attenuation_arguments[attenuation] ssh.call(config["user"], config["host"], command) ############################################################################## # Internal functions ############################################################################## def get_commands(buttons): """Return a dictionary with all possible attenuations""" combinations = get_combinations(buttons.keys()) commands = dict() for combination in combinations: (attenuation, command) = translate(combination, buttons) commands[attenuation] = command return commands def get_combinations(keys): """Return all possible combinations obtained with the keys""" acc = [[]] for key in keys: new_acc = [] for elem in acc: new_acc.append(elem + [key]) acc.extend(new_acc) return acc def translate(combination, buttons): """Translate a combination of buttons in an attenuation and a command""" keys = buttons.keys() string_a = "" attenuation = 0 for key in combination: string_a = string_a + str(key) attenuation = attenuation + buttons[key] keys.remove(key) string_b = "" for key in keys: string_b = string_b + str(key) string = "" if string_a != "": string = string + "A" + string_a if string_b != "": string = string + "B" + string_b assert attenuation >= 0 return (attenuation, string) if __name__ == "__main__": def test_get_combinations(): assert get_combinations([]) == [[]] assert get_combinations([1]) == [[], [1]] assert get_combinations([1, 2]) == [[], [1], [2], [1, 2]] assert get_combinations([1, 2, 3]) == [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] def test_translate(): attenuations = {1:1, 2:2, 3:4} assert translate([], attenuations) == (0, "B123") assert translate([1], attenuations) == (1, "A1B23") assert translate([2], attenuations) == (2, "A2B13") assert translate([1, 2], attenuations) == (3, "A12B3") assert translate([1, 2, 3], attenuations) == (7, "A123") def test_get_commands(): assert get_commands({}) == {0: ''} assert get_commands({1:1}) == {0: 'B1', 1: 'A1'} assert get_commands({1:1, 2:2}) == {0: 'B12', 1: 'A1B2', 2: 'A2B1', 3: 'A12'} assert get_commands({1:1, 2:2, 3:4, 4:4}) == {0: 'B1234', 1: 'A1B234', 2: 'A2B134', 3: 'A12B34', 4: 'A4B123', 5: 'A14B23', 6: 'A24B13', 7: 'A124B3', 8: 'A34B12', 9: 'A134B2', 10: 'A234B1', 11: 'A1234'} test_get_combinations() test_translate() test_get_commands()