#!/usr/bin/python #For Python version > = 2.7.8 from Tkinter import * import atexit from collections import defaultdict from operator import add import time import libpysmu # define ADALM1000 interface class Smu(object): def __init__(self): atexit.register(libpysmu.cleanup) libpysmu.setup() self.load_channels() def load_channels(self): self.devices = libpysmu.get_dev_info() self.serials = {i: v[0] for i, v in enumerate(self.devices)} self.devices = [x[1] for x in self.devices] names = (chr(x) for x in xrange(65,91)) channels = {names.next(): (i, v) for i, d in enumerate(self.devices) for k, v in d.items()} self.channels = {k: Channel(k, self.serials[v[0]], v[1]) for k, v in channels.items()} device_channels = defaultdict(list) for k, v in channels.items(): device_channels[v[0]].append(Channel(k, *v)) self.devices = {i: Device(self.serials[i], device_channels[i]) for i, v in enumerate(self.devices)} def ctrl_transfer(self, device, bm_request_type, b_request, wValue, wIndex, data, wLength, timeout): data = str(data) if bm_request_type&0x80 == 0x80: if data == '0': data = '\x00'*wLength else: wLength = 0 ret = libpysmu.ctrl_transfer(device, bm_request_type, b_request, wValue, wIndex, data, wLength, timeout) if bm_request_type&0x80 == 0x80: return map(ord, data) else: return ret def __repr__(self): return 'Devices: ' + str(self.devices) class Device(object): def __init__(self, serial, channels): self.serial = serial self.channels = channels def get_samples(self, n_samples): """query device for a list of samples from all channels :param n_samples: number of samples :type n_samples: int :return: list of n samples from all the device's channels """ return libpysmu.get_all_inputs(self.serial, n_samples) @property def samples(self): """iterator for samples from the device run in continuous mode""" return libpysmu.iterate_inputs(self.serial) class Channel(object): def __init__(self, chan, dev_serial, signals): self.dev = dev_serial self.chan = ord(chan) - 65 self.signals = {v: i for i, v in enumerate(signals)} def set_mode(self, mode): if mode == 'v' or mode == 'V': libpysmu.set_mode(self.dev, self.chan, 1) self.mode = 1 elif mode == 'i' or mode == 'I': libpysmu.set_mode(self.dev, self.chan, 2) self.mode = 2 elif mode == 'd' or mode == 'D': libpysmu.set_mode(self.dev, self.chan, 0) self.mode = 0 else: raise ValueError('invalid mode') def arbitrary(self, *args, **kwargs): repeat = 0 if 'repeat' in map(str.lower, kwargs.keys()): repeat = 1 wave = map(float, reduce(add, [[s]*100*n for s, n in args])) return libpysmu.set_output_buffer(wave, self.dev, self.chan, self.mode, repeat) def get_samples(self, n_samples): """query channel for samples :param n_samples: number of samples :type n_samples: int :return: list of n samples from the channel """ return libpysmu.get_inputs(self.dev, self.chan, n_samples) def constant(self, val): """set output to a constant waveform""" return libpysmu.set_output_constant(self.dev, self.chan, self.mode, val) def square(self, midpoint, peak, period, phase, duty_cycle): """set output to a square waveform""" return libpysmu.set_output_wave(self.dev, self.chan, self.mode, 1, midpoint, peak, period, phase, duty_cycle) def sawtooth(self, midpoint, peak, period, phase): """set output to a sawtooth waveform""" return libpysmu.set_output_wave(self.dev, self.chan, self.mode, 2, midpoint, peak, period, phase, 42) def stairstep(self, midpoint, peak, period, phase): """set output to a stairstep waveform""" return libpysmu.set_output_wave(self.dev, self.chan, self.mode, 3, midpoint, peak, period, phase, 42) def sine(self, midpoint, peak, period, phase): """set output to a sinusoidal waveform""" return libpysmu.set_output_wave(self.dev, self.chan, self.mode, 4, midpoint, peak, period, phase, 42) def triangle(self, midpoint, peak, period, phase): """set output to a triangle waveform""" return libpysmu.set_output_wave(self.dev, self.chan, self.mode, 5, midpoint, peak, period, phase, 42) def __repr__(self): return 'Device: ' + str(self.dev) + '\nChannel: ' + str(self.chan) + '\nSignals: ' + str(self.signals) # define button actions def Analog_in(): while (True): # Main loop if (RUNstatus.get() == 1): DCVA = 0.0 # initalize measurment variable ADsignal1 = CHA.get_samples(20) # get 20 readings # get_samples returns a list of values for voltage [0] and current [1] for index in range(10): # calculate average SPA = ADsignal1[index+10][0] # skip over first 10 readings VAdata = float(SPA) DCVA += VAdata # Sum for average voltage DCVA = DCVA / 10.0 # calculate average VAString = ' {0:.4f} '.format(DCVA) # format with 4 decimal places label.config(text = VAString) # change displayed value time.sleep(0.1) # Update tasks and screens by TKinter root.update_idletasks() root.update() # setup main window root = Tk() label = Label(root, font = "Arial 16 bold") label.grid(row=1, columnspan=1, sticky=W) label.config(text = " ") RUNstatus = IntVar(0) rb1 = Radiobutton(root, text="Stop", variable=RUNstatus, value=0 ) rb1.grid(row=2, column=0, sticky=W) rb2 = Radiobutton(root, text="Run", variable=RUNstatus, value=1 ) rb2.grid(row=2, column=1, sticky=W) # Setup ADAML1000 devx = Smu() DevID = devx.serials[0] print DevID CHA = devx.channels['A'] # Open CHA CHA.set_mode('d') # Put CHA in Hi Z mode ADsignal1 = [] # Ain signal array channel A # start main loop root.update() # Start sampling Analog_in() #