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