Skip to content

Commit

Permalink
🚸 Adjust encoder multiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Feb 8, 2024
1 parent 371fb5a commit 4aa48be
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 56 deletions.
14 changes: 7 additions & 7 deletions Marlin/src/lcd/e3v2/common/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ EncoderState encoderReceiveAnalyze() {
#if ENABLED(ENCODER_RATE_MULTIPLIER)

millis_t ms = millis();
int32_t encoderMultiplier = 1;
int32_t encoder_multiplier = 1;

// if must encoder rati multiplier
if (encoderRate.enabled) {
Expand All @@ -137,23 +137,23 @@ EncoderState encoderReceiveAnalyze() {
// Note that the rate is always calculated between two passes through the
// loop and that the abs of the temp_diff value is tracked.
const float encoderStepRate = encoderMovementSteps / float(ms - encoderRate.lastEncoderTime) * 1000;
if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;
if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoder_multiplier = 100;
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoder_multiplier = 10;
#if ENCODER_5X_STEPS_PER_SEC
else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoder_multiplier = 5;
#endif
}
encoderRate.lastEncoderTime = ms;
}

#else

constexpr int32_t encoderMultiplier = 1;
constexpr int32_t encoder_multiplier = 1;

#endif

// encoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
encoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
// encoderRate.encoderMoveValue += (temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP);
encoderRate.encoderMoveValue = (temp_diff * encoder_multiplier) / (ENCODER_PULSES_PER_STEP);
if (encoderRate.encoderMoveValue < 0) encoderRate.encoderMoveValue = -encoderRate.encoderMoveValue;

temp_diff = 0;
Expand Down
63 changes: 26 additions & 37 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,7 @@ void MarlinUI::init() {
bool MarlinUI::screen_changed;

#if ENABLED(ENCODER_RATE_MULTIPLIER)
bool MarlinUI::encoderRateMultiplierEnabled;
millis_t MarlinUI::lastEncoderMovementMillis = 0;
void MarlinUI::enable_encoder_multiplier(const bool onoff) {
encoderRateMultiplierEnabled = onoff;
lastEncoderMovementMillis = 0;
}
bool MarlinUI::encoder_multiplier_enabled;
#endif

#if ANY(REVERSE_MENU_DIRECTION, REVERSE_SELECT_DIRECTION)
Expand Down Expand Up @@ -614,8 +609,6 @@ void MarlinUI::init() {

void MarlinUI::status_screen() {

TERN_(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLY(false));

#if BASIC_PROGRESS_BAR

//
Expand Down Expand Up @@ -1053,41 +1046,37 @@ void MarlinUI::init() {

#if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER)

int32_t encoderMultiplier = 1;

if (encoderRateMultiplierEnabled) {
const float encoderMovementSteps = float(abs_diff) / epps;

if (lastEncoderMovementMillis) {
// Note that the rate is always calculated between two passes through the
// loop and that the abs of the encoderDiff value is tracked.
const float encoderStepRate = encoderMovementSteps / float(ms - lastEncoderMovementMillis) * 1000;

if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoderMultiplier = 100;
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoderMultiplier = 10;

// Enable to output the encoder steps per second value
//#define ENCODER_RATE_MULTIPLIER_DEBUG
#if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
SERIAL_ECHO_START();
SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate);
SERIAL_ECHOPGM(" Multiplier: ", encoderMultiplier);
SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
SERIAL_EOL();
#endif
}

lastEncoderMovementMillis = ms;
} // encoderRateMultiplierEnabled
int32_t encoder_multiplier = 1;

if (encoder_multiplier_enabled) {
// Note that the rate is always calculated between two passes through the
// loop and that the abs of the encoderDiff value is tracked.
static millis_t encoder_mult_prev_ms = 0;
const float encoderStepRate = ((float(abs_diff) / float(epps)) * 1000.0f) / float(ms - encoder_mult_prev_ms);
encoder_mult_prev_ms = ms;

if (encoderStepRate >= ENCODER_100X_STEPS_PER_SEC) encoder_multiplier = 100;
else if (encoderStepRate >= ENCODER_10X_STEPS_PER_SEC) encoder_multiplier = 10;

// Enable to output the encoder steps per second value
//#define ENCODER_RATE_MULTIPLIER_DEBUG
#if ENABLED(ENCODER_RATE_MULTIPLIER_DEBUG)
SERIAL_ECHO_START();
SERIAL_ECHOPGM("Enc Step Rate: ", encoderStepRate);
SERIAL_ECHOPGM(" Multiplier: ", encoder_multiplier);
SERIAL_ECHOPGM(" ENCODER_10X_STEPS_PER_SEC: ", ENCODER_10X_STEPS_PER_SEC);
SERIAL_ECHOPGM(" ENCODER_100X_STEPS_PER_SEC: ", ENCODER_100X_STEPS_PER_SEC);
SERIAL_EOL();
#endif
}

#else

constexpr int32_t encoderMultiplier = 1;
constexpr int32_t encoder_multiplier = 1;

#endif // ENCODER_RATE_MULTIPLIER

if (can_encode()) encoderPosition += (encoderDiff * encoderMultiplier) / epps;
if (can_encode()) encoderPosition += (encoderDiff * encoder_multiplier) / epps;

encoderDiff = 0;
}
Expand Down
16 changes: 7 additions & 9 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ class MarlinUI {
TERN(HAS_SCREEN_TIMEOUT, return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS, UNUSED(ms));
}

#if ALL(HAS_MARLINUI_MENU, ENCODER_RATE_MULTIPLIER)
static bool encoder_multiplier_enabled;
static void enable_encoder_multiplier(const bool onoff) { encoder_multiplier_enabled = onoff; }
#else
static void enable_encoder_multiplier(const bool) {}
#endif

#if HAS_MARLINUI_MENU

#if HAS_TOUCH_BUTTONS
Expand All @@ -667,15 +674,6 @@ class MarlinUI {
static constexpr uint8_t touch_buttons = 0;
#endif

#if ENABLED(ENCODER_RATE_MULTIPLIER)
static bool encoderRateMultiplierEnabled;
static millis_t lastEncoderMovementMillis;
static void enable_encoder_multiplier(const bool onoff);
#define ENCODER_RATE_MULTIPLY(F) (ui.encoderRateMultiplierEnabled = F)
#else
#define ENCODER_RATE_MULTIPLY(F) NOOP
#endif

// Manual Movement
static ManualMove manual_move;
static bool can_show_slider() { return !external_control && currentScreen != manual_move.screen_ptr; }
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, co
TERN_(HAS_MARLINUI_U8GLIB, drawing_screen = false);

TERN_(HAS_MARLINUI_MENU, encoder_direction_normal());
enable_encoder_multiplier(false);

set_selection(false);
}
Expand Down Expand Up @@ -255,7 +256,6 @@ void MarlinUI::synchronize(FSTR_P const fmsg/*=nullptr*/) {
*/
void scroll_screen(const uint8_t limit, const bool is_menu) {
ui.encoder_direction_menus();
ENCODER_RATE_MULTIPLY(false);
if (int32_t(ui.encoderPosition) < 0) ui.encoderPosition = 0;
if (ui.first_page) {
encoderLine = ui.encoderPosition / (ENCODER_STEPS_PER_MENU_ITEM);
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ class MenuItem_bool : public MenuEditItemBase {
#define _MENU_INNER_F(TYPE, USE_MULTIPLIER, FLABEL, V...) do { \
FSTR_P const flabel = FLABEL; \
if (CLICKED()) { \
_MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER); \
MenuItem_##TYPE::action(flabel, ##V); \
_MENU_ITEM_MULTIPLIER_CHECK(USE_MULTIPLIER); \
if (ui.screen_changed) return; \
} \
if (ui.should_draw()) \
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu_mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

void _lcd_mixer_gradient_z_edit(const bool isend) {
ui.defer_status_screen();
ENCODER_RATE_MULTIPLY(true);
ui.enable_encoder_multiplier(true);

float &zvar = isend ? mixer.gradient.end_z : mixer.gradient.start_z;

Expand Down

0 comments on commit 4aa48be

Please sign in to comment.