Removed specialized Endpoint_ClearControl* and Pipe_ClearControl* macros in favour...
[pub/USBasp.git] / Demos / Device / MIDI / MIDI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
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.
35 */
36
37 #include "MIDI.h"
38
39 /* Scheduler Task List */
40 TASK_LIST
41 {
42 { .Task = USB_USBTask , .TaskStatus = TASK_STOP },
43 { .Task = USB_MIDI_Task , .TaskStatus = TASK_STOP },
44 };
45
46 /** Main program entry point. This routine configures the hardware required by the application, then
47 * starts the scheduler to run the application tasks.
48 */
49 int main(void)
50 {
51 /* Disable watchdog if enabled by bootloader/fuses */
52 MCUSR &= ~(1 << WDRF);
53 wdt_disable();
54
55 /* Disable clock division */
56 clock_prescale_set(clock_div_1);
57
58 /* Hardware Initialization */
59 Joystick_Init();
60 LEDs_Init();
61 HWB_Init();
62
63 /* Indicate USB not ready */
64 UpdateStatus(Status_USBNotReady);
65
66 /* Initialize Scheduler so that it can be used */
67 Scheduler_Init();
68
69 /* Initialize USB Subsystem */
70 USB_Init();
71
72 /* Scheduling - routine never returns, so put this last in the main function */
73 Scheduler_Start();
74 }
75
76 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */
77 EVENT_HANDLER(USB_Connect)
78 {
79 /* Start USB management task */
80 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
81
82 /* Indicate USB enumerating */
83 UpdateStatus(Status_USBEnumerating);
84 }
85
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.
88 */
89 EVENT_HANDLER(USB_Disconnect)
90 {
91 /* Stop running audio and USB management tasks */
92 Scheduler_SetTaskMode(USB_MIDI_Task, TASK_STOP);
93 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
94
95 /* Indicate USB not ready */
96 UpdateStatus(Status_USBNotReady);
97 }
98
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.
101 */
102 EVENT_HANDLER(USB_ConfigurationChanged)
103 {
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);
108
109 Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM, EP_TYPE_BULK,
110 ENDPOINT_DIR_IN, MIDI_STREAM_EPSIZE,
111 ENDPOINT_BANK_SINGLE);
112
113 /* Indicate USB connected and ready */
114 UpdateStatus(Status_USBReady);
115
116 /* Start MIDI task */
117 Scheduler_SetTaskMode(USB_MIDI_Task, TASK_RUN);
118 }
119
120 /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them
121 * to the host.
122 */
123 TASK(USB_MIDI_Task)
124 {
125 static uint8_t PrevJoystickStatus;
126
127 /* Select the MIDI IN stream */
128 Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
129
130 /* Check if endpoint is ready to be written to */
131 if (Endpoint_IsINReady())
132 {
133 /* Get current joystick mask, XOR with previous to detect joystick changes */
134 uint8_t JoystickStatus = Joystick_GetStatus();
135 uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);
136
137 /* Get HWB status - if set use channel 10 (percussion), otherwise use channel 1 */
138 uint8_t Channel = ((HWB_GetStatus()) ? MIDI_CHANNEL(10) : MIDI_CHANNEL(1));
139
140 if (JoystickChanges & JOY_LEFT)
141 SendMIDINoteChange(0x3C, (JoystickStatus & JOY_LEFT), 0, Channel);
142
143 if (JoystickChanges & JOY_UP)
144 SendMIDINoteChange(0x3D, (JoystickStatus & JOY_UP), 0, Channel);
145
146 if (JoystickChanges & JOY_RIGHT)
147 SendMIDINoteChange(0x3E, (JoystickStatus & JOY_RIGHT), 0, Channel);
148
149 if (JoystickChanges & JOY_DOWN)
150 SendMIDINoteChange(0x3F, (JoystickStatus & JOY_DOWN), 0, Channel);
151
152 if (JoystickChanges & JOY_PRESS)
153 SendMIDINoteChange(0x3B, (JoystickStatus & JOY_PRESS), 0, Channel);
154
155 /* Save previous joystick value for next joystick change detection */
156 PrevJoystickStatus = JoystickStatus;
157 }
158
159 /* Select the MIDI OUT stream */
160 Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM);
161
162 /* Check if endpoint is ready to be read from, if so discard its (unused) data */
163 if (Endpoint_IsOUTReceived())
164 Endpoint_ClearOUT();
165 }
166
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.
169 *
170 * \param CurrentStatus Current status of the system, from the MIDI_StatusCodes_t enum
171 */
172 void UpdateStatus(uint8_t CurrentStatus)
173 {
174 uint8_t LEDMask = LEDS_NO_LEDS;
175
176 /* Set the LED mask to the appropriate LED mask based on the given status code */
177 switch (CurrentStatus)
178 {
179 case Status_USBNotReady:
180 LEDMask = (LEDS_LED1);
181 break;
182 case Status_USBEnumerating:
183 LEDMask = (LEDS_LED1 | LEDS_LED2);
184 break;
185 case Status_USBReady:
186 LEDMask = (LEDS_LED2 | LEDS_LED4);
187 break;
188 }
189
190 /* Set the board LEDs to the new LED mask */
191 LEDs_SetAllLEDs(LEDMask);
192 }
193
194 /** Sends a MIDI note change event (note on or off) to the MIDI output jack, on the given virtual cable ID and channel.
195 *
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
200 */
201 void SendMIDINoteChange(const uint8_t Pitch, const bool OnOff, const uint8_t CableID, const uint8_t Channel)
202 {
203 /* Wait until endpoint ready for more data */
204 while (!(Endpoint_IsReadWriteAllowed()));
205
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);
208
209 /* Write the Packet Header to the endpoint */
210 Endpoint_Write_Byte((CableID << 4) | (Command >> 4));
211
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);
216
217 /* Send the data in the endpoint to the host */
218 Endpoint_ClearIN();
219 }