and the functions that handele serial in / out
void serialIn(void* arg)
{
while(!Bela_stopRequested())
{
unsigned int maxLen = 128;
char serialBuffer[maxLen];
// read from the serial port with a timeout of 100ms
int ret = gSerial.read(serialBuffer, maxLen, 100);
if (ret > 0)
{
// Bit mask for commands
// |7 |6 |5 |4 |3 |2 |1 |0 |
// |but 0 | nummer van de control |
// |enc 1 |0- 1+ | nummer van de control |
for (unsigned i = 0; i < ret; i++)
{
int _command = serialBuffer[i];
rt_printf("%i", _command);
if (_command >> 7)
{
// Encoder command
if ((_command >> 6) & 0x01)
{
// Encoder waarde omhoog
rt_printf(" (Encoder++ %i)\n", _command & 0x3F);
switch(_command & 0x3F)
{
case 0:
synth.changeSource(gEditTrack, 1);
break;
case 1:
synth.changeEffect(gEditTrack, 1);
break;
case 2:
synth.incrementParameter(0 /* portamento */, gEditTrack, 1);
break;
case 3:
if (!gateOutputParameterShift)
synth.incrementParameter(1 /* gate width */, gEditTrack, 1);
else
synth.incrementParameter(2 /* gate delay */, gEditTrack, 1);
break;
case 4:
data.addToBPM(0.5f);
break;
};
}
else
{
// Encoder waarde naar benee
rt_printf(" (Encoder-- %i)\n", _command & 0x3F);
switch(_command & 0x3F)
{
case 0:
synth.changeSource(gEditTrack, -1);
break;
case 1:
synth.changeEffect(gEditTrack, -1);
break;
case 2:
synth.incrementParameter(0 /* portamento */, gEditTrack, -1);
break;
case 3:
if (!gateOutputParameterShift)
synth.incrementParameter(1 /* gate width */, gEditTrack, -1);
else
synth.incrementParameter(2 /* gate delay */, gEditTrack, -1);
break;
case 4:
data.addToBPM(-0.5f);
break;
};
}
}
else
{
// Button command
rt_printf(" (Button %i)\n", _command & 0x3F);
switch(_command & 0x3F)
{
case 0:
// Schakel de LDR mode van de huidige track
synth.switchLDRMode(gEditTrack);
break;
case 1:
// Gate output enable
synth.switchOutputEnable(gEditTrack);
break;
case 2:
gBPMPaused = data.toggleBPMPlayPause();
break;
case 3:
// Flip de parameter shift knop
gateOutputParameterShift = !gateOutputParameterShift;
// Print welke parameter nu wordt bewerkt
// false = gate width
// true = gate delay
if (!gateOutputParameterShift)
rt_printf("editing gate width\n");
else
rt_printf("editing gate delay\n");
break;
case 4:
// Gate invert
synth.switchOutputInversion(gEditTrack);
break;
};
}
}
}
usleep(gSerialInTaskSleepTime);
}
}
void serialOut(void* arg)
{
while(!Bela_stopRequested())
{
SerialCommand command;
unsigned nMessages = 0;
unsigned messagesSent = 0;
bool sendMessages = false;
// -1 is niet versturen
int messageValues[4][13] = {
{
-100, -100, -100, -100,
-100, -100, -100, -100,
-100, -100, -100, -100,
-100
},
{
-100, -100, -100, -100,
-100, -100, -100, -100,
-100, -100, -100, -100,
-100
},
{
-100, -100, -100, -100,
-100, -100, -100, -100,
-100, -100, -100, -100,
-100
},
{
-100, -100, -100, -100,
-100, -100, -100, -100,
-100, -100, -100, -100,
-100
}
};
/*
int messageRings[13] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0
};
*/
// De gegevens gaan hier de pijp uit en de meest recente van elk bericht wordt genoteerd
// Er kunnen dus maximaal 12 berichten worden verstuurd per keer dat deze thread bezig is
while (gPipe.readNonRt(command) > 0 && !gSerialNotWorking)
{
sendMessages = true;
// Sla de waarde van deze message op op de index van het message type
// Na deze iteratie blijft alleen de meest recente message dus over..
messageValues[command.ring][command.cmd] = command.value;
// messageRings[command.cmd] = command.ring;
/*
gSerial.write("<");
gSerial.write(std::to_string(command.cmd).c_str());
gSerial.write(",");
gSerial.write(std::to_string(command.value).c_str());
gSerial.write(",");
gSerial.write(std::to_string(command.ring).c_str());
gSerial.write(">");
*/
nMessages++;
}
// Zijn er ansich berichten uit de pipe gehaald?
if (sendMessages)
{
// Over alle ringen itereren
for (int r = 0; r < 4; r++)
{
// Over alle commands itereren
for (int m = 0; m < 13; m++)
{
// Kijken of er voor dit command een message uit de pipe is gehaald
if (!(messageValues[r][m] == -100))
{
gSerial.write("<");
gSerial.write(std::to_string(m).c_str());
gSerial.write(",");
gSerial.write(std::to_string(messageValues[r][m]).c_str());
gSerial.write(",");
gSerial.write(std::to_string(r).c_str());
gSerial.write(">");
messagesSent++;
}
}
}
rt_printf("%i serial messages sent out of %i messages (%f%)\n", messagesSent, nMessages, (static_cast<float>(messagesSent) / static_cast<float>(nMessages)) * 100);
}
// rt_printf("serial out task while() 3\n");
usleep(gSerialOutTaskSleepTime);
// rt_printf("serial out task while() 4\n");
}
}