![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MIDIoIP test This test program controls blinking of Four LEDs by Knob#1/slider#1/key of KORG nano Kontrol/Key, and needs midi_bridge.c on linux to transfer MIDI data to mbed (over PC-serial or IP packet)
Dependencies: EthernetNetIf mbed
main.cpp@0:189db48772ae, 2010-09-20 (annotated)
- Committer:
- xshige
- Date:
- Mon Sep 20 13:18:42 2010 +0000
- Revision:
- 0:189db48772ae
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:189db48772ae | 1 | |
xshige | 0:189db48772ae | 2 | // MIDI over IP/serial test (MIDIoIP_test) |
xshige | 0:189db48772ae | 3 | |
xshige | 0:189db48772ae | 4 | // This test program controls blinking of Four LEDs |
xshige | 0:189db48772ae | 5 | // by Knob#1/slider#1/key of KORG nano Kontrol/Key, |
xshige | 0:189db48772ae | 6 | // and needs midi_bridge.c on linux to trasfer MIDI data |
xshige | 0:189db48772ae | 7 | // to mbed (over PC-serial or IP packet) |
xshige | 0:189db48772ae | 8 | // |
xshige | 0:189db48772ae | 9 | // Note: |
xshige | 0:189db48772ae | 10 | // midi_bridge.c is included in this file as comment. |
xshige | 0:189db48772ae | 11 | // |
xshige | 0:189db48772ae | 12 | |
xshige | 0:189db48772ae | 13 | // written by: xshige |
xshige | 0:189db48772ae | 14 | // 2010/9/20 |
xshige | 0:189db48772ae | 15 | |
xshige | 0:189db48772ae | 16 | |
xshige | 0:189db48772ae | 17 | //#define SERIAL_MIDI // get MIDI data from PC serial |
xshige | 0:189db48772ae | 18 | #define UDP_MIDI // get MIDI data from IP packet(UDP) |
xshige | 0:189db48772ae | 19 | |
xshige | 0:189db48772ae | 20 | #include "mbed.h" |
xshige | 0:189db48772ae | 21 | #include "EthernetNetIf.h" |
xshige | 0:189db48772ae | 22 | #include "UDPSocket.h" |
xshige | 0:189db48772ae | 23 | |
xshige | 0:189db48772ae | 24 | EthernetNetIf eth; |
xshige | 0:189db48772ae | 25 | #if 0 |
xshige | 0:189db48772ae | 26 | EthernetNetIf eth( |
xshige | 0:189db48772ae | 27 | IpAddr(192,168,0,25), //IP Address |
xshige | 0:189db48772ae | 28 | IpAddr(255,255,255,0), //Network Mask |
xshige | 0:189db48772ae | 29 | IpAddr(192,168,0,1), //Gateway |
xshige | 0:189db48772ae | 30 | IpAddr(192,168,0,1) //DNS |
xshige | 0:189db48772ae | 31 | ); |
xshige | 0:189db48772ae | 32 | #endif |
xshige | 0:189db48772ae | 33 | |
xshige | 0:189db48772ae | 34 | UDPSocket udp; |
xshige | 0:189db48772ae | 35 | |
xshige | 0:189db48772ae | 36 | |
xshige | 0:189db48772ae | 37 | //------------------------------------------------- |
xshige | 0:189db48772ae | 38 | |
xshige | 0:189db48772ae | 39 | class RingBuffer |
xshige | 0:189db48772ae | 40 | { |
xshige | 0:189db48772ae | 41 | protected: |
xshige | 0:189db48772ae | 42 | unsigned char *buffer; |
xshige | 0:189db48772ae | 43 | int buffersize; |
xshige | 0:189db48772ae | 44 | int rp; // read index |
xshige | 0:189db48772ae | 45 | int wp; // write index |
xshige | 0:189db48772ae | 46 | int num; // how many bytes you have in buffer |
xshige | 0:189db48772ae | 47 | |
xshige | 0:189db48772ae | 48 | public: |
xshige | 0:189db48772ae | 49 | RingBuffer(int bufsiz) |
xshige | 0:189db48772ae | 50 | { |
xshige | 0:189db48772ae | 51 | buffersize = bufsiz; |
xshige | 0:189db48772ae | 52 | |
xshige | 0:189db48772ae | 53 | buffer = new unsigned char[buffersize]; |
xshige | 0:189db48772ae | 54 | // memset(buffer, 0, sizeof(char) * buffersize); |
xshige | 0:189db48772ae | 55 | |
xshige | 0:189db48772ae | 56 | // reset pointer to make empty |
xshige | 0:189db48772ae | 57 | rp = 0; |
xshige | 0:189db48772ae | 58 | wp = 0; |
xshige | 0:189db48772ae | 59 | // zero byte in buffer |
xshige | 0:189db48772ae | 60 | num = 0; |
xshige | 0:189db48772ae | 61 | }; |
xshige | 0:189db48772ae | 62 | |
xshige | 0:189db48772ae | 63 | ~RingBuffer(void) |
xshige | 0:189db48772ae | 64 | { |
xshige | 0:189db48772ae | 65 | delete[] buffer; |
xshige | 0:189db48772ae | 66 | }; |
xshige | 0:189db48772ae | 67 | |
xshige | 0:189db48772ae | 68 | int GetByte(unsigned char *cp); |
xshige | 0:189db48772ae | 69 | int PutByte(unsigned char c); |
xshige | 0:189db48772ae | 70 | int IsEmpty(void); |
xshige | 0:189db48772ae | 71 | int IsNumBytes(void); |
xshige | 0:189db48772ae | 72 | }; |
xshige | 0:189db48772ae | 73 | |
xshige | 0:189db48772ae | 74 | |
xshige | 0:189db48772ae | 75 | int RingBuffer::GetByte(unsigned char *cp ) |
xshige | 0:189db48772ae | 76 | { |
xshige | 0:189db48772ae | 77 | if( rp != wp ) { // not empty |
xshige | 0:189db48772ae | 78 | *cp = buffer[rp]; |
xshige | 0:189db48772ae | 79 | rp = (rp + 1) % buffersize; |
xshige | 0:189db48772ae | 80 | num--; |
xshige | 0:189db48772ae | 81 | return 0; |
xshige | 0:189db48772ae | 82 | } else { // empty |
xshige | 0:189db48772ae | 83 | num = 0; |
xshige | 0:189db48772ae | 84 | return -1; |
xshige | 0:189db48772ae | 85 | } |
xshige | 0:189db48772ae | 86 | } |
xshige | 0:189db48772ae | 87 | |
xshige | 0:189db48772ae | 88 | |
xshige | 0:189db48772ae | 89 | int RingBuffer::PutByte(unsigned char c ) |
xshige | 0:189db48772ae | 90 | { |
xshige | 0:189db48772ae | 91 | int next = (wp + 1) % buffersize; |
xshige | 0:189db48772ae | 92 | if( next == rp ) { |
xshige | 0:189db48772ae | 93 | printf("*** Buffer Full ***\r\n" ); |
xshige | 0:189db48772ae | 94 | // printf("*");//debug |
xshige | 0:189db48772ae | 95 | return -1; |
xshige | 0:189db48772ae | 96 | } |
xshige | 0:189db48772ae | 97 | buffer[wp] = c; |
xshige | 0:189db48772ae | 98 | wp = next; |
xshige | 0:189db48772ae | 99 | num++; |
xshige | 0:189db48772ae | 100 | return 0; |
xshige | 0:189db48772ae | 101 | } |
xshige | 0:189db48772ae | 102 | |
xshige | 0:189db48772ae | 103 | |
xshige | 0:189db48772ae | 104 | int RingBuffer::IsEmpty(void) |
xshige | 0:189db48772ae | 105 | { |
xshige | 0:189db48772ae | 106 | return (rp==wp)?(true):(false); |
xshige | 0:189db48772ae | 107 | } |
xshige | 0:189db48772ae | 108 | |
xshige | 0:189db48772ae | 109 | int RingBuffer::IsNumBytes(void) |
xshige | 0:189db48772ae | 110 | { |
xshige | 0:189db48772ae | 111 | return num; |
xshige | 0:189db48772ae | 112 | } |
xshige | 0:189db48772ae | 113 | |
xshige | 0:189db48772ae | 114 | //----------------- |
xshige | 0:189db48772ae | 115 | |
xshige | 0:189db48772ae | 116 | // create RingBuffer |
xshige | 0:189db48772ae | 117 | RingBuffer UDPbuf(64); |
xshige | 0:189db48772ae | 118 | |
xshige | 0:189db48772ae | 119 | void onUDPSocketEvent(UDPSocketEvent e) |
xshige | 0:189db48772ae | 120 | { |
xshige | 0:189db48772ae | 121 | switch(e) |
xshige | 0:189db48772ae | 122 | { |
xshige | 0:189db48772ae | 123 | case UDPSOCKET_READABLE: //The only event for now |
xshige | 0:189db48772ae | 124 | char buf[64] = {0}; |
xshige | 0:189db48772ae | 125 | Host host; |
xshige | 0:189db48772ae | 126 | while( int len = udp.recvfrom( buf, 63, &host ) ) |
xshige | 0:189db48772ae | 127 | { |
xshige | 0:189db48772ae | 128 | if( len <= 0 ) break; |
xshige | 0:189db48772ae | 129 | for(int i=0;i<len;i++) { |
xshige | 0:189db48772ae | 130 | UDPbuf.PutByte(buf[i]); |
xshige | 0:189db48772ae | 131 | } |
xshige | 0:189db48772ae | 132 | // printf("\r\nFrom %d.%d.%d.%d\r\n", |
xshige | 0:189db48772ae | 133 | // host.getIp()[0], host.getIp()[1], host.getIp()[2], host.getIp()[3]); //debug |
xshige | 0:189db48772ae | 134 | // printf("len:%d\r\n",len); // debug |
xshige | 0:189db48772ae | 135 | } |
xshige | 0:189db48772ae | 136 | break; |
xshige | 0:189db48772ae | 137 | } |
xshige | 0:189db48772ae | 138 | } |
xshige | 0:189db48772ae | 139 | |
xshige | 0:189db48772ae | 140 | DigitalOut led1(LED1, "led1"); |
xshige | 0:189db48772ae | 141 | DigitalOut led2(LED2, "led2"); |
xshige | 0:189db48772ae | 142 | DigitalOut led3(LED3, "led3"); |
xshige | 0:189db48772ae | 143 | DigitalOut led4(LED4, "led4"); |
xshige | 0:189db48772ae | 144 | |
xshige | 0:189db48772ae | 145 | BusOut myleds(LED1, LED2, LED3, LED4); |
xshige | 0:189db48772ae | 146 | |
xshige | 0:189db48772ae | 147 | int main() { |
xshige | 0:189db48772ae | 148 | |
xshige | 0:189db48772ae | 149 | // make debug port Fast |
xshige | 0:189db48772ae | 150 | Serial pc(USBTX, USBRX); |
xshige | 0:189db48772ae | 151 | // pc.baud(9600); |
xshige | 0:189db48772ae | 152 | pc.baud(115200); |
xshige | 0:189db48772ae | 153 | // pc.baud(230400); |
xshige | 0:189db48772ae | 154 | |
xshige | 0:189db48772ae | 155 | |
xshige | 0:189db48772ae | 156 | printf("Setting up...\r\n"); |
xshige | 0:189db48772ae | 157 | EthernetErr ethErr = eth.setup(); |
xshige | 0:189db48772ae | 158 | if(ethErr) |
xshige | 0:189db48772ae | 159 | { |
xshige | 0:189db48772ae | 160 | printf("Error %d in setup.\r\n", ethErr); |
xshige | 0:189db48772ae | 161 | return -1; |
xshige | 0:189db48772ae | 162 | } |
xshige | 0:189db48772ae | 163 | printf("Setup OK\r\n"); |
xshige | 0:189db48772ae | 164 | |
xshige | 0:189db48772ae | 165 | //Host broadcast(IpAddr(192, 168, 0,255), 12345, NULL); // local broadcast over router |
xshige | 0:189db48772ae | 166 | //Host multicast(IpAddr(239, 192, 1, 100), 50000, NULL); // Join multicast group on port 50000 |
xshige | 0:189db48772ae | 167 | Host broadcast(IpAddr(255, 255, 255,255), 8888, NULL); // broadcast in a router |
xshige | 0:189db48772ae | 168 | |
xshige | 0:189db48772ae | 169 | udp.setOnEvent(&onUDPSocketEvent); |
xshige | 0:189db48772ae | 170 | |
xshige | 0:189db48772ae | 171 | udp.bind(broadcast); |
xshige | 0:189db48772ae | 172 | |
xshige | 0:189db48772ae | 173 | Timer tmr; |
xshige | 0:189db48772ae | 174 | tmr.start(); |
xshige | 0:189db48772ae | 175 | |
xshige | 0:189db48772ae | 176 | #ifdef SERIAL_MIDI |
xshige | 0:189db48772ae | 177 | printf("Serial MIDI enable\r\n"); |
xshige | 0:189db48772ae | 178 | #else |
xshige | 0:189db48772ae | 179 | printf("Serial MIDI disable\r\n"); |
xshige | 0:189db48772ae | 180 | #endif |
xshige | 0:189db48772ae | 181 | |
xshige | 0:189db48772ae | 182 | |
xshige | 0:189db48772ae | 183 | #ifdef UDP_MIDI |
xshige | 0:189db48772ae | 184 | printf("IP MIDI enable\r\n"); |
xshige | 0:189db48772ae | 185 | #else |
xshige | 0:189db48772ae | 186 | printf("IP MIDI disable\r\n"); |
xshige | 0:189db48772ae | 187 | #endif |
xshige | 0:189db48772ae | 188 | |
xshige | 0:189db48772ae | 189 | |
xshige | 0:189db48772ae | 190 | while(true) |
xshige | 0:189db48772ae | 191 | { |
xshige | 0:189db48772ae | 192 | Net::poll(); |
xshige | 0:189db48772ae | 193 | int velocity,MIDIch,pitch; |
xshige | 0:189db48772ae | 194 | // |
xshige | 0:189db48772ae | 195 | #ifdef SERIAL_MIDI |
xshige | 0:189db48772ae | 196 | if(pc.readable()) { |
xshige | 0:189db48772ae | 197 | int c; |
xshige | 0:189db48772ae | 198 | c=pc.getc(); // get 1st byte |
xshige | 0:189db48772ae | 199 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 200 | MIDIch=c&0x0F; |
xshige | 0:189db48772ae | 201 | c=c&0xF0; |
xshige | 0:189db48772ae | 202 | if (0xB0==c) { |
xshige | 0:189db48772ae | 203 | c=pc.getc(); |
xshige | 0:189db48772ae | 204 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 205 | if (0x0E==c) { // Knob#1 on KORG Kontrol (SCENE#1) |
xshige | 0:189db48772ae | 206 | c=pc.getc(); // get volume |
xshige | 0:189db48772ae | 207 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 208 | // MIDI controls LEDs |
xshige | 0:189db48772ae | 209 | myleds=(15*c)/127; |
xshige | 0:189db48772ae | 210 | } |
xshige | 0:189db48772ae | 211 | if (0x02==c) { // Slider#1 on KORG Kontrol (SCENE#1) |
xshige | 0:189db48772ae | 212 | c=pc.getc(); // get volume |
xshige | 0:189db48772ae | 213 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 214 | // MIDI controls LEDs |
xshige | 0:189db48772ae | 215 | myleds=(15*c)/127; |
xshige | 0:189db48772ae | 216 | } |
xshige | 0:189db48772ae | 217 | } // if (0xB0==c) |
xshige | 0:189db48772ae | 218 | if (0x90==c) { // Note On |
xshige | 0:189db48772ae | 219 | c=pc.getc(); // get Note# |
xshige | 0:189db48772ae | 220 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 221 | // Note# controls LEDs |
xshige | 0:189db48772ae | 222 | myleds=(c%0x10); |
xshige | 0:189db48772ae | 223 | c=pc.getc(); // get Velocity |
xshige | 0:189db48772ae | 224 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 225 | velocity=c; |
xshige | 0:189db48772ae | 226 | } |
xshige | 0:189db48772ae | 227 | if (0x80==c) { // Note Off |
xshige | 0:189db48772ae | 228 | c=pc.getc(); // get Note# |
xshige | 0:189db48772ae | 229 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 230 | // Note# controls LEDs |
xshige | 0:189db48772ae | 231 | myleds=(c%0x10); |
xshige | 0:189db48772ae | 232 | c=pc.getc(); // get Velocity |
xshige | 0:189db48772ae | 233 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 234 | // Velocity(at Note On) controls LEDs |
xshige | 0:189db48772ae | 235 | myleds=(15*velocity)/127; |
xshige | 0:189db48772ae | 236 | } |
xshige | 0:189db48772ae | 237 | if (0xE0==c) { // Pich Bend |
xshige | 0:189db48772ae | 238 | c=pc.getc(); // get LSB |
xshige | 0:189db48772ae | 239 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 240 | if (0xE0==c) c=pc.getc(); // re-get for error recover |
xshige | 0:189db48772ae | 241 | pitch=(c&0x7F); |
xshige | 0:189db48772ae | 242 | c=pc.getc(); // get MSB |
xshige | 0:189db48772ae | 243 | printf("Received:%02X\r\n",c); |
xshige | 0:189db48772ae | 244 | pitch+=((c&0x7F)<<7);pitch-=8192; |
xshige | 0:189db48772ae | 245 | printf("Pitch Bend:%d\r\n",pitch); |
xshige | 0:189db48772ae | 246 | |
xshige | 0:189db48772ae | 247 | } |
xshige | 0:189db48772ae | 248 | } // pc.readable |
xshige | 0:189db48772ae | 249 | #endif // ifdef SERIAL_MIDI |
xshige | 0:189db48772ae | 250 | #ifdef UDP_MIDI |
xshige | 0:189db48772ae | 251 | // get MIDI data in UDP packet |
xshige | 0:189db48772ae | 252 | if(UDPbuf.IsNumBytes()>3) { |
xshige | 0:189db48772ae | 253 | unsigned char uc; |
xshige | 0:189db48772ae | 254 | UDPbuf.GetByte(&uc); // get 1st byte |
xshige | 0:189db48772ae | 255 | printf("\r\nFrom Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 256 | MIDIch=uc&0x0F; |
xshige | 0:189db48772ae | 257 | uc=uc&0xF0; |
xshige | 0:189db48772ae | 258 | if (0xB0==uc) { |
xshige | 0:189db48772ae | 259 | UDPbuf.GetByte(&uc); |
xshige | 0:189db48772ae | 260 | if (0xB0==uc) UDPbuf.GetByte(&uc); // re-get for error recover |
xshige | 0:189db48772ae | 261 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 262 | if (0x0E==uc) { // Knob#1 on KORG Kontrol (SCENE#1) |
xshige | 0:189db48772ae | 263 | UDPbuf.GetByte(&uc); // get volume |
xshige | 0:189db48772ae | 264 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 265 | // MIDI controls LEDs |
xshige | 0:189db48772ae | 266 | myleds=(15*uc)/127; |
xshige | 0:189db48772ae | 267 | } |
xshige | 0:189db48772ae | 268 | if (0x02==uc) { // Slider#1 on KORG Kontrol (SCENE#1) |
xshige | 0:189db48772ae | 269 | UDPbuf.GetByte(&uc); // get volume |
xshige | 0:189db48772ae | 270 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 271 | // MIDI controls LEDs |
xshige | 0:189db48772ae | 272 | myleds=(15*uc)/127; |
xshige | 0:189db48772ae | 273 | } |
xshige | 0:189db48772ae | 274 | } // if (0xB0==uc) |
xshige | 0:189db48772ae | 275 | if (0x90==uc) { // Note On |
xshige | 0:189db48772ae | 276 | UDPbuf.GetByte(&uc); // get Note# |
xshige | 0:189db48772ae | 277 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 278 | // Note# controls LEDs |
xshige | 0:189db48772ae | 279 | myleds=(uc%0x10); |
xshige | 0:189db48772ae | 280 | UDPbuf.GetByte(&uc); // get Velocity |
xshige | 0:189db48772ae | 281 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 282 | velocity=uc; |
xshige | 0:189db48772ae | 283 | } |
xshige | 0:189db48772ae | 284 | if (0x80==uc) { // Note Off |
xshige | 0:189db48772ae | 285 | UDPbuf.GetByte(&uc); // get Note# |
xshige | 0:189db48772ae | 286 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 287 | // Note# controls LEDs |
xshige | 0:189db48772ae | 288 | myleds=(uc%0x10); |
xshige | 0:189db48772ae | 289 | UDPbuf.GetByte(&uc); // get Velocity |
xshige | 0:189db48772ae | 290 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 291 | // Velocity(at Note On) controls LEDs |
xshige | 0:189db48772ae | 292 | myleds=(15*velocity)/127; |
xshige | 0:189db48772ae | 293 | } |
xshige | 0:189db48772ae | 294 | if (0xE0==uc) { // Pich Bend |
xshige | 0:189db48772ae | 295 | UDPbuf.GetByte(&uc); // get LSB |
xshige | 0:189db48772ae | 296 | if (0xE0==uc) UDPbuf.GetByte(&uc); // re-get for error recover |
xshige | 0:189db48772ae | 297 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 298 | pitch=(uc&0x7F); |
xshige | 0:189db48772ae | 299 | UDPbuf.GetByte(&uc); // get MSB |
xshige | 0:189db48772ae | 300 | printf("From Net:%02X\r\n",uc); |
xshige | 0:189db48772ae | 301 | pitch+=((uc&0x7F)<<7);pitch-=8192; |
xshige | 0:189db48772ae | 302 | printf("Pitch Bend From Net:%d\r\n",pitch); |
xshige | 0:189db48772ae | 303 | } |
xshige | 0:189db48772ae | 304 | } // if !UDPbuf.IsNumBytes() |
xshige | 0:189db48772ae | 305 | #endif // ifdef UDP_MIDI |
xshige | 0:189db48772ae | 306 | // |
xshige | 0:189db48772ae | 307 | if(tmr.read() > 0.5) { |
xshige | 0:189db48772ae | 308 | tmr.reset(); |
xshige | 0:189db48772ae | 309 | |
xshige | 0:189db48772ae | 310 | // blink to indicate alive |
xshige | 0:189db48772ae | 311 | led1=!led1; |
xshige | 0:189db48772ae | 312 | } |
xshige | 0:189db48772ae | 313 | } // while(true) |
xshige | 0:189db48772ae | 314 | } |
xshige | 0:189db48772ae | 315 | |
xshige | 0:189db48772ae | 316 | //--------------------------------------------------------------------------- |
xshige | 0:189db48772ae | 317 | #if 0 |
xshige | 0:189db48772ae | 318 | /* |
xshige | 0:189db48772ae | 319 | midi_bridge.c |
xshige | 0:189db48772ae | 320 | |
xshige | 0:189db48772ae | 321 | How to Compile: |
xshige | 0:189db48772ae | 322 | gcc midi_bridge.c -o midi_bridge |
xshige | 0:189db48772ae | 323 | |
xshige | 0:189db48772ae | 324 | How to execute(on linux): |
xshige | 0:189db48772ae | 325 | <connet USB-MIDI devices and mbed USB to PC> |
xshige | 0:189db48772ae | 326 | midi_bridge[Enter] |
xshige | 0:189db48772ae | 327 | init serial between mbed and PC |
xshige | 0:189db48772ae | 328 | init serial for USB-MIDI |
xshige | 0:189db48772ae | 329 | Connected on /dev/mid2 |
xshige | 0:189db48772ae | 330 | Connected on /dev/mid3 |
xshige | 0:189db48772ae | 331 | Setting up... |
xshige | 0:189db48772ae | 332 | [..\fwk\if\eth\EthernetNetIf.cpp:setup@86] HW Addr is : 00:02:f7:f0:4f:9d. |
xshige | 0:189db48772ae | 333 | [..\fwk\if\eth\EthernetNetIf.cpp:setup@99] DHCP Started, waiting for IP... |
xshige | 0:189db48772ae | 334 | [..\fwk\if\eth\EthernetNetIf.cpp:setup@142] Connected, IP : 192.168.0.2 |
xshige | 0:189db48772ae | 335 | Setup OK |
xshige | 0:189db48772ae | 336 | Serial MIDI disable |
xshige | 0:189db48772ae | 337 | IP MIDI enable |
xshige | 0:189db48772ae | 338 | <move knob#1> |
xshige | 0:189db48772ae | 339 | B0 |
xshige | 0:189db48772ae | 340 | 0E |
xshige | 0:189db48772ae | 341 | 35 |
xshige | 0:189db48772ae | 342 | |
xshige | 0:189db48772ae | 343 | From Net:B0 |
xshige | 0:189db48772ae | 344 | From Net:0E |
xshige | 0:189db48772ae | 345 | From Net:36 |
xshige | 0:189db48772ae | 346 | B0 |
xshige | 0:189db48772ae | 347 | 0E |
xshige | 0:189db48772ae | 348 | 34 |
xshige | 0:189db48772ae | 349 | |
xshige | 0:189db48772ae | 350 | From Net:B0 |
xshige | 0:189db48772ae | 351 | From Net:0E |
xshige | 0:189db48772ae | 352 | From Net:35 |
xshige | 0:189db48772ae | 353 | .... |
xshige | 0:189db48772ae | 354 | |
xshige | 0:189db48772ae | 355 | */ |
xshige | 0:189db48772ae | 356 | |
xshige | 0:189db48772ae | 357 | |
xshige | 0:189db48772ae | 358 | //---- |
xshige | 0:189db48772ae | 359 | |
xshige | 0:189db48772ae | 360 | // MIDI bridge among USB-MIDI, PC-serial, and IP-packet |
xshige | 0:189db48772ae | 361 | // |
xshige | 0:189db48772ae | 362 | // written by:xshige |
xshige | 0:189db48772ae | 363 | |
xshige | 0:189db48772ae | 364 | // v0.5 2010/9/18 |
xshige | 0:189db48772ae | 365 | // (implemented path: USB-MIDI to PC-serial, USB-MIDI to IP-packet) |
xshige | 0:189db48772ae | 366 | |
xshige | 0:189db48772ae | 367 | #include <stdio.h> |
xshige | 0:189db48772ae | 368 | #include <stdlib.h> |
xshige | 0:189db48772ae | 369 | |
xshige | 0:189db48772ae | 370 | //#include <io.h> |
xshige | 0:189db48772ae | 371 | #include <fcntl.h> |
xshige | 0:189db48772ae | 372 | |
xshige | 0:189db48772ae | 373 | #include <unistd.h> |
xshige | 0:189db48772ae | 374 | #include <termios.h> |
xshige | 0:189db48772ae | 375 | #include <signal.h> |
xshige | 0:189db48772ae | 376 | #include <errno.h> |
xshige | 0:189db48772ae | 377 | |
xshige | 0:189db48772ae | 378 | // socket |
xshige | 0:189db48772ae | 379 | #include <sys/types.h> |
xshige | 0:189db48772ae | 380 | #include <sys/socket.h> |
xshige | 0:189db48772ae | 381 | #include <netinet/in.h> |
xshige | 0:189db48772ae | 382 | |
xshige | 0:189db48772ae | 383 | |
xshige | 0:189db48772ae | 384 | #define max(a,b) ((a) > (b) ? (a) : (b)) |
xshige | 0:189db48772ae | 385 | |
xshige | 0:189db48772ae | 386 | int main(void) |
xshige | 0:189db48772ae | 387 | { |
xshige | 0:189db48772ae | 388 | // init socket |
xshige | 0:189db48772ae | 389 | int sock; |
xshige | 0:189db48772ae | 390 | struct sockaddr_in addr; |
xshige | 0:189db48772ae | 391 | int yes = 1; |
xshige | 0:189db48772ae | 392 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
xshige | 0:189db48772ae | 393 | addr.sin_family = AF_INET; |
xshige | 0:189db48772ae | 394 | // ******* IP setup ******* |
xshige | 0:189db48772ae | 395 | addr.sin_port = htons(8888); // port# |
xshige | 0:189db48772ae | 396 | //addr.sin_port = htons(50000); // port# |
xshige | 0:189db48772ae | 397 | //addr.sin_addr.s_addr = inet_addr("239.192.1.100"); //Join multicast group on port 50000 |
xshige | 0:189db48772ae | 398 | addr.sin_addr.s_addr = inet_addr("255.255.255.255"); // broadcast in a router |
xshige | 0:189db48772ae | 399 | //addr.sin_addr.s_addr = inet_addr("192.168.0.255"); // local broadcast over routers |
xshige | 0:189db48772ae | 400 | //memo: multicast IP 224.0.0.1 hosts in the subnet |
xshige | 0:189db48772ae | 401 | // ************************* |
xshige | 0:189db48772ae | 402 | // |
xshige | 0:189db48772ae | 403 | setsockopt(sock, |
xshige | 0:189db48772ae | 404 | SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof(yes)); |
xshige | 0:189db48772ae | 405 | |
xshige | 0:189db48772ae | 406 | // *** serial for mbed *** |
xshige | 0:189db48772ae | 407 | printf("init serial between mbed and PC\n"); |
xshige | 0:189db48772ae | 408 | struct termios options; |
xshige | 0:189db48772ae | 409 | int fdt=open("/dev/ttyACM0",O_RDWR|O_NOCTTY|O_NONBLOCK); |
xshige | 0:189db48772ae | 410 | if (fdt == -1) { |
xshige | 0:189db48772ae | 411 | perror("open_port: Unable to open port between mbed and PC - "); |
xshige | 0:189db48772ae | 412 | } |
xshige | 0:189db48772ae | 413 | // Get the current options for the port... |
xshige | 0:189db48772ae | 414 | tcgetattr(fdt, &options); |
xshige | 0:189db48772ae | 415 | // Set the baud rates to 1115200 bps for mbed |
xshige | 0:189db48772ae | 416 | cfsetispeed(&options, B115200); |
xshige | 0:189db48772ae | 417 | cfsetospeed(&options, B115200); |
xshige | 0:189db48772ae | 418 | // No parity (8N1) |
xshige | 0:189db48772ae | 419 | options.c_cflag &= ~PARENB; |
xshige | 0:189db48772ae | 420 | options.c_cflag &= ~CSTOPB; |
xshige | 0:189db48772ae | 421 | options.c_cflag &= ~CSIZE; |
xshige | 0:189db48772ae | 422 | options.c_cflag |= CS8; |
xshige | 0:189db48772ae | 423 | // Set the new options for the port... |
xshige | 0:189db48772ae | 424 | tcsetattr(fdt, TCSANOW, &options); |
xshige | 0:189db48772ae | 425 | |
xshige | 0:189db48772ae | 426 | // *** serial for MIDI *** |
xshige | 0:189db48772ae | 427 | printf("init serial for USB-MIDI\n"); |
xshige | 0:189db48772ae | 428 | int fdm2=open("/dev/midi2", O_RDONLY|O_NONBLOCK); |
xshige | 0:189db48772ae | 429 | int fdm3=open("/dev/midi3", O_RDONLY|O_NONBLOCK); |
xshige | 0:189db48772ae | 430 | int fdm4=open("/dev/midi4", O_RDONLY|O_NONBLOCK); |
xshige | 0:189db48772ae | 431 | if (fdm2 != -1) printf("Connected on /dev/mid2\n"); |
xshige | 0:189db48772ae | 432 | if (fdm3 != -1) printf("Connected on /dev/mid3\n"); |
xshige | 0:189db48772ae | 433 | if (fdm4 != -1) printf("Connected on /dev/mid4\n"); |
xshige | 0:189db48772ae | 434 | |
xshige | 0:189db48772ae | 435 | // Init FDs for Select |
xshige | 0:189db48772ae | 436 | int maxfd=-1; |
xshige | 0:189db48772ae | 437 | fd_set fds,readfds; |
xshige | 0:189db48772ae | 438 | struct timeval timeout; |
xshige | 0:189db48772ae | 439 | // Init the read FD set |
xshige | 0:189db48772ae | 440 | FD_ZERO(&readfds); |
xshige | 0:189db48772ae | 441 | FD_SET(fdt, &readfds); |
xshige | 0:189db48772ae | 442 | if (fdm2 != -1) FD_SET(fdm2, &readfds); |
xshige | 0:189db48772ae | 443 | if (fdm3 != -1) FD_SET(fdm3, &readfds); |
xshige | 0:189db48772ae | 444 | if (fdm4 != -1) FD_SET(fdm4, &readfds); |
xshige | 0:189db48772ae | 445 | // get max fd |
xshige | 0:189db48772ae | 446 | maxfd=max(maxfd,fdt); |
xshige | 0:189db48772ae | 447 | if (fdm2 != -1) maxfd=max(maxfd,fdm2); |
xshige | 0:189db48772ae | 448 | if (fdm3 != -1) maxfd=max(maxfd,fdm3); |
xshige | 0:189db48772ae | 449 | if (fdm4 != -1) maxfd=max(maxfd,fdm4); |
xshige | 0:189db48772ae | 450 | maxfd=maxfd+1; |
xshige | 0:189db48772ae | 451 | // printf("FDs:%d %d %d %d\n",fdt,fdm2,fdm3,fdm4); // debug |
xshige | 0:189db48772ae | 452 | // printf("maxfd:%d\n",maxfd); // debug |
xshige | 0:189db48772ae | 453 | |
xshige | 0:189db48772ae | 454 | unsigned char cs,cm; |
xshige | 0:189db48772ae | 455 | while(1) { |
xshige | 0:189db48772ae | 456 | // init FDs for the select at everytime |
xshige | 0:189db48772ae | 457 | memcpy(&fds, &readfds, sizeof(fd_set)); |
xshige | 0:189db48772ae | 458 | // Init the timeout structure |
xshige | 0:189db48772ae | 459 | timeout.tv_sec = 3600; // set one hour for timeout |
xshige | 0:189db48772ae | 460 | timeout.tv_usec = 0; |
xshige | 0:189db48772ae | 461 | // Do the select |
xshige | 0:189db48772ae | 462 | int n = select(maxfd, &fds, NULL, NULL, &timeout); |
xshige | 0:189db48772ae | 463 | if (n == 0) { |
xshige | 0:189db48772ae | 464 | puts("*** TIMEOUT *** "); |
xshige | 0:189db48772ae | 465 | } else { |
xshige | 0:189db48772ae | 466 | // |
xshige | 0:189db48772ae | 467 | // for /dev/midi2 |
xshige | 0:189db48772ae | 468 | if (fdm2 != -1) |
xshige | 0:189db48772ae | 469 | if (FD_ISSET(fdm2,&fds)) if (read(fdm2,&cm,1) == 1) { |
xshige | 0:189db48772ae | 470 | // readable |
xshige | 0:189db48772ae | 471 | printf("%02X\n",cm); // display MIDI in HEX on console |
xshige | 0:189db48772ae | 472 | write(fdt,&cm,1); // USB-MIDI to mbed |
xshige | 0:189db48772ae | 473 | sendto(sock,&cm,1, // USB-MIDI to IP network |
xshige | 0:189db48772ae | 474 | 0, (struct sockaddr *)&addr, sizeof(addr)); |
xshige | 0:189db48772ae | 475 | } |
xshige | 0:189db48772ae | 476 | // for /dev/midi3 |
xshige | 0:189db48772ae | 477 | if (fdm3 != -1) |
xshige | 0:189db48772ae | 478 | if (FD_ISSET(fdm3,&fds)) if (read(fdm3,&cm,1) == 1) { |
xshige | 0:189db48772ae | 479 | // readable |
xshige | 0:189db48772ae | 480 | printf("%02X\n",cm); // display MIDI in HEX on console |
xshige | 0:189db48772ae | 481 | write(fdt,&cm,1); // USB-MIDI to mbed |
xshige | 0:189db48772ae | 482 | sendto(sock,&cm,1, // USB-MIDI to IP network |
xshige | 0:189db48772ae | 483 | 0, (struct sockaddr *)&addr, sizeof(addr)); |
xshige | 0:189db48772ae | 484 | |
xshige | 0:189db48772ae | 485 | |
xshige | 0:189db48772ae | 486 | } |
xshige | 0:189db48772ae | 487 | // for /dev/midi4 |
xshige | 0:189db48772ae | 488 | if (fdm4 != -1) |
xshige | 0:189db48772ae | 489 | if (FD_ISSET(fdm4,&fds)) if (read(fdm4,&cm,1) == 1) { |
xshige | 0:189db48772ae | 490 | // readable |
xshige | 0:189db48772ae | 491 | printf("%02X\n",cm); // display MIDI in HEX on console |
xshige | 0:189db48772ae | 492 | write(fdt,&cm,1); // USB-MIDI to mbed |
xshige | 0:189db48772ae | 493 | sendto(sock,&cm,1, // USB-MIDI to IP network |
xshige | 0:189db48772ae | 494 | 0, (struct sockaddr *)&addr, sizeof(addr)); |
xshige | 0:189db48772ae | 495 | |
xshige | 0:189db48772ae | 496 | |
xshige | 0:189db48772ae | 497 | } |
xshige | 0:189db48772ae | 498 | // for /dev/ttyACM0 |
xshige | 0:189db48772ae | 499 | if (FD_ISSET(fdt,&fds)) if (read(fdt,&cs,1) == 1) { |
xshige | 0:189db48772ae | 500 | // readable on mbed |
xshige | 0:189db48772ae | 501 | putchar(cs); // display to console |
xshige | 0:189db48772ae | 502 | //write(fdm2,&cs,1); // mbed to USB-MIDI |
xshige | 0:189db48772ae | 503 | } |
xshige | 0:189db48772ae | 504 | }// if else |
xshige | 0:189db48772ae | 505 | |
xshige | 0:189db48772ae | 506 | } // while |
xshige | 0:189db48772ae | 507 | } |
xshige | 0:189db48772ae | 508 | #endif |