Fix flip-ee DFU build target accidentally erasing the target FLASH memory space.
[pub/USBasp.git] / Demos / Device / ClassDriver / MIDI / MIDI.c
index f627d55..9277659 100644 (file)
@@ -1,21 +1,21 @@
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2012.
+
   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
-  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
@@ -45,17 +45,22 @@ USB_ClassInfo_MIDI_Device_t Keyboard_MIDI_Interface =
                .Config =
                        {
                                .StreamingInterfaceNumber = 1,
-
-                               .DataINEndpointNumber      = MIDI_STREAM_IN_EPNUM,
-                               .DataINEndpointSize        = MIDI_STREAM_EPSIZE,
-                               .DataINEndpointDoubleBank  = false,
-
-                               .DataOUTEndpointNumber     = MIDI_STREAM_OUT_EPNUM,
-                               .DataOUTEndpointSize       = MIDI_STREAM_EPSIZE,
-                               .DataOUTEndpointDoubleBank = false,
+                               .DataINEndpoint           =
+                                       {
+                                               .Address          = MIDI_STREAM_IN_EPADDR,
+                                               .Size             = MIDI_STREAM_EPSIZE,
+                                               .Banks            = 1,
+                                       },
+                               .DataOUTEndpoint          =
+                                       {
+                                               .Address          = MIDI_STREAM_OUT_EPADDR,
+                                               .Size             = MIDI_STREAM_EPSIZE,
+                                               .Banks            = 1,
+                                       },
                        },
        };
 
+
 /** Main program entry point. This routine contains the overall program flow, including initial
  *  setup of all components and the main program loop.
  */
@@ -64,21 +69,21 @@ int main(void)
        SetupHardware();
 
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-       sei();
-       
+       GlobalInterruptEnable();
+
        for (;;)
        {
                CheckJoystickMovement();
-               
+
                MIDI_EventPacket_t ReceivedMIDIEvent;
-               if (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent))
+               while (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent))
                {
-                       if ((ReceivedMIDIEvent.Command == (MIDI_COMMAND_NOTE_ON >> 4)) && (ReceivedMIDIEvent.Data3 > 0))
+                       if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && (ReceivedMIDIEvent.Data3 > 0))
                          LEDs_SetAllLEDs(ReceivedMIDIEvent.Data2 > 64 ? LEDS_LED1 : LEDS_LED2);
                        else
                          LEDs_SetAllLEDs(LEDS_NO_LEDS);
                }
-       
+
                MIDI_Device_USBTask(&Keyboard_MIDI_Interface);
                USB_USBTask();
        }
@@ -93,7 +98,7 @@ void SetupHardware(void)
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
-       
+
        /* Hardware Initialization */
        Joystick_Init();
        LEDs_Init();
@@ -108,11 +113,11 @@ void CheckJoystickMovement(void)
 
        uint8_t MIDICommand = 0;
        uint8_t MIDIPitch;
-       
+
        /* Get current joystick mask, XOR with previous to detect joystick changes */
        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));
 
@@ -133,7 +138,7 @@ void CheckJoystickMovement(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);
@@ -145,19 +150,18 @@ void CheckJoystickMovement(void)
                MIDICommand = ((JoystickStatus & JOY_PRESS)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
                MIDIPitch   = 0x3B;
        }
-       
+
        if (MIDICommand)
        {
                MIDI_EventPacket_t MIDIEvent = (MIDI_EventPacket_t)
                        {
-                               .CableNumber = 0,
-                               .Command     = (MIDICommand >> 4),
-                               
+                               .Event       = MIDI_EVENT(0, MIDICommand),
+
                                .Data1       = MIDICommand | Channel,
                                .Data2       = MIDIPitch,
-                               .Data3       = MIDI_STANDARD_VELOCITY,                  
+                               .Data3       = MIDI_STANDARD_VELOCITY,
                        };
-                       
+
                MIDI_Device_SendEventPacket(&Keyboard_MIDI_Interface, &MIDIEvent);
                MIDI_Device_Flush(&Keyboard_MIDI_Interface);
        }
@@ -184,13 +188,12 @@ void EVENT_USB_Device_ConfigurationChanged(void)
 
        ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&Keyboard_MIDI_Interface);
 
-       USB_Device_EnableSOFEvents();
-
        LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
-/** Event handler for the library USB Unhandled Control Request event. */
-void EVENT_USB_Device_UnhandledControlRequest(void)
+/** Event handler for the library USB Control Request reception event. */
+void EVENT_USB_Device_ControlRequest(void)
 {
        MIDI_Device_ProcessControlRequest(&Keyboard_MIDI_Interface);
 }
+