Remove legacy AS4 project files, now that they can be automatically generated by...
[pub/USBasp.git] / Demos / Device / LowLevel / MIDI / MIDI.c
index a9843ed..335c714 100644 (file)
@@ -1,21 +1,21 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2012.
+
   dean [at] fourwalledcubicle [dot] com
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2012  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
   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
   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
   software without specific, written prior permission.
 
   The author disclaim all warranties with regard to this
 int main(void)
 {
        SetupHardware();
 int main(void)
 {
        SetupHardware();
-       
+
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
        sei();
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
        sei();
-       
+
        for (;;)
        {
                MIDI_Task();
        for (;;)
        {
                MIDI_Task();
@@ -62,7 +62,7 @@ void SetupHardware(void)
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
-       
+
        /* Hardware Initialization */
        Joystick_Init();
        LEDs_Init();
        /* Hardware Initialization */
        Joystick_Init();
        LEDs_Init();
@@ -91,23 +91,14 @@ void EVENT_USB_Device_Disconnect(void)
  */
 void EVENT_USB_Device_ConfigurationChanged(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_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
+       ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, 1);
+
+       /* 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
 }
 
 /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them
@@ -121,16 +112,16 @@ void MIDI_Task(void)
        if (USB_DeviceState != DEVICE_STATE_Configured)
          return;
 
        if (USB_DeviceState != DEVICE_STATE_Configured)
          return;
 
-       Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
+       Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPADDR);
 
        if (Endpoint_IsINReady())
        {
                uint8_t MIDICommand = 0;
                uint8_t MIDIPitch;
 
        if (Endpoint_IsINReady())
        {
                uint8_t MIDICommand = 0;
                uint8_t MIDIPitch;
-       
+
                uint8_t JoystickStatus  = Joystick_GetStatus();
                uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);
                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));
 
                /* 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 +142,7 @@ void MIDI_Task(void)
                        MIDICommand = ((JoystickStatus & JOY_RIGHT)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
                        MIDIPitch   = 0x3E;
                }
                        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);
                if (JoystickChanges & JOY_DOWN)
                {
                        MIDICommand = ((JoystickStatus & JOY_DOWN)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
@@ -167,40 +158,39 @@ void MIDI_Task(void)
                /* Check if a MIDI command is to be sent */
                if (MIDICommand)
                {
                /* 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),
-                                       
+                                       .Event       = MIDI_EVENT(0, MIDICommand),
+
                                        .Data1       = MIDICommand | Channel,
                                        .Data2       = MIDIPitch,
                                        .Data1       = MIDICommand | Channel,
                                        .Data2       = MIDIPitch,
-                                       .Data3       = MIDI_STANDARD_VELOCITY,                  
+                                       .Data3       = MIDI_STANDARD_VELOCITY,
                                };
                                };
-                               
+
                        /* Write the MIDI event packet to the endpoint */
                        /* 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();
                }
                        /* Send the data in the endpoint to the host */
                        Endpoint_ClearIN();
                }
-               
+
                /* Save previous joystick value for next joystick change detection */
                PrevJoystickStatus = JoystickStatus;
        }
 
        /* Select the MIDI OUT stream */
                /* Save previous joystick value for next joystick change detection */
                PrevJoystickStatus = JoystickStatus;
        }
 
        /* Select the MIDI OUT stream */
-       Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM);
+       Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPADDR);
 
        /* Check if a MIDI command has been received */
        if (Endpoint_IsOUTReceived())
        {
 
        /* 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 */
                /* 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 */
                /* 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))
+               if ((MIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && (MIDIEvent.Data3 > 0))
                {
                        /* Change LEDs depending on the pitch of the sent note */
                        LEDs_SetAllLEDs(MIDIEvent.Data2 > 64 ? LEDS_LED1 : LEDS_LED2);
                {
                        /* Change LEDs depending on the pitch of the sent note */
                        LEDs_SetAllLEDs(MIDIEvent.Data2 > 64 ? LEDS_LED1 : LEDS_LED2);
@@ -210,8 +200,13 @@ void MIDI_Task(void)
                        /* Turn off all LEDs in response to non Note On messages */
                        LEDs_SetAllLEDs(LEDS_NO_LEDS);
                }
                        /* 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();
+               }
        }
 }
        }
 }
+