View Raw SPL
/*****************************************************************************
*                                                                            *
*   RTWRITE.SPL  Copyright (C) 1998 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Example of writing data in real time                        *
*                                                                            *
*   Revisions:   25 Sep 1998  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_RTWRITE

    RTWRITE

    Purpose: Reads real time data from a file

    Syntax:  RTWRITE(len, freq)

             len - optional integer specifying number of samples, defaults
                   to 1000.

               f - optional real specifying freequency, defaults to 8 Hz


    Returns: Places new series in W1

    Example:
             Start first DADiSP to simulate a real time data source:

             Rttinit("rtwrite")


             Start second DADiSP to read real time data:

             Rttinit("rtread")

             The series generated by the first DADiSP is read
             synchronously by the second DADiSP.  The real time data
             appears in W1 of the second DADiSP.



             Rttinit("rtwrite(300, 2)")

             Same as above, but the series contains 300 points with a
             frequency of 2 Hz.


    Remarks:
             The first DADiSP writes a binary series to the file
             named RTDATA.DAT.  An ASCII value of 1.0 is also written
             to the text file GATE.TXT to indicate new data is
             available.  New data is not written to RTDATA.DAT until
             an ASCII value of 0.0 is detected in GATE.TXT

             The second DADiSP polls GATE.TXT.  If the value is 1.0,
             the file RTDATA.DAT is read and the result is placed in
             W1.  A value of 0.0 is then written to GATE.TXT to
             indicate that the new data was read.

             Both the RTREAD and RWRITE are perfomed in the
             background via RTTINIT.

    See Also:
             Rtread
             Rttinit
             Rttterm
#endif


/* write a noisey "rise time" squarewave in real time */
rtwrite(len, f)
{
        local a, gfile, dfile;

        /* default args */
        if (argc < 2)
        {
                f = 8;
                
                if (argc < 1)
                {
                        len = 1000;
                }
        }

        /* gate file */
        gfile = getmiscpath(1, 1) + "gate.txt";

        /* data file */
        dfile = getmiscpath(1, 1) + "rtdata.dat";

        /* create gate.txt if it doesn't exist */
        if (fstat(gfile) < 1)
        {
                writea(gfile, {0}, 1);
        }

        a = reada(gfile);
        
        if (a[1] == 0)
        {
                /* if gate value is 0, write new data */
                writeb(dfile, double, 1, grtsqr(len, 1 / len, f, 0, 0.02) + gnorm(len, 1 / len, 1) / 20);

                /* indicate that new data has been written */
                writea(gfile, {1}, 1);
        }
}