From 60d8a123a06c59ca5ce75a52b1c9d4676320e1fb Mon Sep 17 00:00:00 2001 From: Oleksiy Date: Thu, 12 Oct 2023 20:40:49 +0300 Subject: [PATCH] FW: added midi in and out related code --- Software/Gravity/Gravity.ino | 33 ++++++++++++++++++++++++++++--- Software/Gravity/Interactions.ino | 4 ++-- Software/Gravity/UI.ino | 4 ++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Software/Gravity/Gravity.ino b/Software/Gravity/Gravity.ino index a1860c3..2a840a0 100644 --- a/Software/Gravity/Gravity.ino +++ b/Software/Gravity/Gravity.ino @@ -108,11 +108,12 @@ unsigned int pulsePeriod; bool isPlaying = false; bool isRecording = false; bool recordToNextStep = false; +bool MIDIClockRecived = false; unsigned int tickCount = 0; unsigned int pulseCount = 0; -byte masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - external beat +byte masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - MIDI unsigned long lastExtPulseTime; unsigned long newExtPulseTime; @@ -216,6 +217,23 @@ void loop() { void sendMIDIClock() { Serial.write(0xF8); } +void sendMIDIStart() { + Serial.write(0xFA); +} +void sendMIDIStop() { + Serial.write(0xFC); +} +void receiveMIDI() { + if(Serial.available() > 0) { + if (Serial.read() == 0xF8) { //Clock + MIDIClockRecived = true; + } else if (Serial.read() == 0xFA || Serial.read() == 0xFB) { //start and continue + isPlaying = true; + } else if (Serial.read() == 0xFC) { //stop + isPlaying = false; + } + } +} void clock() { if (isPlaying) { @@ -244,6 +262,16 @@ void clock() { } } + if (masterClockMode == 2 && MIDIClockRecived) { // MIDI should happen here (needs testing) + tickCount = 0; //to make things happen in the main clock function + if (pulseCount < (PPQN - 1)) { + pulseCount++; + } else { + pulseCount = 0; + } + MIDIClockRecived = false; + } + // pull low all outputs after set pulse length if (tickCount >= PULSE_LENGTH) { for (byte i = 0; i < 6; i++) { @@ -275,8 +303,7 @@ void externalClock() { } else { pulseCount = 0; } - - } + } } void sendTriggers() { diff --git a/Software/Gravity/Interactions.ino b/Software/Gravity/Interactions.ino index 2aaf48b..8aa9b2a 100644 --- a/Software/Gravity/Interactions.ino +++ b/Software/Gravity/Interactions.ino @@ -171,8 +171,8 @@ void checkInputs() { masterClockMode = masterClockMode + change; if (masterClockMode > 100) { masterClockMode = 0; - } else if (masterClockMode > 1) { - masterClockMode = 1; + } else if (masterClockMode > 2) { + masterClockMode = 2; } saveState(); } else if (insideTab && (menuItemSelected || shiftBtnPushed) && displayTab == 0 && menuItem == 2) { //Modulation channel diff --git a/Software/Gravity/UI.ino b/Software/Gravity/UI.ino index af3aae9..df01750 100644 --- a/Software/Gravity/UI.ino +++ b/Software/Gravity/UI.ino @@ -52,6 +52,8 @@ void updateScreen() { valueStr = "INT"; } else if (i == 1 && masterClockMode == 1) { valueStr = "EXT"; + } else if (i == 1 && masterClockMode == 2) { + valueStr = "MIDI"; } else if (i == 2 && masterClockMode == 0 && bpmModulationRange != 0 && bpmModulationChannel == 0) { valueStr = "CV1"; } else if (i == 2 && masterClockMode == 0 && bpmModulationRange != 0 && bpmModulationChannel == 1) { @@ -79,6 +81,8 @@ void updateScreen() { bpmStr = String(bpm); } else if (masterClockMode == 1) { bpmStr = "24"; + } else if (masterClockMode == 2) { + bpmStr = "MIDI"; } char bpmChar[5]; bpmStr.toCharArray(bpmChar, 5);