Subversion Repositories HomeAutomation

Rev

Rev 338 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
337 andfri 1
#include <sys/types.h>
2
#include <sys/stat.h>
3
#include <fcntl.h>
4
#include <termios.h>
5
#include <unistd.h>
6
#include <stdio.h>
7
 
338 andfri 8
#define DEVICE "/dev/midi1"
9
#define BAUDRATE B115200
10
#define TRIG_LEVEL 128
11
#define SAMPLE_FREQ 1000
12
 
337 andfri 13
#define SAMPLES_PER_PLOT 200
14
 
338 andfri 15
struct termios oldtio,newtio;
16
 
337 andfri 17
int main(int argc, char* argv[]) {
18
    int input;
19
    FILE* gnuplot;
20
    int i;
21
    float out_data;
22
    unsigned char buf[SAMPLES_PER_PLOT];
338 andfri 23
    unsigned char trig = TRIG_LEVEL;
24
    unsigned freq = SAMPLE_FREQ;
337 andfri 25
 
338 andfri 26
    input = open(DEVICE, O_RDONLY | O_NOCTTY);
27
    if (input <0) {perror(DEVICE); return -1; }
337 andfri 28
    gnuplot = popen("gnuplot", "w");
29
 
338 andfri 30
    tcgetattr(input,&oldtio); /* save current port settings */
31
 
32
    // bzero not defined? moving newtio outside main to zero it. 
33
    //bzero(&newtio, sizeof(newtio));
34
    newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
35
    newtio.c_iflag = IGNPAR;
36
    newtio.c_oflag = 0;
37
 
38
    /* set input mode (non-canonical, no echo,...) */
39
    newtio.c_lflag = 0;
40
 
41
    newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
42
    newtio.c_cc[VMIN]     = SAMPLES_PER_PLOT;   /* blocking read until all samples received */
43
 
44
    tcflush(input, TCIFLUSH);
45
    tcsetattr(input,TCSANOW,&newtio);
46
 
337 andfri 47
    fprintf(gnuplot, "set yrange [-0.1:5.1]\n");
48
    while(1) {
49
        // Wait for trig
50
        do {
51
            if (read(input, buf, 1) != 1) return 0;
52
        } while (buf[0] >= trig);
53
        do {
54
            if (read(input, buf, 1) != 1) return 0;
55
        } while (buf[0] < trig);
56
 
57
        // Read rest of the buffer
58
        for (i=1; i<SAMPLES_PER_PLOT; i++) {
59
            if (read(input, buf+i, 1) != 1) return 0;
60
        }
61
        //read(input, buf+i, SAMPLES_PER_PLOT-1);
62
 
63
        fprintf(gnuplot, "plot '-' with lines\n");
64
 
65
        for (i=0; i<SAMPLES_PER_PLOT; i++) {
66
            out_data = buf[i]*5.0/256;
67
            fprintf(gnuplot, "%g %g\n", (float)i/freq, out_data);
68
        }
69
        fprintf(gnuplot, "e\n"); // Display output
70
        fflush(gnuplot);
71
    }
72
 
338 andfri 73
    tcsetattr(input,TCSANOW,&oldtio);
337 andfri 74
    close(input);
75
    pclose(gnuplot);
76
    printf("\n");
77
    return 0;
78
}