Tue, 27 Oct 2020 21:48:28 +0100
hp34comm: add support for boot keycode
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 | /** | |
127 | * Includes | |
128 | */ | |
129 | #include "QEI.h" | |
130 | ||
131 | QEI::QEI(PinName channelA, | |
132 | PinName channelB, | |
133 | PinName index, | |
134 | int pulsesPerRev, | |
135 | Encoding encoding, | |
136 | const event_callback_t& ev_callback) : | |
137 | channelA_(channelA), | |
138 | channelB_(channelB), | |
139 | index_(index), | |
140 | _callback(ev_callback) { | |
141 | ||
142 | pulses_ = 0; | |
143 | revolutions_ = 0; | |
144 | pulsesPerRev_ = pulsesPerRev; | |
145 | encoding_ = encoding; | |
146 | ||
147 | //Workout what the current state is. | |
148 | int chanA = channelA_.read(); | |
149 | int chanB = channelB_.read(); | |
150 | ||
151 | //2-bit state. | |
152 | currState_ = (chanA << 1) | (chanB); | |
153 | prevState_ = currState_; | |
154 | ||
155 | //X2 encoding uses interrupts on only channel A. | |
156 | //X4 encoding uses interrupts on channel A, | |
157 | //and on channel B. | |
158 | channelA_.rise(callback(this, &QEI::encode)); | |
159 | channelA_.fall(callback(this, &QEI::encode)); | |
160 | ||
161 | //If we're using X4 encoding, then attach interrupts to channel B too. | |
162 | if (encoding == X4_ENCODING) { | |
163 | channelB_.rise(callback(this, &QEI::encode)); | |
164 | channelB_.fall(callback(this, &QEI::encode)); | |
165 | } | |
166 | //Index is optional. | |
167 | if (index_ != NC) { | |
168 | index_.rise(callback(this, &QEI::index)); | |
169 | } | |
170 | ||
171 | } | |
172 | ||
173 | void QEI::reset(void) { | |
174 | ||
175 | pulses_ = 0; | |
176 | revolutions_ = 0; | |
177 | ||
178 | } | |
179 | ||
180 | int QEI::getCurrentState(void) { | |
181 | ||
182 | return currState_; | |
183 | ||
184 | } | |
185 | ||
186 | int QEI::getPulses(void) { | |
187 | ||
188 | return pulses_; | |
189 | ||
190 | } | |
191 | ||
192 | int QEI::getRevolutions(void) { | |
193 | ||
194 | return revolutions_; | |
195 | ||
196 | } | |
197 | ||
198 | // +-------------+ | |
199 | // | X2 Encoding | | |
200 | // +-------------+ | |
201 | // | |
202 | // When observing states two patterns will appear: | |
203 | // | |
204 | // Counter clockwise rotation: | |
205 | // | |
206 | // 10 -> 01 -> 10 -> 01 -> ... | |
207 | // | |
208 | // Clockwise rotation: | |
209 | // | |
210 | // 11 -> 00 -> 11 -> 00 -> ... | |
211 | // | |
212 | // We consider counter clockwise rotation to be "forward" and | |
213 | // counter clockwise to be "backward". Therefore pulse count will increase | |
214 | // during counter clockwise rotation and decrease during clockwise rotation. | |
215 | // | |
216 | // +-------------+ | |
217 | // | X4 Encoding | | |
218 | // +-------------+ | |
219 | // | |
220 | // There are four possible states for a quadrature encoder which correspond to | |
221 | // 2-bit gray code. | |
222 | // | |
223 | // A state change is only valid if of only one bit has changed. | |
224 | // A state change is invalid if both bits have changed. | |
225 | // | |
226 | // Clockwise Rotation -> | |
227 | // | |
228 | // 00 01 11 10 00 | |
229 | // | |
230 | // <- Counter Clockwise Rotation | |
231 | // | |
232 | // If we observe any valid state changes going from left to right, we have | |
233 | // moved one pulse clockwise [we will consider this "backward" or "negative"]. | |
234 | // | |
235 | // If we observe any valid state changes going from right to left we have | |
236 | // moved one pulse counter clockwise [we will consider this "forward" or | |
237 | // "positive"]. | |
238 | // | |
239 | // We might enter an invalid state for a number of reasons which are hard to | |
240 | // predict - if this is the case, it is generally safe to ignore it, update | |
241 | // the state and carry on, with the error correcting itself shortly after. | |
242 | void QEI::encode(void) { | |
243 | ||
244 | int change = 0; | |
245 | int chanA = channelA_.read(); | |
246 | int chanB = channelB_.read(); | |
247 | ||
248 | //2-bit state. | |
249 | currState_ = (chanA << 1) | (chanB); | |
250 | ||
251 | if (encoding_ == X2_ENCODING) { | |
252 | ||
253 | //11->00->11->00 is counter clockwise rotation or "forward". | |
254 | if ((prevState_ == 0x3 && currState_ == 0x0) || | |
255 | (prevState_ == 0x0 && currState_ == 0x3)) { | |
256 | change = 1; | |
257 | pulses_++; | |
258 | ||
259 | } | |
260 | //10->01->10->01 is clockwise rotation or "backward". | |
261 | else if ((prevState_ == 0x2 && currState_ == 0x1) || | |
262 | (prevState_ == 0x1 && currState_ == 0x2)) { | |
263 | change = -1; | |
264 | pulses_--; | |
265 | ||
266 | } | |
267 | ||
268 | } else if (encoding_ == X4_ENCODING) { | |
269 | ||
270 | //Entered a new valid state. | |
271 | if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) { | |
272 | //2 bit state. Right hand bit of prev XOR left hand bit of current | |
273 | //gives 0 if clockwise rotation and 1 if counter clockwise rotation. | |
274 | change = (prevState_ & PREV_MASK) ^ ((currState_ & CURR_MASK) >> 1); | |
275 | ||
276 | if (change == 0) { | |
277 | change = -1; | |
278 | } | |
279 | ||
280 | pulses_ -= change; | |
281 | } | |
282 | ||
283 | } | |
284 | ||
285 | if (_callback && (change != 0)) | |
286 | _callback.call(change==1?1:0); | |
287 | ||
288 | prevState_ = currState_; | |
289 | ||
290 | } | |
291 | ||
292 | void QEI::index(void) { | |
293 | ||
294 | revolutions_++; | |
295 | ||
296 | } |