From b39f7a0bbc16867041e3e41dadeeee6b15a176fd Mon Sep 17 00:00:00 2001 From: Adam Wonak Date: Mon, 1 Sep 2025 08:41:47 -0700 Subject: [PATCH] refactor when we recalculate pulses. --- firmware/Gravity/Gravity.ino | 6 +++ firmware/Gravity/channel.h | 72 ++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 41 deletions(-) diff --git a/firmware/Gravity/Gravity.ino b/firmware/Gravity/Gravity.ino index fe1fd50..bff5e9a 100644 --- a/firmware/Gravity/Gravity.ino +++ b/firmware/Gravity/Gravity.ino @@ -89,6 +89,11 @@ void loop() { // Check if cv run or reset is active and read cv. CheckRunReset(gravity.cv1, gravity.cv2); + + // Process clock pulses. + for (int i = 0; i < Gravity::OUTPUT_COUNT; i++) { + app.channel[i].recalculatePulses(); + } // Check for dirty state eligible to be saved. stateManager.update(app); @@ -281,6 +286,7 @@ void HandleRotate(int val) { void HandlePressedRotate(int val) { updateSelection(app.selected_channel, val, Gravity::OUTPUT_COUNT + 1); app.selected_param = 0; + app.editing_param = false; stateManager.markDirty(); app.refresh_screen = true; } diff --git a/firmware/Gravity/channel.h b/firmware/Gravity/channel.h index 02754e6..6b72023 100644 --- a/firmware/Gravity/channel.h +++ b/firmware/Gravity/channel.h @@ -77,16 +77,15 @@ class Channel { pattern.Init(DEFAULT_PATTERN); // Calcule the clock mod pulses on init. - _recalculatePulses(); + recalculatePulses(); } - bool isCvModActive() const { return cv1_dest != CV_DEST_NONE || cv2_dest != CV_DEST_NONE; } + bool inline isCvModActive() const { return cv1_dest != CV_DEST_NONE || cv2_dest != CV_DEST_NONE; } // Setters (Set the BASE value) void setClockMod(int index) { base_clock_mod_index = constrain(index, 0, MOD_CHOICE_SIZE - 1); - _recalculatePulses(); } void setProbability(int prob) { @@ -95,16 +94,13 @@ class Channel { void setDutyCycle(int duty) { base_duty_cycle = constrain(duty, 1, 99); - _recalculatePulses(); } void setOffset(int off) { base_offset = constrain(off, 0, 99); - _recalculatePulses(); } void setSwing(int val) { base_swing = constrain(val, 50, 95); - _recalculatePulses(); } // Euclidean @@ -117,11 +113,11 @@ class Channel { void setCv1Dest(CvDestination dest) { cv1_dest = dest; - _recalculatePulses(); + recalculatePulses(); } void setCv2Dest(CvDestination dest) { cv2_dest = dest; - _recalculatePulses(); + recalculatePulses(); } CvDestination getCv1Dest() const { return cv1_dest; } CvDestination getCv2Dest() const { return cv2_dest; } @@ -194,26 +190,19 @@ class Channel { return; } - if (isCvModActive()) _recalculatePulses(); - - int cv1 = gravity.cv1.Read(); - int cv2 = gravity.cv2.Read(); - int cvmod_clock_mod_index = getClockModIndexWithMod(cv1, cv2); - int cvmod_probability = getProbabilityWithMod(cv1, cv2); - - const uint16_t mod_pulses = pgm_read_word_near(&CLOCK_MOD_PULSES[cvmod_clock_mod_index]); + int cvmod_probability = getProbabilityWithMod(gravity.cv1.Read(), gravity.cv2.Read()); // Conditionally apply swing on down beats. uint16_t swing_pulses = 0; - if (_swing_pulse_amount > 0 && (tick / mod_pulses) % 2 == 1) { - swing_pulses = _swing_pulse_amount; + if (_swing_pulses > 0 && (tick / _mod_pulses) % 2 == 1) { + swing_pulses = _swing_pulses; } // Duty cycle high check logic const uint32_t current_tick_offset = tick + _offset_pulses + swing_pulses; if (!output.On()) { // Step check - if (current_tick_offset % mod_pulses == 0) { + if (current_tick_offset % _mod_pulses == 0) { bool hit = cvmod_probability >= random(0, 100); // Euclidean rhythm hit check switch (pattern.NextStep()) { @@ -232,11 +221,31 @@ class Channel { // Duty cycle low check const uint32_t duty_cycle_end_tick = tick + _duty_pulses + _offset_pulses + swing_pulses; - if (duty_cycle_end_tick % mod_pulses == 0) { + if (duty_cycle_end_tick % _mod_pulses == 0) { output.Low(); } } + void recalculatePulses() { + int cv1 = gravity.cv1.Read(); + int cv2 = gravity.cv2.Read(); + int clock_mod_index = getClockModIndexWithMod(cv1, cv2); + int duty_cycle = getDutyCycleWithMod(cv1, cv2); + int offset = getOffsetWithMod(cv1, cv2); + int swing = getSwingWithMod(cv1, cv2); + _mod_pulses = pgm_read_word_near(&CLOCK_MOD_PULSES[clock_mod_index]); + _duty_pulses = max((long)((_mod_pulses * (100L - duty_cycle)) / 100L), 1L); + _offset_pulses = (long)((_mod_pulses * (100L - offset)) / 100L); + + // Calculate the down beat swing amount. + if (swing > 50) { + int shifted_swing = swing - 50; + _swing_pulses = (long)((_mod_pulses * (100L - shifted_swing)) / 100L); + } else { + _swing_pulses = 0; + } + } + private: int _calculateMod(CvDestination dest, int cv1_val, int cv2_val, int min_range, int max_range) { int mod1 = (cv1_dest == dest) ? map(cv1_val, -512, 512, min_range, max_range) : 0; @@ -244,26 +253,6 @@ class Channel { return mod1 + mod2; } - void _recalculatePulses() { - int cv1 = gravity.cv1.Read(); - int cv2 = gravity.cv2.Read(); - int clock_mod_index = getClockModIndexWithMod(cv1, cv2); - int duty_cycle = getDutyCycleWithMod(cv1, cv2); - int offset = getOffsetWithMod(cv1, cv2); - int swing = getSwingWithMod(cv1, cv2); - const uint16_t mod_pulses = pgm_read_word_near(&CLOCK_MOD_PULSES[clock_mod_index]); - _duty_pulses = max((long)((mod_pulses * (100L - duty_cycle)) / 100L), 1L); - _offset_pulses = (long)((mod_pulses * (100L - offset)) / 100L); - - // Calculate the down beat swing amount. - if (swing > 50) { - int shifted_swing = swing - 50; - _swing_pulse_amount = (long)((mod_pulses * (100L - shifted_swing)) / 100L); - } else { - _swing_pulse_amount = 0; - } - } - // User-settable base values. byte base_clock_mod_index; byte base_probability; @@ -282,9 +271,10 @@ class Channel { bool mute; // Pre-calculated pulse values for ISR performance + uint16_t _mod_pulses; uint16_t _duty_pulses; uint16_t _offset_pulses; - uint16_t _swing_pulse_amount; + uint16_t _swing_pulses; }; #endif // CHANNEL_H \ No newline at end of file