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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
   Copyright 2019 Ilya Epifanov

   Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
   http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
   http://opensource.org/licenses/MIT>, at your option. This file may not be
   copied, modified, or distributed except according to those terms.
*/
/*!
A helper for handling rotary encoder input, mainly for use on embedded platforms.

```
use embtk_rotary_encoder::RotaryEncoder;

# fn main() {
// The `u16` type will be accepted as a raw position output from your QEI peripheral
let mut enc: RotaryEncoder<u16, _, _> =
// `4i8` below is the number of raw divisions per full physical division.
// `10u32` is the timeout in any kind of ticks you like.
// You'll supply a timestamp every time you receive an even from a peripheral.
    RotaryEncoder::new(4i8, 10u32);

assert_eq!(enc.get_delta(65534u16, 1), 0); // we haven't moved full 4 divisions yet
assert_eq!(enc.get_delta(65532u16, 2), -1); // full 4 divisions down => 1 logical division down

assert_eq!(enc.get_delta(65530u16, 3), 0);
assert_eq!(enc.get_delta(65528u16, 20), 0); // too late, read about timeouts below
# }
```

A note about timeout:

Sometimes you may lose an event from a peripheral or even a peripheral might be buggy.
In this case you'll end up slightly off grid, i.e. you'll see the transition to a next
division not when you feel the tactile feedback from an encoder but somewhere between the positions.

To remedy that, there's a timeout which, on expiry, makes a current position of an encoder a
reference for subsequent moves.

*/
#![no_std]

use core::default::Default;

use num_traits::Num;
use num_traits::WrappingAdd;
use num_traits::WrappingSub;
use num_traits::Bounded;
use num_traits::AsPrimitive;
use num_traits::CheckedSub;
use num_traits::Unsigned;
use num_traits::Signed;

pub struct RotaryEncoder<Pos, Tick, Delta> where
    Pos: Num + WrappingAdd + WrappingSub + Bounded + Copy + PartialOrd + AsPrimitive<Delta> + Default,
    Tick: Unsigned + Bounded + Copy + PartialOrd + CheckedSub + Default,
    Delta: Signed + Copy + AsPrimitive<Pos>,
{
    last_active: Tick,
    last_effective_raw_position: Pos,
    last_real_raw_position: Pos,
    reset_timeout: Tick,
    div: Delta,
}

impl<Pos, Tick, Delta> RotaryEncoder<Pos, Tick, Delta> where
    Pos: Num + WrappingAdd + WrappingSub + Bounded + Copy + PartialOrd + AsPrimitive<Delta> + Default,
    Tick: Unsigned + Bounded + Copy + PartialOrd + CheckedSub + Default,
    Delta: Signed + Copy + AsPrimitive<Pos>,
{
    pub fn new(div: Delta, reset_timeout: Tick) -> Self {
        RotaryEncoder {
            div,
            last_active: Default::default(),
            last_effective_raw_position: Default::default(),
            last_real_raw_position: Default::default(),
            reset_timeout,
        }
    }

    pub fn get_delta(&mut self, raw_position: Pos, ts: Tick) -> Delta where
    {
        if (self.last_active + self.reset_timeout).checked_sub(&ts) == None {
            self.last_effective_raw_position = self.last_real_raw_position;
        }

        let delta: Delta = raw_position.wrapping_sub(&self.last_effective_raw_position).as_();

        let divisions = delta / self.div;
        let remainder = delta % self.div;

        self.last_effective_raw_position = self.last_effective_raw_position.wrapping_add(&(delta - remainder).as_());
        if self.last_real_raw_position != raw_position {
            self.last_active = ts;
            self.last_real_raw_position = raw_position;
        }
        divisions
    }
}

#[cfg(test)]
mod tests {
    use self::super::*;

