import ctypes as ct
import daqparam as dqp
import mascara_config as cfg
import os
if os.sys.platform == 'windows':
mccdaq = ct.windll.cbw32
else:
print "Wrong Operating system!!! There are no dll on a linux machine!"
[docs]class DAQ():
''' mccdaq is a class for connecting with and reading the USB-1208FS+ data
acquisition board.
The functions defined here are the main functions embedded in the cbw32.dll
'''
[docs] def __init__(self, BoardNum=0, gain=1):
'''
The DAQ instance regroups all functions to communicate with the Data
Acquisition board. The Board is defined by a board number (default 0).
The gain is programmable but set to 1 per default.
'''
self.BoardNum = BoardNum
self.status = mccdaq.cbErrHandling(dqp.DONTPRINT, dqp.DONTSTOP)
self.gain = gain
## configure both ports as output
self.configPorts()
## set all bits to 0
self.dAllOut(1)
#self.channel = channel
[docs] def getBoardName(self):
''' Gives the name of the DAQ'''
name = ct.create_string_buffer(20)
mccdaq.cbGetBoardName(self.BoardNum, ct.byref(name))
self.name = name.value
[docs] def configPorts(self, inout='out'):
''' ConfigPort configurates the digital ports for Output or input.
Keyword:
- inout, if dqp.DIGITALOUT, the port delivers a voltage
if dqp.DIGITALIN, the port read the voltage
'''
if inout.lower() == 'out':
mccdaq.cbDConfigPort(self.BoardNum, dqp.FIRSTPORTA, dqp.DIGITALOUT)
mccdaq.cbDConfigPort(self.BoardNum, dqp.FIRSTPORTB, dqp.DIGITALOUT)
elif inout.lower() == 'in':
mccdaq.cbDConfigPort(self.BoardNum, dqp.FIRSTPORTA, dqp.DIGITALIN)
mccdaq.cbDConfigPort(self.BoardNum, dqp.FIRSTPORTB, dqp.DIGITALIN)
[docs] def dBitOut(self, channel, bitValue, port=cfg.portDAQA):
'''
Sets the state of a single digital output bit. This function treats all
the DI/O ports of a particular type on the board as a single large port.
The port should be configured as output beforehand.
Inputs:
- port, the port considered ('A' or 'B')
- channel, the channel considered (0-7)
- bitValue, if 0 logic output low (~0V) if 1 logic input high (~5V)
'''
if port ==cfg.portDAQB:
self.status = mccdaq.cbDBitOut(self.BoardNum, dqp.FIRSTPORTB, channel, bitValue)
else:
self.status = mccdaq.cbDBitOut(self.BoardNum, dqp.FIRSTPORTA, channel, bitValue)
[docs] def dOut(self, byteValue, port=cfg.portDAQA):
'''
Sets the state of all digital output bits of a given port. This function treats all
the DI/O ports of a particular type on the board as a single large port.
The port should be configured as output beforehand.
Inputs:
- port, the port considered ('A' or 'B')
- byteValue, value of the byte (0-255)
'''
if byteValue < 0 or byteValue > 255:
raise ValueError('The bit must be between 0 and 255')
if port == cfg.portDAQB:
self.status = mccdaq.cbDOut(self.BoardNum, dqp.FIRSTPORTB, byteValue)
else:
self.status = mccdaq.cbDOut(self.BoardNum, dqp.FIRSTPORTA, byteValue)
[docs] def dAllOut(self, bitValue):
'''
Sets the state of all digital output bits. This function treats all
the DI/O ports of a particular type on the board as a single large port.
The port should be configured as output beforehand.
Inputs:
- port, the port considered ('A' or 'B')
- bitValue, value of the byte (0-1)
'''
if bitValue == 0:
self.dOut(0,port=cfg.portDAQA)
self.dOut(0,port=cfg.portDAQB)
elif bitValue == 1:
self.dOut(255,port=cfg.portDAQA)
self.dOut(255,port=cfg.portDAQB)
else:
raise ValueError('The bit value can only be 0 or 1')
[docs] def dBitIn(self, channel, port=cfg.portDAQA):
'''
Reads the state of a single digital input bit.
Inputs:
- channel, the bit to read
Outputs:
- The value of the bit.
If 1, logical high => ~ 5V
If 0, logical low, => ~0V
'''
bit = ct.c_ushort()
if port == cfg.portDAQB:
self.status = mccdaq.cbDBitIn(self.BoardNum, dqp.FIRSTPORTB, channel, ct.byref(bit))
else:
self.status = mccdaq.cbDBitIn(self.BoardNum, dqp.FIRSTPORTA, channel, ct.byref(bit))
return bit.value
[docs] def vIn(self, channel):
''' Reads an A/D input channel and returns a voltage value.
The voltage is returned to data
Inputs: Channel, the A/D channel number
Outputs: the voltage
'''
dat = ct.c_float()
self.status = mccdaq.cbVIn(self.BoardNum, channel, self.gain, ct.byref(dat),0)
return dat.value ## We force the value to be positive untill we understand why on
## one computer it is positive and negative in the other one??
[docs] def vOut(self, channel, voltage):
''' Sets the value of a D/A output.
Inputs: channel, channel of the analog output (0-1)
voltage, the desired voltage (0.0-5.0)
'''
if voltage < 0 or voltage > 5.0:
raise ValueError('The voltage must be between 0 and 5V')
nBits = 12
maxVal = 2**nBits-1
self.status = mccdaq.cbAOut(self.BoardNum, channel, dqp.UNI5VOLTS,int(round(maxVal*voltage/5.0)))
[docs] def flashLED(self):
''' Flashes the led of the daq'''
self.status = mccdaq.cbFlashLED(self.BoardNum)