From fa32e9eac31f2f83df2ec913bbe8d37cc69bf6bd Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Sun, 18 Oct 2020 13:58:42 +0200 Subject: [PATCH] Bugfixes of Python/C++ interaction layer, first tests for DaqConfiguration code --- lasp/device/__init__.py | 1 + lasp/device/lasp_common_decls.pxd | 3 +- lasp/device/lasp_cpprtaudio.cpp | 2 +- lasp/device/lasp_daqconfig.pxd | 1 + lasp/device/lasp_daqconfig.pyx | 59 ++++++++++++++----------------- lasp/device/lasp_device_common.py | 29 +++++++++++++-- scripts/lasp_record | 7 ++-- test/test_uldaq.cpp | 2 +- 8 files changed, 62 insertions(+), 42 deletions(-) diff --git a/lasp/device/__init__.py b/lasp/device/__init__.py index f6c878b..7bd944a 100644 --- a/lasp/device/__init__.py +++ b/lasp/device/__init__.py @@ -3,4 +3,5 @@ from .lasp_device_common import * from .lasp_daq import * from .lasp_deviceinfo import * from .lasp_daqconfig import * +from .lasp_daq import * diff --git a/lasp/device/lasp_common_decls.pxd b/lasp/device/lasp_common_decls.pxd index 1332d70..ec4f583 100644 --- a/lasp/device/lasp_common_decls.pxd +++ b/lasp/device/lasp_common_decls.pxd @@ -117,7 +117,8 @@ cdef extern from "lasp_cppdaq.h" nogil: usvec inputRangeIndices - DaqConfiguration() + cppDaqConfiguration() + cppDaqConfiguration(cppDeviceInfo& devinfo) int getHighestInChannel() int getHighestOutChannel() diff --git a/lasp/device/lasp_cpprtaudio.cpp b/lasp/device/lasp_cpprtaudio.cpp index b3cc7e8..f2176e5 100644 --- a/lasp/device/lasp_cpprtaudio.cpp +++ b/lasp/device/lasp_cpprtaudio.cpp @@ -277,7 +277,7 @@ int mycallback( if(inputBuffer) { us j=0; // OUR buffer channel counter us i=0; // RtAudio channel counter - for(us ch=daq->getLowestInChannel();ch<=daq->getHighestInChannel();ch++) { + for(int ch=daq->getLowestInChannel();ch<=daq->getHighestInChannel();ch++) { if(eninchannels[ch]) { memcpy( &(inbuffercpy[monitorOffset+j*bytesperchan]), diff --git a/lasp/device/lasp_daqconfig.pxd b/lasp/device/lasp_daqconfig.pxd index 83ed5bd..60bcdeb 100644 --- a/lasp/device/lasp_daqconfig.pxd +++ b/lasp/device/lasp_daqconfig.pxd @@ -1,4 +1,5 @@ include "lasp_common_decls.pxd" +from .lasp_deviceinfo cimport DeviceInfo cdef class DaqConfiguration: cdef: diff --git a/lasp/device/lasp_daqconfig.pyx b/lasp/device/lasp_daqconfig.pyx index d11029c..be054a4 100644 --- a/lasp/device/lasp_daqconfig.pyx +++ b/lasp/device/lasp_daqconfig.pyx @@ -8,43 +8,31 @@ Description: Data Acquistiion (DAQ) device descriptors, and the DAQ devices themselves """ -from dataclasses import dataclass, field -from typing import List -from dataclasses_json import dataclass_json +__all__ = ['DaqChannel', 'DaqConfiguration'] from ..lasp_common import lasp_shelve, SIQtys, Qty - +from .lasp_device_common import DaqChannel import json -class CouplingMode: - ac = 'AC' - dc = 'DC' - undefined = 'Undefined' - -class Range: - oneV = '+/- 1 V' - tenV = '+/- 10 V' - undefined = 'Undefined' - - -@dataclass_json -@dataclass -class DaqChannel: - channel_enabled: bool - channel_name: str = 'Unnamed channel' - sensitivity: float = 1.0 - range_index: int = 0 - ACCoupling_enabled: bool = False - IEPE_enabled: bool = False - - cdef class DaqConfiguration: """ Initialize a device descriptor """ + def __init__(self): + pass @staticmethod - def from_json(self, jsonstring): + def fromDeviceInfo(DeviceInfo devinfo): + cdef: + cppDaqConfiguration cconfig + + d = DaqConfiguration() + cconfig = cppDaqConfiguration(devinfo.devinfo) + d.config = cconfig + return d + + @staticmethod + def from_json(jsonstring): config_dict = json.loads(jsonstring) return DaqConfiguration.from_dict(config_dict) @@ -58,8 +46,13 @@ cdef class DaqConfiguration: config.device_name = pydict['device_name'].encode('utf-8') config.eninchannels = pydict['eninchannels'] config.enoutchannels = pydict['enoutchannels'] - config.inchannel_names = pydict['inchannel_names'] - config.outchannel_names = pydict['outchannel_names'] + + config.inchannel_names = [inchname.encode('utf-8') for inchname in + pydict['inchannel_names']] + + config.outchannel_names = [outchname.encode('utf-8') for outchname in + pydict['outchannel_names']] + config.sampleRateIndex = pydict['sampleRateIndex'] config.framesPerBlockIndex = pydict['framesPerBlockIndex'] config.dataTypeIndex = pydict['dataTypeIndex'] @@ -75,7 +68,7 @@ cdef class DaqConfiguration: def to_json(self): return json.dumps(dict( - apicode = self.api.apicode, + apicode = self.config.api.apicode, device_name = self.config.device_name.decode('utf-8'), eninchannels = self.eninchannels(), @@ -87,7 +80,7 @@ cdef class DaqConfiguration: self.config.outchannel_names], sampleRateIndex = self.config.sampleRateIndex, dataTypeIndex = self.config.dataTypeIndex, - nFramesPerBlockIndex = self.config.framesPerBlockIndex, + framesPerBlockIndex = self.config.framesPerBlockIndex, monitorOutput = self.config.monitorOutput, inputIEPEEnabled = self.config.inputIEPEEnabled, @@ -111,7 +104,7 @@ cdef class DaqConfiguration: ) def setInChannel(self, i:int, daqchannel: DaqChannel): self.config.eninchannels[i] = daqchannel.channel_enabled - self.config.inchannel_names[i] = daqchannel.channel_name + self.config.inchannel_names[i] = daqchannel.channel_name.encode('utf-8') self.config.inchannel_sensitivities[i] = daqchannel.sensitivity self.config.inputRangeIndices[i] = daqchannel.range_index self.config.inputACCouplingMode[i] = daqchannel.ACCoupling_enabled @@ -142,7 +135,7 @@ cdef class DaqConfiguration: configs = {} for name, val in configs_json.items(): configs[name] = DaqConfiguration.from_json(val) - return configs + return configs def saveConfig(self, name): configs_json = DaqConfiguration.loadConfigsJSON() diff --git a/lasp/device/lasp_device_common.py b/lasp/device/lasp_device_common.py index 32d75d1..e4ab424 100644 --- a/lasp/device/lasp_device_common.py +++ b/lasp/device/lasp_device_common.py @@ -1,5 +1,7 @@ - -__all__ = ['AvType'] +__all__ = ['AvType', 'DaqChannel'] +from dataclasses import dataclass, field +from dataclasses_json import dataclass_json +from typing import List class AvType: """Specificying the type of data, for adding and removing callbacks from @@ -7,3 +9,26 @@ class AvType: audio_input = 1 audio_output = 2 video = 4 + +@dataclass_json +@dataclass +class DaqChannel: + channel_enabled: bool + channel_name: str = 'Unnamed channel' + sensitivity: float = 1.0 + range_index: int = 0 + ACCoupling_enabled: bool = False + IEPE_enabled: bool = False + +class CouplingMode: + ac = 'AC' + dc = 'DC' + undefined = 'Undefined' + +class Range: + oneV = '+/- 1 V' + tenV = '+/- 10 V' + undefined = 'Undefined' + + + diff --git a/scripts/lasp_record b/scripts/lasp_record index d2a4ade..0d42279 100755 --- a/scripts/lasp_record +++ b/scripts/lasp_record @@ -21,9 +21,9 @@ args = parser.parse_args() from lasp.lasp_avstream import AvStream, AvType from lasp.lasp_record import Recording -from lasp.device import DAQConfiguration, RtAudio, UlDaq +from lasp.device import DaqConfiguration, Daq, DaqChannel -configs = DAQConfiguration.loadConfigs() +configs = DaqConfiguration.loadConfigs() for i, (key, val) in enumerate(configs.items()): print(f'{i:2} : {key}') @@ -45,8 +45,7 @@ config = configs[key] print(config) # daq = RtAudio() -daq = UlDaq() -devices = daq.getDeviceInfo() +devices = Daq.getDeviceInfo() input_devices = {} for device in devices: diff --git a/test/test_uldaq.cpp b/test/test_uldaq.cpp index 9668bf6..b72b8fb 100644 --- a/test/test_uldaq.cpp +++ b/test/test_uldaq.cpp @@ -40,7 +40,7 @@ int main() { cout << "Inchannnels size: " << config.eninchannels.size() << endl; - Daq* daq = Daq::createDevice(config, devinfos); + Daq* daq = Daq::createDaq(devinfo, config); SafeQueue inqueue; SafeQueue outqueue;