librfn
An ad-hoc utility library
rotenctest.c
Go to the documentation of this file.
1 /*
2  * rotenctest.c
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2016 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #undef NDEBUG
15 
16 #include <librfn.h>
17 
18 int main()
19 {
20 #define STEP(x, y) rotenc_decode(&myenc, (x << 1) + y)
21  rotenc_t myenc = ROTENC_VAR_INIT;
22 
23  verify(0 == rotenc_count(&myenc));
24  verify(0 == rotenc_count14(&myenc));
25 
26  /* no turn */
27  STEP(0, 0);
28  verify(0 == rotenc_count(&myenc));
29  verify(0 == rotenc_count14(&myenc));
30 
31  /* turn cw */
32  STEP(0, 1);
33  verify(0 == rotenc_count(&myenc));
34  verify(0 == rotenc_count14(&myenc));
35  STEP(1, 1);
36  verify(0 == rotenc_count(&myenc));
37  verify(0 == rotenc_count14(&myenc));
38  STEP(1, 0);
39  verify(0 == rotenc_count(&myenc));
40  verify(0 == rotenc_count14(&myenc));
41  STEP(0, 0);
42  verify(1 == rotenc_count(&myenc));
43  verify(1 == rotenc_count14(&myenc));
44 
45  /* bounce */
46  STEP(0, 1);
47  verify(1 == rotenc_count(&myenc));
48  verify(1 == rotenc_count14(&myenc));
49  STEP(0, 0);
50  verify(1 == rotenc_count(&myenc));
51  verify(1 == rotenc_count14(&myenc));
52  STEP(1, 0);
53  verify(1 == rotenc_count(&myenc));
54  verify(1 == rotenc_count14(&myenc));
55  STEP(0, 0);
56  verify(1 == rotenc_count(&myenc));
57  verify(1 == rotenc_count14(&myenc));
58 
59  /* noise */
60  STEP(1, 1);
61  STEP(0, 0);
62  verify(1 == rotenc_count(&myenc));
63  verify(1 == rotenc_count14(&myenc));
64 
65  /* turn ccw */
66  STEP(1, 0);
67  verify(1 == rotenc_count(&myenc));
68  verify(1 == rotenc_count14(&myenc));
69  STEP(1, 1);
70  verify(1 == rotenc_count(&myenc));
71  verify(1 == rotenc_count14(&myenc));
72  STEP(0, 1);
73  verify(1 == rotenc_count(&myenc));
74  verify(1 == rotenc_count14(&myenc));
75  STEP(0, 0);
76  verify(0 == rotenc_count(&myenc));
77  verify(0 == rotenc_count14(&myenc));
78 
79  /* wrapped count */
80  STEP(1, 0);
81  STEP(1, 1);
82  STEP(0, 1);
83  STEP(0, 0);
84  verify(255 == rotenc_count(&myenc));
85  verify(16383 == rotenc_count14(&myenc));
86 
87  /* extremely wrapped count */
88  for (int i=0; i<1024; i++) {
89  STEP(1, 0);
90  STEP(1, 1);
91  STEP(0, 1);
92  STEP(0, 0);
93  }
94  verify(255 == rotenc_count(&myenc));
95  verify(16383-1024 == rotenc_count14(&myenc));
96 
97  /* cw turn */
98  STEP(0, 1);
99  STEP(1, 1);
100  STEP(1, 0);
101  STEP(0, 0);
102  verify(0 == rotenc_count(&myenc));
103  verify(16383-1023 == rotenc_count14(&myenc));
104 
105  return 0;
106 }
#define STEP(x, y)
int main()
Definition: rotenctest.c:18
#define verify(x)
Definition: util.h:55
uint16_t rotenc_count14(rotenc_t *r)
Read a 14-bit count of the current knob position.
Definition: rotenc.c:45
#define ROTENC_VAR_INIT
Static initializer for a rotary encoder.
Definition: rotenc.h:39
Rotary encoder click counts and state tracking.
Definition: rotenc.h:30