summaryrefslogtreecommitdiff
path: root/validation/validlib/attenuator.py
blob: d92eaad7280a396af6346fae22d65cb73d5e3e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-

"""Module allowing to command an attenuator"""

import os

config = [
    #Name of the attenuator used by the GPIB driver
    "Attenuator", 
    #List of the buttons with their attenuations
    {1:1, 2:2, 3:4, 4:4, 5:10, 6:20, 7:40, 8:0}
    ]

#List of the possible attenuations, computed from the config, 
#with the corresponding arguments passed to gpib_control
attenuation_arguments = None

def init():
    """Compute the possible attenuations of the attenuator"""
    global attenuation_arguments
    attenuation_arguments = get_commands(config[1])

def set_attenuation(attenuation):
    """Set the attenuation in dB"""
    if attenuation_arguments is None:
        init()
    command = "gpib_control " + config[0] + " " + \
        attenuation_arguments[attenuation]
    return os.popen(command).read()

##############################################################################
# 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
    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'}
        
    print "Executing unit tests of",  __file__, "..."
    test_get_combinations()
    test_translate()
    test_get_commands()