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
#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]
use embedded_hal::digital::v2::InputPin;
pub type Velocity = f32;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
None,
Clockwise,
Anticlockwise,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Sensitivity {
Default = 2,
Low = 4,
}
const STATES: [i8; 16] = [0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0];
#[cfg(feature = "angular-velocity")]
const DEFAULT_VELOCITY_INC_FACTOR: f32 = 0.2;
#[cfg(feature = "angular-velocity")]
const DEFAULT_VELOCITY_DEC_FACTOR: f32 = 0.01;
#[cfg(feature = "angular-velocity")]
const DEFAULT_VELOCITY_ACTION_MS: u64 = 25;
pub struct RotaryEncoder<DT, CLK> {
pin_dt: DT,
pin_clk: CLK,
pos_calc: i8,
sensitivity: Sensitivity,
transition: u8,
direction: Direction,
}
#[cfg(feature = "angular-velocity")]
pub struct RotaryEncoderWithVelocity<DT, CLK> {
inner: RotaryEncoder<DT, CLK>,
velocity: Velocity,
velocity_inc_factor: f32,
velocity_dec_factor: f32,
velocity_action_ms: u64,
previous_time: u64,
}
impl<DT, CLK> RotaryEncoder<DT, CLK>
where
DT: InputPin,
CLK: InputPin,
{
pub fn new(pin_dt: DT, pin_clk: CLK) -> Self {
RotaryEncoder {
pin_dt,
pin_clk,
pos_calc: 0,
transition: 0,
sensitivity: Sensitivity::Default,
direction: Direction::None,
}
}
pub fn set_sensitivity(&mut self, sensitivity: Sensitivity) {
self.sensitivity = sensitivity;
}
pub fn borrow_pins(&mut self) -> (&mut DT, &mut CLK) {
(&mut self.pin_dt, &mut self.pin_clk)
}
pub fn release(self) -> (DT, CLK) {
(self.pin_dt, self.pin_clk)
}
pub fn update(&mut self) {
let dt_state = self.pin_dt.is_high().unwrap_or_default() as u8;
let clk_state = self.pin_clk.is_high().unwrap_or_default() as u8;
let current = (dt_state << 1) | clk_state;
self.transition = (self.transition << 2) | current;
let index = (self.transition & 0x0F) as usize;
self.pos_calc += STATES[index];
let sensitivity = self.sensitivity as i8;
if self.pos_calc == sensitivity || self.pos_calc == -sensitivity {
self.direction = if self.pos_calc == sensitivity {
Direction::Clockwise
} else {
Direction::Anticlockwise
};
self.pos_calc = 0;
return;
}
self.direction = Direction::None;
}
pub fn direction(&self) -> Direction {
self.direction
}
}
#[cfg(feature = "angular-velocity")]
impl<DT, CLK> RotaryEncoderWithVelocity<DT, CLK>
where
DT: InputPin,
CLK: InputPin,
{
pub fn new(pin_dt: DT, pin_clk: CLK) -> Self {
RotaryEncoderWithVelocity {
inner: RotaryEncoder::new(pin_dt, pin_clk),
velocity: 0.0,
velocity_inc_factor: DEFAULT_VELOCITY_INC_FACTOR,
velocity_dec_factor: DEFAULT_VELOCITY_DEC_FACTOR,
velocity_action_ms: DEFAULT_VELOCITY_ACTION_MS,
previous_time: 0,
}
}
pub fn set_velocity_inc_factor(&mut self, inc_factor: f32) {
self.velocity_inc_factor = inc_factor;
}
pub fn set_velocity_dec_factor(&mut self, dec_factor: f32) {
self.velocity_dec_factor = dec_factor;
}
pub fn set_velocity_action_ms(&mut self, action_ms: u64) {
self.velocity_action_ms = action_ms;
}
pub fn decay_velocity(&mut self) {
self.velocity -= self.velocity_dec_factor;
if self.velocity < 0.0 {
self.velocity = 0.0;
}
}
pub fn borrow_pins(&mut self) -> (&mut DT, &mut CLK) {
self.inner.borrow_pins()
}
pub fn set_sensitivity(&mut self, sensitivity: Sensitivity) {
self.inner.sensitivity = sensitivity;
}
pub fn borrow_inner(&mut self) -> &mut RotaryEncoder<DT, CLK> {
&mut self.inner
}
pub fn release(self) -> (DT, CLK) {
self.inner.release()
}
pub fn update(&mut self, current_time: u64) {
self.inner.update();
if self.inner.direction() != Direction::None {
if current_time - self.previous_time < self.velocity_action_ms && self.velocity < 1.0 {
self.velocity += self.velocity_inc_factor;
if self.velocity > 1.0 {
self.velocity = 1.0;
}
}
return;
}
self.previous_time = current_time;
}
pub fn direction(&self) -> Direction {
self.inner.direction
}
pub fn velocity(&self) -> Velocity {
self.velocity
}
}