-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMode_LSDJ_Midiout_teensy.ino
235 lines (212 loc) · 5.37 KB
/
Mode_LSDJ_Midiout_teensy.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
/**
* TeensyBoy
* 1.2.3
* ArduinoBoy Firmware for Teensy 2.0
*
* http://noizeinabox.blogspot.com/2012/12/teensyboy.htm
*
*
* Original ArduinoBoy
* Timothy Lamb ([email protected])
* https://github.com/trash80/Arduinoboy
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*/
void modeLSDJMidioutSetup()
{
digitalWrite(pinStatusLed, LOW);
DDRF = B00000011; //Set analog in pins as inputs
PORTF = B00000001;
countGbClockTicks = 0;
lastMidiData[0] = -1;
lastMidiData[1] = -1;
midiValueMode = false;
blinkMaxCount = 60;
modeLSDJMidiout();
}
void modeLSDJMidiout()
{
while (1)
{
if (getIncommingSlaveByte())
{
if (incomingMidiByte > 0x6f)
{
switch (incomingMidiByte)
{
case 0x7F: //clock tick
//Send the MIDI Clock Tick
usbMIDI.sendRealTime(0xF8); //to use this USBMidi function you need to use the modified Teensy usbmidi lib i've made (see first file for the doc)
break;
case 0x7E: //seq stop
//send MIDI transport stop message
usbMIDI.sendRealTime(0xFC); //to use this USBMidi function you need to use the modified Teensy usbmidi lib i've made (see first file for the doc)
stopAllNotes();
break;
case 0x7D: //seq start
//send MIDI transport start message
usbMIDI.sendRealTime(0xFA);
break;
default:
midiData[0] = (incomingMidiByte - 0x70);
midiValueMode = true;
break;
}
}
else if (midiValueMode == true)
{
midiValueMode = false;
midioutDoAction(midiData[0], incomingMidiByte);
}
}
else
{
setMode(); // Check if mode button was depressed
updateBlinkLights();
}
}
}
void midioutDoAction(byte m, byte v)
{
if (m < 4)
{
//note message
if (v)
{
checkStopNote(m);
playNote(m, v);
}
else if (midiOutLastNote[m] >= 0)
{
stopNote(m);
}
}
else if (m < 8)
{
m -= 4;
//cc message
playCC(m, v);
}
else if (m < 0x0C)
{
m -= 8;
playPC(m, v);
}
blinkLight(0x90 + m, v);
}
void checkStopNote(byte m)
{
if ((midioutNoteTimer[m] + midioutNoteTimerThreshold) < millis())
{
stopNote(m);
}
}
void stopNote(byte m)
{
for (int x = 0; x < midioutNoteHoldCounter[m]; x++)
{
//Need testing ... Maybe channel +1
usbMIDI.sendNoteOff(midioutNoteHold[m][x], 0x00, (memory[MEM_MIDIOUT_NOTE_CH + m]));
}
midiOutLastNote[m] = -1;
midioutNoteHoldCounter[m] = 0;
}
void playNote(byte m, byte n)
{
//midiData[0] = (0x90 + (memory[MEM_MIDIOUT_NOTE_CH+m]));
//midiData[1] = n;
//midiData[2] = 0x7F;
//Serial.write(midiData,3);
//Need testing ... Maybe channel +1
usbMIDI.sendNoteOn(n, 0x7F, (memory[MEM_MIDIOUT_NOTE_CH + m]));
midioutNoteHold[m][midioutNoteHoldCounter[m]] = n;
midioutNoteHoldCounter[m]++;
midioutNoteTimer[m] = millis();
midiOutLastNote[m] = n;
}
void playCC(byte m, byte n)
{
byte v = n;
if (memory[MEM_MIDIOUT_CC_MODE + m])
{
if (memory[MEM_MIDIOUT_CC_SCALING + m])
{
v = (v & 0x0F) * 8;
//if(v) v --;
}
n = (m * 7) + ((n >> 4) & 0x07);
//midiData[0] = (0xB0 + (memory[MEM_MIDIOUT_CC_CH+m]));
//midiData[1] = (memory[MEM_MIDIOUT_CC_NUMBERS+n]);
//midiData[2] = v;
//Serial.write(midiData,3);
//Need testing ... Maybe channel +1
usbMIDI.sendControlChange((memory[MEM_MIDIOUT_CC_NUMBERS + n]), v, (memory[MEM_MIDIOUT_CC_CH + m]));
}
else
{
if (memory[MEM_MIDIOUT_CC_SCALING + m])
{
float s;
s = n;
v = ((s / 0x6f) * 0x7f);
}
n = (m * 7);
//midiData[0] = (0xB0 + (memory[MEM_MIDIOUT_CC_CH+m]));
//midiData[1] = (memory[MEM_MIDIOUT_CC_NUMBERS+n]);
//midiData[2] = v;
//Serial.write(midiData,3);
//Need testing ... Maybe channel +1
usbMIDI.sendControlChange((memory[MEM_MIDIOUT_CC_NUMBERS + n]), v, (memory[MEM_MIDIOUT_CC_CH + m]));
}
}
void playPC(byte m, byte n)
{
//midiData[0] = (0xC0 + (memory[MEM_MIDIOUT_NOTE_CH+m]));
//midiData[1] = n;
//Serial.write(midiData,2);
//Need testing ... Maybe channel +1
usbMIDI.sendProgramChange(n, (memory[MEM_MIDIOUT_NOTE_CH + m]));
}
void stopAllNotes()
{
for (int m = 0; m < 4; m++)
{
if (midiOutLastNote[m] >= 0)
{
stopNote(m);
}
//midiData[0] = (0xB0 + (memory[MEM_MIDIOUT_NOTE_CH+m]));
//midiData[1] = 123;
//midiData[2] = 0x7F;
//Serial.write(midiData,3); //Send midi
//Need testing ... Maybe channel +1
usbMIDI.sendControlChange(123, 0x7F, (memory[MEM_MIDIOUT_NOTE_CH + m]));
}
}
boolean getIncommingSlaveByte()
{
delayMicroseconds(midioutBitDelay);
PORTF = B00000000; //rightmost bit is clock line, 2nd bit is data to gb, 3rd is your mom
delayMicroseconds(midioutBitDelay);
PORTF = B00000001;
delayMicroseconds(2);
if ((PINF & B00010000))
{
incomingMidiByte = 0;
for (countClockPause = 0; countClockPause != 7; countClockPause++)
{
PORTF = B00000000;
delayMicroseconds(2);
PORTF = B00000001;
incomingMidiByte = (incomingMidiByte << 1) + ((PINF & B00010000) >> 4);
}
//serial.write ( incomingMidiByte);
return true;
}
return false;
}