From e686cc2c9c66a7e7e05a312d45de370f0a5e2a67 Mon Sep 17 00:00:00 2001 From: "kevin.larke" Date: Fri, 21 Feb 2020 15:41:24 -0500 Subject: [PATCH] cmAudDsp.h/c, cmAudioSys.h/c, cmDspSys.h/c, cmDspNet.h : Added support for serial port. --- cmAudDsp.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- cmAudDsp.h | 2 ++ cmAudioSys.c | 30 +++++++++++++++++++++++++ cmAudioSys.h | 2 ++ dsp/cmDspNet.h | 1 + dsp/cmDspSys.c | 17 ++++++++++---- dsp/cmDspSys.h | 3 ++- 7 files changed, 108 insertions(+), 7 deletions(-) diff --git a/cmAudDsp.c b/cmAudDsp.c index 654a1cc..85eded6 100644 --- a/cmAudDsp.c +++ b/cmAudDsp.c @@ -19,6 +19,7 @@ #include "cmApBuf.h" #include "cmMidi.h" #include "cmMidiPort.h" +#include "cmSerialPort.h" #include "dsp/cmDspValue.h" #include "cmMsgProtocol.h" #include "cmThread.h" @@ -91,6 +92,12 @@ typedef struct unsigned msgsPerClientPoll; const cmChar_t* dfltProgramLabel; + char* serialDeviceStr; + unsigned serialBaud; + unsigned serialCfgFlags; + unsigned serialPollPeriodMs; + cmSeH_t serialPortH; + cmAdAggDev_t* aggDevArray; unsigned aggDevCnt; @@ -169,6 +176,7 @@ cmAdRC_t _cmAdParseSysJsonTree( cmAd_t* p ) cmJsonNode_t* nrtDevArrNodePtr = NULL; cmJsonNode_t* afpDevArrNodePtr = NULL; cmJsonNode_t* audDspNodePtr = NULL; + cmJsonNode_t* serialNodePtr = NULL; const cmChar_t* errLabelPtr = NULL; unsigned i; cmJsRC_t jsRC = kOkJsRC; @@ -190,12 +198,28 @@ cmAdRC_t _cmAdParseSysJsonTree( cmAd_t* p ) "aggDevArray", kArrayTId | kOptArgJsFl, &aggDevArrNodePtr, "nrtDevArray", kArrayTId | kOptArgJsFl, &nrtDevArrNodePtr, "afpDevArray", kArrayTId | kOptArgJsFl, &afpDevArrNodePtr, + "serial", kObjectTId | kOptArgJsFl, &serialNodePtr, NULL )) != kOkJsRC ) { rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, "aud_dsp" ); goto errLabel; } + // parse the serial port cfg + if( serialNodePtr != NULL ) + { + if(( jsRC = cmJsonMemberValues( serialNodePtr, &errLabelPtr, + "device", kStringTId, &p->serialDeviceStr, + "baud", kIntTId, &p->serialBaud, + "flags", kIntTId, &p->serialCfgFlags, + "pollPeriodMs", kIntTId, &p->serialPollPeriodMs, + NULL )) != kOkJsRC ) + { + rc = _cmAdParseMemberErr(p, jsRC, errLabelPtr, "serial" ); + goto errLabel; + } + } + // parse the aggregate device specifications into p->aggDevArray[]. if( aggDevArrNodePtr != NULL && (p->aggDevCnt = cmJsonChildCount(aggDevArrNodePtr)) > 0) { @@ -382,6 +406,22 @@ cmAdRC_t _cmAdSetup( cmAd_t* p ) return rc; } +cmAdRC_t _cmAdCreateSerialPort( cmAd_t* p ) +{ + cmAdRC_t rc = kOkAdRC; + + if( p->serialDeviceStr != NULL ) + { + p->serialPortH = cmSeCreate( &p->ctx, &p->serialPortH, p->serialDeviceStr, p->serialBaud, p->serialCfgFlags, NULL, NULL, p->serialPollPeriodMs ); + + if( !cmSeIsOpen(p->serialPortH) ) + { + rc = cmErrMsg(&p->err,kSerialDevCreateFailAdRC,"The serial device '%s' creation failed.",cmStringNullGuard(p->serialDeviceStr)); + } + } + return rc; +} + cmAdRC_t _cmAdCreateAggDevices( cmAd_t* p ) { cmAdRC_t rc = kOkAdRC; @@ -570,6 +610,15 @@ cmAdRC_t _cmAudDspFree( cmAd_t* p ) goto errLabel; } + if( cmSeIsOpen(p->serialPortH) ) + { + if( cmSeDestroy(&p->serialPortH) != kOkSeRC ) + { + rc = cmErrMsg(&p->err,kSerialPortFailAdRC,"Serial port finalize failed."); + goto errLabel; + } + } + if( cmMpIsInitialized() ) if( cmMpFinalize() != kOkMpRC ) { @@ -695,6 +744,13 @@ cmAdRC_t cmAudDspAlloc( cmCtx_t* ctx, cmAdH_t* hp, cmMsgSendFuncPtr_t cbFunc, vo goto errLabel; } + // create the serial port + if( _cmAdCreateSerialPort(p) != kOkAdRC ) + { + rc = cmErrMsg(&p->err,kSerialPortFailAdRC,"The MIDI system initialization failed."); + goto errLabel; + } + // initialize the MIDI system if( cmMpInitialize(ctx,NULL,NULL,p->midiPortBufByteCnt,"app") != kOkMpRC ) { @@ -713,7 +769,7 @@ cmAdRC_t cmAudDspAlloc( cmCtx_t* ctx, cmAdH_t* hp, cmMsgSendFuncPtr_t cbFunc, vo goto errLabel; // initialize the DSP system - if( cmDspSysInitialize(ctx,&p->dsH,p->netH) ) + if( cmDspSysInitialize(ctx,&p->dsH,p->netH,p->serialPortH) ) { rc = cmErrMsg(&p->err,kDspSysFailAdRC,"The DSP system initialization failed."); goto errLabel; @@ -1143,7 +1199,7 @@ cmAdRC_t _cmAudDspLoadAudioSys( cmAd_t* p, unsigned asCfgIdx ) { // ... and allocate additional DSP systems when more than one sub-sys is // defined in the audio system configuration - if( cmDspSysInitialize(&p->ctx,&dsH,p->netH) != kOkDspRC ) + if( cmDspSysInitialize(&p->ctx,&dsH,p->netH,p->serialPortH) != kOkDspRC ) { rc = cmErrMsg(&p->err,kDspSysFailAdRC,"Unable to initialize an additional DSP system."); goto errLabel; diff --git a/cmAudDsp.h b/cmAudDsp.h index f33077f..152c94e 100644 --- a/cmAudDsp.h +++ b/cmAudDsp.h @@ -13,6 +13,7 @@ extern "C" { kAudioPortFailAdRC, kAudioSysFailAdRC, kMidiSysFailAdRC, + kSerialPortFailAdRC, kDspSysFailAdRC, kFileSysFailAdRC, kJsonFailAdRC, @@ -21,6 +22,7 @@ extern "C" { kNoPgmLoadedAdRC, kInvalidSubSysIdxAdRC, kUnknownMsgTypeAdRC, + kSerialDevCreateFailAdRC, kAggDevSysFailAdRC, kAggDevCreateFailAdRC, kNrtDevSysFailAdRC, diff --git a/cmAudioSys.c b/cmAudioSys.c index 28c3761..d819ae6 100644 --- a/cmAudioSys.c +++ b/cmAudioSys.c @@ -15,6 +15,7 @@ #include "cmThread.h" #include "cmUdpPort.h" #include "cmUdpNet.h" +#include "cmSerialPort.h" #include "cmAudioSysMsg.h" #include "cmAudioSys.h" #include "cmMidi.h" @@ -37,6 +38,7 @@ typedef struct cmTsMp1cH_t htdQueueH; // host-to-dsp thread safe msg queue cmThreadMutexH_t engMutexH; // thread mutex and condition variable cmUdpNetH_t netH; + cmSeH_t serialPortH; bool enableFl; // application controlled pause flag bool runFl; // false during finalization otherwise true bool statusFl; // true if regular status notifications should be sent @@ -507,6 +509,13 @@ void _cmAudioSysMidiCallback( const cmMidiPacket_t* pktArray, unsigned pktCnt ) } +void _cmAudioSysSerialPortCallback( void* cbArg, const void* byteA, unsigned byteN ) +{ + //_cmAsCfg_t* p (_cmAsCfg_t*)cbArg; + + // TODO: handle serial receive +} + cmAsRC_t cmAudioSysAllocate( cmAudioSysH_t* hp, cmRpt_t* rpt, const cmAudioSysCfg_t* cfg ) { cmAsRC_t rc; @@ -832,6 +841,7 @@ cmAsRC_t cmAudioSysInitialize( cmAudioSysH_t h, const cmAudioSysCfg_t* cfg ) cp->iMeterArray = cmMemAllocZ( double, cp->status.iMeterCnt ); cp->oMeterArray = cmMemAllocZ( double, cp->status.oMeterCnt ); cp->netH = cfg->netH; + cp->serialPortH = cfg->serialPortH; // create the audio System thread if((rc = cmThreadCreate( &cp->threadH, _cmAsThreadCallback, cp, ss->args.rpt )) != kOkThRC ) @@ -874,6 +884,16 @@ cmAsRC_t cmAudioSysInitialize( cmAudioSysH_t h, const cmAudioSysCfg_t* cfg ) goto errLabel; } + // install the serial port + if( cmSeIsOpen(cp->serialPortH) ) + { + if( cmSeSetCallback(cp->serialPortH, _cmAudioSysSerialPortCallback, cp ) != kOkSeRC ) + { + rc = _cmAsError(p,kSerialPortFailAsRC,"Serial port callback installation failed."); + goto errLabel; + } + } + // setup the sub-system status notification cp->statusUpdateSmpCnt = floor(cmApBufMeterMs() * cp->ss.args.srate / 1000.0 ); cp->statusUpdateSmpIdx = 0; @@ -886,6 +906,16 @@ cmAsRC_t cmAudioSysInitialize( cmAudioSysH_t h, const cmAudioSysCfg_t* cfg ) rc = _cmAsError(p,kThreadErrAsRC,"Thread start failed."); goto errLabel; } + + if( cmSeIsOpen(cp->serialPortH) ) + { + if( cmSeStart( cp->serialPortH ) != kOkSeRC ) + { + rc = _cmAsError(p,kSerialPortFailAsRC,"Serial port start failed."); + goto errLabel; + } + + } } diff --git a/cmAudioSys.h b/cmAudioSys.h index a6b3246..4c7c5c3 100644 --- a/cmAudioSys.h +++ b/cmAudioSys.h @@ -73,6 +73,7 @@ extern "C" { kBufTooSmallAsRC, kNoMsgWaitingAsRC, kMidiSysFailAsRC, + kSerialPortFailAsRC, kMsgSerializeFailAsRC, kStateBufFailAsRC, kInvalidArgAsRC, @@ -197,6 +198,7 @@ extern "C" { cmTsQueueCb_t clientCbFunc; // Called by cmAudioSysReceiveMsg() to deliver internally generated msg's to the host. // Set to NULL if msg's will be directly returned by buffers passed to cmAudioSysReceiveMsg(). cmUdpNetH_t netH; + cmSeH_t serialPortH; } cmAudioSysCfg_t; extern cmAudioSysH_t cmAudioSysNullHandle; diff --git a/dsp/cmDspNet.h b/dsp/cmDspNet.h index 9d8e9f1..f38b7aa 100644 --- a/dsp/cmDspNet.h +++ b/dsp/cmDspNet.h @@ -72,6 +72,7 @@ extern "C" { cmDspPresetMgr_t pm; unsigned nextInstId; unsigned pgmIdx; + cmSeH_t serialPortH; cmUdpNetH_t netH; // diff --git a/dsp/cmDspSys.c b/dsp/cmDspSys.c index fd68959..b466964 100644 --- a/dsp/cmDspSys.c +++ b/dsp/cmDspSys.c @@ -19,6 +19,7 @@ #include "cmThread.h" #include "cmUdpPort.h" #include "cmUdpNet.h" +#include "cmSerialPort.h" #include "cmAudioSys.h" #include "cmProcObj.h" #include "cmDspCtx.h" @@ -165,7 +166,7 @@ cmDspRC_t _cmDspSysFinalize( cmDsp_t* p ) return rc; } -cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH ) +cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH, cmSeH_t serialPortH ) { unsigned i; cmDspRC_t rc = kOkDspRC; @@ -178,9 +179,10 @@ cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH ) cmErrSetup(&p->err,&ctx->rpt,"DSP System"); //p->ctx.ctx = asCtx; - p->cmCtx = *ctx; - p->netH = netH; - p->pgmIdx = cmInvalidIdx; + p->cmCtx = *ctx; + p->netH = netH; + p->serialPortH = serialPortH; + p->pgmIdx = cmInvalidIdx; // create the DSP class linked heap if(cmLHeapIsValid( p->lhH = cmLHeapCreate(1024,ctx)) == false) @@ -1560,6 +1562,13 @@ cmSymTblH_t cmDspSysSymbolTable( cmDspSysH_t h ) return p->ctx.stH; } +cmSeH_t cmDspSysSerialPort( cmDspSysH_t h ) +{ + cmDsp_t* p = _cmDspHandleToPtr(h); + return p->serialPortH; +} + + unsigned cmDspSysRegisterStaticSymbol( cmDspSysH_t h, const cmChar_t* symLabel ) { return cmSymTblRegisterStaticSymbol( cmDspSysSymbolTable(h), symLabel ); } diff --git a/dsp/cmDspSys.h b/dsp/cmDspSys.h index 8ca79ac..28fef38 100644 --- a/dsp/cmDspSys.h +++ b/dsp/cmDspSys.h @@ -23,7 +23,7 @@ extern "C" { // Control Functions // - cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH ); + cmDspRC_t cmDspSysInitialize( cmCtx_t* ctx, cmDspSysH_t* hp, cmUdpNetH_t netH, cmSeH_t serialPortH ); cmDspRC_t cmDspSysFinalize( cmDspSysH_t* hp ); bool cmDspSysIsValid( cmDspSysH_t h ); @@ -156,6 +156,7 @@ extern "C" { double cmDspSysSampleRate( cmDspSysH_t h ); cmJsonH_t cmDspSysPgmRsrcHandle( cmDspSysH_t h ); cmSymTblH_t cmDspSysSymbolTable( cmDspSysH_t h ); + cmSeH_t cmDspSysSerialPort( cmDspSysH_t h ); unsigned cmDspSysRegisterStaticSymbol( cmDspSysH_t h, const cmChar_t* symLabel ); unsigned cmDspSysRegisterSymbol( cmDspSysH_t h, const cmChar_t* symLabel );