keyboard.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. typedef enum keyevent_type_t { TICK_EVENT = 0, KEY_EVENT = 1, ENCODER_CW_EVENT = 2, ENCODER_CCW_EVENT = 3, COMBO_EVENT = 4 } keyevent_type_t;
  26. /* key event */
  27. typedef struct {
  28. keypos_t key;
  29. uint16_t time;
  30. keyevent_type_t type;
  31. bool pressed;
  32. } keyevent_t;
  33. /* equivalent test of keypos_t */
  34. #define KEYEQ(keya, keyb) ((keya).row == (keyb).row && (keya).col == (keyb).col)
  35. /* special keypos_t entries */
  36. #define KEYLOC_ENCODER_CW 253
  37. #define KEYLOC_ENCODER_CCW 252
  38. static inline bool IS_NOEVENT(const keyevent_t event) {
  39. return event.type == TICK_EVENT;
  40. }
  41. static inline bool IS_EVENT(const keyevent_t event) {
  42. return event.type != TICK_EVENT;
  43. }
  44. static inline bool IS_KEYEVENT(const keyevent_t event) {
  45. return event.type == KEY_EVENT;
  46. }
  47. static inline bool IS_COMBOEVENT(const keyevent_t event) {
  48. return event.type == COMBO_EVENT;
  49. }
  50. static inline bool IS_ENCODEREVENT(const keyevent_t event) {
  51. return event.type == ENCODER_CW_EVENT || event.type == ENCODER_CCW_EVENT;
  52. }
  53. /* Common keypos_t object factory */
  54. #define MAKE_KEYPOS(row_num, col_num) ((keypos_t){.row = (row_num), .col = (col_num)})
  55. /* Common keyevent_t object factory */
  56. #define MAKE_EVENT(row_num, col_num, press, event_type) ((keyevent_t){.key = MAKE_KEYPOS((row_num), (col_num)), .pressed = (press), .time = timer_read(), .type = (event_type)})
  57. /**
  58. * @brief Constructs a key event for a pressed or released key.
  59. */
  60. #define MAKE_KEYEVENT(row_num, col_num, press) MAKE_EVENT((row_num), (col_num), (press), KEY_EVENT)
  61. /**
  62. * @brief Constructs a combo event.
  63. */
  64. #define MAKE_COMBOEVENT(press) MAKE_EVENT(0, 0, (press), COMBO_EVENT)
  65. /**
  66. * @brief Constructs a internal tick event that is used to drive the internal QMK state machine.
  67. */
  68. #define MAKE_TICK_EVENT MAKE_EVENT(0, 0, false, TICK_EVENT)
  69. #ifdef ENCODER_MAP_ENABLE
  70. /* Encoder events */
  71. # define MAKE_ENCODER_CW_EVENT(enc_id, press) MAKE_EVENT(KEYLOC_ENCODER_CW, (enc_id), (press), ENCODER_CW_EVENT)
  72. # define MAKE_ENCODER_CCW_EVENT(enc_id, press) MAKE_EVENT(KEYLOC_ENCODER_CCW, (enc_id), (press), ENCODER_CCW_EVENT)
  73. #endif // ENCODER_MAP_ENABLE
  74. /* it runs once at early stage of startup before keyboard_init. */
  75. void keyboard_setup(void);
  76. /* it runs once after initializing host side protocol, debug and MCU peripherals. */
  77. void keyboard_init(void);
  78. /* it runs repeatedly in main loop */
  79. void keyboard_task(void);
  80. /* it runs whenever code has to behave differently on a slave */
  81. bool is_keyboard_master(void);
  82. /* it runs whenever code has to behave differently on left vs right split */
  83. bool is_keyboard_left(void);
  84. void keyboard_pre_init_kb(void);
  85. void keyboard_pre_init_user(void);
  86. void keyboard_post_init_kb(void);
  87. void keyboard_post_init_user(void);
  88. void housekeeping_task(void); // To be executed by the main loop in each backend TMK protocol
  89. void housekeeping_task_kb(void); // To be overridden by keyboard-level code
  90. void housekeeping_task_user(void); // To be overridden by user/keymap-level code
  91. uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder or pointing device activity
  92. uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder or pointing device activity
  93. uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity
  94. uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity
  95. uint32_t last_encoder_activity_time(void); // Timestamp of the last encoder activity
  96. uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity
  97. uint32_t last_pointing_device_activity_time(void); // Timestamp of the last pointing device activity
  98. uint32_t last_pointing_device_activity_elapsed(void); // Number of milliseconds since the last pointing device activity
  99. void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp, uint32_t pointing_device_timestamp); // Set the timestamps of the last matrix and encoder activity
  100. uint32_t get_matrix_scan_rate(void);
  101. #ifdef __cplusplus
  102. }
  103. #endif