keyboard.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* key matrix position */
  21. typedef struct {
  22. uint8_t col;
  23. uint8_t row;
  24. } keypos_t;
  25. /* key event */
  26. typedef struct {
  27. keypos_t key;
  28. bool pressed;
  29. uint16_t time;
  30. } keyevent_t;
  31. /* equivalent test of keypos_t */
  32. #define KEYEQ(keya, keyb) ((keya).row == (keyb).row && (keya).col == (keyb).col)
  33. /* special keypos_t entries */
  34. #define KEYLOC_TICK 255
  35. #define KEYLOC_COMBO 254
  36. #define KEYLOC_ENCODER_CW 253
  37. #define KEYLOC_ENCODER_CCW 252
  38. /* Rules for No Event:
  39. * 1) (time == 0) to handle (keyevent_t){} as empty event
  40. * 2) Matrix(255, 255) to make TICK event available
  41. */
  42. static inline bool IS_NOEVENT(keyevent_t event) {
  43. return event.time == 0 || (event.key.row == KEYLOC_TICK && event.key.col == KEYLOC_TICK);
  44. }
  45. static inline bool IS_KEYEVENT(keyevent_t event) {
  46. return event.key.row < MATRIX_ROWS && event.key.col < MATRIX_COLS;
  47. }
  48. static inline bool IS_COMBOEVENT(keyevent_t event) {
  49. return event.key.row == KEYLOC_COMBO;
  50. }
  51. static inline bool IS_ENCODEREVENT(keyevent_t event) {
  52. return event.key.row == KEYLOC_ENCODER_CW || event.key.row == KEYLOC_ENCODER_CCW;
  53. }
  54. static inline bool IS_PRESSED(keyevent_t event) {
  55. return !IS_NOEVENT(event) && event.pressed;
  56. }
  57. static inline bool IS_RELEASED(keyevent_t event) {
  58. return !IS_NOEVENT(event) && !event.pressed;
  59. }
  60. /* Common keyevent object factory */
  61. #define MAKE_KEYPOS(row_num, col_num) ((keypos_t){.row = (row_num), .col = (col_num)})
  62. /**
  63. * @brief Constructs a key event for a pressed or released key.
  64. */
  65. #define MAKE_KEYEVENT(row_num, col_num, press) ((keyevent_t){.key = MAKE_KEYPOS((row_num), (col_num)), .pressed = (press), .time = (timer_read() | 1)})
  66. /**
  67. * @brief Constructs a internal tick event that is used to drive the internal QMK state machine.
  68. */
  69. #define TICK_EVENT MAKE_KEYEVENT(KEYLOC_TICK, KEYLOC_TICK, false)
  70. #ifdef ENCODER_MAP_ENABLE
  71. /* Encoder events */
  72. # define ENCODER_CW_EVENT(enc_id, press) MAKE_KEYEVENT(KEYLOC_ENCODER_CW, (enc_id), (press))
  73. # define ENCODER_CCW_EVENT(enc_id, press) MAKE_KEYEVENT(KEYLOC_ENCODER_CCW, (enc_id), (press))
  74. #endif // ENCODER_MAP_ENABLE
  75. /* it runs once at early stage of startup before keyboard_init. */
  76. void keyboard_setup(void);
  77. /* it runs once after initializing host side protocol, debug and MCU peripherals. */
  78. void keyboard_init(void);
  79. /* it runs repeatedly in main loop */
  80. void keyboard_task(void);
  81. /* it runs whenever code has to behave differently on a slave */
  82. bool is_keyboard_master(void);
  83. /* it runs whenever code has to behave differently on left vs right split */
  84. bool is_keyboard_left(void);
  85. void keyboard_pre_init_kb(void);
  86. void keyboard_pre_init_user(void);
  87. void keyboard_post_init_kb(void);
  88. void keyboard_post_init_user(void);
  89. void housekeeping_task(void); // To be executed by the main loop in each backend TMK protocol
  90. void housekeeping_task_kb(void); // To be overridden by keyboard-level code
  91. void housekeeping_task_user(void); // To be overridden by user/keymap-level code
  92. uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder activity
  93. uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder activity
  94. uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity
  95. uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity
  96. uint32_t last_encoder_activity_time(void); // Timestamp of the last encoder activity
  97. uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity
  98. uint32_t get_matrix_scan_rate(void);
  99. #ifdef __cplusplus
  100. }
  101. #endif