Removed new Start of Frame event from the library; performance suffered far too much...
[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 #include "MIDI.h"
32
33 USB_ClassInfo_MIDI_t Keyboard_MIDI_Interface =
34 {
35 .InterfaceNumber = 0,
36
37 .DataINEndpointNumber = MIDI_STREAM_IN_EPNUM,
38 .DataINEndpointSize = MIDI_STREAM_EPSIZE,
39
40 .DataOUTEndpointNumber = MIDI_STREAM_OUT_EPNUM,
41 .DataOUTEndpointSize = MIDI_STREAM_EPSIZE,
42 };
43
44 int main(void)
45 {
46 SetupHardware();
47
48 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
49
50 for (;;)
51 {
52 CheckJoystickMovement();
53
54 USB_USBTask();
55 }
56 }
57
58 void SetupHardware(void)
59 {
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR &= ~(1 << WDRF);
62 wdt_disable();
63
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1);
66
67 /* Hardware Initialization */
68 Joystick_Init();
69 LEDs_Init();
70 Buttons_Init();
71 USB_Init();
72 }
73
74 void CheckJoystickMovement(void)
75 {
76 static uint8_t PrevJoystickStatus;
77
78 uint8_t MIDICommand = 0;
79 uint8_t MIDIPitch;
80
81 /* Get current joystick mask, XOR with previous to detect joystick changes */
82 uint8_t JoystickStatus = Joystick_GetStatus();
83 uint8_t JoystickChanges = (JoystickStatus ^ PrevJoystickStatus);
84
85 /* Get board button status - if pressed use channel 10 (percussion), otherwise use channel 1 */
86 uint8_t Channel = ((Buttons_GetStatus() & BUTTONS_BUTTON1) ? MIDI_CHANNEL(10) : MIDI_CHANNEL(1));
87
88 if (JoystickChanges & JOY_LEFT)
89 {
90 MIDICommand = ((JoystickStatus & JOY_LEFT)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
91 MIDIPitch = 0x3C;
92 }
93
94 if (JoystickChanges & JOY_UP)
95 {
96 MIDICommand = ((JoystickStatus & JOY_UP)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
97 MIDIPitch = 0x3D;
98 }
99
100 if (JoystickChanges & JOY_RIGHT)
101 {
102 MIDICommand = ((JoystickStatus & JOY_RIGHT)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
103 MIDIPitch = 0x3E;
104 }
105
106 if (JoystickChanges & JOY_DOWN)
107 {
108 MIDICommand = ((JoystickStatus & JOY_DOWN)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
109 MIDIPitch = 0x3F;
110 }
111
112 if (JoystickChanges & JOY_PRESS)
113 {
114 MIDICommand = ((JoystickStatus & JOY_PRESS)? MIDI_COMMAND_NOTE_ON : MIDI_COMMAND_NOTE_OFF);
115 MIDIPitch = 0x3B;
116 }
117
118 if (MIDICommand)
119 {
120 USB_MIDI_EventPacket_t MIDIEvent = (USB_MIDI_EventPacket_t)
121 {
122 .CableNumber = 0,
123 .Command = MIDICommand,
124
125 .Data1 = (MIDICommand << 4) | Channel,
126 .Data2 = MIDIPitch,
127 .Data3 = MIDI_STANDARD_VELOCITY,
128 };
129
130 USB_MIDI_SendEventPacket(&Keyboard_MIDI_Interface, &MIDIEvent);
131 }
132
133 PrevJoystickStatus = JoystickStatus;
134 }
135
136 void EVENT_USB_Connect(void)
137 {
138 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
139 }
140
141 void EVENT_USB_Disconnect(void)
142 {
143 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
144 }
145
146 void EVENT_USB_ConfigurationChanged(void)
147 {
148 LEDs_SetAllLEDs(LEDMASK_USB_READY);
149
150 if (!(USB_MIDI_ConfigureEndpoints(&Keyboard_MIDI_Interface)))
151 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
152 }