Skip to content

Commit

Permalink
Merge pull request #25 from 16n-faderbank/simplify_midi_interrupts
Browse files Browse the repository at this point in the history
Simplify MIDI interrupts.

This reduces the amount going on inside interrupts, leading to better stability when I2C master is enabled, and hopefully improving MIDI performance for a few people.
  • Loading branch information
infovore authored Mar 17, 2019
2 parents f2cf546 + 40fb39e commit b93a3e0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Once upon a time, Sean Hellfritsch and Brian Crabtree [made a faderbank][linespo

**16n** is the revised version of that object: it is open-source and ready for you to make, modify, or hack.

It is currently at version **1.31**.
It is currently at version **1.33**.

# Repository contents

Expand Down
60 changes: 44 additions & 16 deletions firmware/_16n_faderbank_firmware/_16n_faderbank_firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ const int muxMapping[16] = {8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0
const int muxMapping[16] = {0, 1, 2, 3, 4, 5, 6, 7, 15, 14, 13, 12, 11, 10, 9, 8};
#endif

// the MIDI write timer
// MIDI timers
IntervalTimer midiWriteTimer;
IntervalTimer midiReadTimer;
int midiInterval = 5000; // 5ms
int midiInterval = 1000; // 1ms
bool shouldDoMidiRead = false;
bool shouldDoMidiWrite = false;

// helper values for i2c reading and future expansion
int activeInput = 0;
Expand Down Expand Up @@ -185,33 +187,66 @@ void loop()
temp = map(temp, MINFADER, MAXFADER, 0, 16383);

// map and update the value
noInterrupts();
currentValue[i] = temp;
}

if (shouldDoMidiRead)
{
doMidiRead();
noInterrupts();
shouldDoMidiRead = false;
interrupts();
}

if (shouldDoMidiWrite)
{
doMidiWrite();
noInterrupts();
shouldDoMidiWrite = false;
interrupts();
}
}

/*
* Tiny function called via interrupt
* (it's important to catch inbound MIDI messages even if we do nothing with
* them.)
*/
void readMidi()
{
// important to catch inbound MIDI messages even if we do nothiing with them.
shouldDoMidiRead = true;
}

/*
* Function called when shouldDoMidiRead flag is HIGH
*/

void doMidiRead()
{
MIDI.read();
usbMIDI.read();
}

/*
* The function that writes changes in slider positions out the midi ports
* Tiny function called via interrupt
*/
void writeMidi()
{
shouldDoMidiWrite = true;
}

/*
* The function that writes changes in slider positions out the midi ports
* Called when shouldDoMidiWrite flag is HIGH
*/
void doMidiWrite()
{
// write loop using the q counter (
// (can't use i or temp cuz this might interrupt the reads)
for (q = 0; q < channelCount; q++)
{

noInterrupts();
notShiftyTemp = currentValue[q];
interrupts();

// shift for MIDI precision (0-127)
shiftyTemp = notShiftyTemp >> 7;

Expand All @@ -237,7 +272,6 @@ void writeMidi()

if (notShiftyTemp != lastValue[q])
{

#ifdef DEBUG
Serial.printf("i2c Master[%d]: %d\n", q, notShiftyTemp);
#endif
Expand Down Expand Up @@ -338,24 +372,18 @@ void i2cReadRequest()
Serial.print("i2c Read\n");
#endif

// disable interrupts. get and cast the value
// get and cast the value
uint16_t shiftReady = 0;
switch (activeMode)
{
case 1:
noInterrupts();
shiftReady = (uint16_t)currentValue[activeInput];
interrupts();
break;
case 2:
noInterrupts();
shiftReady = (uint16_t)currentValue[activeInput];
interrupts();
break;
default:
noInterrupts();
shiftReady = (uint16_t)currentValue[activeInput];
interrupts();
break;
}

Expand Down

0 comments on commit b93a3e0

Please sign in to comment.