⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 data_lgr.cpp

📁 a program that generates a pulse-width modulated (PWM)signal.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            break;
        case LOG_LONG:
            DataSize = sizeof (long);
            break;
        case LOG_FLOAT:
            DataSize = sizeof (float);
            break;
        case LOG_DOUBLE:
            DataSize = sizeof (double);
            break;
        case LOG_POINTER:
            DataSize = sizeof (void*);
            break;
        }

    //  Create a new array to hold the data, of the type specified by the caller
    switch (aArType)
        {
        case LOG_FINITE:
            TheArray = new CQueueArray (DataSize, aBufferSize);
            break;
        case LOG_EXPANDING:
            TheArray = new CExpandingArray (DataSize, aBufferSize);
            break;
        case LOG_CIRCULAR:
            TheArray = new CCircularArray (DataSize, aBufferSize);
            break;
        }

    //  If there's no room for data, write complaint to error string
    if (TheArray == NULL)
        *ErrorString << "ERROR:  Unable to allocate memory for data array";
    }


//-------------------------------------------------------------------------------------
//  Destructor:  ~CLogArray
//      This destructor just frees the memory which has been used by the log array.

CLogArray::~CLogArray (void)
    {
    delete HeaderLine;
    delete ErrorString;

    //  Delete the data array, specifying the type of the array for proper deletion
    switch (ArrayType)
        {
        case LOG_FINITE:
            delete ((CQueueArray*)TheArray);
            break;
        case LOG_EXPANDING:
            delete ((CExpandingArray*)TheArray);
            break;
        case LOG_CIRCULAR:
            delete ((CCircularArray*)TheArray);
            break;
        }

    }


//-------------------------------------------------------------------------------------
//  Function:  GetData
//      This function returns one item of data from the array in the form of a char-
//      acter string.  The intended use of this GetData() function is where the data
//      taken during a real-time run nust be printed on the screen or saved in a file.
//      The buffer pTemp stores the data in its native binary format - int, double, 
//      whatever; aBuf is where the character string goes. 

const char *CLogArray::GetData (void) 
    {
    static char aBuf[36];           //  Keep an array around which holds output strings 


    //  Look up and convert the data item indexed by current read pointer 
    if (DataType == LOG_INT)
        {
        int Data;
        (*this) >> &Data;
        sprintf (aBuf, "%d", Data);
        }
    else if (DataType == LOG_LONG)
        {
        long Data;
        (*this) >> &Data;
        sprintf (aBuf, "%ld", Data);
        }
    else if (DataType == LOG_FLOAT)
        {
        float Data;
        (*this) >> &Data;
        sprintf (aBuf, "%g", Data);
        }
    else if (DataType == LOG_DOUBLE)
        {
        double Data;
        (*this) >> &Data;
        sprintf (aBuf, "%lg", Data);
        }
    else if (DataType == LOG_POINTER)
        {
        void* Data;
        (*this) >> &Data;
        sprintf (aBuf, "%p", Data);
        }

    return (aBuf);                  //  Return pointer to the array holding the output
    }


//-------------------------------------------------------------------------------------
//  Function:  AddHeader
//      This function adds the string pointed to by the pointer given in the argument
//      to the header line which will be printed above this data in the output file.

void CLogArray::AddHeader (char *aString) 
    {
    *HeaderLine << aString;
    }


//-------------------------------------------------------------------------------------
//  Function:  GetErrorString 
//      This function returns the error message(s) which this array has generated.  It 
//      should be called *once* when the data is being read out by the logger object.  

const char *CLogArray::GetErrorString (void)
    {
    //  Call the error string's method and return the pointer it gives to us 
    return (ErrorString->GetString());
    }


//-------------------------------------------------------------------------------------
//  Operators:  << 
//      These overloaded arrays allow a user to write data into the array by using the 
//      "<<" operator, much as he would use when writing output to cout.  They only 
//      work when the argument is one of the types in the list given in DATA_LGR.HPP.  

CLogArray& CLogArray::operator<< (int aData) 
    {
    int* here;                                      //  Store pointer to the data here 

    if (DataType == LOG_INT)
        {
        here = (int*)TheArray->WritePointer ();
        if (here != NULL)
            *here = aData;
        }
    return (*this);
    }

CLogArray& CLogArray::operator<< (long aData)
    {
    long* here;

    if (DataType == LOG_LONG)
        {
        here = (long*)TheArray->WritePointer ();
        if (here != NULL)
            *here = aData;
        }
    return (*this);
    }

CLogArray& CLogArray::operator<< (float aData)
    {
    float* here;

    if (DataType == LOG_FLOAT)
        {
        here = (float*)TheArray->WritePointer ();
        if (here != NULL)
            *here = aData;
        }
    return (*this);
    }

CLogArray& CLogArray::operator<< (double aData)
    {
    double* here;

    if (DataType == LOG_DOUBLE)
        {
        here = (double*)TheArray->WritePointer ();
        if (here != NULL)
            *here = aData;
        }
    return (*this);
    }

CLogArray& CLogArray::operator<< (void *aData)
    {
    void** here;

    if (DataType == LOG_POINTER)
        {
        here = (void**)TheArray->WritePointer ();
        if (here != NULL)
            *here = aData;
        }
    return (*this);
    }


//-------------------------------------------------------------------------------------
//  Operators:  >>
//      These overloaded arrays allow a user to read data from the array by using the
//      ">>" operator in the same way as one uses cin.  Note that the only indication
//      you have that the number is invalid is a big number which is returned, and in
//      many cases that's valid data.  The moral: the user program should keep track
//      of how many data items it saved and only ask for data which is really there.  

