-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.cpp
139 lines (111 loc) · 2.87 KB
/
read.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <string>
#include <mysql.h>
#include <stdio.h>
#include <cstring>
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <cstdlib>
#include "RtMidi.h"
// Platform-dependent sleep routines.
#if defined(__WINDOWS_MM__)
#include <windows.h>
#define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
#else // Unix variants
#include <unistd.h>
#define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
#endif
using namespace std;
bool chooseMidiPort( RtMidiOut *rtmidi );
void noteOn (int);
void noteOff (int);
bool chooseMidiPort( RtMidiOut *rtmidi )
{
std::cout << "\nWould you like to open a virtual output port? [y/N] ";
std::string keyHit="n";
if ( keyHit == "y" ) {
rtmidi->openVirtualPort();
return true;
}
std::string portName;
unsigned int i = 0, nPorts = rtmidi->getPortCount();
if ( nPorts == 0 ) {
std::cout << "No output ports available!" << std::endl;
return false;
}
if ( nPorts == 1 ) {
std::cout << "\nOpening " << rtmidi->getPortName() << std::endl;
}
else {
for ( i=0; i<nPorts; i++ ) {
portName = rtmidi->getPortName(i);
std::cout << " Output port #" << i << ": " << portName << '\n';
}
do {
std::cout << "\nChoose a port number: ";
i=8;
} while ( i >= nPorts );
}
rtmidi->openPort( i );
return true;
}
int serialport_read_until(int fd, char* buf, char until)
{
char b[1];
int i=0;
do {
int n = read(fd, b, 1); // read a char at a time
if( n==-1) return -1; // couldn't read
if( n==0 ) {
usleep( 10 * 1000 ); // wait 10 msec try again
continue;
}
buf[i] = b[0]; i++;
} while( b[0] != until );
buf[i] = 0; // null terminate the string
return 0;
}
int main()
{
RtMidiOut *midiout = 0;
std::vector<unsigned char> message;
// RtMidiOut constructor
try {
midiout = new RtMidiOut();
}
catch ( RtError &error ) {
error.printMessage();
exit( EXIT_FAILURE );
}
// Call function to select port.
try {
if ( chooseMidiPort( midiout ) == false ) goto cleanup;
}
catch ( RtError &error ) {
error.printMessage();
goto cleanup;
}
cout<<"BUILDS -- Musical Stairs"<<endl;
int fd;
struct termios options;
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NONBLOCK);
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
char buf[256];
while(1)
{
serialport_read_until(fd, buf, '\n');
printf("read: %s\n",buf);
message[0] = 144;
message[1] = (stair+29);
message[2] = 90;
midiout->sendMessage(&message);
message[0] = 128;
message[1] = (stair+29);
message[2] = 40;
midiout->sendMessage(&message);
}
return 1;
}