1 |
jakw |
1 |
#include "mex.h"
|
|
|
2 |
#include "class_handle.hpp"
|
|
|
3 |
|
|
|
4 |
// Output printf statements in the Matlab workspace
|
|
|
5 |
#define printf mexPrintf
|
|
|
6 |
|
|
|
7 |
// The class that we are interfacing to
|
|
|
8 |
#include "Camera.h"
|
|
|
9 |
|
|
|
10 |
using namespace std;
|
|
|
11 |
|
28 |
jakw |
12 |
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
|
|
|
13 |
|
74 |
jakw |
14 |
//mexPrintf("nlhs: %d , nrhs: %d\n", nlhs, nrhs);
|
28 |
jakw |
15 |
|
1 |
jakw |
16 |
// Get the command string
|
|
|
17 |
char cmd[64];
|
|
|
18 |
if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
|
|
|
19 |
mexErrMsgTxt("First input should be a command string less than 64 characters long.");
|
|
|
20 |
|
|
|
21 |
// Static methods
|
|
|
22 |
// InterfaceCameraList
|
|
|
23 |
if (!strcmp("GetInterfaceCameraList", cmd)) {
|
|
|
24 |
// Call the method
|
|
|
25 |
vector< vector<CameraInfo> > interfaceCameraList;
|
|
|
26 |
interfaceCameraList = Camera::GetInterfaceCameraList();
|
|
|
27 |
unsigned int nCameras = 0;
|
|
|
28 |
for(unsigned int i=0; i<interfaceCameraList.size(); i++)
|
|
|
29 |
nCameras += interfaceCameraList[i].size();
|
|
|
30 |
const char* fieldNames[] = {"interfaceNum", "cameraNum", "vendor","model"};
|
|
|
31 |
plhs[0] = mxCreateStructMatrix(1, nCameras, 4, fieldNames);
|
|
|
32 |
unsigned int iCamera = 0;
|
|
|
33 |
for (unsigned int i=0; i<interfaceCameraList.size(); i++) {
|
|
|
34 |
for (unsigned int j=0; j<interfaceCameraList[i].size(); j++) {
|
|
|
35 |
mxSetFieldByNumber(plhs[0], iCamera, 0, mxCreateDoubleScalar(i));
|
|
|
36 |
mxSetFieldByNumber(plhs[0], iCamera, 1, mxCreateDoubleScalar(j));
|
|
|
37 |
mxSetFieldByNumber(plhs[0], iCamera, 2, mxCreateString(interfaceCameraList[i][j].vendor.c_str()));
|
|
|
38 |
mxSetFieldByNumber(plhs[0], iCamera, 3, mxCreateString(interfaceCameraList[i][j].model.c_str()));
|
|
|
39 |
iCamera++;
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
return;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// New Camera
|
|
|
46 |
if (!strcmp("NewCamera", cmd)) {
|
|
|
47 |
// Check parameters
|
|
|
48 |
if (nlhs != 1)
|
|
|
49 |
mexErrMsgTxt("newCamera: One output expected.");
|
|
|
50 |
if (nrhs < 3)
|
28 |
jakw |
51 |
mexErrMsgTxt("newCamera: Expected interface and camera number argument.");
|
1 |
jakw |
52 |
unsigned int interfaceNum = (unsigned int)mxGetScalar(prhs[1]);
|
|
|
53 |
unsigned int camNum = (unsigned int)mxGetScalar(prhs[2]);
|
|
|
54 |
// Return a handle to a new C++ instance
|
28 |
jakw |
55 |
plhs[0] = convertPtr2Mat<Camera>(Camera::NewCamera(interfaceNum, camNum, triggerModeSoftware));
|
1 |
jakw |
56 |
return;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// Class methods
|
|
|
60 |
// Check if there is a second input, which should be the class instance handle
|
|
|
61 |
if (nrhs < 2)
|
|
|
62 |
mexErrMsgTxt("Second input should be a class instance handle.");
|
|
|
63 |
|
|
|
64 |
// Delete
|
|
|
65 |
if (!strcmp("delete", cmd)) {
|
|
|
66 |
// Destroy the C++ object
|
|
|
67 |
destroyObject<Camera>(prhs[1]);
|
|
|
68 |
// Warn if other commands were ignored
|
|
|
69 |
if (nlhs != 0 || nrhs != 2)
|
|
|
70 |
mexWarnMsgTxt("Delete: Unexpected arguments ignored.");
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// Get the class instance pointer from the second input
|
|
|
75 |
Camera *Camera_instance = convertMat2Ptr<Camera>(prhs[1]);
|
|
|
76 |
|
28 |
jakw |
77 |
// Call the various class methods
|
|
|
78 |
// startCapture
|
|
|
79 |
if (!strcmp("startCapture", cmd)) {
|
|
|
80 |
// Check parameters
|
|
|
81 |
if (nlhs > 0 || nrhs > 2)
|
|
|
82 |
mexErrMsgTxt("startCapture: Unexpected arguments.");
|
|
|
83 |
// Call the method
|
|
|
84 |
Camera_instance->startCapture();
|
|
|
85 |
return;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Call the various class methods
|
|
|
89 |
// stopCapture
|
|
|
90 |
if (!strcmp("stopCapture", cmd)) {
|
|
|
91 |
// Check parameters
|
|
|
92 |
if (nlhs > 0 || nrhs > 2)
|
|
|
93 |
mexErrMsgTxt("stopCapture: Unexpected arguments.");
|
|
|
94 |
// Call the method
|
|
|
95 |
Camera_instance->stopCapture();
|
|
|
96 |
return;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// trigger
|
|
|
100 |
if (!strcmp("trigger", cmd)) {
|
|
|
101 |
// Check parameters
|
|
|
102 |
if (nlhs > 0 || nrhs > 2)
|
|
|
103 |
mexErrMsgTxt("trigger: Unexpected arguments.");
|
|
|
104 |
// Call the method
|
|
|
105 |
Camera_instance->trigger();
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
1 |
jakw |
109 |
// Call the various class methods
|
28 |
jakw |
110 |
// getFrame
|
|
|
111 |
if (!strcmp("getFrame", cmd)) {
|
1 |
jakw |
112 |
// Check parameters
|
|
|
113 |
if (nlhs < 1 || nrhs > 2)
|
28 |
jakw |
114 |
mexErrMsgTxt("getFrame: Unexpected arguments.");
|
1 |
jakw |
115 |
// Call the method
|
28 |
jakw |
116 |
CameraFrame frame = Camera_instance->getFrame();
|
|
|
117 |
// receive frame data in permuted dimensional order
|
|
|
118 |
int dims[] = {frame.channels, frame.width, frame.height};
|
|
|
119 |
plhs[0] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
|
|
|
120 |
memcpy(mxGetData(plhs[0]), frame.memory, frame.height*frame.width*frame.channels);
|
1 |
jakw |
121 |
return;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// Got here, so command not recognized
|
|
|
125 |
mexErrMsgTxt("Command not recognized.");
|
|
|
126 |
}
|