From f71e3dc2388c355532d7686cf8922615f30f50c3 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Tue, 12 Jan 2021 20:43:52 -0600 Subject: [PATCH] Rotary encoder cleanup (#20753) --- Marlin/src/HAL/LPC1768/inc/SanityCheck.h | 2 +- Marlin/src/lcd/TFTGLCD/ultralcd_TFTGLCD.h | 11 +- Marlin/src/lcd/buttons.h | 234 ++++++++++++++++++ Marlin/src/lcd/dwin/e3v2/rotary_encoder.cpp | 144 +++++------ Marlin/src/lcd/dwin/e3v2/rotary_encoder.h | 23 +- .../lib/mks_ui/tft_lvgl_configuration.cpp | 17 +- Marlin/src/lcd/lcdprint.h | 18 +- Marlin/src/pins/mega/pins_CNCONTROLS_11.h | 6 +- Marlin/src/pins/mega/pins_CNCONTROLS_12.h | 6 +- Marlin/src/pins/mega/pins_GT2560_REV_A.h | 8 +- Marlin/src/pins/mega/pins_HJC2560C_REV2.h | 8 +- Marlin/src/pins/mega/pins_MEGATRONICS_2.h | 8 +- Marlin/src/pins/mega/pins_MEGATRONICS_3.h | 8 +- Marlin/src/pins/pinsDebug_list.h | 16 +- Marlin/src/pins/rambo/pins_RAMBO.h | 8 +- Marlin/src/pins/ramps/pins_ULTIMAKER.h | 8 +- Marlin/src/pins/ramps/pins_ULTIMAKER_OLD.h | 8 +- Marlin/src/pins/ramps/pins_ZRIB_V20.h | 6 +- Marlin/src/pins/samd/pins_RAMPS_144.h | 14 +- Marlin/src/pins/stm32f1/pins_CHITU3D.h | 14 +- 20 files changed, 393 insertions(+), 174 deletions(-) create mode 100644 Marlin/src/lcd/buttons.h diff --git a/Marlin/src/HAL/LPC1768/inc/SanityCheck.h b/Marlin/src/HAL/LPC1768/inc/SanityCheck.h index ad5641f3a9284..0b97e91e9d63d 100644 --- a/Marlin/src/HAL/LPC1768/inc/SanityCheck.h +++ b/Marlin/src/HAL/LPC1768/inc/SanityCheck.h @@ -116,7 +116,7 @@ static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported o #elif HAS_WIRED_LCD #if IS_TX1(BTN_EN2) || IS_RX1(BTN_EN1) #error "Serial port pins (1) conflict with Encoder Buttons!" - #elif ANY_TX(1, SD_SCK_PIN, LCD_PINS_D4, DOGLCD_SCK, LCD_RESET_PIN, LCD_PINS_RS, SHIFT_CLK) \ + #elif ANY_TX(1, SD_SCK_PIN, LCD_PINS_D4, DOGLCD_SCK, LCD_RESET_PIN, LCD_PINS_RS, SHIFT_CLK_PIN) \ || ANY_RX(1, LCD_SDSS, LCD_PINS_RS, SD_MISO_PIN, DOGLCD_A0, SD_SS_PIN, LCD_SDSS, DOGLCD_CS, LCD_RESET_PIN, LCD_BACKLIGHT_PIN) #error "Serial port pins (1) conflict with LCD pins!" #endif diff --git a/Marlin/src/lcd/TFTGLCD/ultralcd_TFTGLCD.h b/Marlin/src/lcd/TFTGLCD/ultralcd_TFTGLCD.h index eedcc4afa0739..c399b907e460c 100644 --- a/Marlin/src/lcd/TFTGLCD/ultralcd_TFTGLCD.h +++ b/Marlin/src/lcd/TFTGLCD/ultralcd_TFTGLCD.h @@ -61,15 +61,12 @@ extern TFTGLCD lcd; #include "../lcdprint.h" // Use panel encoder - free old encoder pins -#undef BTN_EN1 -#undef BTN_EN2 -#undef BTN_ENC -#define BTN_EN1 -1 -#define BTN_EN2 -1 -#define BTN_ENC -1 +#undef BTN_EN1 +#undef BTN_EN2 +#undef BTN_ENC #ifndef EN_C - #define EN_C 4 //for click + #define EN_C 4 // for click #endif #endif // IS_TFTGLCD_PANEL diff --git a/Marlin/src/lcd/buttons.h b/Marlin/src/lcd/buttons.h new file mode 100644 index 0000000000000..07a4524def18d --- /dev/null +++ b/Marlin/src/lcd/buttons.h @@ -0,0 +1,234 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +#include "../inc/MarlinConfig.h" + +#if ((!HAS_ADC_BUTTONS && IS_NEWPANEL) || BUTTONS_EXIST(EN1, EN2)) && !IS_TFTGLCD_PANEL + #define HAS_ENCODER_WHEEL 1 +#endif +#if HAS_ENCODER_WHEEL || ANY_BUTTON(ENC, BACK, UP, DWN, LFT, RT) + #define HAS_DIGITAL_BUTTONS 1 +#endif +#if !HAS_ADC_BUTTONS && (IS_RRW_KEYPAD || (HAS_WIRED_LCD && !IS_NEWPANEL)) + #define HAS_SHIFT_ENCODER 1 +#endif + +// I2C buttons must be read in the main thread +#if ANY(LCD_I2C_VIKI, LCD_I2C_PANELOLU2, IS_TFTGLCD_PANEL) + #define HAS_SLOW_BUTTONS 1 +#endif + +#if HAS_ENCODER_WHEEL + #define ENCODER_PHASE_0 0 + #define ENCODER_PHASE_1 2 + #define ENCODER_PHASE_2 3 + #define ENCODER_PHASE_3 1 +#endif + +#if EITHER(HAS_DIGITAL_BUTTONS, DWIN_CREALITY_LCD) + + // Wheel spin pins where BA is 00, 10, 11, 01 (1 bit always changes) + #define BLEN_A 0 + #define BLEN_B 1 + + #define EN_A _BV(BLEN_A) + #define EN_B _BV(BLEN_B) + + #define _BUTTON_PRESSED(BN) !READ(BTN_##BN) + + #if BUTTON_EXISTS(ENC) || HAS_TOUCH_BUTTONS + #define BLEN_C 2 + #define EN_C _BV(BLEN_C) + #endif + + #if ENABLED(LCD_I2C_VIKI) + + #include + + #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C) + + // button and encoder bit positions within 'buttons' + #define B_LE (BUTTON_LEFT << B_I2C_BTN_OFFSET) // The remaining normalized buttons are all read via I2C + #define B_UP (BUTTON_UP << B_I2C_BTN_OFFSET) + #define B_MI (BUTTON_SELECT << B_I2C_BTN_OFFSET) + #define B_DW (BUTTON_DOWN << B_I2C_BTN_OFFSET) + #define B_RI (BUTTON_RIGHT << B_I2C_BTN_OFFSET) + + #if BUTTON_EXISTS(ENC) // The pause/stop/restart button is connected to BTN_ENC when used + #define B_ST (EN_C) // Map the pause/stop/resume button into its normalized functional name + #define BUTTON_CLICK() (buttons & (B_MI|B_RI|B_ST)) // Pause/stop also acts as click until a proper pause/stop is implemented. + #else + #define BUTTON_CLICK() (buttons & (B_MI|B_RI)) + #endif + + // I2C buttons take too long to read inside an interrupt context and so we read them during lcd_update + + #elif ENABLED(LCD_I2C_PANELOLU2) + + #if !BUTTON_EXISTS(ENC) // Use I2C if not directly connected to a pin + + #define B_I2C_BTN_OFFSET 3 // (the first three bit positions reserved for EN_A, EN_B, EN_C) + + #define B_MI (PANELOLU2_ENCODER_C << B_I2C_BTN_OFFSET) // requires LiquidTWI2 library v1.2.3 or later + + #define BUTTON_CLICK() (buttons & B_MI) + + #endif + + #endif + +#else + + #undef BUTTON_EXISTS + #define BUTTON_EXISTS(...) false + + // Dummy button, never pressed + #define _BUTTON_PRESSED(BN) false + + // Shift register bits correspond to buttons: + #define BL_LE 7 // Left + #define BL_UP 6 // Up + #define BL_MI 5 // Middle + #define BL_DW 4 // Down + #define BL_RI 3 // Right + #define BL_ST 2 // Red Button + #define B_LE _BV(BL_LE) + #define B_UP _BV(BL_UP) + #define B_MI _BV(BL_MI) + #define B_DW _BV(BL_DW) + #define B_RI _BV(BL_RI) + #define B_ST _BV(BL_ST) + + #ifndef BUTTON_CLICK + #define BUTTON_CLICK() (buttons & (B_MI|B_ST)) + #endif + +#endif + +#if IS_RRW_KEYPAD + #define BTN_OFFSET 0 // Bit offset into buttons for shift register values + + #define BLEN_KEYPAD_F3 0 + #define BLEN_KEYPAD_F2 1 + #define BLEN_KEYPAD_F1 2 + #define BLEN_KEYPAD_DOWN 3 + #define BLEN_KEYPAD_RIGHT 4 + #define BLEN_KEYPAD_MIDDLE 5 + #define BLEN_KEYPAD_UP 6 + #define BLEN_KEYPAD_LEFT 7 + + #define EN_KEYPAD_F1 _BV(BTN_OFFSET + BLEN_KEYPAD_F1) + #define EN_KEYPAD_F2 _BV(BTN_OFFSET + BLEN_KEYPAD_F2) + #define EN_KEYPAD_F3 _BV(BTN_OFFSET + BLEN_KEYPAD_F3) + #define EN_KEYPAD_DOWN _BV(BTN_OFFSET + BLEN_KEYPAD_DOWN) + #define EN_KEYPAD_RIGHT _BV(BTN_OFFSET + BLEN_KEYPAD_RIGHT) + #define EN_KEYPAD_MIDDLE _BV(BTN_OFFSET + BLEN_KEYPAD_MIDDLE) + #define EN_KEYPAD_UP _BV(BTN_OFFSET + BLEN_KEYPAD_UP) + #define EN_KEYPAD_LEFT _BV(BTN_OFFSET + BLEN_KEYPAD_LEFT) + + #define RRK(B) (keypad_buttons & (B)) + + #ifdef EN_C + #define BUTTON_CLICK() ((buttons & EN_C) || RRK(EN_KEYPAD_MIDDLE)) + #else + #define BUTTON_CLICK() RRK(EN_KEYPAD_MIDDLE) + #endif +#endif + +#ifndef EN_A + #define EN_A 0 +#endif +#ifndef EN_B + #define EN_B 0 +#endif +#ifndef EN_C + #define EN_C 0 +#endif +#if BUTTON_EXISTS(BACK) || EITHER(HAS_TOUCH_BUTTONS, IS_TFTGLCD_PANEL) + #define BLEN_D 3 + #define EN_D _BV(BLEN_D) +#else + #define EN_D 0 +#endif + +#define BUTTON_PRESSED(BN) (_BUTTON_PRESSED_##BN) + +#if BUTTON_EXISTS(EN1) + #define _BUTTON_PRESSED_EN1 _BUTTON_PRESSED(EN1) +#else + #define _BUTTON_PRESSED_EN1 false +#endif +#if BUTTON_EXISTS(EN2) + #define _BUTTON_PRESSED_EN2 _BUTTON_PRESSED(EN2) +#else + #define _BUTTON_PRESSED_EN2 false +#endif +#if BUTTON_EXISTS(ENC_EN) + #define _BUTTON_PRESSED_ENC_EN _BUTTON_PRESSED(ENC_EN) +#else + #define _BUTTON_PRESSED_ENC_EN false +#endif +#if BUTTON_EXISTS(ENC) + #define _BUTTON_PRESSED_ENC _BUTTON_PRESSED(ENC) +#else + #define _BUTTON_PRESSED_ENC false +#endif +#if BUTTON_EXISTS(UP) + #define _BUTTON_PRESSED_UP _BUTTON_PRESSED(UP) +#else + #define _BUTTON_PRESSED_UP false +#endif +#if BUTTON_EXISTS(DWN) + #define _BUTTON_PRESSED_DWN _BUTTON_PRESSED(DWN) +#else + #define _BUTTON_PRESSED_DWN false +#endif +#if BUTTON_EXISTS(LFT) + #define _BUTTON_PRESSED_LFT _BUTTON_PRESSED(LFT) +#else + #define _BUTTON_PRESSED_LFT false +#endif +#if BUTTON_EXISTS(RT) + #define _BUTTON_PRESSED_RT _BUTTON_PRESSED(RT) +#else + #define _BUTTON_PRESSED_RT false +#endif +#if BUTTON_EXISTS(BACK) + #define _BUTTON_PRESSED_BACK _BUTTON_PRESSED(BACK) +#else + #define _BUTTON_PRESSED_BACK false +#endif + +#ifndef BUTTON_CLICK + #if EN_C > 0 + #define BUTTON_CLICK() (buttons & EN_C) + #else + #define BUTTON_CLICK() false + #endif +#endif + +#if EN_D > 0 + #define LCD_BACK_CLICKED() (buttons & EN_D) +#else + #define LCD_BACK_CLICKED() false +#endif diff --git a/Marlin/src/lcd/dwin/e3v2/rotary_encoder.cpp b/Marlin/src/lcd/dwin/e3v2/rotary_encoder.cpp index d39c6cfbd5e89..6c229b7aca223 100644 --- a/Marlin/src/lcd/dwin/e3v2/rotary_encoder.cpp +++ b/Marlin/src/lcd/dwin/e3v2/rotary_encoder.cpp @@ -33,6 +33,7 @@ #if ENABLED(DWIN_CREALITY_LCD) #include "rotary_encoder.h" +#include "../../buttons.h" #include "../../../MarlinCore.h" #include "../../../HAL/shared/Delay.h" @@ -43,17 +44,23 @@ #include +#ifndef ENCODER_PULSES_PER_STEP + #define ENCODER_PULSES_PER_STEP 4 +#endif + ENCODER_Rate EncoderRate; // Buzzer -void Encoder_tick(void) { - WRITE(BEEPER_PIN, 1); - delay(10); - WRITE(BEEPER_PIN, 0); +void Encoder_tick() { + #if PIN_EXISTS(BEEPER) + WRITE(BEEPER_PIN, HIGH); + delay(10); + WRITE(BEEPER_PIN, LOW); + #endif } // Encoder initialization -void Encoder_Configuration(void) { +void Encoder_Configuration() { #if BUTTON_EXISTS(EN1) SET_INPUT_PULLUP(BTN_EN1); #endif @@ -63,21 +70,21 @@ void Encoder_Configuration(void) { #if BUTTON_EXISTS(ENC) SET_INPUT_PULLUP(BTN_ENC); #endif - #ifdef BEEPER_PIN + #if PIN_EXISTS(BEEPER) SET_OUTPUT(BEEPER_PIN); #endif } // Analyze encoder value and return state -ENCODER_DiffState Encoder_ReceiveAnalyze(void) { +ENCODER_DiffState Encoder_ReceiveAnalyze() { const millis_t now = millis(); - static unsigned char lastEncoderBits; - unsigned char newbutton = 0; + static uint8_t lastEncoderBits; + uint8_t newbutton = 0; static signed char temp_diff = 0; ENCODER_DiffState temp_diffState = ENCODER_DIFF_NO; - if (BUTTON_PRESSED(EN1)) newbutton |= 0x01; - if (BUTTON_PRESSED(EN2)) newbutton |= 0x02; + if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; + if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; if (BUTTON_PRESSED(ENC)) { static millis_t next_click_update_ms; if (ELAPSED(now, next_click_update_ms)) { @@ -94,22 +101,22 @@ ENCODER_DiffState Encoder_ReceiveAnalyze(void) { } if (newbutton != lastEncoderBits) { switch (newbutton) { - case ENCODER_PHASE_0: { - if (lastEncoderBits == ENCODER_PHASE_3) temp_diff++; + case ENCODER_PHASE_0: + if (lastEncoderBits == ENCODER_PHASE_3) temp_diff++; else if (lastEncoderBits == ENCODER_PHASE_1) temp_diff--; - }break; - case ENCODER_PHASE_1: { - if (lastEncoderBits == ENCODER_PHASE_0) temp_diff++; + break; + case ENCODER_PHASE_1: + if (lastEncoderBits == ENCODER_PHASE_0) temp_diff++; else if (lastEncoderBits == ENCODER_PHASE_2) temp_diff--; - }break; - case ENCODER_PHASE_2: { - if (lastEncoderBits == ENCODER_PHASE_1) temp_diff++; + break; + case ENCODER_PHASE_2: + if (lastEncoderBits == ENCODER_PHASE_1) temp_diff++; else if (lastEncoderBits == ENCODER_PHASE_3) temp_diff--; - }break; - case ENCODER_PHASE_3: { - if (lastEncoderBits == ENCODER_PHASE_2) temp_diff++; + break; + case ENCODER_PHASE_3: + if (lastEncoderBits == ENCODER_PHASE_2) temp_diff++; else if (lastEncoderBits == ENCODER_PHASE_0) temp_diff--; - }break; + break; } lastEncoderBits = newbutton; } @@ -137,9 +144,12 @@ ENCODER_DiffState Encoder_ReceiveAnalyze(void) { } EncoderRate.lastEncoderTime = ms; } + #else + constexpr int32_t encoderMultiplier = 1; - #endif // ENCODER_RATE_MULTIPLIER + + #endif // EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP); EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP); @@ -153,23 +163,23 @@ ENCODER_DiffState Encoder_ReceiveAnalyze(void) { #if PIN_EXISTS(LCD_LED) // Take the low 24 valid bits 24Bit: G7 G6 G5 G4 G3 G2 G1 G0 R7 R6 R5 R4 R3 R2 R1 R0 B7 B6 B5 B4 B3 B2 B1 B0 - unsigned int LED_DataArray[LED_NUM]; + uint16_t LED_DataArray[LED_NUM]; // LED light operation - void LED_Action(void) { + void LED_Action() { LED_Control(RGB_SCALE_WARM_WHITE,0x0F); delay(30); LED_Control(RGB_SCALE_WARM_WHITE,0x00); } // LED initialization - void LED_Configuration(void) { + void LED_Configuration() { SET_OUTPUT(LCD_LED_PIN); } // LED write data - void LED_WriteData(void) { - unsigned char tempCounter_LED, tempCounter_Bit; + void LED_WriteData() { + uint8_t tempCounter_LED, tempCounter_Bit; for (tempCounter_LED = 0; tempCounter_LED < LED_NUM; tempCounter_LED++) { for (tempCounter_Bit = 0; tempCounter_Bit < 24; tempCounter_Bit++) { if (LED_DataArray[tempCounter_LED] & (0x800000 >> tempCounter_Bit)) { @@ -190,14 +200,13 @@ ENCODER_DiffState Encoder_ReceiveAnalyze(void) { // LED control // RGB_Scale: RGB color ratio // luminance: brightness (0~0xFF) - void LED_Control(unsigned char RGB_Scale, unsigned char luminance) { - unsigned char temp_Counter; - for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) { - LED_DataArray[temp_Counter] = 0; + void LED_Control(const uint8_t RGB_Scale, const uint8_t luminance) { + for (uint8_t i = 0; i < LED_NUM; i++) { + LED_DataArray[i] = 0; switch (RGB_Scale) { - case RGB_SCALE_R10_G7_B5: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*7/10) << 16 | luminance*5/10; break; - case RGB_SCALE_R10_G7_B4: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*7/10) << 16 | luminance*4/10; break; - case RGB_SCALE_R10_G8_B7: LED_DataArray[temp_Counter] = (luminance*10/10) << 8 | (luminance*8/10) << 16 | luminance*7/10; break; + case RGB_SCALE_R10_G7_B5: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 5/10; break; + case RGB_SCALE_R10_G7_B4: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 7/10) << 16 | luminance * 4/10; break; + case RGB_SCALE_R10_G8_B7: LED_DataArray[i] = (luminance * 10/10) << 8 | (luminance * 8/10) << 16 | luminance * 7/10; break; } } LED_WriteData(); @@ -207,45 +216,38 @@ ENCODER_DiffState Encoder_ReceiveAnalyze(void) { // RGB_Scale: RGB color ratio // luminance: brightness (0~0xFF) // change_Time: gradient time (ms) - void LED_GraduallyControl(unsigned char RGB_Scale, unsigned char luminance, unsigned int change_Interval) { - unsigned char temp_Counter; - unsigned char LED_R_Data[LED_NUM], LED_G_Data[LED_NUM], LED_B_Data[LED_NUM]; - bool LED_R_Flag = 0, LED_G_Flag = 0, LED_B_Flag = 0; - - for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) { + void LED_GraduallyControl(const uint8_t RGB_Scale, const uint8_t luminance, const uint16_t change_Interval) { + struct { uint8_t g, r, b; } led_data[LED_NUM]; + for (uint8_t i = 0; i < LED_NUM; i++) { switch (RGB_Scale) { - case RGB_SCALE_R10_G7_B5: { - LED_R_Data[temp_Counter] = luminance*10/10; - LED_G_Data[temp_Counter] = luminance*7/10; - LED_B_Data[temp_Counter] = luminance*5/10; - }break; - case RGB_SCALE_R10_G7_B4: { - LED_R_Data[temp_Counter] = luminance*10/10; - LED_G_Data[temp_Counter] = luminance*7/10; - LED_B_Data[temp_Counter] = luminance*4/10; - }break; - case RGB_SCALE_R10_G8_B7: { - LED_R_Data[temp_Counter] = luminance*10/10; - LED_G_Data[temp_Counter] = luminance*8/10; - LED_B_Data[temp_Counter] = luminance*7/10; - }break; + case RGB_SCALE_R10_G7_B5: + led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 5/10 }; + break; + case RGB_SCALE_R10_G7_B4: + led_data[i] = { luminance * 7/10, luminance * 10/10, luminance * 4/10 }; + break; + case RGB_SCALE_R10_G8_B7: + led_data[i] = { luminance * 8/10, luminance * 10/10, luminance * 7/10 }; + break; } } - for (temp_Counter = 0; temp_Counter < LED_NUM; temp_Counter++) { - if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) > LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000100; - else if ((unsigned char)(LED_DataArray[temp_Counter] >> 8) < LED_R_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000100; - while (1) { - else LED_R_Flag = 1; - if ((unsigned char)(LED_DataArray[temp_Counter]>>16) > LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x010000; - else if ((unsigned char)(LED_DataArray[temp_Counter]>>16) < LED_G_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x010000; - else LED_G_Flag = 1; - if ((unsigned char)LED_DataArray[temp_Counter] > LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] -= 0x000001; - else if ((unsigned char)LED_DataArray[temp_Counter] < LED_B_Data[temp_Counter]) LED_DataArray[temp_Counter] += 0x000001; - else LED_B_Flag = 1; + + struct { bool g, r, b; } led_flag = { false, false, false }; + for (uint8_t i = 0; i < LED_NUM; i++) { + while (1) { + const uint8_t g = uint8_t(LED_DataArray[i] >> 16), + r = uint8_t(LED_DataArray[i] >> 8), + b = uint8_t(LED_DataArray[i]); + if (g == led_data[i].g) led_flag.g = true; + else LED_DataArray[i] += (g > led_data[i].g) ? -0x010000 : 0x010000; + if (r == led_data[i].r) led_flag.r = true; + else LED_DataArray[i] += (r > led_data[i].r) ? -0x000100 : 0x000100; + if (b == led_data[i].b) led_flag.b = true; + else LED_DataArray[i] += (b > led_data[i].b) ? -0x000001 : 0x000001; + LED_WriteData(); + if (led_flag.r && led_flag.g && led_flag.b) break; + delay(change_Interval); } - LED_WriteData(); - if (LED_R_Flag && LED_G_Flag && LED_B_Flag) break; - else delay(change_Interval); } } diff --git a/Marlin/src/lcd/dwin/e3v2/rotary_encoder.h b/Marlin/src/lcd/dwin/e3v2/rotary_encoder.h index 93e54839d625d..bbba753a0baca 100644 --- a/Marlin/src/lcd/dwin/e3v2/rotary_encoder.h +++ b/Marlin/src/lcd/dwin/e3v2/rotary_encoder.h @@ -34,15 +34,6 @@ /*********************** Encoder Set ***********************/ -#define ENCODER_PHASE_0 0 -#define ENCODER_PHASE_1 2 -#define ENCODER_PHASE_2 3 -#define ENCODER_PHASE_3 1 - -#define ENCODER_PULSES_PER_STEP 4 - -#define BUTTON_PRESSED(BN) !READ(BTN_## BN) - typedef struct { bool enabled = false; int encoderMoveValue = 0; @@ -59,10 +50,10 @@ typedef enum { } ENCODER_DiffState; // Encoder initialization -void Encoder_Configuration(void); +void Encoder_Configuration(); // Analyze encoder value and return state -ENCODER_DiffState Encoder_ReceiveAnalyze(void); +ENCODER_DiffState Encoder_ReceiveAnalyze(); /*********************** Encoder LED ***********************/ @@ -82,23 +73,23 @@ ENCODER_DiffState Encoder_ReceiveAnalyze(void); extern unsigned int LED_DataArray[LED_NUM]; // LED light operation - void LED_Action(void); + void LED_Action(); // LED initialization - void LED_Configuration(void); + void LED_Configuration(); // LED write data - void LED_WriteData(void); + void LED_WriteData(); // LED control // RGB_Scale: RGB color ratio // luminance: brightness (0~0xFF) - void LED_Control(unsigned char RGB_Scale, unsigned char luminance); + void LED_Control(const uint8_t RGB_Scale, const uint8_t luminance); // LED gradient control // RGB_Scale: RGB color ratio // luminance: brightness (0~0xFF) // change_Time: gradient time (ms) - void LED_GraduallyControl(unsigned char RGB_Scale, unsigned char luminance, unsigned int change_Interval); + void LED_GraduallyControl(const uint8_t RGB_Scale, const uint8_t luminance, const uint16_t change_Interval); #endif // LCD_LED diff --git a/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp b/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp index 8342b4338a8a8..a39ff2a0e44fb 100644 --- a/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp +++ b/Marlin/src/lcd/extui/lib/mks_ui/tft_lvgl_configuration.cpp @@ -462,19 +462,10 @@ void lv_encoder_pin_init() { #if ANY_BUTTON(EN1, EN2, ENC, BACK) uint8_t newbutton = 0; - - #if BUTTON_EXISTS(EN1) - if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; - #endif - #if BUTTON_EXISTS(EN2) - if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; - #endif - #if BUTTON_EXISTS(ENC) - if (BUTTON_PRESSED(ENC)) newbutton |= EN_C; - #endif - #if BUTTON_EXISTS(BACK) - if (BUTTON_PRESSED(BACK)) newbutton |= EN_D; - #endif + if (BUTTON_PRESSED(EN1)) newbutton |= EN_A; + if (BUTTON_PRESSED(EN2)) newbutton |= EN_B; + if (BUTTON_PRESSED(ENC)) newbutton |= EN_C; + if (BUTTON_PRESSED(BACK)) newbutton |= EN_D; #else diff --git a/Marlin/src/lcd/lcdprint.h b/Marlin/src/lcd/lcdprint.h index cf34a7ade92eb..b7732d3198896 100644 --- a/Marlin/src/lcd/lcdprint.h +++ b/Marlin/src/lcd/lcdprint.h @@ -76,8 +76,8 @@ #define INFO_FONT_HEIGHT (INFO_FONT_ASCENT + INFO_FONT_DESCENT) #define INFO_FONT_WIDTH 6 - #define SETCURSOR(col, row) lcd_moveto((col) * (MENU_FONT_WIDTH), ((row) + 1) * (MENU_FONT_HEIGHT)) - #define SETCURSOR_RJ(len, row) lcd_moveto(LCD_PIXEL_WIDTH - (len) * (MENU_FONT_WIDTH), ((row) + 1) * (MENU_FONT_HEIGHT)) + #define LCD_COL_X(col) (( (col)) * (MENU_FONT_WIDTH)) + #define LCD_ROW_Y(row) ((1 + (row)) * (MENU_FONT_HEIGHT)) #else @@ -94,14 +94,18 @@ #define LCD_PIXEL_WIDTH LCD_WIDTH #define LCD_PIXEL_HEIGHT LCD_HEIGHT - #define SETCURSOR(col, row) lcd_moveto(col, row) - #define SETCURSOR_RJ(len, row) SETCURSOR(LCD_WIDTH - (len), row) + #define LCD_COL_X(col) (col) + #define LCD_ROW_Y(row) (row) #endif -#define SETCURSOR_X(col) SETCURSOR(col, _lcdLineNr) -#define SETCURSOR_X_RJ(len) SETCURSOR_RJ(len, _lcdLineNr) -#define START_OF_UTF8_CHAR(C) (((C) & 0xC0u) != 0x80U) +#define LCD_COL_X_RJ(len) (LCD_PIXEL_WIDTH - LCD_COL_X(len)) +#define LCD_BOTTOM_ROW (LCD_PIXEL_HEIGHT - 1) +#define SETCURSOR(col, row) lcd_moveto(LCD_COL_X(col), LCD_ROW_Y(row)) +#define SETCURSOR_RJ(len, row) lcd_moveto(LCD_COL_X_RJ(len), LCD_ROW_Y(row)) +#define SETCURSOR_X(col) SETCURSOR(col, _lcdLineNr) +#define SETCURSOR_X_RJ(len) SETCURSOR_RJ(len, _lcdLineNr) +#define START_OF_UTF8_CHAR(C) (((C) & 0xC0u) != 0x80U) int lcd_glyph_height(); diff --git a/Marlin/src/pins/mega/pins_CNCONTROLS_11.h b/Marlin/src/pins/mega/pins_CNCONTROLS_11.h index 675d4b7262f8e..cb0cdb51e34a9 100644 --- a/Marlin/src/pins/mega/pins_CNCONTROLS_11.h +++ b/Marlin/src/pins/mega/pins_CNCONTROLS_11.h @@ -149,9 +149,9 @@ #define BTN_ENC 27 // Hardware buttons for manual movement of XYZ -#define SHIFT_OUT 19 -#define SHIFT_LD 18 -#define SHIFT_CLK 17 +#define SHIFT_OUT_PIN 19 +#define SHIFT_LD_PIN 18 +#define SHIFT_CLK_PIN 17 //#define UI1 31 //#define UI2 22 diff --git a/Marlin/src/pins/mega/pins_CNCONTROLS_12.h b/Marlin/src/pins/mega/pins_CNCONTROLS_12.h index 35426a453139c..540f5c29f5177 100644 --- a/Marlin/src/pins/mega/pins_CNCONTROLS_12.h +++ b/Marlin/src/pins/mega/pins_CNCONTROLS_12.h @@ -156,9 +156,9 @@ #define BTN_ENC 38 // Hardware buttons for manual movement of XYZ -#define SHIFT_OUT 42 -#define SHIFT_LD 41 -#define SHIFT_CLK 40 +#define SHIFT_OUT_PIN 42 +#define SHIFT_LD_PIN 41 +#define SHIFT_CLK_PIN 40 //#define UI1 43 //#define UI2 37 diff --git a/Marlin/src/pins/mega/pins_GT2560_REV_A.h b/Marlin/src/pins/mega/pins_GT2560_REV_A.h index 14b3c7cd42ee4..2f122d4a53f87 100644 --- a/Marlin/src/pins/mega/pins_GT2560_REV_A.h +++ b/Marlin/src/pins/mega/pins_GT2560_REV_A.h @@ -132,10 +132,10 @@ #else // !NEWPANEL - #define SHIFT_CLK 38 - #define SHIFT_LD 42 - #define SHIFT_OUT 40 - #define SHIFT_EN 17 + #define SHIFT_CLK_PIN 38 + #define SHIFT_LD_PIN 42 + #define SHIFT_OUT_PIN 40 + #define SHIFT_EN_PIN 17 #define LCD_PINS_RS 16 #define LCD_PINS_ENABLE 5 diff --git a/Marlin/src/pins/mega/pins_HJC2560C_REV2.h b/Marlin/src/pins/mega/pins_HJC2560C_REV2.h index e30a65b90e1b7..ea08eec0eedc5 100644 --- a/Marlin/src/pins/mega/pins_HJC2560C_REV2.h +++ b/Marlin/src/pins/mega/pins_HJC2560C_REV2.h @@ -156,10 +156,10 @@ #else // Buttons attached to a shift register - #define SHIFT_CLK 38 - #define SHIFT_LD 42 - #define SHIFT_OUT 40 - #define SHIFT_EN 17 + #define SHIFT_CLK_PIN 38 + #define SHIFT_LD_PIN 42 + #define SHIFT_OUT_PIN 40 + #define SHIFT_EN_PIN 17 #define LCD_PINS_RS 16 #define LCD_PINS_ENABLE 5 diff --git a/Marlin/src/pins/mega/pins_MEGATRONICS_2.h b/Marlin/src/pins/mega/pins_MEGATRONICS_2.h index ff7cf15714655..e44d903148cea 100644 --- a/Marlin/src/pins/mega/pins_MEGATRONICS_2.h +++ b/Marlin/src/pins/mega/pins_MEGATRONICS_2.h @@ -146,10 +146,10 @@ #define BTN_ENC 43 #else // Buttons attached to shift register of reprapworld keypad v1.1 - #define SHIFT_CLK 63 - #define SHIFT_LD 42 - #define SHIFT_OUT 17 - #define SHIFT_EN 17 + #define SHIFT_CLK_PIN 63 + #define SHIFT_LD_PIN 42 + #define SHIFT_OUT_PIN 17 + #define SHIFT_EN_PIN 17 #endif #endif // HAS_WIRED_LCD diff --git a/Marlin/src/pins/mega/pins_MEGATRONICS_3.h b/Marlin/src/pins/mega/pins_MEGATRONICS_3.h index 45ebd163a79f1..9f85d46a544b0 100644 --- a/Marlin/src/pins/mega/pins_MEGATRONICS_3.h +++ b/Marlin/src/pins/mega/pins_MEGATRONICS_3.h @@ -162,10 +162,10 @@ #define LCD_PINS_D6 39 #define LCD_PINS_D7 15 - #define SHIFT_CLK 43 - #define SHIFT_LD 35 - #define SHIFT_OUT 34 - #define SHIFT_EN 44 + #define SHIFT_CLK_PIN 43 + #define SHIFT_LD_PIN 35 + #define SHIFT_OUT_PIN 34 + #define SHIFT_EN_PIN 44 #if MB(MEGATRONICS_31, MEGATRONICS_32) #define SD_DETECT_PIN 56 diff --git a/Marlin/src/pins/pinsDebug_list.h b/Marlin/src/pins/pinsDebug_list.h index 4563c79af853b..23f5334f75882 100644 --- a/Marlin/src/pins/pinsDebug_list.h +++ b/Marlin/src/pins/pinsDebug_list.h @@ -830,17 +830,17 @@ #if PIN_EXISTS(SERVO3) REPORT_NAME_DIGITAL(__LINE__, SERVO3_PIN) #endif -#if defined(SHIFT_CLK) && SHIFT_CLK >= 0 - REPORT_NAME_DIGITAL(__LINE__, SHIFT_CLK) +#if PIN_EXISTS(SHIFT_CLK) + REPORT_NAME_DIGITAL(__LINE__, SHIFT_CLK_PIN) #endif -#if defined(SHIFT_EN) && SHIFT_EN >= 0 - REPORT_NAME_DIGITAL(__LINE__, SHIFT_EN) +#if PIN_EXISTS(SHIFT_EN) + REPORT_NAME_DIGITAL(__LINE__, SHIFT_EN_PIN) #endif -#if defined(SHIFT_LD) && SHIFT_LD >= 0 - REPORT_NAME_DIGITAL(__LINE__, SHIFT_LD) +#if PIN_EXISTS(SHIFT_LD) + REPORT_NAME_DIGITAL(__LINE__, SHIFT_LD_PIN) #endif -#if defined(SHIFT_OUT) && SHIFT_OUT >= 0 - REPORT_NAME_DIGITAL(__LINE__, SHIFT_OUT) +#if PIN_EXISTS(SHIFT_OUT) + REPORT_NAME_DIGITAL(__LINE__, SHIFT_OUT_PIN) #endif #if PIN_EXISTS(SLED) REPORT_NAME_DIGITAL(__LINE__, SLED_PIN) diff --git a/Marlin/src/pins/rambo/pins_RAMBO.h b/Marlin/src/pins/rambo/pins_RAMBO.h index 735852b2ff796..000faf3abbaad 100644 --- a/Marlin/src/pins/rambo/pins_RAMBO.h +++ b/Marlin/src/pins/rambo/pins_RAMBO.h @@ -235,10 +235,10 @@ // Buttons attached to a shift register // Not wired yet - //#define SHIFT_CLK 38 - //#define SHIFT_LD 42 - //#define SHIFT_OUT 40 - //#define SHIFT_EN 17 + //#define SHIFT_CLK_PIN 38 + //#define SHIFT_LD_PIN 42 + //#define SHIFT_OUT_PIN 40 + //#define SHIFT_EN_PIN 17 #define LCD_PINS_RS 75 #define LCD_PINS_ENABLE 17 diff --git a/Marlin/src/pins/ramps/pins_ULTIMAKER.h b/Marlin/src/pins/ramps/pins_ULTIMAKER.h index 5b55ac0c4ee32..08e1c7a283363 100644 --- a/Marlin/src/pins/ramps/pins_ULTIMAKER.h +++ b/Marlin/src/pins/ramps/pins_ULTIMAKER.h @@ -142,10 +142,10 @@ #else // !NEWPANEL - Old style panel with shift register // Buttons attached to a shift register - #define SHIFT_CLK 38 - #define SHIFT_LD 42 - #define SHIFT_OUT 40 - #define SHIFT_EN 17 + #define SHIFT_CLK_PIN 38 + #define SHIFT_LD_PIN 42 + #define SHIFT_OUT_PIN 40 + #define SHIFT_EN_PIN 17 #define LCD_PINS_RS 16 #define LCD_PINS_ENABLE 5 diff --git a/Marlin/src/pins/ramps/pins_ULTIMAKER_OLD.h b/Marlin/src/pins/ramps/pins_ULTIMAKER_OLD.h index bf73a75b30b44..db7b5f72047cc 100644 --- a/Marlin/src/pins/ramps/pins_ULTIMAKER_OLD.h +++ b/Marlin/src/pins/ramps/pins_ULTIMAKER_OLD.h @@ -197,10 +197,10 @@ #else // !NEWPANEL - Old style panel with shift register // Buttons attached to a shift register - #define SHIFT_CLK 38 - #define SHIFT_LD 42 - #define SHIFT_OUT 40 - #define SHIFT_EN 17 + #define SHIFT_CLK_PIN 38 + #define SHIFT_LD_PIN 42 + #define SHIFT_OUT_PIN 40 + #define SHIFT_EN_PIN 17 #define LCD_PINS_RS 16 #define LCD_PINS_ENABLE 5 diff --git a/Marlin/src/pins/ramps/pins_ZRIB_V20.h b/Marlin/src/pins/ramps/pins_ZRIB_V20.h index 90433c62d751b..6c4b28d0b8740 100644 --- a/Marlin/src/pins/ramps/pins_ZRIB_V20.h +++ b/Marlin/src/pins/ramps/pins_ZRIB_V20.h @@ -69,9 +69,9 @@ #undef ADC_KEYPAD_PIN #undef BEEPER_PIN - #undef SHIFT_OUT - #undef SHIFT_CLK - #undef SHIFT_LD + #undef SHIFT_OUT_PIN + #undef SHIFT_CLK_PIN + #undef SHIFT_LD_PIN #undef BTN_EN1 #undef BTN_EN2 #undef BTN_ENC diff --git a/Marlin/src/pins/samd/pins_RAMPS_144.h b/Marlin/src/pins/samd/pins_RAMPS_144.h index b29b229c45476..18863312a92ad 100644 --- a/Marlin/src/pins/samd/pins_RAMPS_144.h +++ b/Marlin/src/pins/samd/pins_RAMPS_144.h @@ -363,10 +363,10 @@ #if DISABLED(NEWPANEL) // Buttons attached to a shift register // Not wired yet - //#define SHIFT_CLK 38 - //#define SHIFT_LD 42 - //#define SHIFT_OUT 40 - //#define SHIFT_EN 17 + //#define SHIFT_CLK_PIN 38 + //#define SHIFT_LD_PIN 42 + //#define SHIFT_OUT_PIN 40 + //#define SHIFT_EN_PIN 17 #endif #endif @@ -567,9 +567,9 @@ // Buttons are directly attached to AUX-2 #if ENABLED(REPRAPWORLD_KEYPAD) // TO TEST - //#define SHIFT_OUT 40 - //#define SHIFT_CLK 44 - //#define SHIFT_LD 42 + //#define SHIFT_OUT_PIN 40 + //#define SHIFT_CLK_PIN 44 + //#define SHIFT_LD_PIN 42 //#define BTN_EN1 56 // Mega/Due:64 - AGCM4:56 //#define BTN_EN2 72 // Mega/Due:59 - AGCM4:72 //#define BTN_ENC 55 // Mega/Due:63 - AGCM4:55 diff --git a/Marlin/src/pins/stm32f1/pins_CHITU3D.h b/Marlin/src/pins/stm32f1/pins_CHITU3D.h index 88e30bf499ed0..df6a7761122fa 100644 --- a/Marlin/src/pins/stm32f1/pins_CHITU3D.h +++ b/Marlin/src/pins/stm32f1/pins_CHITU3D.h @@ -144,10 +144,10 @@ #define BEEPER_PIN PC1 // 33 // Buttons attached to a shift register // Not wired yet - //#define SHIFT_CLK PC6 // 38 - //#define SHIFT_LD PC10 // 42 - //#define SHIFT_OUT PC8 // 40 - //#define SHIFT_EN PA1 // 17 + //#define SHIFT_CLK_PIN PC6 // 38 + //#define SHIFT_LD_PIN PC10 // 42 + //#define SHIFT_OUT_PIN PC8 // 40 + //#define SHIFT_EN_PIN PA1 // 17 #endif #endif @@ -258,9 +258,9 @@ #define BTN_EN1 PE0 // 64 #define BTN_EN2 PD11 // 59 #define BTN_ENC PD15 // 63 - #define SHIFT_OUT PC8 // 40 - #define SHIFT_CLK PC12 // 44 - #define SHIFT_LD PC10 // 42 + #define SHIFT_OUT_PIN PC8 // 40 + #define SHIFT_CLK_PIN PC12 // 44 + #define SHIFT_LD_PIN PC10 // 42 #elif ENABLED(PANEL_ONE) #define BTN_EN1 PD11 // 59 // AUX2 PIN 3 #define BTN_EN2 PD15 // 63 // AUX2 PIN 4