Skip to content

Commit

Permalink
Merge pull request #1 from microdaq/dev
Browse files Browse the repository at this point in the history
PyMLink 1.1v
  • Loading branch information
witczenko committed Nov 13, 2017
2 parents 6994822 + 5f201ba commit 2cb0e17
Show file tree
Hide file tree
Showing 10 changed files with 449 additions and 297 deletions.
26 changes: 9 additions & 17 deletions demos/ai-demo.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
# Analog inputs demo
# Analog input demo
# visit site www.microdaq.org
# author Witczenko
# email witczenko@gmail.com
# Embedded-solutions, November 2017

from py_mlink import PyMLink

try:
# Create MLink object, connect to MicroDAQ device
pml = PyMLink.MLink('10.10.1.1')
# Create MLink object, connect to MicroDAQ device
mdaq = PyMLink.MLink('10.10.1.1')

# Choose channels to read eg. 1..8
channels = [ch for ch in range(1, 9)]
# Read data from channels 1..4, input range from -10V to 10V, single ended
data = mdaq.ai_read([1, 2, 3, 4], [-10, 10], False)

# Read data
data = pml.ai_read(channels)
# Print data
for i, volt in enumerate(data):
print 'Channel[%d]: %f V' % (i, volt)

# Print data
i = 0
for ch in channels:
print 'Channel[%d]: %f V' % (ch, data[i])
i += 1

except PyMLink.MLinkError, errval:
print "Error:", errval

24 changes: 10 additions & 14 deletions demos/ao-demo.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# Analog outputs demo
# Analog output demo
# visit site www.microdaq.org
# author Witczenko
# email witczenko@gmail.com
# Embedded-solutions, November 2017

from py_mlink import PyMLink

try:
# Create MLink object, connect to MicroDAQ device
pml = PyMLink.MLink('10.10.1.1')
# Create MLink object, connect to MicroDAQ device
mdaq = PyMLink.MLink('10.10.1.1')

# Choose channels to write eg. 1..4
channels = [ch for ch in range(1, 5)]
# Set values
channels_voltage = [0.5, 1, 1.5, 2]
# Choose channels to write eg. 1..4
ch = [1, 2, 3, 4]
# Set values
ch_voltage = [0.5, 1, 1.5, 2]

# Set analog outputs
pml.ao_write(channels, channels_voltage)
# Set analog outputs, output range from 0V to 5V
mdaq.ao_write(ch, [0, 5], ch_voltage)

except PyMLink.MLinkError, errval:
print "Error:", errval

59 changes: 24 additions & 35 deletions demos/dio-demo.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
# Digital I/O demo
# visit site www.microdaq.org
# author Witczenko
# email witczenko@gmail.com
# Embedded-solutions, November 2017

from py_mlink import PyMLink

try:
# Create MLink object, connect to MicroDAQ device
pml = PyMLink.MLink('10.10.1.1')

# Configure Digital I/O
# Disable all functions: encoder, pwm, uart
for i in range(1, 7):
pml.dio_func(i, False)

# Set first 8 I/O to input mode
pml.dio_dir(1, False)
# Set next 8 I/O to output mode
pml.dio_dir(2, True)

# Choose channels to read eg. 1..8
channels_in = [ch for ch in range(1, 9)]
# Choose channels to write eg. 9..16
channels_out = [ch for ch in range(9, 17)]
channels_out_data = [True for state in range(8)]

# Read data
data_in = pml.dio_read(channels_in)
# Set data
pml.dio_write(channels_out, channels_out_data)

# Print data
i = 0
for ch in channels_in:
print 'DI[%d]: %d' % (ch, data_in[i])
i += 1

except PyMLink.MLinkError, errval:
print "Error:", errval
# Create MLink object, connect to MicroDAQ device
mdaq = PyMLink.MLink('10.10.1.2')

# Configure Digital I/O, disable all functions: encoder, pwm, uart
for i in range(1, 7):
mdaq.dio_func(i, False)

# Choose channels to be digital input
DI = [1, 2, 3, 4, 5, 6, 7, 8]

# Choose channels to be digital output
DO = [9, 10, 11, 12, 13, 14, 15, 16]
DO_state = [True for state in range(8)]

# Read DI states
di_state = mdaq.dio_read(DI)
# Set DO states
mdaq.dio_write(DO, DO_state)

# Print data
for i, di in enumerate(di_state):
print 'DI[%d]: %d' % (i, di)

30 changes: 13 additions & 17 deletions demos/led-demo.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
# LED demo
# visit site www.microdaq.org
# author Witczenko
# email witczenko@gmail.com
# Embedded-solutions, November 2017

from py_mlink import PyMLink
import time

try:
# Create MLink object, connect to MicroDAQ device
pml = PyMLink.MLink('10.10.1.1')
# Create MLink object, connect to MicroDAQ device
mdaq = PyMLink.MLink('10.10.1.1')

