[76dce7]: mex / block_interface.c Maximize Restore History

Download this file

block_interface.c    166 lines (141 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "block_interface.h"
#include <string.h>
void defaultSetter(biEntry* obj,mxArray* in)
{
if(obj->var!=NULL)
mxDestroyArray(obj->var);
obj->var=mxDuplicateArray(in);
mexMakeArrayPersistent(obj->var);
}
mxArray* defaultGetter(biEntry* obj)
{
if(obj->var!=NULL)
return obj->var;
else
return mxCreateDoubleMatrix(0,0,mxREAL);
}
static biEntry vars[] =
{
// Default setters and getters
{.name="Ls",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="Pos",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="BufCount",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="PlayChanList",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="RecChanList",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="PageList",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="PageNo",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="Skipped",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="BufLen",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="ClassId",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="AnaOverlap",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="SynOverlap",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="DispLoad",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
{.name="IsLoop",.var=NULL,.setter=defaultSetter,.getter=defaultGetter},
// Default setters and custom getters
{.name="Source",.var=NULL,.setter=defaultSetter,.getter=getSource},
{.name="EnqBufCount",.var=NULL,.setter=NULL,.getter=getEnqBufCount},
{.name="ToPlay",.var=NULL,.setter=defaultSetter,.getter=getToPlay},
};
mxArray* getSource(biEntry* obj)
{
if(obj->var==NULL || mxIsNumeric(obj->var))
return mxCreateString("numeric");
else
return obj->var;
}
mxArray* getEnqBufCount(biEntry* obj)
{
double retVal = 0.0;
biEntry* cmdStruct = lookupEntry("PageList",vars,sizeof(vars)/sizeof(*vars));
if(cmdStruct->var!=NULL)
retVal = mxGetNumberOfElements(cmdStruct->var);
return mxCreateDoubleScalar(retVal);
}
void incPageNo()
{
biEntry* cmdStruct = lookupEntry("PageNo",vars,sizeof(vars)/sizeof(*vars));
if(cmdStruct->var==NULL)
{
cmdStruct->var = mxCreateDoubleScalar(1.0);
mexMakeArrayPersistent(cmdStruct->var);
}
else
{
double* pageNo = mxGetPr(cmdStruct->var);
*pageNo = *pageNo + 1.0;
}
}
mxArray* getToPlay(biEntry* obj)
{
if(obj->var!=NULL)
{
mxArray* duplicate = mxDuplicateArray(obj->var);
mxDestroyArray(obj->var);
obj->var = NULL;
return duplicate;
}
}
biEntry* lookupEntry(const char* name, biEntry* dict,size_t dictLen)
{
for(size_t ii=0;ii<dictLen;ii++)
{
if(!strncmp(name,dict[ii].name,strlen(dict[ii].name)))
{
return &dict[ii];
}
}
return NULL;
}
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
static int atExitRegistered = 0;
if(!atExitRegistered)
{
// mexAtExit();
}
if(nrhs<1)
{
mexErrMsgTxt("BLOCK_INTERFACE: Not enough input arguments.");
}
mxArray* mxCmd = prhs[0];
char command[COMMAND_LENGTH];
size_t comStrLen = mxGetNumberOfElements(mxCmd);
mxGetString(mxCmd,command,comStrLen+1);
if(!strncmp(command,"set",3))
{
if(nrhs<2)
{
mexErrMsgTxt("BLOCK_INTERFACE: Not enough arguments for the set method.");
}
biEntry* cmdStruct = lookupEntry(command+3,vars,sizeof(vars)/sizeof(*vars));
if(cmdStruct==NULL)
{
mexErrMsgTxt("BLOCK_INTERFACE: Unrecognized set command.");
}
cmdStruct->setter(cmdStruct,prhs[1]);
return;
}
else if(!strncmp(command,"get",3))
{
if(nrhs>1)
{
mexErrMsgTxt("BLOCK_INTERFACE: Too many input arguments for get method.");
}
biEntry* cmdStruct = lookupEntry(command+3,vars,sizeof(vars)/sizeof(*vars));
if(cmdStruct==NULL)
{
mexErrMsgTxt("BLOCK_INTERFACE: Unrecognized get command.");
}
plhs[0]=cmdStruct->getter(cmdStruct);
return;
}
else if(!strcmp(command,"reset"))
{
}
else if(!strcmp(command,"incPageNo"))
{
incPageNo();
return;
}
mexErrMsgTxt("BLOCK_INTERFACE: Unrecognized command.");
}