-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi2qwerty.ino
261 lines (227 loc) · 6.15 KB
/
midi2qwerty.ino
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Arduino Pro Micro MIDI to QWERTY conversion.
by: Marco Merlin 2019
Original code:
Pro Micro Test Code
by: Nathan Seidle
modified by: Jim Lindblom
SparkFun Electronics
date: September 16, 2013
license: Public Domain - please use this code however you'd like.
It's provided as a learning tool.
This code is provided to show how to control the SparkFun
ProMicro's TX and RX LEDs within a sketch. It also serves
to explain the difference between Serial.print() and
Serial1.print().
*/
#include <MIDI.h>
#include "Keyboard.h"
#define RXLED_ON digitalWrite(RXLED, LOW) // set the LED on
#define RXLED_OFF digitalWrite(RXLED, HIGH) // set the LED off
// 1 turns on debug, 0 off
#define DBGSERIAL if (0) SERIAL_PORT_MONITOR
#ifdef USBCON
#define MIDI_SERIAL_PORT Serial1
#else
#define MIDI_SERIAL_PORT Serial
#endif
//button pressbutton unpressbutton pressbutton unpress
int RXLED = 17; // The RX LED has a defined Arduino pin
// The TX LED was not so lucky, we'll need to use pre-defined
// macros (TXLED1, TXLED0) to control that.
// (We could use the same macros for the RX LED too -- RXLED1,
// and RXLED0.)
const int buttonPin = 4; // input pin for pushbutton
struct MySettings : public midi::DefaultSettings
{
static const bool Use1ByteParsing = false;
static const unsigned SysExMaxSize = 1026; // Accept SysEx messages up to 1024 bytes long.
static const long BaudRate = 31250;
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, MIDI_SERIAL_PORT, MIDIUART, MySettings);
inline uint8_t writeUARTwait(uint8_t *p, uint16_t size)
{
// Apparently, not needed. write blocks, if needed
// while (MIDI_SERIAL_PORT.availableForWrite() < size) {
// delay(1);
// }
return MIDI_SERIAL_PORT.write(p, size);
}
uint16_t sysexSize = 0;
void sysex_end(uint8_t i)
{
sysexSize += i;
DBGSERIAL.print(F("sysexSize="));
DBGSERIAL.println(sysexSize);
sysexSize = 0;
}
const uint8_t MIDI_passthru_pin=2;
bool MIDI_passthru;
enum midi_pitch {
MIDI_B2 = 47,
MIDI_C3 = 48,
MIDI_D3b,
MIDI_D3,
MIDI_E3b,
MIDI_E3,
MIDI_F3,
MIDI_G3b,
MIDI_G3,
MIDI_A3b,
MIDI_A3,
MIDI_B3b,
MIDI_B3,
MIDI_C4,
MIDI_D4b,
MIDI_D4,
MIDI_E4b,
MIDI_E4,
MIDI_F4,
MIDI_G4b,
MIDI_G4,
MIDI_A4b,
MIDI_A4,
MIDI_B4b,
MIDI_B4,
MIDI_C5,
MIDI_D5b
};
#define FIRST_MIDI_NOTE MIDI_B2
#define LAST_MIDI_NOTE MIDI_D5b
char midi_map[] = {
' ', // [MIDI_B2]
'z', // [MIDI_C3]
's', // [MIDI_D3b]
'x', // [MIDI_D3]
'd', // [MIDI_E3b]
'c', // [MIDI_E3]
'v', // [MIDI_F3]
'g', // [MIDI_G3b]
'b', // [MIDI_G3]
'h', // [MIDI_A3b]
'n', // [MIDI_A3]
'j', // [MIDI_B3b]
'm', // [MIDI_B3]
'q', // [MIDI_C4]
'2', // [MIDI_D4b]
'w', // [MIDI_D4]
'3', // [MIDI_E4b]
'e', // [MIDI_E4]
'r', // [MIDI_F4]
'5', // [MIDI_G4b]
't', // [MIDI_G4]
'6', // [MIDI_A4b]
'y', // [MIDI_A4]
'7', // [MIDI_B4b]
'u', // [MIDI_B4]
'i', // [MIDI_C5]
' ' // [MIDI_D5b]
};
bool midi_note_on[LAST_MIDI_NOTE - FIRST_MIDI_NOTE] = {false};
void midi_retrigger()
{
uint8_t pitch;
bool trigger = false;
for(pitch = 0; pitch <= (LAST_MIDI_NOTE-FIRST_MIDI_NOTE) ; pitch++)
{
if(midi_note_on[pitch])
{
Keyboard.print( midi_map[ pitch ] );
trigger = true;
}
}
// wait for extra time if a note was triggered
// to reduce the number of characters generated
if(trigger)
delay(50);
}
void setup()
{
pinMode(RXLED, OUTPUT); // Set RX LED as an output
// TX LED is set as an output behind the scenes
//Serial.begin(9600); //This pipes to the serial monitor
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
//if(keyboard_en)
// initialize control over the keyboard:
Keyboard.begin();
// Pin 0 LOW selects MIDI pass through on
pinMode(MIDI_passthru_pin, INPUT_PULLUP);
MIDI_passthru = (digitalRead(MIDI_passthru_pin) == LOW);
MIDIUART.begin(MIDI_CHANNEL_OMNI);
if (MIDI_passthru) {
DBGSERIAL.println("MIDI thru on");
}
else {
DBGSERIAL.println("MIDI thru off");
MIDIUART.turnThruOff();
}
}
void loop()
{
uint8_t pitch, mempitch;
// delay(10); // wait for 100ms
// midi_retrigger();
/* MIDI UART -> MIDI USB */
if (MIDIUART.read())
{
midi::MidiType msgType = MIDIUART.getType();
DBGSERIAL.print(F("UART "));
DBGSERIAL.print(msgType, HEX);
DBGSERIAL.print(' ');
DBGSERIAL.print(MIDIUART.getData1(), HEX);
DBGSERIAL.print(' ');
DBGSERIAL.println(MIDIUART.getData2(), HEX);
switch (msgType) {
case midi::InvalidType:
break;
case midi::NoteOff:
// Keyboard.print("Note Off\n");
pitch = MIDIUART.getData1();
mempitch = pitch - FIRST_MIDI_NOTE;
if (mempitch < (LAST_MIDI_NOTE - FIRST_MIDI_NOTE))
midi_note_on[ pitch - FIRST_MIDI_NOTE ] = false;
break;
case midi::NoteOn:
// Keyboard.print("Note On\n");
pitch = MIDIUART.getData1();
// velocity = MIDIUART.getData2();
// Keyboard.print( midi_map[ MIDIUART.getData1() ] );
// Keyboard.println(velocity);
// Keyboard.print( midi_map[ pitch - FIRST_MIDI_NOTE ] );
mempitch = pitch - FIRST_MIDI_NOTE;
if (mempitch < (LAST_MIDI_NOTE - FIRST_MIDI_NOTE))
{
midi_note_on[ mempitch ] = true;
midi_retrigger();
}
break;
case midi::AfterTouchPoly:
case midi::ControlChange:
case midi::ProgramChange:
case midi::AfterTouchChannel:
case midi::PitchBend:
{
// MIDIUART.getData1(),
// MIDIUART.getData2()
break;
}
case midi::SystemExclusive:
DBGSERIAL.print("sysex size ");
DBGSERIAL.println(MIDIUART.getSysExArrayLength());
break;
case midi::TuneRequest:
case midi::Clock:
case midi::Start:
case midi::Continue:
case midi::Stop:
case midi::ActiveSensing:
case midi::SystemReset:
case midi::TimeCodeQuarterFrame:
case midi::SongSelect:
case midi::SongPosition:
default:
break;
}
}
}