Jump to content
  • 0

waveforms SDK adding custom pattern to digital discovery


menach_g

Question

I'm trying to use output a custom pattern contained in csv format out of a digital discovery.

I succeeded doing so using the program and adding manually, now I want to do this in python using the sdk.

I can see the function I need to use is "FDwfDigitalOutDataSet" but I don't seem to get it working.

the only example on custom data is on the analog discovery and it doesn't seem to be the same.
 

here is the code I try running

from ctypes import *
from dwfconstants import *
import math
import time
import matplotlib.pyplot as plt
import sys


dwf = cdll.dwf

hdwf = c_int()
sts = c_byte()

version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: " + str(version.value))

print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

if hdwf.value == 0:
    print("failed to open device")
    szerr = create_string_buffer(512)
    dwf.FDwfGetLastErrorMsg(szerr)
    print(str(szerr.value))
    quit()

hzSys = c_double()
dwf.FDwfDigitalOutInternalClockInfo(hdwf, byref(hzSys))
print("Internal frequency is " + str(hzSys.value / 1e6) + " MHz")

# 100kHz counter rate, SystemFrequency/100kHz
cntFreq = c_uint(int(hzSys.value / 10e6))


data_py=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]

data=(c_int*len(data_py))()

# for i in range(0,len(data)):
#     data[i] = 1.0*i/len(data_py);
for i in range(len(data_py)):
    data[i]=c_int( data_py[i])

pin=0
# generate pattern
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1))
dwf.FDwfDigitalOutOutputSet(hdwf, c_int(pin),c_int(3))
dwf.FDwfDigitalOutDataSet(hdwf,c_int(pin),data,c_int(len(data)))




dwf.FDwfDigitalOutConfigure(hdwf, c_int(pin),c_int(1))

print("Generating pattern...")
time.sleep(10)

dwf.FDwfDigitalOutReset(hdwf)
dwf.FDwfDeviceCloseAll()

if anyone have an example of a working patterns code using custom pattern is would be much appreciated

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

Hi @menach_g

1. The FDwfDigitalOutDataSet expects of bits. See the code below. 

2. With FDwfDigitalOutOutputSet(.. .3) did you wanted ThreeState ?
In this case the bits will represent: value, output-enable, value....

3. No need for pin argument in FDwfDigitalOutConfigure

4. You forgot to specify the frequency with FDwfDigitalOutDividerSet

See the WF SDK manual and examples. 

image.png.8108e56854a92a9b191d7fbe5a95927a.png

from ctypes import *
from dwfconstants import *
import sys
import time

if sys.platform.startswith("win"):
    dwf = cdll.dwf
elif sys.platform.startswith("darwin"):
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
    dwf = cdll.LoadLibrary("libdwf.so")

hdwf = c_int()

version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))


print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

if hdwf.value == 0:
    print("failed to open device")
    szerr = create_string_buffer(512)
    dwf.FDwfGetLastErrorMsg(szerr)
    print(str(szerr.value))
    quit()

print("Configuring Digital Out")

hzSys = c_double()
dwf.FDwfDigitalOutInternalClockInfo(hdwf, byref(hzSys))


data_py=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]

# how many bytes we need to fit this many bits, (+7)/8
rgbdata=(c_ubyte*((len(data_py)+7)>>3))(0) 

# array to bits in byte array
for i in range(len(data_py)):
    if data_py[i] != 0:
        rgbdata[i>>3] |= 1<<(i&7)
        
        
pin=0
# generate pattern
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1))
dwf.FDwfDigitalOutTypeSet(hdwf, c_int(pin), DwfDigitalOutTypeCustom)
# 100kHz sample rate
dwf.FDwfDigitalOutDividerSet(hdwf, c_int(pin), c_int(int(hzSys.value/100e3))) # set sample rate
dwf.FDwfDigitalOutDataSet(hdwf, c_int(pin), byref(rgbdata), c_int(len(data_py)))


print("Generating pattern...")
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

