Emakefun Matrix Keyboard Arduino Lib 1.0.1
Loading...
Searching...
No Matches
matrix_keyboard.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef _EMAKEFUN_MATRIX_KEY_BOARD_H_
4#define _EMAKEFUN_MATRIX_KEY_BOARD_H_
5
6#include <Wire.h>
7#include <stdint.h>
8
9#include "debouncer.h"
10
11/**
12 * @file matrix_keyboard.h
13 */
14
15namespace emakefun {
16
17/**
18 * @~Chinese
19 * @class MatrixKeyboard
20 * @brief MatrixKeyboard是用于矩阵键盘模块的驱动类。
21 */
22/**
23 * @~English
24 * @class MatrixKeyboard
25 * @brief MatrixKeyboard is a driver class for the Matrix Keyboard module.
26 */
28 public:
29 /**
30 * @~Chinese
31 * @brief 默认I2C地址。
32 */
33 /**
34 * @~English
35 * @brief Default I2C address.
36 */
37 static constexpr uint8_t kDefaultI2cAddress = 0x65;
38
39 /**
40 * @~Chinese
41 * @enum ErrorCode
42 * @brief 错误码。
43 */
44 /**
45 * @~English
46 * @enum ErrorCode
47 * @brief Error codes.
48 */
49 enum class ErrorCode : uint32_t {
50 /**
51 * @~Chinese
52 * @brief 成功。
53 */
54 /**
55 * @~English
56 * @brief Success.
57 */
58 kOK = 0,
59 /**
60 * @~Chinese
61 * @brief I2C数据太长,无法装入传输缓冲区。
62 */
63 /**
64 * @~English
65 * @brief I2C data too long to fit in transmit buffer.
66 */
68 /**
69 * @~Chinese
70 * @brief 在I2C发送地址时收到NACK。
71 */
72 /**
73 * @~English
74 * @brief NACK received on I2C transmit of address.
75 */
77 /**
78 * @~Chinese
79 * @brief 在I2C发送数据时收到NACK。
80 */
81 /**
82 * @~English
83 * @brief NACK received on I2C transmit of data.
84 */
86 /**
87 * @~Chinese
88 * @brief 其他I2C错误。
89 */
90 /**
91 * @~English
92 * @brief Other I2C errors.
93 */
95 /**
96 * @~Chinese
97 * @brief I2C通讯超时。
98 */
99 /**
100 * @~English
101 * @brief I2C communication timed out.
102 */
104 /**
105 * @~Chinese
106 * @brief 参数错误。
107 */
108 /**
109 * @~English
110 * @brief Invalid parameter.
111 */
113 /**
114 * @~Chinese
115 * @brief 未知错误。
116 */
117 /**
118 * @~English
119 * @brief Unknown error.
120 */
122 };
123
124 /**
125 * @~Chinese
126 * @enum Key
127 * @brief 键值。
128 */
129 /**
130 * @~English
131 * @enum Key
132 * @brief Key values.
133 */
134 enum Key : uint16_t {
135 /**
136 * @~Chinese
137 * @brief 无按键。
138 */
139 /**
140 * @~English
141 * @brief No Key.
142 */
143 kKeyNone = static_cast<Key>(0),
144 /**
145 * @~Chinese
146 * @brief 按键0。
147 */
148 /**
149 * @~English
150 * @brief Key 0.
151 */
152 kKey0 = static_cast<Key>(1) << 7,
153 /**
154 * @~Chinese
155 * @brief 按键1。
156 */
157 /**
158 * @~English
159 * @brief Key 1.
160 */
161 kKey1 = static_cast<Key>(1) << 0,
162 /**
163 * @~Chinese
164 * @brief 按键2。
165 */
166 /**
167 * @~English
168 * @brief Key 2.
169 */
170 kKey2 = static_cast<Key>(1) << 4,
171 /**
172 * @~Chinese
173 * @brief 按键3。
174 */
175 /**
176 * @~English
177 * @brief Key 3.
178 */
179 kKey3 = static_cast<Key>(1) << 8,
180 /**
181 * @~Chinese
182 * @brief 按键4。
183 */
184 /**
185 * @~English
186 * @brief Key 4.
187 */
188 kKey4 = static_cast<Key>(1) << 1,
189 /**
190 * @~Chinese
191 * @brief 按键5。
192 */
193 /**
194 * @~English
195 * @brief Key 5.
196 */
197 kKey5 = static_cast<Key>(1) << 5,
198 /**
199 * @~Chinese
200 * @brief 按键6。
201 */
202 /**
203 * @~English
204 * @brief Key 6.
205 */
206 kKey6 = static_cast<Key>(1) << 9,
207 /**
208 * @~Chinese
209 * @brief 按键7。
210 */
211 /**
212 * @~English
213 * @brief Key 7.
214 */
215 kKey7 = static_cast<Key>(1) << 2,
216 /**
217 * @~Chinese
218 * @brief 按键8。
219 */
220 /**
221 * @~English
222 * @brief Key 8.
223 */
224 kKey8 = static_cast<Key>(1) << 6,
225 /**
226 * @~Chinese
227 * @brief 按键9。
228 */
229 /**
230 * @~English
231 * @brief Key 9.
232 */
233 kKey9 = static_cast<Key>(1) << 10,
234 /**
235 * @~Chinese
236 * @brief 按键A。
237 */
238 /**
239 * @~English
240 * @brief Key A.
241 */
242 kKeyA = static_cast<Key>(1) << 12,
243 /**
244 * @~Chinese
245 * @brief 按键B。
246 */
247 /**
248 * @~English
249 * @brief Key B.
250 */
251 kKeyB = static_cast<Key>(1) << 13,
252 /**
253 * @~Chinese
254 * @brief 按键C。
255 */
256 /**
257 * @~English
258 * @brief Key C.
259 */
260 kKeyC = static_cast<Key>(1) << 14,
261 /**
262 * @~Chinese
263 * @brief 按键D。
264 */
265 /**
266 * @~English
267 * @brief Key D.
268 */
269 kKeyD = static_cast<Key>(1) << 15,
270 /**
271 * @~Chinese
272 * @brief 按键星号。
273 */
274 /**
275 * @~English
276 * @brief Key asterisk.
277 */
278 kKeyAsterisk = static_cast<Key>(1) << 3,
279 /**
280 * @~Chinese
281 * @brief 按键井号。
282 */
283 /**
284 * @~English
285 * @brief Key number sign.
286 */
287 kKeyNumberSign = static_cast<Key>(1) << 11,
288 };
289
290 /**
291 * @~Chinese
292 * @enum KeyState
293 * @brief 按键状态。
294 */
295 /**
296 * @~English
297 * @enum KeyState
298 * @brief Key state.
299 */
300 enum class KeyState : uint8_t {
301 /**
302 * @~Chinese
303 * @brief 按键空闲。
304 */
305 /**
306 * @~English
307 * @brief Key is idle.
308 */
310 /**
311 * @~Chinese
312 * @brief 按键被按下。
313 */
314 /**
315 * @~English
316 * @brief Key is pressed.
317 */
319 /**
320 * @~Chinese
321 * @brief 按键被按住。
322 */
323 /**
324 * @~English
325 * @brief Key is pressing.
326 */
328 /**
329 * @~Chinese
330 * @brief 按键被弹起。
331 */
332 /**
333 * @~English
334 * @brief Key is released.
335 */
337 };
338
339 /**
340 * @~Chinese
341 * @brief 构造函数
342 * @param[in] wire TwoWire 对象引用。
343 * @param[in] i2c_address I2C地址。
344 */
345 /**
346 * @~English
347 * @brief 构造函数
348 * @param[in] wire TwoWire object reference.
349 * @param[in] i2c_address I2C address.
350 */
351 explicit MatrixKeyboard(TwoWire& wire, const uint8_t i2c_address);
352
353 /**
354 * @~Chinese
355 * @brief 初始化。
356 * @return 返回值请参考 @ref ErrorCode。
357 */
358 /**
359 * @~English
360 * @brief Initialize.
361 * @return Return value refer to @ref ErrorCode.
362 */
364
365 /**
366 * @~Chinese
367 * @brief 扫描按键,在函数loop中调用,每次循环先调用该函数再查询按键状态。
368 */
369 /**
370 * @~English
371 * @brief Scan keys, call this function in the loop function, and then query the key state each time the loop is executed.
372 */
373 void Tick();
374
375 /**
376 * @~Chinese
377 * @brief 查询按键是否被按下。
378 * @param[in] key 按键,参考枚举 @ref Key。
379 * @return 返回true代表按键被按下。
380 */
381 /**
382 * @~English
383 * @brief Check whether the button is pressed.
384 * @param[in] Key, refer to the enumeration @ref Key.
385 * @return Returning true indicates that the button has been pressed.
386 */
387 bool Pressed(const Key key) const;
388
389 /**
390 * @~Chinese
391 * @brief 查询按键是否被按住。
392 * @param[in] Key, refer to the enumeration @ref Key.
393 * @return 返回true代表按键被按住。
394 */
395 /**
396 * @~English
397 * @brief Check whether the key is held down.
398 * @param[in] Key, refer to the enumeration @ref Key.
399 * @return Return true to indicate that the key is held down.
400 */
401 bool Pressing(const Key key) const;
402
403 /**
404 * @~Chinese
405 * @brief 查询按键是否被释放。
406 * @param[in] Key, refer to the enumeration @ref Key.
407 * @return 返回true代表按键被释放。
408 */
409 /**
410 * @~English
411 * @brief Check whether the key has been released.
412 * @param[in] Key, refer to the enumeration @ref Key.
413 * @return Returning true indicates that the key has been released.
414 */
415 bool Released(const Key key) const;
416
417 /**
418 * @~Chinese
419 * @brief 查询按键是否空闲。
420 * @param[in] Key, refer to the enumeration @ref Key.
421 * @return 返回true代表按键空闲。
422 */
423 /**
424 * @~English
425 * @brief Check whether the key is idle.
426 * @param[in] Key, refer to the enumeration @ref Key.
427 * @return Return true to indicate that the key is idle.
428 */
429 bool Idle(const Key key) const;
430
431 /**
432 * @~Chinese
433 * @brief 查询按键状态。
434 * @param[in] Key, refer to the enumeration @ref Key.
435 * @return 按键状态,参考枚举 @ref KeyState。
436 */
437 /**
438 * @~English
439 * @brief Query key state.
440 * @param[in] Key, refer to the enumeration @ref Key.
441 * @return Key state, refer to enumeration @ref KeyState.
442 */
443 KeyState GetKeyState(const Key key) const;
444
445 /**
446 * @~Chinese
447 * @brief 获取当前按下的按键。
448 * @return char 按键字符:'0'-'9'、'A'-'D'、'*'、'#',返回'\0'表示没有按键按下。
449 * @note 如果多个按键同时按下,则按顺序返回第一个检测到的按键。
450 */
451 /**
452 * @~English
453 * @brief Get the currently pressed key.
454 * @return char key character: '0'-'9', 'A'-'D', '*', '#', returns '\0' to indicate no key pressed.
455 * @note If multiple buttons are pressed simultaneously, return to the first detected button in order.
456 */
457 char GetCurrentPressedKey() const;
458
459 private:
460 MatrixKeyboard(const MatrixKeyboard&) = delete;
461 MatrixKeyboard& operator=(const MatrixKeyboard&) = delete;
462
463 Key ReadKey();
464
465 TwoWire& wire_ = Wire;
466 const uint8_t i2c_address_ = kDefaultI2cAddress;
467 Debouncer<Key> key_;
468 Key last_key_ = kKeyNone;
469};
470} // namespace emakefun
471#endif
MatrixKeyboard is a driver class for the Matrix Keyboard module.
char GetCurrentPressedKey() const
Get the currently pressed key.
@ kI2cTimeout
I2C communication timed out.
@ kI2cReceivedNackOnTransmitOfAddress
NACK received on I2C transmit of address.
@ kI2cReceivedNackOnTransmitOfData
NACK received on I2C transmit of data.
@ kI2cDataTooLongToFitInTransmitBuffer
I2C data too long to fit in transmit buffer.
bool Idle(const Key key) const
Check whether the key is idle.
static constexpr uint8_t kDefaultI2cAddress
Default I2C address.
MatrixKeyboard(TwoWire &wire, const uint8_t i2c_address)
构造函数
@ kKeyNumberSign
Key number sign.
bool Pressing(const Key key) const
Check whether the key is held down.
bool Pressed(const Key key) const
Check whether the button is pressed.
ErrorCode Initialize()
Initialize.
KeyState GetKeyState(const Key key) const
Query key state.
bool Released(const Key key) const
Check whether the key has been released.
void Tick()
Scan keys, call this function in the loop function, and then query the key state each time the loop i...