#include "stdafx.h"
#include "mcadincl.h"

#ifdef __cplusplus
extern "C"
{
#endif
    int remez(int N, int npt, double *g, double *r, double *w, double *h);
#ifdef __cplusplus
}
#endif
/************************************************************************/
/*                  User Error Messages                                 */
/************************************************************************/
enum errMsg2
{ INTERRUPTED = 1, INSUFFICIENT_MEMORY, MUST_BE_REAL, NERR };

char *errMsgs[NERR] = {
    "interrupted",
    "insufficient memory",
    "must be real"
};

/************************************************************************/
/*                  Mathcad Wrapper function                            */
/************************************************************************/
LRESULT f_remez(COMPLEXARRAY * const coefs, // result
                LPCCOMPLEXARRAY grd,    // frequency grd
                LPCCOMPLEXARRAY res,    // FIR resonse
                LPCCOMPLEXARRAY wei,    // Weight
                LPCCOMPLEXSCALAR ord)   // Order
{
    int k, N;

    // check that the scalar argument is real
    if (ord->imag != 0.0)
        return MAKELRESULT(MUST_BE_REAL, 4);
    N = (int) ord->real;
    // check that the array argument is real
    if (grd->hImag != NULL || res->hImag != NULL || wei->hImag != NULL)
        return MAKELRESULT(MUST_BE_REAL, 1);
    // allocate memory for the result
    if (!MathcadArrayAllocate(coefs, N, 1, TRUE, FALSE))
        return INSUFFICIENT_MEMORY;
    //------------------------------------------
    k = remez(N, grd->rows, grd->hReal[0], res->hReal[0], wei->hReal[0],
              coefs->hReal[0]);
    //------------------------------------------
    return 0;                   // OK !!!
}

/************************************************************************/
/*          Fill out a FUNCTIONINFO structure for Mathcad               */
/************************************************************************/
FUNCTIONINFO r2Info = {
    "remez2",                   // MathCad function name
    "g,r,w,n",                  // Parameters
    "returns the array of FIR coefficients",    // Description
    (LPCFUNCTION) f_remez,      // Pointer to Executable code
    COMPLEX_ARRAY,              // Type of return value
    4,                          // Number of arguments
    {COMPLEX_ARRAY, COMPLEX_ARRAY, COMPLEX_ARRAY, COMPLEX_SCALAR}   // Argument types
};

/************************************************************************/
/*                  DLL  Entry Point Code                               */
/************************************************************************/
BOOL APIENTRY DllMain(HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{
    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        CreateUserFunction(hDLL, &r2Info);  // register user function
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

/* EOF */
