-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoder.h
99 lines (84 loc) · 2.75 KB
/
encoder.h
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
/**
* Encoder Library
* // https://github.com/soligen2010/encoder
*/
#include <ClickEncoder.h>
// : error: 'ClickEncoder::buttonHeldEnabled' will be initialized after [-Werror=reorder]
#define ENCODER_PINA 2 // not working
#define ENCODER_PINB 16 // working
#define ENCODER_BTN 0 // using analog, could maybe use rx/3?
int encoder_value;
// int encoder_numsteps =2; // Type 1
int encoder_numsteps = 4; // Type 2
#define ENCODER_STEPS_PER_NOTCH 1 // Change this depending on which encoder is used
// ClickEncoder encoder = ClickEncoder(ENCODER_PINA,ENCODER_PINB,ENCODER_BTN,ENCODER_STEPS_PER_NOTCH);
ClickEncoder *encoder;
void test_encoder(){
bool pinclk = digitalRead(ENCODER_PINA) == LOW;
bool pindata = digitalRead(ENCODER_PINB) == LOW;
if(pinclk && !pindata){
Serial.println("pinclk");
}
if(!pinclk && pindata){
Serial.println("pindata");
}
if(pinclk && pindata){
Serial.println("both");
}
}
int encoder_prev;
int8_t encoder_change;
bool process_encoder(){
// test_encoder();return;
static uint32_t lastService = 0;
// encoder_value; // running total
// // throttle
// if (micros() - lastService <= 200) {
// return;
// }
encoder->service();
encoder_value += encoder->getValue();
// workaround for
if (abs(encoder_value - encoder_prev) >= encoder_numsteps) {
// if (encoder_value != encoder_prev) {
Serial.print("Encoder Value: ");
Serial.print(encoder_value);
Serial.print(" Dir: ");
Serial.print((encoder_value > encoder_prev) ? "10" : "20"); // L:R ints for plotting
Serial.print("\t");
Serial.print(" Steps: ");
Serial.print((encoder_value-encoder_prev)/encoder_numsteps);
Serial.print("\n");
encoder_change = (encoder_value-encoder_prev)/encoder_numsteps;
encoder_prev = encoder_value;
return true;
}
return false;
}
uint8_t getButtonState(){
ClickEncoder::Button b = encoder->getButton();
if (b != ClickEncoder::Open) {
Serial.print("Button: ");
#define VERBOSECASE(label) case label: Serial.println(#label); break;
switch (b) {
VERBOSECASE(ClickEncoder::Pressed);
VERBOSECASE(ClickEncoder::Held)
VERBOSECASE(ClickEncoder::Released)
VERBOSECASE(ClickEncoder::Clicked)
VERBOSECASE(ClickEncoder::DoubleClicked)
VERBOSECASE(ClickEncoder::Open)
VERBOSECASE(ClickEncoder::Closed)
}
}
return b;
}
int8_t getEncoder(){
return encoder_change;
}
void init_encoder(int8_t A, int8_t B, int8_t BTN, uint8_t stepsPerNotch){
// int8_t A, int8_t B, int8_t BTN, uint8_t stepsPerNotch, bool active
encoder = new ClickEncoder(A, B, BTN, stepsPerNotch);
if(BTN == 0)encoder->setButtonOnPinZeroEnabled(true);
encoder->setDoubleClickEnabled(false); // add delay for dblclick checking
encoder->setButtonHeldEnabled(true);
}