SM_ADC  1.0
Приём данных АЦП через разделяемую память
basefunction.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 //
3 // This file is part of easyLink Library.
4 //
5 // Copyright (c) 2014 FEMTO-ST, ENSMM, UFC, CNRS.
6 //
7 // License: GNU General Public License 3
8 // Author: Guillaume J. Laurent
9 //
10 //------------------------------------------------------------------------------
11 #ifndef EASYLINK_BASEFUNCTION_H
12 #define EASYLINK_BASEFUNCTION_H
13 
14 //------------------------------------------------------------------------------
15 #include "array.h"
16 
17 //------------------------------------------------------------------------------
18 #ifndef INPUT_PORT_NUMBER
19 #define INPUT_PORT_NUMBER 0
21 int inputPortType[1] = { 1 };
22 int inputPortRows[1] = { 1 };
23 int inputPortCols[1] = { 1 };
24 #endif
25 
26 //------------------------------------------------------------------------------
27 #ifndef OUTPUT_PORT_NUMBER
28 #define OUTPUT_PORT_NUMBER 0
30 int outputPortRows[1] = { 1 };
31 int outputPortCols[1] = { 1 };
32 #endif
33 
34 //------------------------------------------------------------------------------
35 /** BaseFunction is the basis class for designing new C++ MEX functions.
36  *
37  * Use the file mex_array_product.cpp as a template to write a new MEX function.
38  */
40 {
41 protected:
42 
43 public:
44 
45  static int nrhs;
46  const static mxArray **prhs;
47  static int nlhs;
48  static vector<mxArray *> plhs;
49 
50  /**
51  * This is the first static method that is called within the MEX-Function.
52  *
53  * This method checks the number of input ports, their types dimensions.
54  *
55  * The possible types of the inputs are:
56  * - 1 for real inputs (scalar or array, use -1 as size to specify
57  * a dynamically dimensioned array)
58  * - 2 for literal string inputs
59  * - 3 for literal identifiers of MATLAB variables
60  */
62  {
63  /* check for proper number of inputs */
65  throw runtime_error(toString(INPUT_PORT_NUMBER) + " inputs required.");
66 
67  /* check for proper type and size of inputs */
68  for (int i=0;i<INPUT_PORT_NUMBER;i++)
69  {
70  if ((inputPortType[i]==2)||(inputPortType[i]==3))
71  {
72  // check if it is really a string
73  if (!mxIsChar(prhs[i]))
74  throw runtime_error("Argument " + toString(i+1) + " must be a string.");
75 
76  if (inputPortType[i]==3)
77  {
78  // check if it is really a identifier
79  string str=getInputString(i);
80  if (!isIdentifier(str))
81  throw runtime_error("Argument " + toString(i+1) + " must be a valid identifier.");
82  }
83  }
84  else
85  {
86  // check if it is a scalar or a vector
87  if (mxIsSparse(prhs[i])||!mxIsDouble(prhs[i]))
88  throw runtime_error("Argument " + toString(i+1) + " must be a scalar or an array of real values.");
89 
90  // check the number of rows
91  if ( (inputPortRows[i]>0) && (mxGetM(prhs[i])!=inputPortRows[i]) )
92  throw runtime_error("Argument " + toString(i+1) + " must have " + toString(inputPortRows[i]) + " rows.");
93 
94  // check the number of cols
95  if ( (inputPortCols[i]>0) && (mxGetN(prhs[i])!=inputPortCols[i]) )
96  throw runtime_error("Argument " + toString(i+1) + " must have " + toString(inputPortCols[i]) + " cols.");
97  }
98  }
99 
100  }
101 
102  /**
103  * This is the second static method that is called within the MEX-Function.
104  *
105  * This method specifies the number of output ports and their dimensions.
106  *
107  * Use -1 to specify dynamically dimensioned output signals.
108  */
110  {
111  /* check for proper number of outputs */
113  throw runtime_error(toString(OUTPUT_PORT_NUMBER) + " outputs required.");
114 
115  plhs.resize(nlhs);
116  /* set the proper size of outputs */
117  for (int i=0;i<nlhs;i++) {
119  }
120  }
121 
122  /**
123  * This is the third static method that is called within the MEX-Function.
124  *
125  * The method have to compute the function's outputs for given inputs
126  * and to store the results using outputArray or using writeOutput.
127  */
128  static void outputs()
129  {
130  }
131 
132  //--------------------------------------------------------------------------
133  /**
134  * Returns the scalar value of an input port.
135  */
136  static inline double getInputDouble(int port)
137  {
138  return mxGetScalar(prhs[port]);
139  }
140 
141  /**
142  * Returns the integer value of an input port
143  */
144  static inline double getInputInt(int port)
145  {
146  return round(getInputDouble(port));
147  }
148 
149  /**
150  * Returns the input port Array.
151  */
152  static inline Array getInputArray(int port)
153  {
154  return Array(prhs[port],"input port "+toString(port),true);
155  }
156 
157  /**
158  * Returns the string value of an input port.
159  */
160  static inline string getInputString(int port)
161  {
162  char buffer[256];
163  mxGetString(prhs[port], buffer, 256);
164  return string(buffer);
165  }
166 
167  /**
168  * Returns the input port number of elements.
169  * If the input port is a 1-D array with w elements, this function returns w.
170  * If the input port is an M-by-N array, this function returns m*n.
171  */
172  static inline int getInputWidth(int port)
173  {
174  return getInputNRows(port)*getInputNCols(port);
175  }
176 
177  /**
178  * Returns the input port number of rows.
179  */
180  static inline int getInputNRows(int port)
181  {
182  return mxGetM(prhs[port]);
183  }
184 
185  /**
186  * Returns the input port number of cols.
187  */
188  static inline int getInputNCols(int port)
189  {
190  return mxGetN(prhs[port]);
191  }
192 
193  //--------------------------------------------------------------------------
194  /**
195  * Writes a double or int value to an output port.
196  */
197  static inline void setOutputDouble(int port,double value)
198  {
199  double *x=mxGetPr(plhs[port]);
200  x[0]=value;
201  }
202 
203  /**
204  * Writes an Array to an output port. Dimensions must agree.
205  */
206  static inline void setOutputArray(int port,Array & array)
207  {
208  if (getOutputNRows(port)!=array.getNRows() || getOutputNCols(port)!=array.getNCols())
209  throw runtime_error("easyLink error: Unable to write "+array.getName()+" to output port "+toString(port)+". Array dimensions must agree.");
210 
211  memcpy((void*)mxGetPr(plhs[port]),(void*)array.getData(), array.getWidth()*sizeof(double) );
212 #ifdef __TEST__
213  printf("easyLink test message: hard copy of array in setOutputArray(\"%s\").\n",array.getName().c_str());
214 #endif
215  }
216 
217  /**
218  * Returns the output port Array.
219  */
220  static inline Array getOutputArray(int port)
221  {
222  return Array(plhs[port],"output port "+toString(port),true);
223  }
224 
225  /**
226  * Returns the output port number of elements.
227  * If the output port is a 1-D array with w elements, this function returns w.
228  * If the output port is an M-by-N array, this function returns m*n.
229  */
230  static inline int getOutputWidth(int port)
231  {
232  return getOutputNRows(port)*getOutputNCols(port);
233  }
234 
235  /**
236  * Returns the output port number of rows.
237  */
238  static inline int getOutputNRows(int port)
239  {
240  return mxGetM(plhs[port]);
241  }
242 
243  /**
244  * Returns the output port number of cols.
245  */
246  static inline int getOutputNCols(int port)
247  {
248  return mxGetN(plhs[port]);
249  }
250 
251  /**
252  * Sets the output port dimensions.
253  * Must be used only in initializeOutputPortSizes
254  */
255  static inline void setOutputPortDimensions(int port,int nrows,int ncols)
256  {
257  if (ncols>0 && nrows>0) {
258  plhs[port]=mxCreateDoubleMatrix(nrows,ncols, mxREAL);
259  }
260  else {
261  plhs[port]=NULL;
262  }
263  }
264 
265 };
266 
267 int BaseFunction::nrhs=0;
268 const mxArray** BaseFunction::prhs=NULL;
269 int BaseFunction::nlhs=0;
270 vector<mxArray *> BaseFunction::plhs;
271 
272 //------------------------------------------------------------------------------
273 #endif
int getNRows()
Definition: array.h:293
static int getInputWidth(int port)
Definition: basefunction.h:172
Definition: array.h:29
int outputPortRows[1]
Definition: basefunction.h:30
static void setOutputArray(int port, Array &array)
Definition: basefunction.h:206
static int getOutputWidth(int port)
Definition: basefunction.h:230
static void setOutputDouble(int port, double value)
Definition: basefunction.h:197
outputPortName
Definition: baseblock.h:28
static void initializeInputPortSizes()
Definition: basefunction.h:61
string toString(T value)
Definition: utils.h:35
#define OUTPUT_PORT_NUMBER
Definition: basefunction.h:28
static string getInputString(int port)
Definition: basefunction.h:160
static int getOutputNCols(int port)
Definition: basefunction.h:246
static int getInputNCols(int port)
Definition: basefunction.h:188
static void outputs()
Definition: basefunction.h:128
static double getInputDouble(int port)
Definition: basefunction.h:136
int getNCols()
Definition: array.h:287
bool isIdentifier(string str)
Definition: utils.h:45
static int nrhs
Definition: basefunction.h:45
static void initializeOutputPortSizes()
Definition: basefunction.h:109
static Array getInputArray(int port)
Definition: basefunction.h:152
int inputPortCols[1]
Definition: basefunction.h:23
static vector< mxArray * > plhs
Definition: basefunction.h:48
static int nlhs
Definition: basefunction.h:47
string getName()
Definition: array.h:299
inputPortName
Definition: baseblock.h:20
double * getData()
Definition: array.h:256
int inputPortType[1]
Definition: basefunction.h:21
static Array getOutputArray(int port)
Definition: basefunction.h:220
#define INPUT_PORT_NUMBER
Definition: basefunction.h:19
int getWidth()
Definition: array.h:281
int inputPortRows[1]
Definition: basefunction.h:22
static int getInputNRows(int port)
Definition: basefunction.h:180
static const mxArray ** prhs
Definition: basefunction.h:46
static int getOutputNRows(int port)
Definition: basefunction.h:238
static double getInputInt(int port)
Definition: basefunction.h:144
int outputPortCols[1]
Definition: basefunction.h:31
static void setOutputPortDimensions(int port, int nrows, int ncols)
Definition: basefunction.h:255