]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/USB/Class/Device/MIDIClassDevice.c
b00252597582f9ff6f0ef3076a68db862b7a31e5
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / USB / Class / Device / MIDIClassDevice.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all 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.
20
21   The author disclaims 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
28   this software.
29 */
30
31 #define  __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define  __INCLUDE_FROM_MIDI_DRIVER
37 #define  __INCLUDE_FROM_MIDI_DEVICE_C
38 #include "MIDIClassDevice.h"
39
40 bool MIDI_Device_ConfigureEndpoints(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
41 {
42         memset(&MIDIInterfaceInfo->State, 0x00, sizeof(MIDIInterfaceInfo->State));
43
44         MIDIInterfaceInfo->Config.DataINEndpoint.Type  = EP_TYPE_BULK;
45         MIDIInterfaceInfo->Config.DataOUTEndpoint.Type = EP_TYPE_BULK;
46
47         if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataINEndpoint, 1)))
48           return false;
49
50         if (!(Endpoint_ConfigureEndpointTable(&MIDIInterfaceInfo->Config.DataOUTEndpoint, 1)))
51           return false;
52
53         return true;
54 }
55
56 void MIDI_Device_USBTask(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
57 {
58         if (USB_DeviceState != DEVICE_STATE_Configured)
59           return;
60
61         #if !defined(NO_CLASS_DRIVER_AUTOFLUSH)
62         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
63
64         if (Endpoint_IsINReady())
65           MIDI_Device_Flush(MIDIInterfaceInfo);
66         #endif
67 }
68
69 uint8_t MIDI_Device_SendEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
70                                     const MIDI_EventPacket_t* const Event)
71 {
72         if (USB_DeviceState != DEVICE_STATE_Configured)
73           return ENDPOINT_RWSTREAM_DeviceDisconnected;
74
75         uint8_t ErrorCode;
76
77         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
78
79         if ((ErrorCode = Endpoint_Write_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL)) != ENDPOINT_RWSTREAM_NoError)
80           return ErrorCode;
81
82         if (!(Endpoint_IsReadWriteAllowed()))
83           Endpoint_ClearIN();
84
85         return ENDPOINT_RWSTREAM_NoError;
86 }
87
88 uint8_t MIDI_Device_Flush(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo)
89 {
90         if (USB_DeviceState != DEVICE_STATE_Configured)
91           return ENDPOINT_RWSTREAM_DeviceDisconnected;
92
93         uint8_t ErrorCode;
94
95         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataINEndpoint.Address);
96
97         if (Endpoint_BytesInEndpoint())
98         {
99                 Endpoint_ClearIN();
100
101                 if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)
102                   return ErrorCode;
103         }
104
105         return ENDPOINT_READYWAIT_NoError;
106 }
107
108 bool MIDI_Device_ReceiveEventPacket(USB_ClassInfo_MIDI_Device_t* const MIDIInterfaceInfo,
109                                     MIDI_EventPacket_t* const Event)
110 {
111         if (USB_DeviceState != DEVICE_STATE_Configured)
112           return false;
113
114         Endpoint_SelectEndpoint(MIDIInterfaceInfo->Config.DataOUTEndpoint.Address);
115
116         if (!(Endpoint_IsOUTReceived()))
117                 return false;
118
119         if (!(Endpoint_IsReadWriteAllowed()))
120           return false;
121
122         Endpoint_Read_Stream_LE(Event, sizeof(MIDI_EventPacket_t), NULL);
123
124         if (!(Endpoint_IsReadWriteAllowed()))
125           Endpoint_ClearOUT();
126
127         return true;
128 }
129
130 #endif
131