diff options
-rw-r--r-- | .vs/floppyMusic/v14/.atsuo | bin | 63488 -> 63488 bytes | |||
-rw-r--r-- | floppyMusic/main.c | 5 | ||||
-rw-r--r-- | floppyMusic/midi.c | 107 | ||||
-rw-r--r-- | floppyMusic/midi.h | 23 |
4 files changed, 88 insertions, 47 deletions
diff --git a/.vs/floppyMusic/v14/.atsuo b/.vs/floppyMusic/v14/.atsuo Binary files differindex 148c79a..f99dcac 100644 --- a/.vs/floppyMusic/v14/.atsuo +++ b/.vs/floppyMusic/v14/.atsuo diff --git a/floppyMusic/main.c b/floppyMusic/main.c index 8719312..56df5b2 100644 --- a/floppyMusic/main.c +++ b/floppyMusic/main.c @@ -8,6 +8,7 @@ //#define _TEST_MODE #include <avr/io.h> +#include <util/delay.h> #include "floppy.h" @@ -37,8 +38,8 @@ int main(void) for(;;) { - while (!(UCSRA & (1<<UDRE))); - UDR = 0b10101010; + midi_process(); + _delay_us(100); } #endif diff --git a/floppyMusic/midi.c b/floppyMusic/midi.c index ca32d5a..8e760a8 100644 --- a/floppyMusic/midi.c +++ b/floppyMusic/midi.c @@ -7,53 +7,65 @@ #include "midi.h" -const uint16_t midi_code_frequency_table[] = {8,9,9,10,10,11,12,12,13,14,15,15,16,17,18,19,21,22,23,25,26,28,29,31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978,5274,5588,5920,6272}; +static const uint16_t midi_code_frequency_table[] = {8,9,9,10,10,11,12,12,13,14,15,15,16,17,18,19,21,22,23,25,26,28,29,31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,4435,4699,4978,5274,5588,5920,6272}; + +static uint8_t midi_recv_ptr = 0; ISR(USART_RXC_vect) { - char data[3] = {0x00, 0x00, 0x00}; - char command, /*channel,*/ note/*, velocity*/; + midi_recv_buffer[midi_recv_ptr] = UDR; + midi_recv_ptr++; +} - UCSRB &= ~(1<<RXCIE);//Disable UART interrupt +void midi_setup() +{ + midi_active_channels = 0x00; + DDRA = 0xff; + PORTA = 0xff; + //UART init + unsigned int ubrr = (F_CPU / (16 * _MIDI_LINK_SPEED)) - 1; - //read data from interface - while(!(UCSRA & (1<<RXC))); //Wait for data - data[0] = UDR; + UBRRH = (unsigned char) (ubrr>>8) & 0xff; + UBRRL = (unsigned char) ubrr & 0xff; - for(int i = 0; i < 20; i++) { //Wait 100µs for next datapackage - _delay_us(1); - if((UCSRA & (1<<RXC))) { - data[1] = UDR; - break; - } - } - for(int i = 0; i < 20; i++) { - _delay_us(1); - if((UCSRA & (1<<RXC))) { - data[2] = UDR; - break; - } - } - UCSRB |= (1<<RXCIE);//reenable UART Interrupt + UCSRB = (1<<RXEN) | (1<<TXEN) | (1<<RXCIE); //Enable Tx, Rx and Rx Interrupt + UCSRC = (1<<URSEL) | (1<<USBS) | (3<<UCSZ0); + +} + +void midi_process() +{ + if(!midi_recv_avail()) + return; + + char data[3] = {0x00, 0x00, 0x00}; + char command, /*channel,*/ note, velocity; + + data[0] = midi_get_recv_buf(); + data[1] = midi_get_recv_buf(); + data[2] = midi_get_recv_buf(); - //split data /*channel = data[0] & 0x0f; //Mask out last 4 bits*/ command = (data[0] >> 4) & 0x0f; // Shift command into first 4 bits; note = data[1]; - /*velocity = data2;*/ - - //PORTA = ~command; - - switch(command) + velocity = data[2]; + + midi_command(command, note, velocity); + +} + +void midi_command(char _cmd, char _note, char _vel) +{ + switch(_cmd) { case 0x08: //Note off PORTA = 1; - midi_update_note(note, 0); + midi_update_note(_note, 0); break; case 0x09: //Note on PORTA = 2; - midi_update_note(note, 1); + midi_update_note(_note, 1); break; /* case 0x0A:break; //Note Fade Out @@ -68,23 +80,25 @@ ISR(USART_RXC_vect) } } -void midi_setup() +uint8_t midi_recv_avail() { - midi_active_channels = 0x00; - DDRA = 0xff; - PORTA = 0xff; - //UART init - unsigned int ubrr = (F_CPU / (16 * _MIDI_LINK_SPEED)) - 1; + return midi_recv_ptr; +} - UBRRH = (unsigned char) (ubrr>>8) & 0xff; - UBRRL = (unsigned char) ubrr & 0xff; +char midi_get_recv_buf() +{ + if(midi_recv_ptr > 0) + midi_recv_ptr--; + else + return 0x00; - UCSRB = (1<<RXEN) | (1<<TXEN) | (1<<RXCIE); //Enable Tx, Rx and Rx Interrupt - UCSRC = (1<<URSEL) | (1<<USBS) | (3<<UCSZ0); + return midi_recv_buffer[midi_recv_ptr]; } - void midi_update_note(uint16_t _note, uint8_t _status) + + +void midi_update_note(uint16_t _note, uint8_t _status) { if(_status) //Note on { @@ -113,3 +127,14 @@ void midi_setup() } } } + + +void midi_uart_out(unsigned char _c) + { + + /* Wait for empty transmit buffer */ + while ( !( UCSRA & (1<<UDRE)) ) + ; + /* Put data into buffer, sends the data */ + UDR = _c; + } diff --git a/floppyMusic/midi.h b/floppyMusic/midi.h index 7ee76fc..82248b4 100644 --- a/floppyMusic/midi.h +++ b/floppyMusic/midi.h @@ -38,19 +38,34 @@ #include "floppy.h" -char midi_active_channels; -uint16_t midi_current_notes[8]; -const uint16_t midi_code_frequency_table[232]; +static char midi_recv_buffer[16]; +static uint8_t midi_recv_ptr; + +static char midi_active_channels; +static uint16_t midi_current_notes[8]; +static const uint16_t midi_code_frequency_table[232]; void midi_setup(); /* * initializes UART communication and Interrupts */ +void midi_process(); +/* +* Run in loop +*/ + +static void midi_command(char _cmd, char _note, char _vel); -void midi_update_note(uint16_t _note, uint8_t _status); +static uint8_t midi_recv_avail(); + +static char midi_get_recv_buf(); + +static void midi_update_note(uint16_t _note, uint8_t _status); /* * _note : MIDI-standard note # * _status : 0=OFF >0=ON */ +static void midi_uart_out(unsigned char _c); + #endif /* MIDI_H_ */
\ No newline at end of file |