X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/158afe910947739b1df00000628c1e758bdf0812..77a9df36a77d2523dd2bc24fa17f9f04c6c175c5:/Demos/Device/LowLevel/MIDI/MIDI.c diff --git a/Demos/Device/LowLevel/MIDI/MIDI.c b/Demos/Device/LowLevel/MIDI/MIDI.c index 87ecbd4d0..bd2ffa995 100644 --- a/Demos/Device/LowLevel/MIDI/MIDI.c +++ b/Demos/Device/LowLevel/MIDI/MIDI.c @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2010. - + Copyright (C) Dean Camera, 2011. + dean [at] fourwalledcubicle [dot] com - www.fourwalledcubicle.com + www.lufa-lib.org */ /* - Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com) - Permission to use, copy, modify, distribute, and sell this + Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in + without fee, provided that the above copyright notice appear in all copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -42,10 +42,10 @@ int main(void) { SetupHardware(); - + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); sei(); - + for (;;) { MIDI_Task(); @@ -62,7 +62,7 @@ void SetupHardware(void) /* Disable clock division */ clock_prescale_set(clock_div_1); - + /* Hardware Initialization */ Joystick_Init(); LEDs_Init(); @@ -100,7 +100,7 @@ void EVENT_USB_Device_ConfigurationChanged(void) MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); /* Indicate endpoint configuration success or failure */ - LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); + LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); } /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them @@ -120,10 +120,10 @@ void MIDI_Task(void) { uint8_t MIDICommand = 0; uint8_t MIDIPitch; - + uint8_t JoystickStatus = Joystick_GetStatus(); uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus); - + /* Get board button status - if pressed use channel 10 (percussion), otherwise use channel 1 */ uint8_t Channel = ((Buttons_GetStatus() & BUTTONS_BUTTON1) ? MIDI_CHANNEL(10) : MIDI_CHANNEL(1)); @@ -144,7 +144,7 @@ void MIDI_Task(void) MIDICommand = ((JoystickStatus & JOY_RIGHT)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF); MIDIPitch = 0x3E; } - + if (JoystickChanges & JOY_DOWN) { MIDICommand = ((JoystickStatus & JOY_DOWN)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF); @@ -160,23 +160,23 @@ void MIDI_Task(void) /* Check if a MIDI command is to be sent */ if (MIDICommand) { - USB_MIDI_EventPacket_t MIDIEvent = (USB_MIDI_EventPacket_t) + MIDI_EventPacket_t MIDIEvent = (MIDI_EventPacket_t) { .CableNumber = 0, .Command = (MIDICommand >> 4), - + .Data1 = MIDICommand | Channel, .Data2 = MIDIPitch, - .Data3 = MIDI_STANDARD_VELOCITY, + .Data3 = MIDI_STANDARD_VELOCITY, }; - + /* Write the MIDI event packet to the endpoint */ - Endpoint_Write_Stream_LE(&MIDIEvent, sizeof(MIDIEvent)); - + Endpoint_Write_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL); + /* Send the data in the endpoint to the host */ Endpoint_ClearIN(); } - + /* Save previous joystick value for next joystick change detection */ PrevJoystickStatus = JoystickStatus; } @@ -187,11 +187,11 @@ void MIDI_Task(void) /* Check if a MIDI command has been received */ if (Endpoint_IsOUTReceived()) { - USB_MIDI_EventPacket_t MIDIEvent; - + MIDI_EventPacket_t MIDIEvent; + /* Read the MIDI event packet from the endpoint */ - Endpoint_Read_Stream_LE(&MIDIEvent, sizeof(MIDIEvent)); - + Endpoint_Read_Stream_LE(&MIDIEvent, sizeof(MIDIEvent), NULL); + /* Check to see if the sent command is a note on message with a non-zero velocity */ if ((MIDIEvent.Command == (MIDI_COMMAND_NOTE_ON >> 4)) && (MIDIEvent.Data3 > 0)) { @@ -203,8 +203,13 @@ void MIDI_Task(void) /* Turn off all LEDs in response to non Note On messages */ LEDs_SetAllLEDs(LEDS_NO_LEDS); } - - /* Clear the endpoint ready for new packet */ - Endpoint_ClearOUT(); + + /* If the endpoint is now empty, clear the bank */ + if (!(Endpoint_BytesInEndpoint())) + { + /* Clear the endpoint ready for new packet */ + Endpoint_ClearOUT(); + } } } +