Tue, 10 Nov 2020 22:22:16 +0100
Add support for dimmed flag display
27 | 1 | /** |
2 | * @author Aaron Berk | |
3 | * | |
4 | * @section LICENSE | |
5 | * | |
6 | * Copyright (c) 2010 ARM Limited | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
24 | * THE SOFTWARE. | |
25 | * | |
26 | * @section DESCRIPTION | |
27 | * | |
28 | * Quadrature Encoder Interface. | |
29 | * | |
30 | * A quadrature encoder consists of two code tracks on a disc which are 90 | |
31 | * degrees out of phase. It can be used to determine how far a wheel has | |
32 | * rotated, relative to a known starting position. | |
33 | * | |
34 | * Only one code track changes at a time leading to a more robust system than | |
35 | * a single track, because any jitter around any edge won't cause a state | |
36 | * change as the other track will remain constant. | |
37 | * | |
38 | * Encoders can be a homebrew affair, consisting of infrared emitters/receivers | |
39 | * and paper code tracks consisting of alternating black and white sections; | |
40 | * alternatively, complete disk and PCB emitter/receiver encoder systems can | |
41 | * be bought, but the interface, regardless of implementation is the same. | |
42 | * | |
43 | * +-----+ +-----+ +-----+ | |
44 | * Channel A | ^ | | | | | | |
45 | * ---+ ^ +-----+ +-----+ +----- | |
46 | * ^ ^ | |
47 | * ^ +-----+ +-----+ +-----+ | |
48 | * Channel B ^ | | | | | | | |
49 | * ------+ +-----+ +-----+ +----- | |
50 | * ^ ^ | |
51 | * ^ ^ | |
52 | * 90deg | |
53 | * | |
54 | * The interface uses X2 encoding by default which calculates the pulse count | |
55 | * based on reading the current state after each rising and falling edge of | |
56 | * channel A. | |
57 | * | |
58 | * +-----+ +-----+ +-----+ | |
59 | * Channel A | | | | | | | |
60 | * ---+ +-----+ +-----+ +----- | |
61 | * ^ ^ ^ ^ ^ | |
62 | * ^ +-----+ ^ +-----+ ^ +-----+ | |
63 | * Channel B ^ | ^ | ^ | ^ | ^ | | | |
64 | * ------+ ^ +-----+ ^ +-----+ +-- | |
65 | * ^ ^ ^ ^ ^ | |
66 | * ^ ^ ^ ^ ^ | |
67 | * Pulse count 0 1 2 3 4 5 ... | |
68 | * | |
69 | * This interface can also use X4 encoding which calculates the pulse count | |
70 | * based on reading the current state after each rising and falling edge of | |
71 | * either channel. | |
72 | * | |
73 | * +-----+ +-----+ +-----+ | |
74 | * Channel A | | | | | | | |
75 | * ---+ +-----+ +-----+ +----- | |
76 | * ^ ^ ^ ^ ^ | |
77 | * ^ +-----+ ^ +-----+ ^ +-----+ | |
78 | * Channel B ^ | ^ | ^ | ^ | ^ | | | |
79 | * ------+ ^ +-----+ ^ +-----+ +-- | |
80 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
81 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
82 | * Pulse count 0 1 2 3 4 5 6 7 8 9 ... | |
83 | * | |
84 | * It defaults | |
85 | * | |
86 | * An optional index channel can be used which determines when a full | |
87 | * revolution has occured. | |
88 | * | |
89 | * If a 4 pules per revolution encoder was used, with X4 encoding, | |
90 | * the following would be observed. | |
91 | * | |
92 | * +-----+ +-----+ +-----+ | |
93 | * Channel A | | | | | | | |
94 | * ---+ +-----+ +-----+ +----- | |
95 | * ^ ^ ^ ^ ^ | |
96 | * ^ +-----+ ^ +-----+ ^ +-----+ | |
97 | * Channel B ^ | ^ | ^ | ^ | ^ | | | |
98 | * ------+ ^ +-----+ ^ +-----+ +-- | |
99 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
100 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
101 | * ^ ^ ^ +--+ ^ ^ +--+ ^ | |
102 | * ^ ^ ^ | | ^ ^ | | ^ | |
103 | * Index ------------+ +--------+ +----------- | |
104 | * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ | |
105 | * Pulse count 0 1 2 3 4 5 6 7 8 9 ... | |
106 | * Rev. count 0 1 2 | |
107 | * | |
108 | * Rotational position in degrees can be calculated by: | |
109 | * | |
110 | * (pulse count / X * N) * 360 | |
111 | * | |
112 | * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number | |
113 | * of pulses per revolution. | |
114 | * | |
115 | * Linear position can be calculated by: | |
116 | * | |
117 | * (pulse count / X * N) * (1 / PPI) | |
118 | * | |
119 | * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of | |
120 | * pulses per revolution, and PPI is pulses per inch, or the equivalent for | |
121 | * any other unit of displacement. PPI can be calculated by taking the | |
122 | * circumference of the wheel or encoder disk and dividing it by the number | |
123 | * of pulses per revolution. | |
124 | */ | |
125 | ||
126 | #ifndef QEI_H | |
127 | #define QEI_H | |
128 | ||
129 | /** | |
130 | * Includes | |
131 | */ | |
132 | #include "mbed.h" | |
133 | ||
134 | /** | |
135 | * Defines | |
136 | */ | |
137 | #define PREV_MASK 0x1 //Mask for the previous state in determining direction | |
138 | //of rotation. | |
139 | #define CURR_MASK 0x2 //Mask for the current state in determining direction | |
140 | //of rotation. | |
141 | #define INVALID 0x3 //XORing two states where both bits have changed. | |
142 | ||
143 | /** | |
144 | * Quadrature Encoder Interface. | |
145 | */ | |
146 | class QEI { | |
147 | ||
148 | public: | |
149 | ||
150 | typedef enum Encoding { | |
151 | ||
152 | X2_ENCODING, | |
153 | X4_ENCODING | |
154 | ||
155 | } Encoding; | |
156 | ||
157 | /** | |
158 | * Constructor. | |
159 | * | |
160 | * Reads the current values on channel A and channel B to determine the | |
161 | * initial state. | |
162 | * | |
163 | * Attaches the encode function to the rise/fall interrupt edges of | |
164 | * channels A and B to perform X4 encoding. | |
165 | * | |
166 | * Attaches the index function to the rise interrupt edge of channel index | |
167 | * (if it is used) to count revolutions. | |
168 | * | |
169 | * @param channelA mbed pin for channel A input. | |
170 | * @param channelB mbed pin for channel B input. | |
171 | * @param index mbed pin for optional index channel input, | |
172 | * (pass NC if not needed). | |
173 | * @param pulsesPerRev Number of pulses in one revolution. | |
174 | * @param encoding The encoding to use. Uses X2 encoding by default. X2 | |
175 | * encoding uses interrupts on the rising and falling edges | |
176 | * of only channel A where as X4 uses them on both | |
177 | * channels. | |
178 | */ | |
179 | QEI(PinName channelA, PinName channelB, PinName index, | |
180 | int pulsesPerRev, Encoding encoding = X2_ENCODING, | |
181 | const event_callback_t& ev_callback=NULL); | |
182 | ||
183 | /** | |
184 | * Reset the encoder. | |
185 | * | |
186 | * Sets the pulses and revolutions count to zero. | |
187 | */ | |
188 | void reset(void); | |
189 | ||
190 | /** | |
191 | * Read the state of the encoder. | |
192 | * | |
193 | * @return The current state of the encoder as a 2-bit number, where: | |
194 | * bit 1 = The reading from channel B | |
195 | * bit 2 = The reading from channel A | |
196 | */ | |
197 | int getCurrentState(void); | |
198 | ||
199 | /** | |
200 | * Read the number of pulses recorded by the encoder. | |
201 | * | |
202 | * @return Number of pulses which have occured. | |
203 | */ | |
204 | int getPulses(void); | |
205 | ||
206 | /** | |
207 | * Read the number of revolutions recorded by the encoder on the index channel. | |
208 | * | |
209 | * @return Number of revolutions which have occured on the index channel. | |
210 | */ | |
211 | int getRevolutions(void); | |
212 | ||
213 | void attach(const event_callback_t& callback=NULL) { | |
214 | _callback = callback; | |
215 | }; | |
216 | ||
217 | private: | |
218 | ||
219 | /** | |
220 | * Update the pulse count. | |
221 | * | |
222 | * Called on every rising/falling edge of channels A/B. | |
223 | * | |
224 | * Reads the state of the channels and determines whether a pulse forward | |
225 | * or backward has occured, updating the count appropriately. | |
226 | */ | |
227 | void encode(void); | |
228 | ||
229 | /** | |
230 | * Called on every rising edge of channel index to update revolution | |
231 | * count by one. | |
232 | */ | |
233 | void index(void); | |
234 | ||
235 | Encoding encoding_; | |
236 | ||
237 | InterruptIn channelA_; | |
238 | InterruptIn channelB_; | |
239 | InterruptIn index_; | |
240 | ||
241 | int pulsesPerRev_; | |
242 | int prevState_; | |
243 | int currState_; | |
244 | ||
245 | volatile int pulses_; | |
246 | volatile int revolutions_; | |
247 | event_callback_t _callback; | |
248 | ||
249 | }; | |
250 | ||
251 | #endif /* QEI_H */ |