Added external 24ppqn clock

This commit is contained in:
2023-04-27 22:46:27 +03:00
parent b5e39fcf51
commit 64e7fbf5bf

View File

@ -55,6 +55,9 @@ int needToResetChannel;
bool beatCounted = false;
bool pulseCounted = false;
int masterClockMode = 0; // 0 - internal, 1 - external 24ppqn
bool externalPulseCounted = true;
int displayTab = 0;
int displayTabOld;
int insideTab = 0;
@ -86,7 +89,7 @@ void setup() {
pinMode(ENC_BTN_PIN, INPUT_PULLUP);
pinMode(START_STOP_BTN_PIN, INPUT_PULLUP);
pinMode(START_STOP_BTN_PIN, ANALOGUE_INPUT_1_PIN);
pinMode(INPUT_PIN, INPUT_PULLUP); //probably will need interrupt
pinMode(INPUT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INPUT_PIN), externalClock, RISING);
for (int i=0; i<6; i++) {
@ -98,7 +101,7 @@ void setup() {
updateScreen();
updateTiming();
FlexiTimer2::set(1, 1.0/1000, internalClock); // 1.0/1000 = 1ms period
FlexiTimer2::set(1, 1.0/1000, clock); // 1.0/1000 = 1ms period
FlexiTimer2::start();
}
@ -106,7 +109,7 @@ void loop() {
checkInputs();
}
void internalClock() {
void clock() {
if (isPlaying) {
// Action on each pulse
@ -162,13 +165,24 @@ void internalClock() {
pulseCounted = true;
}
//internal pulse
if (pulseClockCount == 0) {
pulseCount++;
beatCounted = false;
pulseCounted = false;
}
if (pulseClockCount < pulsePeriod) {
switch (masterClockMode) {
case 0: //internal
if (pulseClockCount == 0) {
pulseCount++;
beatCounted = false;
pulseCounted = false;
}
break;
case 1: //external 24ppqn
if (externalPulseCounted == false) {
pulseCount++;
externalPulseCounted = true;
beatCounted = false;
pulseCounted = false;
}
break;
}
if (pulseClockCount < pulsePeriod) {
pulseClockCount++;
} else {
pulseClockCount = 0;
@ -183,12 +197,11 @@ void internalClock() {
digitalWrite(outsPins[i], LOW);
}
}
}
}
void externalClock() {
digitalWrite(outsPins[0], !digitalRead(outsPins[0]));
externalPulseCounted = false;
}
void updateTiming() {
@ -238,6 +251,14 @@ void checkInputs() {
}
updateScreen();
}
else if (encReleasedTime - encPressedTime < 2000 && displayTab == 0) { // switch to external clock mode
if (masterClockMode == 0) {
masterClockMode = 1;
} else {
masterClockMode = 0;
}
updateScreen();
}
}
//encoder
@ -245,7 +266,7 @@ void checkInputs() {
int encPosition = encoder.getPosition();
if (encPositionOld != encPosition) {
int change = encPositionOld - encPosition;
if (displayTab == 0) {
if (displayTab == 0 && masterClockMode == 0) {
bpm = bpm + change;
if (bpm > MAXBPM) {
bpm = MAXBPM;
@ -333,9 +354,11 @@ void updateScreen() {
display.setCursor(4,16);
display.setTextSize(3);
display.setTextColor(SSD1306_WHITE);
if (displayTab == 0) {
if (displayTab == 0 && masterClockMode == 0) {
display.print(bpm);
display.println(F("bpm"));
} else if (displayTab == 0 && masterClockMode != 0) {
display.println(F("EXT"));
} else {
if (clockModes[channels[displayTab-1].mode] == 0) {
display.print(F("OFF"));