Parent directory

keyboard.h

5609 bytes
  1/*
  2Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com>
  3
  4This program is free software: you can redistribute it and/or modify
  5it under the terms of the GNU General Public License as published by
  6the Free Software Foundation, either version 2 of the License, or
  7(at your option) any later version.
  8
  9This program is distributed in the hope that it will be useful,
 10but WITHOUT ANY WARRANTY; without even the implied warranty of
 11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12GNU General Public License for more details.
 13
 14You should have received a copy of the GNU General Public License
 15along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16*/
 17
 18#pragma once
 19
 20#include <stdbool.h>
 21#include <stdint.h>
 22
 23#include "timer.h"
 24
 25#ifdef __cplusplus
 26extern "C" {
 27#endif
 28
 29/* key matrix position */
 30typedef struct {
 31    uint8_t col;
 32    uint8_t row;
 33} keypos_t;
 34
 35typedef enum keyevent_type_t { TICK_EVENT = 0, KEY_EVENT = 1, ENCODER_CW_EVENT = 2, ENCODER_CCW_EVENT = 3, COMBO_EVENT = 4, DIP_SWITCH_ON_EVENT = 5, DIP_SWITCH_OFF_EVENT = 6 } keyevent_type_t;
 36
 37/* key event */
 38typedef struct {
 39    keypos_t        key;
 40    uint16_t        time;
 41    keyevent_type_t type;
 42    bool            pressed;
 43} keyevent_t;
 44
 45/* equivalent test of keypos_t */
 46#define KEYEQ(keya, keyb) ((keya).row == (keyb).row && (keya).col == (keyb).col)
 47
 48/* special keypos_t entries */
 49#define KEYLOC_ENCODER_CW 253
 50#define KEYLOC_ENCODER_CCW 252
 51#define KEYLOC_DIP_SWITCH_ON 251
 52#define KEYLOC_DIP_SWITCH_OFF 250
 53
 54static inline bool IS_NOEVENT(const keyevent_t event) {
 55    return event.type == TICK_EVENT;
 56}
 57static inline bool IS_EVENT(const keyevent_t event) {
 58    return event.type != TICK_EVENT;
 59}
 60static inline bool IS_KEYEVENT(const keyevent_t event) {
 61    return event.type == KEY_EVENT;
 62}
 63static inline bool IS_COMBOEVENT(const keyevent_t event) {
 64    return event.type == COMBO_EVENT;
 65}
 66static inline bool IS_ENCODEREVENT(const keyevent_t event) {
 67    return event.type == ENCODER_CW_EVENT || event.type == ENCODER_CCW_EVENT;
 68}
 69static inline bool IS_DIPSWITCHEVENT(const keyevent_t event) {
 70    return event.type == DIP_SWITCH_ON_EVENT || event.type == DIP_SWITCH_OFF_EVENT;
 71}
 72
 73/* Common keypos_t object factory */
 74#define MAKE_KEYPOS(row_num, col_num) ((keypos_t){.row = (row_num), .col = (col_num)})
 75
 76/* Common keyevent_t object factory */
 77#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)})
 78
 79/**
 80 * @brief Constructs a key event for a pressed or released key.
 81 */
 82#define MAKE_KEYEVENT(row_num, col_num, press) MAKE_EVENT((row_num), (col_num), (press), KEY_EVENT)
 83
 84/**
 85 * @brief Constructs a combo event.
 86 */
 87#define MAKE_COMBOEVENT(press) MAKE_EVENT(0, 0, (press), COMBO_EVENT)
 88
 89/**
 90 * @brief Constructs a internal tick event that is used to drive the internal QMK state machine.
 91 */
 92#define MAKE_TICK_EVENT MAKE_EVENT(0, 0, false, TICK_EVENT)
 93
 94#ifdef ENCODER_MAP_ENABLE
 95/* Encoder events */
 96#    define MAKE_ENCODER_CW_EVENT(enc_id, press) MAKE_EVENT(KEYLOC_ENCODER_CW, (enc_id), (press), ENCODER_CW_EVENT)
 97#    define MAKE_ENCODER_CCW_EVENT(enc_id, press) MAKE_EVENT(KEYLOC_ENCODER_CCW, (enc_id), (press), ENCODER_CCW_EVENT)
 98#endif // ENCODER_MAP_ENABLE
 99
100#ifdef DIP_SWITCH_MAP_ENABLE
101/* Dip Switch events */
102#    define MAKE_DIPSWITCH_ON_EVENT(switch_id, press) MAKE_EVENT(KEYLOC_DIP_SWITCH_ON, (switch_id), (press), DIP_SWITCH_ON_EVENT)
103#    define MAKE_DIPSWITCH_OFF_EVENT(switch_id, press) MAKE_EVENT(KEYLOC_DIP_SWITCH_OFF, (switch_id), (press), DIP_SWITCH_OFF_EVENT)
104#endif // DIP_SWITCH_MAP_ENABLE
105
106/* it runs once at early stage of startup before keyboard_init. */
107void keyboard_setup(void);
108/* it runs once after initializing host side protocol, debug and MCU peripherals. */
109void keyboard_init(void);
110/* it runs repeatedly in main loop */
111void keyboard_task(void);
112/* it runs whenever code has to behave differently on a slave */
113bool is_keyboard_master(void);
114/* it runs whenever code has to behave differently on left vs right split */
115bool is_keyboard_left(void);
116
117void keyboard_pre_init_kb(void);
118void keyboard_pre_init_user(void);
119void keyboard_post_init_kb(void);
120void keyboard_post_init_user(void);
121
122void housekeeping_task(void);      // To be executed by the main loop in each backend TMK protocol
123void housekeeping_task_kb(void);   // To be overridden by keyboard-level code
124void housekeeping_task_user(void); // To be overridden by user/keymap-level code
125
126uint32_t last_input_activity_time(void);    // Timestamp of the last matrix or encoder or pointing device activity
127uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder or pointing device activity
128
129uint32_t last_matrix_activity_time(void);    // Timestamp of the last matrix activity
130uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity
131
132uint32_t last_encoder_activity_time(void);    // Timestamp of the last encoder activity
133uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity
134
135uint32_t last_pointing_device_activity_time(void);    // Timestamp of the last pointing device activity
136uint32_t last_pointing_device_activity_elapsed(void); // Number of milliseconds since the last  pointing device activity
137
138void 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
139
140uint32_t get_matrix_scan_rate(void);
141
142#ifdef __cplusplus
143}
144#endif