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
#![deny(missing_docs)]
#![deny(warnings)]
#![no_std]
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use embedded_hal::digital::v2::InputPin;
pub type Velocity = f32;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
None,
Clockwise,
Anticlockwise,
}
const STATES: [i8; 16] = [0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0];
const DEFAULT_VELOCITY_INC_FACTOR: f32 = 0.2;
const DEFAULT_VELOCITY_DEC_FACTOR: f32 = 0.01;
const DEFAULT_VELOCITY_ACTION_MS: i64 = 25;
pub struct RotaryEncoder<DT, CLK> {
pin_dt: DT,
pin_clk: CLK,
velocity: Velocity,
velocity_inc_factor: f32,
velocity_dec_factor: f32,
velocity_action_ms: i64,
pos_calc: i8,
transition: u8,
direction: Direction,
previous_time: NaiveDateTime,
}
impl<DT, CLK> RotaryEncoder<DT, CLK>
where
DT: InputPin,
CLK: InputPin,
{
pub fn new(
pin_dt: DT,
pin_clk: CLK,
velocity_inc_factor: Option<f32>,
velocity_dec_factor: Option<f32>,
velocity_action_ms: Option<i64>,
) -> Self {
return RotaryEncoder {
pin_dt,
pin_clk,
pos_calc: 0,
velocity: 0.0,
velocity_inc_factor: velocity_inc_factor.unwrap_or(DEFAULT_VELOCITY_INC_FACTOR),
velocity_dec_factor: velocity_dec_factor.unwrap_or(DEFAULT_VELOCITY_DEC_FACTOR),
velocity_action_ms: velocity_action_ms.unwrap_or(DEFAULT_VELOCITY_ACTION_MS),
transition: 0,
direction: Direction::None,
previous_time: NaiveDateTime::new(
NaiveDate::from_ymd(2000, 1, 1),
NaiveTime::from_hms_milli(0, 0, 0, 0),
),
};
}
pub fn tick(&mut self) {
self.velocity -= self.velocity_dec_factor;
if self.velocity < -1.0 {
self.velocity = -1.0;
}
}
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, current_time: NaiveDateTime) {
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 = self.pos_calc + STATES[index];
if self.pos_calc == 4 || self.pos_calc == -4 {
if current_time.timestamp_millis() - self.previous_time.timestamp_millis()
< self.velocity_action_ms
&& self.velocity < 1.0
{
self.velocity += self.velocity_inc_factor;
}
self.direction = if self.pos_calc == 4 {
Direction::Clockwise
} else {
Direction::Anticlockwise
};
self.pos_calc = 0;
return;
}
self.previous_time = current_time;
self.direction = Direction::None;
}
pub fn direction(&self) -> Direction {
self.direction
}
pub fn velocity(&self) -> Velocity {
return if self.velocity < 0.0 {
0.0
} else {
self.velocity
};
}
}