-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
137 lines (110 loc) · 3 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* main.c
*
* Created on: 28 gru 2023
* Author: kwarc
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <string.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Platform/Platform.h>
#include "usb_descriptors.h"
#include "fast_queue.h"
#include "midiXparser/midiXparser.h"
static void system_init(void);
/** LUFA MIDI Class driver interface configuration and state information. This structure is
* passed to all MIDI Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
static USB_ClassInfo_MIDI_Device_t midi_keyb_interface {
{
INTERFACE_ID_AudioStream,
{ MIDI_STREAM_IN_EPADDR, MIDI_STREAM_EPSIZE, 1},
{ MIDI_STREAM_OUT_EPADDR, MIDI_STREAM_EPSIZE, 1},
}
};
static fast_queue<uint8_t, 64> uart_queue;
static midiXparser midiParser;
int main(void)
{
system_init();
/* Only basic MIDI messages are captured */
midiParser.setMidiMsgFilter(midiXparser::channelVoiceMsgTypeMsk);
for (;;)
{
unsigned char uart_char;
while (uart_queue.pop(uart_char))
{
if (midiParser.parse(uart_char))
{
auto midi_msg = midiParser.getMidiMsg();
/* Forward MIDI message to USB event */
const MIDI_EventPacket_t midi_to_usb_evt
{
MIDI_EVENT(0, midi_msg[0]),
midi_msg[0],
midi_msg[1],
midi_msg[2],
};
MIDI_Device_SendEventPacket(&midi_keyb_interface, &midi_to_usb_evt);
MIDI_Device_Flush(&midi_keyb_interface);
}
}
MIDI_EventPacket_t usb_to_midi_evt;
while (MIDI_Device_ReceiveEventPacket(&midi_keyb_interface, &usb_to_midi_evt))
{
/* Ignore received events */
}
MIDI_Device_USBTask(&midi_keyb_interface);
USB_USBTask();
}
}
static void system_init(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Disable clock division */
clock_prescale_set(clock_div_1);
/* Enable UART1 RX with interrupts, 31250 8n1 */
uint16_t ubrr = F_CPU / (16UL * 31250UL) - 1;
UBRR1H = static_cast<uint8_t>(ubrr >> 8);
UBRR1L = static_cast<uint8_t>(ubrr);
UCSR1B |= (1 << RXCIE1) | (1 << RXEN1);
UCSR1C |= (3 << UCSZ10);
/* Configure PD7 for button */
DDRD &= ~(1 << 7);
PORTD |= (1 << 7);
/* Enable USB */
USB_Init();
/* Enable interrupts */
sei();
}
/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
}
/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
}
/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
bool cfg_success = true;
cfg_success &= MIDI_Device_ConfigureEndpoints(&midi_keyb_interface);
}
/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
MIDI_Device_ProcessControlRequest(&midi_keyb_interface);
}
/** UART RX interrupt handler */
ISR(USART1_RX_vect)
{
uart_queue.push(static_cast<uint8_t>(UDR1));
}