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