| 1 | # -*- mode: python -*- |
| 2 | # Version $Id$ |
| 3 | """ |
| 4 | ctypes wrapping of AWG API |
| 5 | |
| 6 | Author: Christopher Wipf, Massachusetts Institute of Technology |
| 7 | |
| 8 | This module calls the libawg library for the standard set of |
| 9 | waveforms, the libtestpoint library for testpoint handling, and the |
| 10 | libSIStr library (from awgstream) for arbitrary loops and streams. |
| 11 | |
| 12 | """ |
| 13 | import os |
| 14 | import platform |
| 15 | from ctypes import CDLL, Structure, c_int, c_uint, c_longlong, c_ulonglong, \ |
| 16 | c_float, c_double, c_char, c_char_p, POINTER |
| 17 | from numpy import array |
| 18 | from numpy.ctypeslib import ndpointer |
| 19 | |
| 20 | __docformat__ = 'restructuredtext' |
| 21 | |
| 22 | sys = platform.system( ) |
| 23 | ext = '.so' |
| 24 | awglibdir = '__libdir__' |
| 25 | if ( len(awglibdir) > 0 ): |
| 26 | awglibdir += os.sep |
| 27 | if ( sys == 'Darwin' ): |
| 28 | ext = '.dylib' |
| 29 | libawg = CDLL(awglibdir + 'libawg' + ext) |
| 30 | libtestpoint = CDLL(awglibdir + 'libtestpoint' + ext) |
| 31 | libSIStr = None |
| 32 | try: |
| 33 | libSIStr = CDLL(awglibdir + 'libSIStr' + ext) |
| 34 | except: |
| 35 | pass |
| 36 | |
| 37 | #### |
| 38 | #### tconv.h |
| 39 | #### |
| 40 | |
| 41 | # One epoch expressed in ns |
| 42 | _EPOCH = 1e9 / 16.0 |
| 43 | |
| 44 | libawg.TAInow.argtypes = [] |
| 45 | libawg.TAInow.restype = c_ulonglong |
| 46 | TAInow = libawg.TAInow |
| 47 | GPSnow = TAInow |
| 48 | |
| 49 | |
| 50 | #### |
| 51 | #### awgtype.h |
| 52 | #### |
| 53 | |
| 54 | # AWG_WaveType enum values |
| 55 | awgNone = 0 |
| 56 | awgSine = 1 |
| 57 | awgSquare = 2 |
| 58 | awgRamp = 3 |
| 59 | awgTriangle = 4 |
| 60 | awgImpulse = 5 |
| 61 | awgConst = 6 |
| 62 | awgNoiseN = 7 |
| 63 | awgNoiseU = 8 |
| 64 | awgArb = 9 |
| 65 | awgStream = 10 |
| 66 | |
| 67 | # Phasing types |
| 68 | AWG_PHASING_STEP = 0 |
| 69 | AWG_PHASING_LINEAR = 1 |
| 70 | AWG_PHASING_QUADRATIC = 2 |
| 71 | AWG_PHASING_LOG = 3 |
| 72 | |
| 73 | class AWG_Component(Structure): |
| 74 | _fields_ = [("wtype", c_uint), |
| 75 | ("_par_ctypes", c_double * 4), |
| 76 | ("start", c_ulonglong), |
| 77 | ("duration", c_ulonglong), |
| 78 | ("restart", c_ulonglong), |
| 79 | ("ramptype", c_int), |
| 80 | ("_ramptime_ctypes", c_ulonglong * 2), |
| 81 | ("_ramppar_ctypes", c_double * 4)] |
| 82 | @property |
| 83 | def par(self): |
| 84 | return tuple(self._par_ctypes) |
| 85 | |
| 86 | @par.setter |
| 87 | def par(self, val): |
| 88 | self._par_ctypes = (c_double * 4)(*val) |
| 89 | |
| 90 | @property |
| 91 | def ramptime(self): |
| 92 | return tuple(self._ramptime_ctypes) |
| 93 | |
| 94 | @ramptime.setter |
| 95 | def ramptime(self, val): |
| 96 | self._ramptime_ctypes = (c_ulonglong * 2)(*val) |
| 97 | |
| 98 | @property |
| 99 | def ramppar(self): |
| 100 | return tuple(self._ramppar_ctypes) |
| 101 | |
| 102 | @ramppar.setter |
| 103 | def ramppar(self, val): |
| 104 | self._ramppar_ctypes = (c_double * 4)(*val) |
| 105 | |
| 106 | |
| 107 | #### |
| 108 | #### awgapi.h |
| 109 | #### |
| 110 | |
| 111 | libawg.awgSetChannel.argtypes = [c_char_p,] |
| 112 | libawg.awgSetChannel.restype = c_int |
| 113 | awgSetChannel = libawg.awgSetChannel |
| 114 | |
| 115 | libawg.awgAddWaveform.argtypes = [c_int, POINTER(AWG_Component), c_int] |
| 116 | libawg.awgAddWaveform.restype = c_int |
| 117 | def awgAddWaveform(slot, comp): |
| 118 | comp_array = (AWG_Component * len(comp))(*comp) |
| 119 | return libawg.awgAddWaveform(slot, comp_array, len(comp)) |
| 120 | |
| 121 | libawg.awgStopWaveform.argtypes = [c_int, c_int, c_ulonglong] |
| 122 | libawg.awgStopWaveform.restype = c_int |
| 123 | awgStopWaveform = libawg.awgStopWaveform |
| 124 | |
| 125 | libawg.awgClearWaveforms.argtypes = [c_int,] |
| 126 | libawg.awgClearWaveforms.restype = c_int |
| 127 | awgClearWaveforms = libawg.awgClearWaveforms |
| 128 | |
| 129 | libawg.awgRemoveChannel.argtypes = [c_int,] |
| 130 | libawg.awgRemoveChannel.restype = c_int |
| 131 | awgRemoveChannel = libawg.awgRemoveChannel |
| 132 | |
| 133 | libawg.awgSetGain.argtypes = [c_int, c_double, c_ulonglong] |
| 134 | libawg.awgSetGain.restype = c_int |
| 135 | awgSetGain = libawg.awgSetGain |
| 136 | |
| 137 | libawg.awgSetFilter.argtypes = [c_int, POINTER(c_double), c_int] |
| 138 | libawg.awgSetFilter.restype = c_int |
| 139 | def awgSetFilter(slot, coeffs): |
| 140 | coeffs_array = (c_double * len(coeffs))(*coeffs) |
| 141 | return libawg.awgSetFilter(slot, coeffs_array, len(coeffs)) |
| 142 | |
| 143 | libawg.awg_cleanup.argtypes = [] |
| 144 | libawg.awg_cleanup.restype = None |
| 145 | awg_cleanup = libawg.awg_cleanup |
| 146 | |
| 147 | |
| 148 | #### |
| 149 | #### testpoint.h |
| 150 | #### |
| 151 | |
| 152 | libtestpoint.tpRequestName.argtypes = [c_char_p, c_ulonglong, |
| 153 | POINTER(c_ulonglong), POINTER(c_int)] |
| 154 | libtestpoint.tpRequestName.restype = c_int |
| 155 | def tpRequestName(tpNames, timeout, time, epoch): |
| 156 | if time is not None: |
| 157 | time_ptr = POINTER(c_ulonglong)(time) |
| 158 | else: |
| 159 | time_ptr = POINTER(c_ulonglong)() |
| 160 | if epoch is not None: |
| 161 | epoch_ptr = POINTER(c_int)(epoch) |
| 162 | else: |
| 163 | epoch_ptr = POINTER(c_int)() |
| 164 | return libtestpoint.tpRequestName(tpNames, timeout, time_ptr, epoch_ptr) |
| 165 | |
| 166 | libtestpoint.tpClearName.argtypes = [c_char_p,] |
| 167 | libtestpoint.tpClearName.restype = c_int |
| 168 | tpClearName = libtestpoint.tpClearName |
| 169 | |
| 170 | libtestpoint.testpoint_cleanup.argtypes = [] |
| 171 | libtestpoint.testpoint_cleanup.restype = None |
| 172 | testpoint_cleanup = libtestpoint.testpoint_cleanup |
| 173 | |
| 174 | |
| 175 | if libSIStr: |
| 176 | #### |
| 177 | #### SIStr.h |
| 178 | #### |
| 179 | |
| 180 | # Compile-time parameters |
| 181 | SIStr_MAXCHANNAMELENGTH = 64 |
| 182 | # Target "lead time" for sending waveform data, in NANOseconds |
| 183 | SIStr_LEADTIME = 7 * 10**9 |
| 184 | # Status codes |
| 185 | SIStr_OK = 0 |
| 186 | |
| 187 | class SIStrBuf(Structure): |
| 188 | pass |
| 189 | |
| 190 | SIStrBuf._fields_ = [('gpstime', c_int), |
| 191 | ('epoch', c_int), |
| 192 | ('iblock', c_int), |
| 193 | ('size', c_int), |
| 194 | ('ndata', c_int), |
| 195 | ('next', POINTER(SIStrBuf)), |
| 196 | ('data', POINTER(c_float))] |
| 197 | |
| 198 | class SIStream(Structure): |
| 199 | _fields_ = [('magic', c_int), |
| 200 | ('id', c_int), |
| 201 | ('channel', c_char * SIStr_MAXCHANNAMELENGTH), |
| 202 | ('samprate', c_int), |
| 203 | ('starttime', c_double), |
| 204 | ('slot', c_int), |
| 205 | ('tp', c_int), |
| 206 | ('comp', c_int), |
| 207 | ('blocksize', c_int), |
| 208 | ('nblocks', c_int), |
| 209 | ('curgps', c_int), |
| 210 | ('curepoch', c_int), |
| 211 | ('sentgps', c_int), |
| 212 | ('sentepoch', c_int), |
| 213 | ('nbufs', c_int), |
| 214 | ('curbuf', POINTER(SIStrBuf)), |
| 215 | ('firstbuf', POINTER(SIStrBuf)), |
| 216 | ('lastbuf', POINTER(SIStrBuf)), |
| 217 | ('lastsend', c_longlong), |
| 218 | ('minwait', c_longlong), |
| 219 | ('aborted', c_int)] |
| 220 | |
| 221 | libSIStr.SIStrAppInfo.argtypes = [c_char_p,] |
| 222 | libSIStr.SIStrAppInfo.restype = None |
| 223 | SIStrAppInfo = libSIStr.SIStrAppInfo |
| 224 | |
| 225 | libSIStr.SIStrOpen.argtypes = [POINTER(SIStream), c_char_p, c_int, c_double] |
| 226 | libSIStr.SIStrOpen.restype = c_int |
| 227 | SIStrOpen = libSIStr.SIStrOpen |
| 228 | |
| 229 | libSIStr.SIStrErrorMsg.argtypes = [c_int,] |
| 230 | libSIStr.SIStrErrorMsg.restype = c_char_p |
| 231 | SIStrErrorMsg = libSIStr.SIStrErrorMsg |
| 232 | |
| 233 | libSIStr.SIStrAppend.argtypes = [POINTER(SIStream), ndpointer(c_float), |
| 234 | c_int, c_float] |
| 235 | libSIStr.SIStrAppend.restype = c_int |
| 236 | def SIStrAppend(sis, data, scale): |
| 237 | data_array = array(data).astype('f') |
| 238 | return libSIStr.SIStrAppend(sis, data_array, data_array.size, scale) |
| 239 | |
| 240 | libSIStr.SIStrClose.argtypes = [POINTER(SIStream),] |
| 241 | libSIStr.SIStrClose.restype = c_int |
| 242 | SIStrClose = libSIStr.SIStrClose |
| 243 | |
| 244 | libSIStr.SIStrAbort.argtypes = [POINTER(SIStream),] |
| 245 | libSIStr.SIStrAbort.restype = c_int |
| 246 | SIStrAbort = libSIStr.SIStrAbort |