File: open_context.py

package info (click to toggle)
libm2k 0.9.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,580 kB
  • sloc: xml: 1,611,497; cpp: 16,278; python: 4,181; cs: 516; sh: 471; ansic: 403; makefile: 35
file content (102 lines) | stat: -rw-r--r-- 2,847 bytes parent folder | download | duplicates (2)
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
#
# Copyright (c) 2024 Analog Devices Inc.
#
# This file is part of libm2k
# (see http://www.github.com/analogdevicesinc/libm2k).
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import sys
import libm2k
import time
import os


def calibrate(ctx):
    # Calibrates ADC and DAC
    # Arguments:
    #    ctx  -- the available m2k context\n
    # Returns:
    #    adc_calib-- Must be True if the calibration was successful
    #    dac_calib--Must be True if the calibration was successful

    # ADC
    adc_calib = ctx.calibrateADC()
    # DAC
    dac_calib = ctx.calibrateDAC()
    return (adc_calib, dac_calib)


def open_context():
    #  Opens the context and retrieves analog objects
    #  Returns:
    #    ctx-- M2k context opened
    #    ain-- AnalogIn object
    #    aout--Analog Out object
    #    trig--Trigger object
    uri = "ip:192.168.2.1"
    if len(sys.argv) >= 3 and ("--uri" in sys.argv):
        for i in range(len(sys.argv)):
            if sys.argv[i] == "--uri":
                uri = sys.argv[i + 1]
                sys.argv.pop(i)
                sys.argv.pop(i)
                break
    print("URI " + uri)
    ctx = libm2k.m2kOpen(uri)
    if ctx is None:
        print("Connection Error: No available ADALM2000 found at the specified URI.")
        exit(1)
    # define input and output
    try:
        ain = ctx.getAnalogIn()
    except:
        ain = 0
    try:
        aout = ctx.getAnalogOut()
    except:
        aout = 0
    # define trigger
    try:
        trig = ain.getTrigger()
    except:
        trig = 0

    return ctx, ain, aout, trig


def create_dir(timestamp):
    # Creates a new directory where all the plot files will be saved
    # Arguments:
    #    timestamp -- Time and date when the program was run

    path = os.getcwd()

    results_dir = "results" + str(timestamp)
    path = os.path.join(path, results_dir)
    os.mkdir(path)

    return results_dir


# open context
ctx_timeout = 5000  # initial value of the timeout
ctx, ain, aout, trig = open_context()  # open context
ctx.setTimeout(ctx_timeout)  # set ctx timeout
if "ip:127.0.0.1" not in ctx.getUri():
    calibration = calibrate(ctx)
dig = ctx.getDigital()
d_trig = dig.getTrigger()
ps = ctx.getPowerSupply()