time.sleep(10)

dwf.FDwfDigitalOutReset(hdwf)
dwf.FDwfDeviceCloseAll()

 

Link to comment
Share on other sites

16 hours ago, attila said:

Hi @menach_g

1. The FDwfDigitalOutDataSet expects of bits. See the code below. 

2. With FDwfDigitalOutOutputSet(.. .3) did you wanted ThreeState ?
In this case the bits will represent: value, output-enable, value....

3. No need for pin argument in FDwfDigitalOutConfigure

4. You forgot to specify the frequency with FDwfDigitalOutDividerSet

See the WF SDK manual and examples. 

image.png.8108e56854a92a9b191d7fbe5a95927a.png


from ctypes import *
from dwfconstants import *
import sys
import time

if sys.platform.startswith("win"):
    dwf = cdll.dwf
elif sys.platform.startswith("darwin"):
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
    dwf = cdll.LoadLibrary("libdwf.so")

hdwf = c_int()

version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))


print("Opening first device")
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

if hdwf.value == 0:
    print("failed to open device")
    szerr = create_string_buffer(512)
    dwf.FDwfGetLastErrorMsg(szerr)
    print(str(szerr.value))
    quit()

print("Configuring Digital Out")

hzSys = c_double()
dwf.FDwfDigitalOutInternalClockInfo(hdwf, byref(hzSys))


data_py=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]

# how many bytes we need to fit this many bits, (+7)/8
rgbdata=(c_ubyte*((len(data_py)+7)>>3))(0) 

# array to bits in byte array
for i in range(len(data_py)):
    if data_py[i] != 0:
        rgbdata[i>>3] |= 1<<(i&7)
        
        
pin=0
# generate pattern
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(pin), c_int(1))
dwf.FDwfDigitalOutTypeSet(hdwf, c_int(pin), DwfDigitalOutTypeCustom)
# 100kHz sample rate
dwf.FDwfDigitalOutDividerSet(hdwf, c_int(pin), c_int(int(hzSys.value/100e3))) # set sample rate
dwf.FDwfDigitalOutDataSet(hdwf, c_int(pin), byref(rgbdata), c_int(len(data_py)))


print("Generating pattern...")
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

time.sleep(10)

dwf.FDwfDigitalOutReset(hdwf)
dwf.FDwfDeviceCloseAll()

 

thank you very much
I finally got it working since then using the example "DigitalOut_Pins.py" 
do you know if there is a way to input custom data not in byte list format but as a list of bits?

also I can't seem to find a command to set the output volatge like in wf program?
I tried "FDwfAnalogImpedanceAmplitudeSet" which is the closest thing I found, but it doesn't change the output volatge.
 I need it to be set to 1.2v instead of 3.3v.
thnks for the help!

Link to comment
Share on other sites

 

yes it works for 1.2v thank you very much!
and I need to output more complicated signals than a simple clock
do you know of a way to input the custom data as bit list and not byte list?(yes I know I can add a function to format it that way nut I want to avoid that as I'm dealing with long signals)

Link to comment
Share on other sites

On 9/3/2019 at 4:00 PM, attila said:

The conversion is needed. The FDwfDigitalOutDataSet only accepts such compact custom data arrays.
The Digital Discovery digital-out-custom supports up to 32.768 bit-samples / channel (4kB)

hey @attila 
thanks for all the great help

I got the custom output setup fully working for my implemntation
but now I want to add digitaln reading back using din pins
I want the recording to be in sync with one of the io output channels which is a clock
I got working in wf program like so:

image.png.7acb337013b9334d50914aa1cc5e666f.png

but when I did not find the equivalent functions in the sdk documentation

I tried using snippets of code from "DigitalIn_Sync.py" nut it just goes into infinite loop with "cAvailable.value" == 0 every time..(I would add my code here but some parts like the custom setup are too long for simple code snippets)

do you know how to set up a simple input in sync with custom output in python sdk
your help will be greatly appreciated!!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...