Encoder: handle encoder (untested)
parent
63f57df758
commit
7c9323c9b8
|
@ -14,27 +14,24 @@ When rotated clockwise, CLK changes before DT; when rotated counter-clockwise, D
|
|||
|
||||
#define SW_DEBOUNCE_MS 10
|
||||
|
||||
bool previousClk = false;
|
||||
bool previousDt = false;
|
||||
|
||||
void setupEncoder() {
|
||||
pinMode(CLK_PIN, INPUT);
|
||||
pinMode(DT_PIN, INPUT);
|
||||
pinMode(SW_PIN, INPUT_PULLUP);
|
||||
|
||||
// save initial state
|
||||
previousClk = digitalRead(CLK_PIN);
|
||||
previousDt = digitalRead(DT_PIN);
|
||||
}
|
||||
|
||||
void handleSwitch();
|
||||
|
||||
void demoEncoder() {
|
||||
bool clk = digitalRead(CLK_PIN);
|
||||
bool dt = digitalRead(DT_PIN);
|
||||
bool sw = digitalRead(SW_PIN);
|
||||
|
||||
Serial.print("CLK: ");
|
||||
Serial.print(clk);
|
||||
Serial.print(" DT: ");
|
||||
Serial.print(dt);
|
||||
Serial.print(" SW: ");
|
||||
Serial.println(sw);
|
||||
|
||||
handleSwitch();
|
||||
handleEncoder();
|
||||
}
|
||||
|
||||
bool previousPressed = false;
|
||||
|
@ -61,3 +58,33 @@ void handleSwitch() {
|
|||
// handle change
|
||||
Serial.println(isPressed ? "Pressed" : "Released");
|
||||
}
|
||||
|
||||
void handleEncoder() {
|
||||
bool clk = digitalRead(CLK_PIN);
|
||||
bool dt = digitalRead(DT_PIN);
|
||||
|
||||
// only handle change
|
||||
if (clk == previousClk && dt == previousDt) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Do we need to debounce encoder?
|
||||
|
||||
// clockwise pattern:
|
||||
// clk: 1001 1001
|
||||
// dt: 1100 1100
|
||||
// counter-clockwise pattern:
|
||||
// clk: 1100 1100
|
||||
// dt: 1001 1001
|
||||
if (clk == dt) {
|
||||
if (clk == previousClk) {
|
||||
Serial.println("Clockwise");
|
||||
} else {
|
||||
Serial.println("Counter-clockwise");
|
||||
}
|
||||
}
|
||||
|
||||
// save state
|
||||
previousClk = clk;
|
||||
previousDt = dt;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue