-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreyEncoder.h
More file actions
181 lines (152 loc) · 5.82 KB
/
Copy pathGreyEncoder.h
File metadata and controls
181 lines (152 loc) · 5.82 KB
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
#include "Arduino.h"
#define TBUF_LEN 8
#define ENCODER_EVENTS_FIFO_SIZE 32
enum {
FULL_CYCLE_ENCODER = 0, // 1 event per full cycle of 4 phases (standard detent click)
HALF_CYCLE_ENCODER, // 1 event per 2 phases of the Gray code
QUARTER_CYCLE_ENCODER // Event on every phase change (maximum resolution)
};
typedef struct ENCODER_SETTINGS_ {
uint8_t id;
uint8_t PIN_A;
uint8_t PIN_B;
uint8_t type;
uint8_t state; // Lower bits: pin history
// Fast direct-mapping AVR registers and bitmasks
volatile uint8_t* port_A;
uint8_t mask_A;
volatile uint8_t* port_B;
uint8_t mask_B;
} ENCODER_SETTINGS;
class GreyEncoder
{
public:
typedef struct {
volatile int push_indx;
volatile int pop_indx;
unsigned char buff[ENCODER_EVENTS_FIFO_SIZE];
} ENCODER_EVENTS_FIFO;
ENCODER_EVENTS_FIFO fifo;
GreyEncoder(ENCODER_SETTINGS *settings_table, int encoders_number)
{
table = settings_table;
encoders_max = encoders_number;
fifo.push_indx = 0;
fifo.pop_indx = 0;
// Pre-calculate AVR input registers and bitmasks for each encoder
for(int i = 0; i < encoders_max; i++)
{
pinMode(table[i].PIN_A, INPUT_PULLUP);
pinMode(table[i].PIN_B, INPUT_PULLUP);
table[i].port_A = portInputRegister(digitalPinToPort(table[i].PIN_A));
table[i].mask_A = digitalPinToBitMask(table[i].PIN_A);
table[i].port_B = portInputRegister(digitalPinToPort(table[i].PIN_B));
table[i].mask_B = digitalPinToBitMask(table[i].PIN_B);
// Read initial startup state
uint8_t a = (*table[i].port_A & table[i].mask_A) ? 1 : 0;
uint8_t b = (*table[i].port_B & table[i].mask_B) ? 1 : 0;
table[i].state = (a << 1) | b;
}
}
~GreyEncoder()
{
}
int fifo_pop(unsigned char *data)
{
int indx;
if (fifo.push_indx == fifo.pop_indx) return 0; // FIFO is empty
indx = (fifo.pop_indx + 1) % ENCODER_EVENTS_FIFO_SIZE;
*data = fifo.buff[indx];
fifo.pop_indx = indx;
return 1;
}
int fifo_push(unsigned char data)
{
int indx = (fifo.push_indx + 1) % ENCODER_EVENTS_FIFO_SIZE;
if (indx == fifo.pop_indx) return 0; // FIFO overflow
fifo.buff[indx] = data;
fifo.push_indx = indx;
return 1;
}
// Must be invoked by a high-frequency timer interrupt (4-5 kHz recommended for fast rotation)
int periodic(void)
{
// Gray code state transition table. Returns: +1 (CW), -1 (CCW), 0 (invalid state or no movement)
static const int8_t knob_states[] = {
0, 1, -1, 0,
-1, 0, 0, 1,
1, 0, 0, -1,
0, -1, 1, 0
};
for(int i = 0; i < encoders_max; i++)
{
// Direct register read (avoids slow digitalRead overhead, completes in a few CPU cycles)
uint8_t a = (*table[i].port_A & table[i].mask_A) ? 2 : 0;
uint8_t b = (*table[i].port_B & table[i].mask_B) ? 1 : 0;
uint8_t current_pins = a | b;
uint8_t prev_pins = table[i].state & 0x03;
if (prev_pins != current_pins)
{
// Form a 4-bit index: [prev_A][prev_B][curr_A][curr_B]
uint8_t index = (prev_pins << 2) | current_pins;
int8_t delta = knob_states[index];
// Save current pin state into the lower 2 bits
table[i].state = (table[i].state & 0xFC) | current_pins;
if (delta != 0)
{
bool emit_event = false;
switch(table[i].type)
{
case QUARTER_CYCLE_ENCODER:
// Trigger an event on absolutely every state transition
emit_event = true;
break;
case HALF_CYCLE_ENCODER:
// Trigger an event every 2 steps when the signal matches detent states (00 or 11)
if (current_pins == 0x00 || current_pins == 0x03) {
emit_event = true;
}
break;
case FULL_CYCLE_ENCODER:
// Standard full step (1 click = 4 transitions).
// Only trigger when returning to the idle detent state (typically 11 due to pull-ups)
if (current_pins == 0x03) {
emit_event = true;
}
break;
}
if (emit_event) {
// Pack event data: Bit 0 = direction (1: CW, 0: CCW), Bits 1-7 = encoder index
fifo_push((i << 1) | (delta > 0));
}
}
}
}
return 1;
}
int8_t nibble_to_hex(int8_t nibble)
{
static const char hex_table[] = "0123456789ABCDEF";
return hex_table[nibble&0x0F];
}
void manage_events(void)
{
uint8_t event;
if(fifo_pop(&event))
{
uint8_t idx = event & 0xFE;
bool is_clockwise = event & 0x01;
uint16_t event_id = table[idx].id + (is_clockwise ? 1 : 0);
tbuf[0] = '{';
tbuf[1] = '2';
tbuf[2] = nibble_to_hex(event >> 4);
tbuf[3] = nibble_to_hex(event & 0x0F);
tbuf[4] = '}';
Serial.write(tbuf,5);
}
}
private:
uint8_t tbuf[TBUF_LEN];
ENCODER_SETTINGS *table;
int encoders_max;
};