Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / Bootloaders / Incomplete / MIDI / BootloaderMIDI.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 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 bootloader. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "BootloaderMIDI.h"
38
39 /** Main program entry point. This routine configures the hardware required by the application, then
40 * enters a loop to run the application tasks in sequence.
41 */
42 int main(void)
43 {
44 SetupHardware();
45
46 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
47
48 for (;;)
49 {
50 MIDI_Task();
51 USB_USBTask();
52 }
53 }
54
55 /** Configures the board hardware and chip peripherals for the demo's functionality. */
56 void SetupHardware(void)
57 {
58 /* Disable watchdog if enabled by bootloader/fuses */
59 MCUSR &= ~(1 << WDRF);
60 wdt_disable();
61
62 /* Disable clock division */
63 clock_prescale_set(clock_div_1);
64
65 /* Hardware Initialization */
66 LEDs_Init();
67 USB_Init();
68 }
69
70 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */
71 void EVENT_USB_Device_Connect(void)
72 {
73 /* Indicate USB enumerating */
74 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
75 }
76
77 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
78 * the status LEDs, disables the sample update and PWM output timers and stops the USB and MIDI management tasks.
79 */
80 void EVENT_USB_Device_Disconnect(void)
81 {
82 /* Indicate USB not ready */
83 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
84 }
85
86 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
87 * of the USB device after enumeration - the device endpoints are configured and the MIDI management task started.
88 */
89 void EVENT_USB_Device_ConfigurationChanged(void)
90 {
91 /* Indicate USB connected and ready */
92 LEDs_SetAllLEDs(LEDMASK_USB_READY);
93
94 /* Setup MIDI stream endpoints */
95 if (!(Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPNUM, EP_TYPE_BULK,
96 ENDPOINT_DIR_OUT, MIDI_STREAM_EPSIZE,
97 ENDPOINT_BANK_SINGLE)))
98 {
99 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
100 }
101
102 if (!(Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPNUM, EP_TYPE_BULK,
103 ENDPOINT_DIR_IN, MIDI_STREAM_EPSIZE,
104 ENDPOINT_BANK_SINGLE)))
105 {
106 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
107 }
108 }
109
110 /** Task to handle the generation of MIDI note change events in response to presses of the board joystick, and send them
111 * to the host.
112 */
113 void MIDI_Task(void)
114 {
115 /* Device must be connected and configured for the task to run */
116 if (USB_DeviceState != DEVICE_STATE_Configured)
117 return;
118
119 /* Select the MIDI OUT stream */
120 Endpoint_SelectEndpoint(MIDI_STREAM_OUT_EPNUM);
121
122 if (Endpoint_IsOUTReceived())
123 {
124 USB_MIDI_EventPacket_t InPacket;
125 Endpoint_Read_Stream_LE(&InPacket, sizeof(InPacket));
126
127 LEDs_SetAllLEDs(InPacket.Data2 > 64 ? LEDS_LED1 : LEDS_LED2);
128 Endpoint_ClearOUT();
129
130 uint8_t Channel = InPacket.Data1;
131 uint8_t Data = ((InPacket.Data2 & 0x7F) | ((InPacket.Data3 == 64) ? 0x80 : 0x00));
132
133 if ((Channel == MIDI_CONTROL_CHANNEL) && (Data == CONTROL_ENTER_PROG_MODE))
134 {
135 USB_MIDI_EventPacket_t MIDIIn = (USB_MIDI_EventPacket_t)
136 {
137 .CableNumber = 0,
138 .Command = (MIDI_COMMAND_NOTE_ON >> 4),
139
140 .Data1 = MIDI_COMMAND_NOTE_ON | MIDI_CONTROL_CHANNEL,
141 .Data2 = CONTROL_DEVICE_READY,
142 .Data3 = 32,
143 };
144
145 Endpoint_SelectEndpoint(MIDI_STREAM_IN_EPNUM);
146 Endpoint_Write_Stream_LE(&MIDIIn, sizeof(MIDIIn));
147 Endpoint_ClearIN();
148 }
149 }
150 }