3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  Main source file for the MIDI input demo. This file contains the main tasks of the demo and 
  34  *  is responsible for the initial application hardware configuration. 
  39 /* Scheduler Task List */ 
  42         { .Task 
= USB_USBTask          
, .TaskStatus 
= TASK_STOP 
}, 
  43         { .Task 
= USB_MIDI_Task        
, .TaskStatus 
= TASK_STOP 
}, 
  46 /** Main program entry point. This routine configures the hardware required by the application, then 
  47  *  starts the scheduler to run the application tasks. 
  51         /* Disable watchdog if enabled by bootloader/fuses */ 
  52         MCUSR 
&= ~(1 << WDRF
); 
  55         /* Disable clock division */ 
  56         clock_prescale_set(clock_div_1
); 
  58         /* Hardware Initialization */ 
  63         /* Indicate USB not ready */ 
  64         UpdateStatus(Status_USBNotReady
); 
  66         /* Initialize Scheduler so that it can be used */ 
  69         /* Initialize USB Subsystem */ 
  72         /* Scheduling - routine never returns, so put this last in the main function */ 
  76 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */ 
  77 void EVENT_USB_Connect(void) 
  79         /* Start USB management task */ 
  80         Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
); 
  82         /* Indicate USB enumerating */ 
  83         UpdateStatus(Status_USBEnumerating
); 
  86 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
  87  *  the status LEDs, disables the sample update and PWM output timers and stops the USB and MIDI management tasks. 
  89 void EVENT_USB_Disconnect(void) 
  91         /* Stop running audio and USB management tasks */ 
  92         Scheduler_SetTaskMode(USB_MIDI_Task
, TASK_STOP
); 
  93         Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
); 
  95         /* Indicate USB not ready */ 
  96         UpdateStatus(Status_USBNotReady
); 
  99 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
 100  *  of the USB device after enumeration - the device endpoints are configured and the MIDI management task started. 
 102 void EVENT_USB_ConfigurationChanged(void) 
 104         /* Setup MIDI stream endpoints */ 
 105         Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPNUM
, EP_TYPE_BULK
, 
 106                                        ENDPOINT_DIR_OUT
, MIDI_STREAM_EPSIZE
, 
 107                                    ENDPOINT_BANK_SINGLE
); 
 109         Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM
, EP_TYPE_BULK
, 
 110                                        ENDPOINT_DIR_IN
, MIDI_STREAM_EPSIZE
, 
 111                                    ENDPOINT_BANK_SINGLE
); 
 113         /* Indicate USB connected and ready */ 
 114         UpdateStatus(Status_USBReady
); 
 116         /* Start MIDI task */ 
 117         Scheduler_SetTaskMode(USB_MIDI_Task
, TASK_RUN
); 
 120 /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them 
 125         static uint8_t PrevJoystickStatus
; 
 127         /* Select the MIDI IN stream */ 
 128         Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM
); 
 130         /* Check if endpoint is ready to be written to */ 
 131         if (Endpoint_IsINReady()) 
 133                 /* Get current joystick mask, XOR with previous to detect joystick changes */ 
 134                 uint8_t JoystickStatus  
= Joystick_GetStatus(); 
 135                 uint8_t JoystickChanges 
= (JoystickStatus 
^ PrevJoystickStatus
); 
 137                 /* Get board button status - if pressed use channel 10 (percussion), otherwise use channel 1 */ 
 138                 uint8_t Channel 
= ((Buttons_GetStatus() & BUTTONS_BUTTON1
) ? 
MIDI_CHANNEL(10) : MIDI_CHANNEL(1)); 
 140                 if (JoystickChanges 
& JOY_LEFT
) 
 141                   SendMIDINoteChange(0x3C, (JoystickStatus 
& JOY_LEFT
), 0, Channel
); 
 143                 if (JoystickChanges 
& JOY_UP
) 
 144                   SendMIDINoteChange(0x3D, (JoystickStatus 
& JOY_UP
), 0, Channel
); 
 146                 if (JoystickChanges 
& JOY_RIGHT
) 
 147                   SendMIDINoteChange(0x3E, (JoystickStatus 
& JOY_RIGHT
), 0, Channel
); 
 149                 if (JoystickChanges 
& JOY_DOWN
) 
 150                   SendMIDINoteChange(0x3F, (JoystickStatus 
& JOY_DOWN
), 0, Channel
); 
 152                 if (JoystickChanges 
& JOY_PRESS
) 
 153                   SendMIDINoteChange(0x3B, (JoystickStatus 
& JOY_PRESS
), 0, Channel
); 
 155                 /* Save previous joystick value for next joystick change detection */ 
 156                 PrevJoystickStatus 
= JoystickStatus
; 
 159         /* Select the MIDI OUT stream */ 
 160         Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM
); 
 162         /* Check if endpoint is ready to be read from, if so discard its (unused) data */ 
 163         if (Endpoint_IsOUTReceived()) 
 167 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to 
 168  *  log to a serial port, or anything else that is suitable for status updates. 
 170  *  \param CurrentStatus  Current status of the system, from the MIDI_StatusCodes_t enum 
 172 void UpdateStatus(uint8_t CurrentStatus
) 
 174         uint8_t LEDMask 
= LEDS_NO_LEDS
; 
 176         /* Set the LED mask to the appropriate LED mask based on the given status code */ 
 177         switch (CurrentStatus
) 
 179                 case Status_USBNotReady
: 
 180                         LEDMask 
= (LEDS_LED1
); 
 182                 case Status_USBEnumerating
: 
 183                         LEDMask 
= (LEDS_LED1 
| LEDS_LED2
); 
 185                 case Status_USBReady
: 
 186                         LEDMask 
= (LEDS_LED2 
| LEDS_LED4
); 
 190         /* Set the board LEDs to the new LED mask */ 
 191         LEDs_SetAllLEDs(LEDMask
); 
 194 /** Sends a MIDI note change event (note on or off) to the MIDI output jack, on the given virtual cable ID and channel. 
 196  *  \param Pitch    Pitch of the note to turn on or off 
 197  *  \param OnOff    Set to true if the note is on (being held down), or false otherwise 
 198  *  \param CableID  ID of the virtual cable to send the note change to 
 199  *  \param Channel  MIDI channel number to send the note change event to 
 201 void SendMIDINoteChange(const uint8_t Pitch
, const bool OnOff
, const uint8_t CableID
, const uint8_t Channel
) 
 203         /* Wait until endpoint ready for more data */ 
 204         while (!(Endpoint_IsReadWriteAllowed())); 
 206         /* Check if the message should be a Note On or Note Off command */ 
 207         uint8_t Command 
= ((OnOff
)? MIDI_COMMAND_NOTE_ON 
: MIDI_COMMAND_NOTE_OFF
); 
 209         /* Write the Packet Header to the endpoint */ 
 210         Endpoint_Write_Byte((CableID 
<< 4) | (Command 
>> 4)); 
 212         /* Write the Note On/Off command with the specified channel, pitch and velocity */ 
 213         Endpoint_Write_Byte(Command 
| Channel
); 
 214         Endpoint_Write_Byte(Pitch
); 
 215         Endpoint_Write_Byte(MIDI_STANDARD_VELOCITY
); 
 217         /* Send the data in the endpoint to the host */