struct MIDIMsg { uint8_t status; uint8_t note; uint8_t velocity; }; // getMIDIMsg - Non-blocking receive of MIDI message: // returns 0 if no MIDI message is ready // 1 if a MIDI note on/off message is read // Pays no attention to value of channel uint8_t getMIDIMsg(struct MIDIMsg *msg) { uint8_t nul; // Don't block if message isn't ready if( !(UCSRA & (1 << RXC)) ) return 0; // Otherwise load in status byte msg->status = UDR; // If note on or note off, then... if(((msg->status & 0xF0) == 0x90) || ((msg->status & 0xF0) == 0x80)) { // Wait for USART receive while( !(UCSRA & (1 << RXC)) ); msg->note = UDR; // Wait for USART receive while( !(UCSRA & (1 << RXC)) ); msg->velocity = UDR; } else { while( !(UCSRA & (1 << RXC)) ); // Read next byte to clear register nul = UDR; } return 1; }