Subversion Repositories seema-scanner

Rev

Rev 74 | Rev 155 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "mex.h"
#include "class_handle.hpp"

// Output printf statements in the Matlab workspace
#define printf mexPrintf

// The class that we are interfacing to
#include "Camera.h"

using namespace std;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
    
    //mexPrintf("nlhs: %d , nrhs: %d\n", nlhs, nrhs);
    
    // Get the command string
    char cmd[64];
        if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
                mexErrMsgTxt("First input should be a command string less than 64 characters long.");
    
    // Static methods
    // InterfaceCameraList
    if (!strcmp("GetInterfaceCameraList", cmd)) {
        // Call the method
        vector< vector<CameraInfo> > interfaceCameraList;
        interfaceCameraList = Camera::GetInterfaceCameraList();
        unsigned int nCameras = 0;
        for(unsigned int i=0; i<interfaceCameraList.size(); i++)
            nCameras += interfaceCameraList[i].size();
        const char* fieldNames[] = {"interfaceNum", "cameraNum", "vendor","model"};
        plhs[0] = mxCreateStructMatrix(1, nCameras, 4, fieldNames);
        unsigned int iCamera = 0;
        for (unsigned int i=0; i<interfaceCameraList.size(); i++) {
            for (unsigned int j=0; j<interfaceCameraList[i].size(); j++) {
                mxSetFieldByNumber(plhs[0], iCamera, 0, mxCreateDoubleScalar(i));
                mxSetFieldByNumber(plhs[0], iCamera, 1, mxCreateDoubleScalar(j));
                mxSetFieldByNumber(plhs[0], iCamera, 2, mxCreateString(interfaceCameraList[i][j].vendor.c_str()));
                mxSetFieldByNumber(plhs[0], iCamera, 3, mxCreateString(interfaceCameraList[i][j].model.c_str()));
                iCamera++;
            }
        }
        return;
    }
    
    // New Camera
    if (!strcmp("NewCamera", cmd)) {
        // Check parameters
        if (nlhs != 1)
            mexErrMsgTxt("newCamera: One output expected.");
        if (nrhs < 3)
            mexErrMsgTxt("newCamera: Expected interface and camera number argument.");
        unsigned int interfaceNum = (unsigned int)mxGetScalar(prhs[1]);
        unsigned int camNum = (unsigned int)mxGetScalar(prhs[2]);
        // Return a handle to a new C++ instance
        plhs[0] = convertPtr2Mat<Camera>(Camera::NewCamera(interfaceNum, camNum, triggerModeSoftware));
        return;
    }
    
    // Class methods
    // Check if there is a second input, which should be the class instance handle
    if (nrhs < 2)
                mexErrMsgTxt("Second input should be a class instance handle.");
    
    // Delete
    if (!strcmp("delete", cmd)) {
        // Destroy the C++ object
        destroyObject<Camera>(prhs[1]);
        // Warn if other commands were ignored
        if (nlhs != 0 || nrhs != 2)
            mexWarnMsgTxt("Delete: Unexpected/missing arguments ignored.");
        return;
    }
    
    // Get the class instance pointer from the second input
    Camera *Camera_instance = convertMat2Ptr<Camera>(prhs[1]);
    
        // Call the various class methods
    // startCapture
    if (!strcmp("startCapture", cmd)) {
        // Check parameters
        if (nlhs != 0 || nrhs != 2)
            mexErrMsgTxt("startCapture: Unexpected/missing arguments.");
        // Call the method
        Camera_instance->startCapture();
        return;
    }
    
        // Call the various class methods
    // stopCapture
    if (!strcmp("stopCapture", cmd)) {
        // Check parameters
        if (nlhs != 0 || nrhs != 2)
            mexErrMsgTxt("stopCapture: Unexpected/missing arguments.");
        // Call the method
        Camera_instance->stopCapture();
        return;
    }
    
        // trigger
    if (!strcmp("trigger", cmd)) {
        // Check parameters
        if (nlhs != 0 || nrhs != 2)
            mexErrMsgTxt("trigger: Unexpected/missing arguments.");
        // Call the method
        Camera_instance->trigger();
        return;
    }
    
    // Call the various class methods
    // getFrame
    if (!strcmp("getFrame", cmd)) {
        // Check parameters
        if (nlhs != 1 || nrhs != 2)
            mexErrMsgTxt("getFrame: Unexpected/missing arguments.");
        // Call the method
        CameraFrame frame = Camera_instance->getFrame();
        // receive frame data in permuted dimensional order
        int dims[] = {frame.channels, frame.width, frame.height};
        plhs[0] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
        memcpy(mxGetData(plhs[0]), frame.memory, frame.height*frame.width*frame.channels);
        return;
    }
    // setCameraSettings
    if (!strcmp("setCameraSettings", cmd)) {
        // Check parameters
        if (nlhs != 0 || nrhs != 4)
            mexErrMsgTxt("setCameraSettings: Unexpected/missing arguments.");
        // Call the method
                float gain = mxGetData(prhs[2]);
                float shutter = mxGetData(prhs[3]);
                CameraSettings settings(gain, shutter);
        CameraFrame frame = Camera_instance->setCameraSettings(settings);
        return;
    }
    
    // Got here, so command not recognized
    mexErrMsgTxt("Command not recognized.");
}