-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotary_encoder.c
146 lines (127 loc) · 2.34 KB
/
rotary_encoder.c
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
#include "rotary_encoder.h"
#include "rotary_encoder_customization.h"
volatile int8_t enc_delta; // -128 ... 127
static int8_t last;
uint8_t y_old = 0;
uint8_t flag = 0;
uint16_t long_counter = 0;
knob_state knob ={0};
void encode_init( void )
{
int8_t new;
new = 0;
if( PHASE_A )
new = 3;
if( PHASE_B )
new ^= 1; // convert gray to binary
last = new; // power on state
enc_delta = 0;
knob.knob_up = 0;
knob.long_pressed = 0;
}
void rotarydecode(void)
{
int8_t new, diff;
new = 0;
if( PHASE_A )
new = 3;
if( PHASE_B )
new ^= 1; // convert gray to binary
diff = last - new; // difference last - new
if( diff & 1 ){ // bit 0 = value (1)
last = new; // store new as next last
enc_delta += (diff & 2) - 1; // bit 1 = direction (+/-)
}
}
int8_t encode_read1( void ) // read single step encoders
{
int8_t val;
val = enc_delta;
enc_delta = 0;
return val; // counts since last call
}
int8_t encode_read2( void ) // read two step encoders
{
int8_t val;
val = enc_delta;
enc_delta = val & 1;
return val >> 1;
}
int8_t encode_read4( void ) // read four step encoders
{
int8_t val;
val = enc_delta;
enc_delta = val & 3;
return val >> 2;
}
void debounce_switch(void)
{
uint8_t temp = 0;
temp=(y_old>>2);
y_old=y_old-temp;
if(KNOB_PIN == 0)
{
y_old=y_old+0x3F;
}
if((y_old > 0xF0)&&(flag==0))
{
flag=1;
knob.long_pressed = 0;
long_counter = 0;
}
if((y_old < 0x0F)&&(flag==1))
{
if(knob.long_pressed== 0)
{
knob.knob_up = 1;
}
flag=0;
knob.long_pressed = 0;
long_counter = 0;
}
if( flag == 1)
{
long_counter++;
}
else
{
long_counter = 0;
}
if(long_counter > 1024)
{
knob.long_pressed = 1;
}
}
knob_state* get_knob_state()
{
return &knob;
}
uint8_t knob_up(void)
{
uint8_t ret = knob.knob_up;
//clear state
knob.knob_up = 0;
return ret;
}
uint8_t long_pressed(void)
{
uint8_t ret = 0;
if(knob.long_pressed > 0)
{
knob.long_pressed = 0;
ret = 1;
}
return ret;
}
/*
int main( void )
{
int32_t val = 0;
LEDS_DDR = 0xFF;
encode_init();
sei();
for(;;){
val += encode_read1(); // read a single step encoder
LEDS = val;
}
}*/