/*
 * (c) d.m.broad. June 1995 (originally for the C= Amiga) & July 2005 (for FREEBSD on the PC).
 *
 * see build notes elsewhere.
 *
 * BSD LICENSE APPLIES.
 */

/*
 * this example application drives the counter device plugged into the pep input
 * port.
 *
 * the counter contains two 74LS393 counter ICs chained in series to provide a
 * 16 bit ripple counter.
 * 
 * control output 1 on the pep is the '393 reset input, control output 0 enables
 * or disables the primary '393s clock input via a nand gate (LS132).
 *
 * the other input to the above nand gate is driven via a high gain transitor
 * amplifier which should enable counting to be achieved for analog as well as
 * digital sources.
 */

#include "pep.h"

/*
 * frequency counter (TEST)
 *
 * operate pep counter expansion device
 */
#define ONE_SECOND (1000000)
void frequency_counter(void)
{
    unsigned short reading;
    float mean=0.0;
    int t;
    
    for(t=0; t<5; t++)
    {
        outPEP(OUT0, HIGH);     /* enable clock input */
        outPEP(OUT1, HIGH);     /* reset counter */
        outPEP(OUT1, LOW);      /* start counter */
        usleep(ONE_SECOND);     /* sleep for a while */
        outPEP(OUT0, LOW);      /* disable clock input */
        reading= readPEP();
        if(t)
            mean= (mean + (float)reading) / 2;
        else
            mean= (float)reading;

        printf("%.2f, ", mean);
        fflush(stdout);
    }
    printf("AVE CPS: %.2f Hz\n", mean);
}

/*
 * demonstration
 */
int main(int argc, char **argv)
{
    /* initialise pep device */
    initPEP_IO();

    /* run test frequency counter */
    while(1)
        frequency_counter();
}