    #[test]
    fn zero() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);
        assert_eq!(enc.get_delta(0, 1), 0);
    }

    #[test]
    fn increment() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);
        assert_eq!(enc.get_delta(1, 1), 1);
    }

    #[test]
    fn decrement() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);
        assert_eq!(enc.get_delta(-1, 1), -1);
    }

    #[test]
    fn rollover_up() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);

        assert_eq!(enc.get_delta(127, 1), 127);
        assert_eq!(enc.get_delta(-128, 1), 1);
    }

    #[test]
    fn rollover_down() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);

        assert_eq!(enc.get_delta(-128, 1), -128);
        assert_eq!(enc.get_delta(127, 1), -1);
    }

    #[test]
    fn all_the_way_up() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);

        for p in 1..512 {
            assert_eq!(enc.get_delta((p % 256) as i8, 1), 1);
        }
    }

    #[test]
    fn all_the_way_down() {
        let mut enc = RotaryEncoder::new(1i8, 10u32);

        for p in -1..-512 {
            assert_eq!(enc.get_delta((p % 256) as i8, 1), 1);
        }
    }

    #[test]
    fn increment_4divs() {
        let mut enc = RotaryEncoder::new(4i8, 10u32);

        assert_eq!(enc.get_delta(1, 1), 0);
        assert_eq!(enc.get_delta(2, 1), 0);
        assert_eq!(enc.get_delta(3, 1), 0);
        assert_eq!(enc.get_delta(4, 1), 1);
    }

    #[test]
    fn decrement_4divs() {
        let mut enc = RotaryEncoder::new(4i8, 10u32);
        assert_eq!(enc.get_delta(-1, 1), 0);
        assert_eq!(enc.get_delta(-2, 1), 0);
        assert_eq!(enc.get_delta(-3, 1), 0);
        assert_eq!(enc.get_delta(-4, 1), -1);
    }

    #[test]
    fn rollover_up_4divs() {
        let mut enc = RotaryEncoder::new(4i8, 10u32);

        assert_eq!(enc.get_delta(124, 1), 31);
        assert_eq!(enc.get_delta(127, 1), 0);
        assert_eq!(enc.get_delta(-128, 1), 1);
        assert_eq!(enc.get_delta(-127, 1), 0);
        assert_eq!(enc.get_delta(-126, 1), 0);
        assert_eq!(enc.get_delta(-125, 1), 0);
        assert_eq!(enc.get_delta(-124, 1), 1);
    }

    #[test]
    fn rollover_down_4divs() {
        let mut enc = RotaryEncoder::new(4i8, 10u32);

        assert_eq!(enc.get_delta(-128, 1), -32);
        assert_eq!(enc.get_delta(127, 1), 0);
        assert_eq!(enc.get_delta(126, 1), 0);
        assert_eq!(enc.get_delta(125, 1), 0);
        assert_eq!(enc.get_delta(124, 1), -1);
    }

    #[test]
    fn all_the_way_up_4divs() {
        let mut enc = RotaryEncoder::new(4i8, 10u32);

        for p in 1..512 {
            assert_eq!(enc.get_delta((p % 256) as i8, 1), if p % 4 == 0 { 1 } else { 0 });
        }
    }

    #[test]
    fn all_the_way_down_4divs() {
        let mut enc = RotaryEncoder::new(4i8, 10u32);

        for p in -1..-512 {
            assert_eq!(enc.get_delta((p % 256) as i8, 1), if p % 4 == 0 { -1 } else { 0 });
        }
    }

    #[test]
    fn zero_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);
        assert_eq!(enc.get_delta(0, 1), 0);
    }

    #[test]
    fn increment_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);
        assert_eq!(enc.get_delta(1, 1), 1);
    }

    #[test]
    fn decrement_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);
        assert_eq!(enc.get_delta(255, 1), -1);
    }

    #[test]
    fn rollover_up_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);

        assert_eq!(enc.get_delta(127, 1), 127);
        assert_eq!(enc.get_delta(128, 1), 1);
        assert_eq!(enc.get_delta(255, 1), 127);
        assert_eq!(enc.get_delta(0, 1), 1);
    }

    #[test]
    fn rollover_down_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);

        assert_eq!(enc.get_delta(255, 1), -1);
    }

    #[test]
    fn all_the_way_up_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);

        for p in 1..512 {
            assert_eq!(enc.get_delta((p % 256) as u8, 1), 1);
        }
    }

    #[test]
    fn all_the_way_down_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(1i8, 10u32);

        for p in -1..-512 {
            assert_eq!(enc.get_delta((p % 256) as u8, 1), 1);
        }
    }

    #[test]
    fn increment_4divs_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(4i8, 10u32);

        assert_eq!(enc.get_delta(1, 1), 0);
        assert_eq!(enc.get_delta(2, 1), 0);
        assert_eq!(enc.get_delta(3, 1), 0);
        assert_eq!(enc.get_delta(4, 1), 1);
    }

    #[test]
    fn decrement_4divs_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(4i8, 10u32);
        assert_eq!(enc.get_delta(255, 1), 0);
        assert_eq!(enc.get_delta(254, 1), 0);
        assert_eq!(enc.get_delta(253, 1), 0);
        assert_eq!(enc.get_delta(252, 1), -1);
    }

    #[test]
    fn rollover_up_4divs_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(4i8, 10u32);

        assert_eq!(enc.get_delta(127, 1), 31);
        assert_eq!(enc.get_delta(128, 1), 1);
        assert_eq!(enc.get_delta(252, 1), 31);
        assert_eq!(enc.get_delta(253, 1), 0);
        assert_eq!(enc.get_delta(254, 1), 0);
        assert_eq!(enc.get_delta(255, 1), 0);
        assert_eq!(enc.get_delta(0, 1), 1);
        assert_eq!(enc.get_delta(1, 1), 0);
        assert_eq!(enc.get_delta(2, 1), 0);
        assert_eq!(enc.get_delta(3, 1), 0);
        assert_eq!(enc.get_delta(4, 1), 1);
    }

    #[test]
    fn rollover_down_4divs_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(4i8, 10u32);

        assert_eq!(enc.get_delta(255, 1), 0);
        assert_eq!(enc.get_delta(254, 1), 0);
        assert_eq!(enc.get_delta(253, 1), 0);
        assert_eq!(enc.get_delta(252, 1), -1);
    }

    #[test]
    fn all_the_way_up_4divs_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(4i8, 10u32);

        for p in 1..512 {
            assert_eq!(enc.get_delta((p % 256) as u8, 1), if p % 4 == 0 { 1 } else { 0 });
        }
    }

    #[test]
    fn all_the_way_down_4divs_unsigned() {
        let mut enc: RotaryEncoder<u8, _, _> = RotaryEncoder::new(4i8, 10u32);

        for p in -1..-512 {
            assert_eq!(enc.get_delta((p % 256) as u8, 1), if p % 4 == 0 { -1 } else { 0 });
        }
    }
}