summaryrefslogtreecommitdiff
path: root/validation/validlib/attenuator.py
diff options
context:
space:
mode:
authorCeline Buret2011-12-23 18:51:12 +0100
committerCeline Buret2011-12-23 18:51:12 +0100
commit4a195dc3b04f8a9c67ea6a0c1902dd31e23355d7 (patch)
tree2b69b5393e7b8167ee78dad624449650008a588a /validation/validlib/attenuator.py
parent112636d5a37f4024bd954b922a46e887c3d47635 (diff)
parentf5c6b799cdce5b4bb1618f4789bb683efa7967ab (diff)
Merge branch 'master' of pessac:/git/cesar into eoc
Conflicts: cesar/cl/src/cl.c cesar/cp/sta/action/Config cesar/mac/pbproc/src/prep_mpdu.c cesar/test_general/station/cco0/s2/Config cleopatre/Makefile cleopatre/application/libspid/src/config_line.c cleopatre/application/libspid/src/image.c cleopatre/application/libspid/src/misc.c cleopatre/application/libspid/src/system.c cleopatre/application/managerd/inc/vs_mme.h cleopatre/application/managerd/src/managerd.c cleopatre/application/managerd/src/vs_mme.c cleopatre/devkit/plcdrv/arm/src/linux_drv.c cleopatre/devkit/tests/libmme/utests/src/mme_utests.c cleopatre/devkit/tests/libspid/utests/Makefile cleopatre/devkit/tests/libspid/utests/inc/system_utests.h cleopatre/devkit/tests/libspid/utests/src/config_line_utests.c cleopatre/devkit/tests/libspid/utests/src/image_utests.c cleopatre/devkit/tests/libspid/utests/src/system_utests.c cleopatre/devkit/tests/libspid/utests/testfiles/nvram1.tst cleopatre/devkit/tests/libspid/utests/testfiles/nvram2.tst cleopatre/devkit/tests/managerd/ftests/Makefile cleopatre/devkit/tests/managerd/utests/Makefile cleopatre/devkit/tests/managerd/utests/override/src/libmme_stub.c cleopatre/devkit/tests/managerd/utests/override/src/libspid_stub.c cleopatre/devkit/tests/managerd/utests/src/vs_mme_utests.c cleopatre/devkit/tests/plcd/Makefile cleopatre/devkit/tests/utests_makerules cleopatre/linux-2.6.25.10-spc300/arch/arm/boot/Makefile common/tests/tests
Diffstat (limited to 'validation/validlib/attenuator.py')
-rw-r--r--validation/validlib/attenuator.py106
1 files changed, 106 insertions, 0 deletions
diff --git a/validation/validlib/attenuator.py b/validation/validlib/attenuator.py
new file mode 100644
index 0000000000..d92eaad728
--- /dev/null
+++ b/validation/validlib/attenuator.py
@@ -0,0 +1,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()