1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/**
* 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 <https://www.gnu.org/licenses/>.
*
*/
/*****************************************************************************
* @file rotary_encoder.cpp
* @author LEO / Creality3D
* @date 2019/07/06
* @version 2.0.1
* @brief Rotary encoder functions
*****************************************************************************/
#include "../../../inc/MarlinConfigPre.h"
#if ENABLED(DWIN_CREALITY_LCD)
#include "rotary_encoder.h"
#include "../../buttons.h"
#include "../../../MarlinCore.h"
#include "../../../HAL/shared/Delay.h"
#if HAS_BUZZER
#include "../../../libs/buzzer.h"
#endif
#include <stdlib.h>
#ifndef ENCODER_PULSES_PER_STEP
#define ENCODER_PULSES_PER_STEP 4
#endif
ENCODER_Rate EncoderRate;
// Buzzer
void Encoder_tick() {
#if PIN_EXISTS(BEEPER)
WRITE(BEEPER_PIN, HIGH);
delay(10);
WRITE(BEEPER_PIN, LOW);
#endif
}
// Encoder initialization
void Encoder_Configuration() {
#if BUTTON_EXISTS(EN1)
SET_INPUT_PULLUP(BTN_EN1);
#endif
#if BUTTON_EXISTS(EN2)
SET_INPUT_PULLUP(BTN_EN2);
#endif
#if BUTTON_EXISTS(ENC)
SET_INPUT_PULLUP(BTN_ENC);
#endif
#if PIN_EXISTS(BEEPER)
SET_OUTPUT(BEEPER_PIN);
#endif
}
// Analyze encoder value and return state
ENCODER_DiffState Encoder_ReceiveAnalyze() {
const millis_t now = millis();
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 |= 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)) {
next_click_update_ms = millis() + 300;
Encoder_tick();
#if PIN_EXISTS(LCD_LED)
//LED_Action();
#endif
const bool was_waiting = wait_for_user;
wait_for_user = false;
return was_waiting ? ENCODER_DIFF_NO : ENCODER_DIFF_ENTER;
}
else return ENCODER_DIFF_NO;
}
if (newbutton != lastEncoderBits) {
switch (newbutton) {
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++;
else if (lastEncoderBits == ENCODER_PHASE_2) 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++;
else if (lastEncoderBits == ENCODER_PHASE_0) temp_diff--;
break;
}
lastEncoderBits = newbutton;
}
if (abs(temp_diff) >= ENCODER_PULSES_PER_STEP) {
if (temp_diff > 0) temp_diffState = ENCODER_DIFF_CW;
else temp_diffState = ENCODER_DIFF_CCW;
#if ENABLED(ENCODER_RATE_MULTIPLIER)
millis_t ms = millis();
int32_t encoderMultiplier = 1;
// if must encoder rati multiplier
if (EncoderRate.enabled) {
const float abs_diff = ABS(temp_diff),
encoderMovementSteps = abs_diff / (ENCODER_PULSES_PER_STEP);
if (EncoderRate.lastEncoderTime) {
// 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;
else if (encoderStepRate >= ENCODER_5X_STEPS_PER_SEC) encoderMultiplier = 5;
}
EncoderRate.lastEncoderTime = ms;
}
#else
constexpr int32_t encoderMultiplier = 1;
#endif
// EncoderRate.encoderMoveValue += (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
EncoderRate.encoderMoveValue = (temp_diff * encoderMultiplier) / (ENCODER_PULSES_PER_STEP);
if (EncoderRate.encoderMoveValue < 0) EncoderRate.encoderMoveValue = -EncoderRate.encoderMoveValue;
temp_diff = 0;
}
return temp_diffState;
}
#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
uint16_t LED_DataArray[LED_NUM];
// LED light operation
void LED_Action() {
LED_Control(RGB_SCALE_WARM_WHITE,0x0F);
delay(30);
LED_Control(RGB_SCALE_WARM_WHITE,0x00);
}
// LED initialization
void LED_Configuration() {
SET_OUTPUT(LCD_LED_PIN);
}
// LED write data
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)) {
LED_DATA_HIGH;
DELAY_NS(300);
LED_DATA_LOW;
DELAY_NS(200);
}
else {
LED_DATA_HIGH;
LED_DATA_LOW;
DELAY_NS(200);
}
}
}
}
// LED control
// RGB_Scale: RGB color ratio
// luminance: brightness (0~0xFF)
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[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();
}
// LED gradient control
// RGB_Scale: RGB color ratio
// luminance: brightness (0~0xFF)
// change_Time: gradient time (ms)
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_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;
}
}
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);
}
}
}
#endif // LCD_LED
#endif // DWIN_CREALITY_LCD
|