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.

MIDIClassDevice.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #define __INCLUDE_FROM_USB_DRIVER
  27. #include "../../Core/USBMode.h"
  28. #if defined(USB_CAN_BE_DEVICE)
  29. #define __INCLUDE_FROM_MIDI_DRIVER
  30. #define __INCLUDE_FROM_MIDI_DEVICE_C
  31. #include "MIDIClassDevice.h"
  32. bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
  33. {
  34. memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
  35. MIDIInterfaceInfo->Config.DataINEndpoint.Type = EP_TYPE_BULK;
  36. MIDIInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;
  37. if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataINEndpoint, 1)))
  38. return false;
  39. if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataOUTEndpoint, 1)))
  40. return false;
  41. return true;
  42. }
  43. void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
  44. {
  45. if (USB_DeviceState != DEVICE_STATE_Configured)
  46. return;
  47. #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
  48. Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
  49. if (Endpoint_IsINReady())
  50. MIDI_Device_Flush(MIDIInterfaceInfo);
  51. #endif
  52. }
  53. uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
  54. const MIDI_EventPacket_t* const Event)
  55. {
  56. if (USB_DeviceState != DEVICE_STATE_Configured)
  57. return ENDPOINT_RWSTREAM_DeviceDisconnected;
  58. uint8_t ErrorCode;
  59. Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
  60. if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != ENDPOINT_RWSTREAM_NoError)
  61. return ErrorCode;
  62. if (!(Endpoint_IsReadWriteAllowed()))
  63. Endpoint_ClearIN();
  64. return ENDPOINT_RWSTREAM_NoError;
  65. }
  66. uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
  67. {
  68. if (USB_DeviceState != DEVICE_STATE_Configured)
  69. return ENDPOINT_RWSTREAM_DeviceDisconnected;
  70. uint8_t ErrorCode;
  71. Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
  72. if (Endpoint_BytesInEndpoint())
  73. {
  74. Endpoint_ClearIN();
  75. if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
  76. return ErrorCode;
  77. }
  78. return ENDPOINT_READYWAIT_NoError;
  79. }
  80. bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
  81. MIDI_EventPacket_t* const Event)
  82. {
  83. if (USB_DeviceState != DEVICE_STATE_Configured)
  84. return false;
  85. Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpoint.Address);
  86. if (!(Endpoint_IsOUTReceived()))
  87. return false;
  88. if (!(Endpoint_IsReadWriteAllowed()))
  89. return false;
  90. Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
  91. if (!(Endpoint_IsReadWriteAllowed()))
  92. Endpoint_ClearOUT();
  93. return true;
  94. }
  95. #endif