Rotary encoder cleanup (#20753)
This commit is contained in:
@ -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 <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user