# turn on LED 1
pml.led_write(1, True)
# turn on LED 2
pml.led_write(2, True)
# turn on LED 1
mdaq.led_write(1, True)
# turn on LED 2
mdaq.led_write(2, True)

# wait a second
time.sleep(1.0)
# wait a second
time.sleep(1.0)

# turn off LED 1
pml.led_write(1, False)
# turn off LED 2
pml.led_write(2, False)
# turn off LED 1
mdaq.led_write(1, False)
# turn off LED 2
mdaq.led_write(2, False)

except PyMLink.MLinkError, errval:
print "Error:", errval

13 changes: 6 additions & 7 deletions demos/scope-demo.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
# Scope demo
# visit site www.microdaq.org
# author Witczenko
# email witczenko@gmail.com
# Embedded-solutions, November 2017

from py_mlink import PyMLink
try:
import pyqtgraph as pg
except ImportError:
print 'To run this demo you have to install pyqtgraph and (PyQt4/5 or PySide).'

DATA_COUNT = 500
SAMPLE_RATE_HZ = 10000
DATA_COUNT = 5000
SAMPLE_RATE_HZ = 100000
DURATION_SEC = 60
CHANNEL = 1

# Create MLink object, connect to MicroDAQ device
pml = PyMLink.MLink('10.10.1.1')
mdaq = PyMLink.MLink('10.10.1.1')

# Init analog input scan
pml.ai_scan_init(CHANNEL, SAMPLE_RATE_HZ, DURATION_SEC, PyMLink.AIRange.AI_5V)
mdaq.ai_scan_init(CHANNEL, PyMLink.AIRange.AI_5V, False, SAMPLE_RATE_HZ, DURATION_SEC)

# Create plot with pyqtgraph
win = pg.GraphicsWindow(title='Scope demo')
Expand All @@ -31,7 +30,7 @@

print 'Acquiring data...'
for i in range((DURATION_SEC*SAMPLE_RATE_HZ)/DATA_COUNT):
data = pml.ai_scan(DATA_COUNT, True)
data = mdaq.ai_scan(DATA_COUNT, True)
plot.setData(x, data[0])
pg.QtGui.QApplication.processEvents()

Expand Down
18 changes: 9 additions & 9 deletions demos/scope-fft-demo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Scope FFT - demo
# Scope FFT demo
# visit site www.microdaq.org
# author Witczenko
# email witczenko@gmail.com
# Embedded-solutions, November 2017

from py_mlink import PyMLink
import numpy.fft as npfft
Expand All @@ -17,9 +16,9 @@
QtCore.qInstallMsgHandler(lambda *args: None)

# Params
DATA_COUNT = 500
SAMPLE_RATE_HZ = 10000
DURATION_SEC = 60*5
DATA_COUNT = 10000
SAMPLE_RATE_HZ = 100000
DURATION_SEC = 60
CHANNEL = 1

# Create plot with pyqtgraph
Expand All @@ -41,16 +40,17 @@


# Create MLink object, connect to MicroDAQ device
pml = PyMLink.MLink('10.10.1.1', connectionless=False)
mdaq = PyMLink.MLink('10.10.1.1')
# Init analog input scan
pml.ai_scan_init(CHANNEL, SAMPLE_RATE_HZ, DURATION_SEC, PyMLink.AIRange.AI_5V)
mdaq.ai_scan_init(CHANNEL, PyMLink.AIRange.AI_5V, False, SAMPLE_RATE_HZ, DURATION_SEC)

print 'Acquiring data...'
for i in range((DURATION_SEC*SAMPLE_RATE_HZ)/DATA_COUNT):
# Get AI data
data = pml.ai_scan(DATA_COUNT, True)
data = mdaq.ai_scan(DATA_COUNT, True)

# Calc FFT and draw plots
data = data - np.mean(data)
y = np.array(abs(npfft.fft(data[0])/DATA_COUNT))
y = y[0:(DATA_COUNT/2)]*2.0
p_fft_handle.setData(xf, y)
Expand Down
26 changes: 26 additions & 0 deletions demos/utils-demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Utils demo
# visit site www.microdaq.org
# Embedded-solutions, November 2017

from py_mlink import PyMLink

# Print documentation e.g:
print PyMLink.MLink.__doc__
print PyMLink.MLink.ai_scan_init.__doc__

# Connect to MicroDAQ device without worrying about connection timeout (maintain_connection=True)
# this option has slightly less performance
mdaq = PyMLink.MLink('10.10.1.1', maintain_connection=True)

# Print model name of connected MicroDAQ device
mdaq.hw_info()

# Catch MLink errors
try:
mdaq.ai_read(99, [-10, 10], False)
except PyMLink.MLinkError, errval:
print "Error:", errval

# Close connection
mdaq.disconnect()

0 comments on commit 2cb0e17

Please sign in to comment.