]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Class/Device/MIDIClassDevice.c
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Class / Device / MIDIClassDevice.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #define  __INCLUDE_FROM_USB_DRIVER\r
32 #include "../../Core/USBMode.h"\r
33 \r
34 #if defined(USB_CAN_BE_DEVICE)\r
35 \r
36 #define  __INCLUDE_FROM_MIDI_DRIVER\r
37 #define  __INCLUDE_FROM_MIDI_DEVICE_C\r
38 #include "MIDIClassDevice.h"\r
39 \r
40 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)\r
41 {\r
42         memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));\r
43 \r
44         MIDIInterfaceInfo->Config.DataINEndpoint.Type  = EP_TYPE_BULK;\r
45         MIDIInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;\r
46 \r
47         if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataINEndpoint, 1)))\r
48           return false;\r
49 \r
50         if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataOUTEndpoint, 1)))\r
51           return false;\r
52 \r
53         return true;\r
54 }\r
55 \r
56 void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)\r
57 {\r
58         if (USB_DeviceState != DEVICE_STATE_Configured)\r
59           return;\r
60 \r
61         #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)\r
62         MIDI_Device_Flush(MIDIInterfaceInfo);\r
63         #endif\r
64 }\r
65 \r
66 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,\r
67                                     const MIDI_EventPacket_t* const Event)\r
68 {\r
69         if (USB_DeviceState != DEVICE_STATE_Configured)\r
70           return ENDPOINT_RWSTREAM_DeviceDisconnected;\r
71 \r
72         uint8_t ErrorCode;\r
73 \r
74         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);\r
75 \r
76         if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != ENDPOINT_RWSTREAM_NoError)\r
77           return ErrorCode;\r
78 \r
79         if (!(Endpoint_IsReadWriteAllowed()))\r
80           Endpoint_ClearIN();\r
81 \r
82         return ENDPOINT_RWSTREAM_NoError;\r
83 }\r
84 \r
85 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)\r
86 {\r
87         if (USB_DeviceState != DEVICE_STATE_Configured)\r
88           return ENDPOINT_RWSTREAM_DeviceDisconnected;\r
89 \r
90         uint8_t ErrorCode;\r
91 \r
92         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);\r
93 \r
94         if (Endpoint_BytesInEndpoint())\r
95         {\r
96                 Endpoint_ClearIN();\r
97 \r
98                 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)\r
99                   return ErrorCode;\r
100         }\r
101 \r
102         return ENDPOINT_READYWAIT_NoError;\r
103 }\r
104 \r
105 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,\r
106                                     MIDI_EventPacket_t* const Event)\r
107 {\r
108         if (USB_DeviceState != DEVICE_STATE_Configured)\r
109           return false;\r
110 \r
111         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpoint.Address);\r
112 \r
113         if (!(Endpoint_IsReadWriteAllowed()))\r
114           return false;\r
115 \r
116         Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);\r
117 \r
118         if (!(Endpoint_IsReadWriteAllowed()))\r
119           Endpoint_ClearOUT();\r
120 \r
121         return true;\r
122 }\r
123 \r
124 #endif\r
125 \r