Skip to content

Commit

Permalink
add USB disconnect failsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
aWZHY0yQH81uOYvH committed Feb 23, 2024
1 parent b30b71f commit b27ab87
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
33 changes: 29 additions & 4 deletions Emulator/patches/MIDI.cpp.patch
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
48a46,47
> }
>
338,346d336
<
339,354d337
< #ifdef PRINTMIDI
< SerialUSB.print("MIDI: ");
< SerialUSB.print(byte1, HEX);
Expand All @@ -30,7 +29,7 @@
< SerialUSB.print(" ");
< SerialUSB.println(byte3, HEX);
< #endif
348,354d337
<
< // Pass MIDI though to hardware port
< if(Serial.availableForWrite() >= 3) { // Rush E protection
< Serial.write(byte1);
Expand All @@ -44,7 +43,9 @@
< return;
< }
< LCD::MIDIping(channel);
387,413d364
385,409d362
< }
< }
<
< void initMIDI() {
< #ifdef PRINTMIDI
Expand All @@ -68,6 +69,30 @@
< hwMIDIbufInd = 0;
< handleMIDI(hwMIDIbuf[0], hwMIDIbuf[1], hwMIDIbuf[2]);
< }
412,438d364
<
< void checkConnected() {
< static uint16_t lastFrameNumber;
< static int8_t missedFrameCount = -1;
<
< // Should increment at 1ms USB frame interval
< const uint16_t frameNumber = (UOTGHS->UOTGHS_DEVFNUM & UOTGHS_DEVFNUM_FNUM_Msk) >> UOTGHS_DEVFNUM_FNUM_Pos;
<
< if(frameNumber == lastFrameNumber) {
< // Only count missed frames if we have received some in the past
< if(missedFrameCount >= 0)
< missedFrameCount++;
< }
<
< else missedFrameCount = 0;
<
< lastFrameNumber = frameNumber;
<
< // Disable oscillators if we go more than 10ms without a USB frame
< if(missedFrameCount >= 10) {
< missedFrameCount = -1;
< for(int x=0; x<NVOICES; x++)
< Voice::voices[x].active = false;
< }
< }
<
Expand Down
5 changes: 3 additions & 2 deletions Emulator/patches/MIDI.h.patch
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
>
> static constexpr float midi2freq[] = {8.18,8.66,9.18,9.72,10.30,10.91,11.56,12.25,12.98,13.75,14.57,15.43,16.35,17.32,18.35,19.45,20.60,21.83,23.12,24.50,25.96,27.50,29.14,30.87,32.70,34.65,36.71,38.89,41.20,43.65,46.25,49.00,51.91,55.00,58.27,61.74,65.41,69.30,73.42,77.78,82.41,87.31,92.50,98.00,103.83,110.00,116.54,123.47,130.81,138.59,146.83,155.56,164.81,174.61,185.00,196.00,207.65,220.00,233.08,246.94,261.63,277.18,293.66,311.13,329.63,349.23,369.99,392.00,415.30,440.00,466.16,493.88,523.25,554.37,587.33,622.25,659.26,698.46,739.99,783.99,830.61,880.00,932.33,987.77,1046.50,1108.73,1174.66,1244.51,1318.51,1396.91,1479.98,1567.98,1661.22,1760.00,1864.66,1975.53,2093.00,2217.46,2349.32,2489.02,2637.02,2793.83,2959.96,3135.96,3322.44,3520.00,3729.31,3951.07,4186.01,4434.92,4698.64,4978.03,5274.04,5587.65,5919.91,6271.93,6644.88,7040.00,7458.62,7902.13,8372.02,8869.84,9397.27,9956.06,10548.08,11175.30,11839.82,12543.85};
>
140,141d145
140,142d145
< void initMIDI();
< void checkConnected();
< void processMIDI();
143c147
144c147
< }
---
> };
25 changes: 25 additions & 0 deletions Tesla_Coil_MIDI_Synth/MIDI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,29 @@ void processMIDI() {
}
}

void checkConnected() {
static uint16_t lastFrameNumber;
static int8_t missedFrameCount = -1;

// Should increment at 1ms USB frame interval
const uint16_t frameNumber = (UOTGHS->UOTGHS_DEVFNUM & UOTGHS_DEVFNUM_FNUM_Msk) >> UOTGHS_DEVFNUM_FNUM_Pos;

if(frameNumber == lastFrameNumber) {
// Only count missed frames if we have received some in the past
if(missedFrameCount >= 0)
missedFrameCount++;
}

else missedFrameCount = 0;

lastFrameNumber = frameNumber;

// Disable oscillators if we go more than 10ms without a USB frame
if(missedFrameCount >= 10) {
missedFrameCount = -1;
for(int x=0; x<NVOICES; x++)
Voice::voices[x].active = false;
}
}

}
1 change: 1 addition & 0 deletions Tesla_Coil_MIDI_Synth/MIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void pitchBend(uint8_t channel, uint8_t low7, uint8_t high7);
void cc(uint8_t channel, uint8_t control, uint8_t value);
void handleMIDI(unsigned char byte1, unsigned char byte2, unsigned char byte3);
void initMIDI();
void checkConnected();
void processMIDI();

}
Expand Down
Binary file removed Tesla_Coil_MIDI_Synth/Tesla_Coil_MIDI_Synth.bin
Binary file not shown.
1 change: 1 addition & 0 deletions Tesla_Coil_MIDI_Synth/Tesla_Coil_MIDI_Synth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void loop() {

extern "C" {
int sysTickHook() { // Runs at 1kHz
MIDI::checkConnected();
Synth::updateSynth();
return 0;
}
Expand Down

0 comments on commit b27ab87

Please sign in to comment.