Subversion Repositories seema-scanner

Rev

Rev 154 | Rev 157 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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)
154 jakw 70
            mexWarnMsgTxt("Delete: Unexpected/missing arguments ignored.");
1 jakw 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
154 jakw 81
        if (nlhs != 0 || nrhs != 2)
82
            mexErrMsgTxt("startCapture: Unexpected/missing arguments.");
28 jakw 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
154 jakw 92
        if (nlhs != 0 || nrhs != 2)
93
            mexErrMsgTxt("stopCapture: Unexpected/missing arguments.");
28 jakw 94
        // Call the method
95
        Camera_instance->stopCapture();
96
        return;
97
    }
98
 
99
	// trigger
100
    if (!strcmp("trigger", cmd)) {
101
        // Check parameters
154 jakw 102
        if (nlhs != 0 || nrhs != 2)
103
            mexErrMsgTxt("trigger: Unexpected/missing arguments.");
28 jakw 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
154 jakw 113
        if (nlhs != 1 || nrhs != 2)
114
            mexErrMsgTxt("getFrame: Unexpected/missing 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
    }
154 jakw 123
    // setCameraSettings
124
    if (!strcmp("setCameraSettings", cmd)) {
125
        // Check parameters
126
        if (nlhs != 0 || nrhs != 4)
127
            mexErrMsgTxt("setCameraSettings: Unexpected/missing arguments.");
128
        // Call the method
155 jakw 129
		double *gain = (double*)mxGetData(prhs[2]);
130
		double *shutter = (double*)mxGetData(prhs[3]);
131
		CameraSettings settings;
132
        settings.gain = *gain;
133
        settings.shutter = *shutter;
134
        Camera_instance->setCameraSettings(settings);
154 jakw 135
        return;
136
    }
1 jakw 137
 
138
    // Got here, so command not recognized
139
    mexErrMsgTxt("Command not recognized.");
140
}