Update UC3 platform driver support to use the bitmasks defined in the header files...
[pub/USBasp.git] / Demos / Device / LowLevel / MIDI / MIDI.c
index a9843ed..bd2ffa9 100644 (file)
@@ -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
 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();
@@ -91,23 +91,16 @@ void EVENT_USB_Device_Disconnect(void)
  */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
-       /* Indicate USB connected and ready */
-       LEDs_SetAllLEDs(LEDMASK_USB_READY);
+       bool ConfigSuccess = true;
 
-       /* Setup MIDI stream endpoints */
-       if (!(Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPNUM, EP_TYPE_BULK,
-                                            ENDPOINT_DIR_OUT, MIDI_STREAM_EPSIZE,
-                                        ENDPOINT_BANK_SINGLE)))
-       {
-               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
-       }       
-       
-       if (!(Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM, EP_TYPE_BULK,
-                                            ENDPOINT_DIR_IN, MIDI_STREAM_EPSIZE,
-                                        ENDPOINT_BANK_SINGLE)))
-       {
-               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
-       }
+       /* Setup MIDI Data Endpoints */
+       ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN,
+                                                   MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
+       ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT,
+                                                   MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
+
+       /* Indicate endpoint configuration success or failure */
+       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
@@ -127,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));
 
@@ -151,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);
@@ -167,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;
        }
@@ -194,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))
                {
@@ -210,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();
+               }
        }
 }
+