Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MIDI.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Main source file for the MIDI demo. This file contains the main tasks of
  29. * the demo and is responsible for the initial application hardware configuration.
  30. */
  31. #include "MIDI.h"
  32. /** LUFA MIDI Class driver interface configuration and state information. This structure is
  33. * passed to all MIDI Class driver functions, so that multiple instances of the same class
  34. * within a device can be differentiated from one another.
  35. */
  36. USB_ClassInfo_MIDI_Device_t Keyboard_MIDI_Interface =
  37. {
  38. .Config =
  39. {
  40. .StreamingInterfaceNumber = INTERFACE_ID_AudioStream,
  41. .DataINEndpoint =
  42. {
  43. .Address = MIDI_STREAM_IN_EPADDR,
  44. .Size = MIDI_STREAM_EPSIZE,
  45. .Banks = 1,
  46. },
  47. .DataOUTEndpoint =
  48. {
  49. .Address = MIDI_STREAM_OUT_EPADDR,
  50. .Size = MIDI_STREAM_EPSIZE,
  51. .Banks = 1,
  52. },
  53. },
  54. };
  55. /** Main program entry point. This routine contains the overall program flow, including initial
  56. * setup of all components and the main program loop.
  57. */
  58. int main(void)
  59. {
  60. SetupHardware();
  61. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  62. GlobalInterruptEnable();
  63. for (;;)
  64. {
  65. CheckJoystickMovement();
  66. MIDI_EventPacket_t ReceivedMIDIEvent;
  67. while (MIDI_Device_ReceiveEventPacket(&Keyboard_MIDI_Interface, &ReceivedMIDIEvent))
  68. {
  69. if ((ReceivedMIDIEvent.Event == MIDI_EVENT(0, MIDI_COMMAND_NOTE_ON)) && (ReceivedMIDIEvent.Data3 > 0))
  70. LEDs_SetAllLEDs(ReceivedMIDIEvent.Data2 > 64 ? LEDS_LED1 : LEDS_LED2);
  71. else
  72. LEDs_SetAllLEDs(LEDS_NO_LEDS);
  73. }
  74. MIDI_Device_USBTask(&Keyboard_MIDI_Interface);
  75. USB_USBTask();
  76. }
  77. }
  78. /** Configures the board hardware and chip peripherals for the demo's functionality. */
  79. void SetupHardware(void)
  80. {
  81. #if (ARCH == ARCH_AVR8)
  82. /* Disable watchdog if enabled by bootloader/fuses */
  83. MCUSR &= ~(1 << WDRF);
  84. wdt_disable();
  85. /* Disable clock division */
  86. clock_prescale_set(clock_div_1);
  87. #elif (ARCH == ARCH_XMEGA)
  88. /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
  89. XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  90. XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
  91. /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
  92. XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  93. XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
  94. PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  95. #endif
  96. /* Hardware Initialization */
  97. Joystick_Init();
  98. LEDs_Init();
  99. Buttons_Init();
  100. USB_Init();
  101. }
  102. /** Checks for changes in the position of the board joystick, sending MIDI events to the host upon each change. */
  103. void CheckJoystickMovement(void)
  104. {
  105. static uint8_t PrevJoystickStatus;
  106. uint8_t MIDICommand = 0;
  107. uint8_t MIDIPitch;
  108. /* Get current joystick mask, XOR with previous to detect joystick changes */
  109. uint8_t JoystickStatus = Joystick_GetStatus();
  110. uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);
  111. /* Get board button status - if pressed use channel 10 (percussion), otherwise use channel 1 */
  112. uint8_t Channel = ((Buttons_GetStatus() & BUTTONS_BUTTON1) ? MIDI_CHANNEL(10) : MIDI_CHANNEL(1));
  113. if (JoystickChanges & JOY_LEFT)
  114. {
  115. MIDICommand = ((JoystickStatus & JOY_LEFT)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
  116. MIDIPitch = 0x3C;
  117. }
  118. if (JoystickChanges & JOY_UP)
  119. {
  120. MIDICommand = ((JoystickStatus & JOY_UP)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
  121. MIDIPitch = 0x3D;
  122. }
  123. if (JoystickChanges & JOY_RIGHT)
  124. {
  125. MIDICommand = ((JoystickStatus & JOY_RIGHT)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
  126. MIDIPitch = 0x3E;
  127. }
  128. if (JoystickChanges & JOY_DOWN)
  129. {
  130. MIDICommand = ((JoystickStatus & JOY_DOWN)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
  131. MIDIPitch = 0x3F;
  132. }
  133. if (JoystickChanges & JOY_PRESS)
  134. {
  135. MIDICommand = ((JoystickStatus & JOY_PRESS)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
  136. MIDIPitch = 0x3B;
  137. }
  138. if (MIDICommand)
  139. {
  140. MIDI_EventPacket_t MIDIEvent = (MIDI_EventPacket_t)
  141. {
  142. .Event = MIDI_EVENT(0, MIDICommand),
  143. .Data1 = MIDICommand | Channel,
  144. .Data2 = MIDIPitch,
  145. .Data3 = MIDI_STANDARD_VELOCITY,
  146. };
  147. MIDI_Device_SendEventPacket(&Keyboard_MIDI_Interface, &MIDIEvent);
  148. MIDI_Device_Flush(&Keyboard_MIDI_Interface);
  149. }
  150. PrevJoystickStatus = JoystickStatus;
  151. }
  152. /** Event handler for the library USB Connection event. */
  153. void EVENT_USB_Device_Connect(void)
  154. {
  155. LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
  156. }
  157. /** Event handler for the library USB Disconnection event. */
  158. void EVENT_USB_Device_Disconnect(void)
  159. {
  160. LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
  161. }
  162. /** Event handler for the library USB Configuration Changed event. */
  163. void EVENT_USB_Device_ConfigurationChanged(void)
  164. {
  165. bool ConfigSuccess = true;
  166. ConfigSuccess &= MIDI_Device_ConfigureEndpoints(&Keyboard_MIDI_Interface);
  167. LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
  168. }
  169. /** Event handler for the library USB Control Request reception event. */
  170. void EVENT_USB_Device_ControlRequest(void)
  171. {
  172. MIDI_Device_ProcessControlRequest(&Keyboard_MIDI_Interface);
  173. }