#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include "drv.h"

#define AD_BUFFER_SIZE  	8192	// 512 * 16
#define CHANNEL_NUM		16
#define AD_LEVEL_DISP_VAL	30000

static int   ad_data[AD_BUFFER_SIZE];
static int main_end_flag;

void sigint_action(int signum, siginfo_t *info, void *ctx)
{
    main_end_flag = 1;
}

void level_display( char *str, int ch, int val )
{
    int i;

    i = sprintf( str, "[%d] ", ch );
    str += i;
    for( i = 1 ; i < 11 ; i++ )
    {
        if( val > ( AD_LEVEL_DISP_VAL * i )) *str++ = '#';
        else *str++ = ' ';
    }
    *str++ = ' ';
    *str++ = '\0';
}

int main(int argc, char **argv)
{
    struct sigaction sa_sigint;
    
    int fd;
    int stat;
    
    int    ret_val;
    struct timeval time_out;
    fd_set rfds, wfds;
    
    int rbyte;
    int wbyte;
    
    char str[64];

    int i, j, rdcnt, smpl, crlf;
    int wave[CHANNEL_NUM], peek[CHANNEL_NUM];

    if(argc == 2)
        strcpy( str, argv[1] );
    else
        strcpy( str, "/dev/kt" );

    /* device open */
    fd = open( str, O_RDWR, 0666 );
    if( fd < 0 )
    {
        printf( "%s open error.\n", str );
        return -1;
    }
    
    /* AD Start */
    stat = ioctl( fd, FIO_AD_START );
    if( stat < 0 )
    {
	printf("ioctl error! (%d)\n", stat);
	close ( fd );
	return -1;
    }
    
    memset(&sa_sigint, 0, sizeof(sa_sigint));
    sa_sigint.sa_sigaction = sigint_action;
    sa_sigint.sa_flags = SA_RESTART | SA_SIGINFO;
    
    if (sigaction(SIGINT, &sa_sigint, NULL) < 0)
        printf("sigaction error.\n");

    /* variable initialize */
    for( i = 0 ; i < CHANNEL_NUM ; i++ )
    {
        wave[i] = 0;
        peek[i] = 0;
    }
    rdcnt = 0;

    while(!main_end_flag)
    {
        do
        {
            FD_ZERO(&rfds);
            FD_ZERO(&wfds);
            FD_SET(fd, &rfds);
            FD_SET(fd, &wfds);
            
            time_out.tv_sec  = 0;
            time_out.tv_usec = 10 * 1000;
            ret_val = select(fd+1, &rfds, &wfds, NULL, &time_out);
        }while(ret_val == 0);
        
        if(ret_val < 0)
        {
            printf("ret_val = %d\n", ret_val);
            main_end_flag = 1;
            break;
        }

        /* データ取得処理 */
        if(FD_ISSET(fd, &rfds))
        {
            rbyte = read(fd, &ad_data[0], sizeof(ad_data));
            smpl = rbyte / sizeof(int) / CHANNEL_NUM;
            for( i = 0 ; i < smpl ; i++ )
            {
                for( j = 0 ; j < CHANNEL_NUM ; j++ )
                {
                    wave[j] = ad_data[i * CHANNEL_NUM + j];
                    if( abs(wave[j]) > abs(peek[j]) )
                        peek[j] = abs(wave[j]);
                }
                rdcnt++;
                if(( rdcnt % 1000 ) == 0 )
                {
                    crlf = 0;
                    for( j = 0 ; j < CHANNEL_NUM ; j++ )
                    {
                        if( peek[j] )
                        {
                            level_display( str, j + 1, peek[j] );
                            printf( "%s", str );
                            peek[j] = 0;
                            crlf++;
                        }
                    }
                    if( crlf )
                        printf("\n");
                }
            }
        }
    }

    fflush( stdout );
    
    /* AD Stop */
    stat = ioctl(fd, FIO_AD_STOP);
    if(stat < 0)
    {
	printf("ioctl error! (%d)\n", stat);
    }

    close(fd);
    
    return 0;
}

/* EOF */

