<commit_before># -*- coding: utf-8 -*-
from debug import *
    
from lib.devices.Device import *
import time, traceback, sys
from protoGUI import *
#from numpy import byte
import numpy
#from scipy.signal import resample, bessel, lfilter
import scipy.signal, scipy.ndimage
import advancedTypes
from debug import *
import Mutex

class NiDAQ(Device):
    """
    Config options:
        defaultAIMode: 'mode'  # mode to use for ai channels by default ('rse', 'nrse', or 'diff')
    """
    def __init__(self, dm, config, name):
        Device.__init__(self, dm, config, name)
        self.config = config
        ## make local copy of device handle
        if config is not None and config.get('mock', False):
            from lib.drivers.nidaq.mock import NIDAQ
            self.n = NIDAQ
        else:
            from lib.drivers.nidaq.nidaq import NIDAQ
            self.n = NIDAQ
        print "Created NiDAQ handle, devices are %s" % repr(self.n.listDevices())
        self.delayedSet = Mutex.threadsafe({})
    
    def createTask(self, cmd):
        return Task(self, cmd)
        
    def setChannelValue(self, chan, value, block=False, delaySetIfBusy=False, ignoreLock=False):
        """Set a channel on this DAQ. 
        Arguments:
            block: bool. If True, wait until the device is available. 
                   If False, return immediately if the device is not available.
            delaySetIfBusy: If True and the hardware is currently reserved, then
                            schedule the set to occur immediately when the hardware becomes available again.
            ignoreLock: attempt to set the channel value even if the device is reserved.
        Returns True if the channel was set, False otherwise.
        """
        #print "Setting channel %s to %f" % (chan, value)
        if ignoreLock:
            res = True
        else:
            res = self.reserve(block=block)
            
        if not block and not res:
            if delaySetIfBusy:
                #print "  busy, schedule for later."
                self.delayedSet[chan] = value
            return False
        
        try:
            if 'ao' in chan:
                self.n.writeAnalogSample(chan, value)
            else:
                if value is True or value == 1:
                    value = 0xFFFFFFFF
                else:
                    value = 0
                self.n.writeDigitalSample(chan, value)
        except:
            printExc("Error while setting channel %s to %s:" % (chan, str(value)))
            raise
        finally:
            if not ignoreLock:
                self.release()
        return True
        
    def release(self):
        ## take care of any channel-value-set requests that arrived while the device was locked
        try:
            self.delayedSet.lock()
            for chan, val in self.delayedSet.iteritems():
                #print "Set delayed:", chan, val
                self.setChannelValue(chan, val, ignoreLock=True)
            self.delayedSet.clear()
        finally:
            self.delayedSet.unlock()
        return Device.release(self)

    def getChannelValue(self, chan, mode=None, block=True):
        res = self.reserve(block=block)
        if not res:  ## False means non-blocking lock attempt failed.
            return False
        #print "Setting channel %s to %f" % (chan, value)
        try:
            if 'ai' in chan:
                val = self.n.readAnalogSample(chan, mode=mode)
            else:
                val = self.n.readDigitalSample(chan)
                if val <= 0:
                    val = 0
                else:
                    val = 1
        except:
            printExc("Error while getting channel value %s:" % str(chan))
            raise<commit_msg>bugfix - nidaq getChannelValue uses incorrect mode in some cases.<commit_after># -*- coding: utf-8 -*-
from debug import *
    
from lib.devices.Device import *
import time, traceback, sys
from protoGUI import *
#from numpy import byte
import numpy
#from scipy.signal import resample, bessel, lfilter
import scipy.signal, scipy.ndimage
import advancedTypes
from debug import *
import Mutex

class NiDAQ(Device):
    """
    Config options:
        defaultAIMode: 'mode'  # mode to use for ai channels by default ('rse', 'nrse', or 'diff')
    """
    def __init__(self, dm, config, name):
        Device.__init__(self, dm, config, name)
        self.config = config
        ## make local copy of device handle
        if config is not None and config.get('mock', False):
            from lib.drivers.nidaq.mock import NIDAQ
            self.n = NIDAQ
        else:
            from lib.drivers.nidaq.nidaq import NIDAQ
            self.n = NIDAQ
        print "Created NiDAQ handle, devices are %s" % repr(self.n.listDevices())
        self.delayedSet = Mutex.threadsafe({})
    
    def createTask(self, cmd):
        return Task(self, cmd)
        
    def setChannelValue(self, chan, value, block=False, delaySetIfBusy=False, ignoreLock=False):
        """Set a channel on this DAQ. 
        Arguments:
            block: bool. If True, wait until the device is available. 
                   If False, return immediately if the device is not available.
            delaySetIfBusy: If True and the hardware is currently reserved, then
                            schedule the set to occur immediately when the hardware becomes available again.
            ignoreLock: attempt to set the channel value even if the device is reserved.
        Returns True if the channel was set, False otherwise.
        """
        #print "Setting channel %s to %f" % (chan, value)
        if ignoreLock:
            res = True
        else:
            res = self.reserve(block=block)
            
        if not block and not res:
            if delaySetIfBusy:
                #print "  busy, schedule for later."
                self.delayedSet[chan] = value
            return False
        
        try:
            if 'ao' in chan:
                self.n.writeAnalogSample(chan, value)
            else:
                if value is True or value == 1:
                    value = 0xFFFFFFFF
                else:
                    value = 0
                self.n.writeDigitalSample(chan, value)
        except:
            printExc("Error while setting channel %s to %s:" % (chan, str(value)))
            raise
        finally:
            if not ignoreLock:
                self.release()
        return True
        
    def release(self):
        ## take care of any channel-value-set requests that arrived while the device was locked
        try:
            self.delayedSet.lock()
            for chan, val in self.delayedSet.iteritems():
                #print "Set delayed:", chan, val
                self.setChannelValue(chan, val, ignoreLock=True)
            self.delayedSet.clear()
        finally:
            self.delayedSet.unlock()
        return Device.release(self)

    def getChannelValue(self, chan, mode=None, block=True):
        if mode is None:
            mode = self.config.get('defaultAIMode', None)
        
        res = self.reserve(block=block)
        if not res:  ## False means non-blocking lock attempt failed.
            return False
        #print "Setting channel %s to %f" % (chan, value)
        try:
            if 'ai' in chan:
                val = self.n.readAnalogSample(chan, mode=mode)
            else:
                val = self.n.readDigitalSample(chan)
                if val <= 0:
                    val = 0
                else:
                    val = 1
        except:
            printExc("Error while getting channel value %s:" % str(chan))
            raise
{'fraction_non_alphanumeric': 0.08283485045513654, 'fraction_numerical': 0.0018205461638491547, 'mean_word_length': 1.6863429968564443, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5255741', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2020, 'n_words': 712}