CLogArray& CLogArray::operator>> (int &aData)
    {
    int* here = (int*)TheArray->ReadPointer ();     //  Get a pointer to the data
    if ((DataType == LOG_INT) && (here != NULL))    //  and use it to fill the refer-
        aData = *here;                              //  ence given by the user
    else
        aData = -MAXINT;
    return (*this);                                 //  Return reference to this obj.
    }

CLogArray& CLogArray::operator>> (long &aData)
    {
    long* here = (long*)TheArray->ReadPointer ();
    if ((DataType == LOG_LONG) && (here != NULL))
        aData = *here;
    else
        aData = -MAXLONG;
    return (*this);
    }

CLogArray& CLogArray::operator>> (float &aData)
    {
    float* here = (float*)TheArray->ReadPointer ();
    if ((DataType == LOG_FLOAT) && (here != NULL))
        aData = *here;
    else
        aData = -MAXFLOAT;
    return (*this);
    }

CLogArray& CLogArray::operator>> (double &aData)
    {
    double* here = (double*)TheArray->ReadPointer ();
    if ((DataType == LOG_DOUBLE) && (here != NULL))
        aData = *here;
    else
        aData = -MAXDOUBLE;
    return (*this);
    }

CLogArray& CLogArray::operator>> (void* &aData)
    {
    void** here = (void**)TheArray->ReadPointer ();
    if ((DataType == LOG_POINTER) && (here != NULL))
        aData = *here;
    else
        aData = NULL;
    return (*this);
    }

//------------------------------  Versions which take pointers  -----------------------

CLogArray& CLogArray::operator>> (int* aData)
    {
    int* here = (int*)TheArray->ReadPointer ();     //  Get a pointer to the data
    if ((DataType == LOG_INT) && (here != NULL))    //  and use it to fill the refer-
        *aData = *here;                              //  ence given by the user
    else
        *aData = -MAXINT;
    return (*this);                                 //  Return reference to this obj.
    }

CLogArray& CLogArray::operator>> (long* aData)
    {
    long* here = (long*)TheArray->ReadPointer ();
    if ((DataType == LOG_LONG) && (here != NULL))
        *aData = *here;
    else
        *aData = -MAXLONG;
    return (*this);
    }

CLogArray& CLogArray::operator>> (float* aData)
    {
    float* here = (float*)TheArray->ReadPointer ();
    if ((DataType == LOG_FLOAT) && (here != NULL))
        *aData = *here;
    else
        *aData = -MAXFLOAT;
    return (*this);
    }

CLogArray& CLogArray::operator>> (double* aData)
    {
    double* here = (double*)TheArray->ReadPointer ();
    if ((DataType == LOG_DOUBLE) && (here != NULL))
        *aData = *here;
    else
        *aData = -MAXDOUBLE;
    return (*this);
    }

CLogArray& CLogArray::operator>> (void** aData)
    {
    void** here = (void**)TheArray->ReadPointer ();
    if ((DataType == LOG_POINTER) && (here != NULL))
        *aData = *here;
    else
        *aData = NULL;
    return (*this);
    }


//=====================================================================================
//  Class:  CDataLogger
//      This class allows the user to simply construct and use a data logging object.
//      The logger keeps one or more arrays into which the user places data with a
//      call to LogDataLine().  After logging is done, the data can be read back
//      out and written into a file.  The goal is to allow a user program to take data
//      in real time and write it to a file when the real-time work is done.
//=====================================================================================

//-------------------------------------------------------------------------------------
//  Constructor:  CDataLogger (version which doesn't use a file) 
//      This constructor creates a data logger object with no arrays and no file name.
//      The user program will get data from the logger and do something with it other 
//      than having this logger object write the data to a file.  

CDataLogger::CDataLogger (LogArrayType aType, unsigned aSize) : CBasicList ()
    {
    Initialize (aType, aSize);          //  First call the initialization function

    FileHandle = NULL;                  //  We don't use a file, so handle is null
    }


//-------------------------------------------------------------------------------------
//  Constructor:  CDataLogger (version which takes a file name)
//      This constructor creates a data logger object with no arrays, but it saves the
//      given size for creating data arrays when the user program calls AddColumn().
//      It does open the data file with the given name for writing.

CDataLogger::CDataLogger (LogArrayType aType, unsigned aSize, char *aFileName)
    : CBasicList ()
    {
    //  First call the initialization function
    Initialize (aType, aSize);

    //  The open the file, replacing the null file buffer with a valid one 
    FileHandle = fopen (aFileName, "w");
    if (FileHandle == NULL)
        *ErrorString << "ERROR:  Unable to open logger file " << aFileName;
    }


//-------------------------------------------------------------------------------------
//  Function:  Initialize 
//      This function contains code that all versions of the overloaded constructor 
//      have in common.  It's just here for convenience really.  

void CDataLogger::Initialize (LogArrayType aType, unsigned aSize) 
    {
    ErrorString = new CString (256);    //  Create a string in which to store errors 
    Separator = new CString (16);       //  Create string for column separator text 
    TitleString = new CString (128);    //  Also create string for the log's title 
    ArraySize = aSize;                  //  Save starting number of items in arrays 
    ArrayType = aType;                  //  Save the array overflow handling method 
    LinesSaved = 0;                     //  Haven't saved any data yet
    MaxLinesSaved = 0;                  //  Same thing for the maximum saved so far 
    WriteLineNumbers = FALSE;           //  Don't write line numbers unless asked to 
    WriteHeaders = FALSE;               //  Don't write headers unless asked to
    *Separator = " ";                   //  Default separator is space for Matlab text

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -