Subversion Repositories HomeAutomation

Rev

Rev 337 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 337 Rev 338
1
#include <sys/types.h>
1
#include <sys/types.h>
2
#include <sys/stat.h>
2
#include <sys/stat.h>
3
#include <fcntl.h>
3
#include <fcntl.h>
4
#include <termios.h>
4
#include <termios.h>
5
#include <unistd.h>
5
#include <unistd.h>
6
#include <stdio.h>
6
#include <stdio.h>
7
 
7
 
-
 
8
#define DEVICE "/dev/midi1"
-
 
9
#define BAUDRATE B115200
-
 
10
#define TRIG_LEVEL 128
-
 
11
#define SAMPLE_FREQ 1000
-
 
12
 
8
#define SAMPLES_PER_PLOT 200
13
#define SAMPLES_PER_PLOT 200
-
 
14
 
-
 
15
struct termios oldtio,newtio;
9
 
16
 
10
int main(int argc, char* argv[]) {
17
int main(int argc, char* argv[]) {
11
    int input;
18
    int input;
12
    FILE* gnuplot;
19
    FILE* gnuplot;
13
    int i;
20
    int i;
14
    float out_data;
21
    float out_data;
15
    unsigned char buf[SAMPLES_PER_PLOT];
22
    unsigned char buf[SAMPLES_PER_PLOT];
16
    unsigned char trig = 128;
23
    unsigned char trig = TRIG_LEVEL;
17
    unsigned freq = 1000;
24
    unsigned freq = SAMPLE_FREQ;
18
   
25
   
19
    input = open("/dev/midi1", O_RDONLY);
26
    input = open(DEVICE, O_RDONLY | O_NOCTTY);
-
 
27
    if (input <0) {perror(DEVICE); return -1; }
20
    gnuplot = popen("gnuplot", "w");
28
    gnuplot = popen("gnuplot", "w");
-
 
29
   
-
 
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);
21
   
46
   
22
    fprintf(gnuplot, "set yrange [-0.1:5.1]\n");
47
    fprintf(gnuplot, "set yrange [-0.1:5.1]\n");
23
    while(1) {
48
    while(1) {
24
        // Wait for trig
49
        // Wait for trig
25
        do {
50
        do {
26
            if (read(input, buf, 1) != 1) return 0;
51
            if (read(input, buf, 1) != 1) return 0;
27
        } while (buf[0] >= trig);
52
        } while (buf[0] >= trig);
28
        do {
53
        do {
29
            if (read(input, buf, 1) != 1) return 0;
54
            if (read(input, buf, 1) != 1) return 0;
30
        } while (buf[0] < trig);
55
        } while (buf[0] < trig);
31
       
56
       
32
        // Read rest of the buffer
57
        // Read rest of the buffer
33
        for (i=1; i<SAMPLES_PER_PLOT; i++) {
58
        for (i=1; i<SAMPLES_PER_PLOT; i++) {
34
            if (read(input, buf+i, 1) != 1) return 0;
59
            if (read(input, buf+i, 1) != 1) return 0;
35
        }
60
        }
36
        //read(input, buf+i, SAMPLES_PER_PLOT-1);
61
        //read(input, buf+i, SAMPLES_PER_PLOT-1);
37
       
62
       
38
        fprintf(gnuplot, "plot '-' with lines\n");
63
        fprintf(gnuplot, "plot '-' with lines\n");
39
       
64
       
40
        for (i=0; i<SAMPLES_PER_PLOT; i++) {
65
        for (i=0; i<SAMPLES_PER_PLOT; i++) {
41
            out_data = buf[i]*5.0/256;
66
            out_data = buf[i]*5.0/256;
42
            fprintf(gnuplot, "%g %g\n", (float)i/freq, out_data);
67
            fprintf(gnuplot, "%g %g\n", (float)i/freq, out_data);
43
        }
68
        }
44
        fprintf(gnuplot, "e\n"); // Display output
69
        fprintf(gnuplot, "e\n"); // Display output
45
        fflush(gnuplot);
70
        fflush(gnuplot);
46
    }
71
    }
47
   
72
   
-
 
73
    tcsetattr(input,TCSANOW,&oldtio);
48
    close(input);
74
    close(input);
49
    pclose(gnuplot);
75
    pclose(gnuplot);
50
    printf("\n");
76
    printf("\n");
51
    return 0;
77
    return 0;
52
}
78
}
53
 
79