quantum.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Copyright 2016-2018 Erez Zukerman, Jack Humbert, Yiancar
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #if defined(__AVR__)
  18. # include <avr/pgmspace.h>
  19. # include <avr/io.h>
  20. # include <avr/interrupt.h>
  21. #endif
  22. #if defined(PROTOCOL_CHIBIOS)
  23. # include "hal.h"
  24. # include "chibios_config.h"
  25. #endif
  26. #include "wait.h"
  27. #include "matrix.h"
  28. #include "keymap.h"
  29. #ifdef BACKLIGHT_ENABLE
  30. # ifdef LED_MATRIX_ENABLE
  31. # include "ledmatrix.h"
  32. # else
  33. # include "backlight.h"
  34. # endif
  35. #endif
  36. #if defined(RGBLIGHT_ENABLE)
  37. # include "rgblight.h"
  38. #elif defined(RGB_MATRIX_ENABLE)
  39. // Dummy define RGBLIGHT_MODE_xxxx
  40. # define RGBLIGHT_H_DUMMY_DEFINE
  41. # include "rgblight.h"
  42. #endif
  43. #ifdef RGB_MATRIX_ENABLE
  44. # include "rgb_matrix.h"
  45. #endif
  46. #include "action_layer.h"
  47. #include "eeconfig.h"
  48. #include "bootloader.h"
  49. #include "timer.h"
  50. #include "config_common.h"
  51. #include "led.h"
  52. #include "action_util.h"
  53. #include "print.h"
  54. #include "send_string_keycodes.h"
  55. #include "suspend.h"
  56. #include <stddef.h>
  57. #include <stdlib.h>
  58. extern layer_state_t default_layer_state;
  59. #ifndef NO_ACTION_LAYER
  60. extern layer_state_t layer_state;
  61. #endif
  62. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  63. # include "process_midi.h"
  64. #endif
  65. #ifdef AUDIO_ENABLE
  66. # include "audio.h"
  67. # include "process_audio.h"
  68. # ifdef AUDIO_CLICKY
  69. # include "process_clicky.h"
  70. # endif
  71. #endif
  72. #ifdef STENO_ENABLE
  73. # include "process_steno.h"
  74. #endif
  75. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  76. # include "process_music.h"
  77. #endif
  78. #ifdef BACKLIGHT_ENABLE
  79. # include "process_backlight.h"
  80. #endif
  81. #ifdef LEADER_ENABLE
  82. # include "process_leader.h"
  83. #endif
  84. #ifdef UNICODE_ENABLE
  85. # include "process_unicode.h"
  86. #endif
  87. #ifdef UCIS_ENABLE
  88. # include "process_ucis.h"
  89. #endif
  90. #ifdef UNICODEMAP_ENABLE
  91. # include "process_unicodemap.h"
  92. #endif
  93. #ifdef TAP_DANCE_ENABLE
  94. # include "process_tap_dance.h"
  95. #endif
  96. #ifdef PRINTING_ENABLE
  97. # include "process_printer.h"
  98. #endif
  99. #ifdef AUTO_SHIFT_ENABLE
  100. # include "process_auto_shift.h"
  101. #endif
  102. #ifdef COMBO_ENABLE
  103. # include "process_combo.h"
  104. #endif
  105. #ifdef KEY_LOCK_ENABLE
  106. # include "process_key_lock.h"
  107. #endif
  108. #ifdef TERMINAL_ENABLE
  109. # include "process_terminal.h"
  110. #else
  111. # include "process_terminal_nop.h"
  112. #endif
  113. #ifdef SPACE_CADET_ENABLE
  114. # include "process_space_cadet.h"
  115. #endif
  116. #ifdef MAGIC_KEYCODE_ENABLE
  117. # include "process_magic.h"
  118. #endif
  119. #ifdef GRAVE_ESC_ENABLE
  120. # include "process_grave_esc.h"
  121. #endif
  122. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  123. # include "process_rgb.h"
  124. #endif
  125. #ifdef HD44780_ENABLE
  126. # include "hd44780.h"
  127. #endif
  128. #ifdef HAPTIC_ENABLE
  129. # include "haptic.h"
  130. #endif
  131. #ifdef OLED_DRIVER_ENABLE
  132. # include "oled_driver.h"
  133. #endif
  134. #ifdef DIP_SWITCH_ENABLE
  135. # include "dip_switch.h"
  136. #endif
  137. #ifdef DYNAMIC_MACRO_ENABLE
  138. # include "process_dynamic_macro.h"
  139. #endif
  140. #ifdef DYNAMIC_KEYMAP_ENABLE
  141. # include "dynamic_keymap.h"
  142. #endif
  143. #ifdef VIA_ENABLE
  144. # include "via.h"
  145. #endif
  146. #ifdef WPM_ENABLE
  147. # include "wpm.h"
  148. #endif
  149. // Function substitutions to ease GPIO manipulation
  150. #if defined(__AVR__)
  151. typedef uint8_t pin_t;
  152. # define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  153. # define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  154. # define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
  155. # define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
  156. # define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  157. # define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  158. # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
  159. # define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
  160. # define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF))
  161. #elif defined(PROTOCOL_CHIBIOS)
  162. typedef ioline_t pin_t;
  163. # define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
  164. # define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
  165. # define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
  166. # define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
  167. # define writePinHigh(pin) palSetLine(pin)
  168. # define writePinLow(pin) palClearLine(pin)
  169. # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
  170. # define readPin(pin) palReadLine(pin)
  171. # define togglePin(pin) palToggleLine(pin)
  172. #endif
  173. #define SEND_STRING(string) send_string_P(PSTR(string))
  174. #define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval)
  175. // Look-Up Tables (LUTs) to convert ASCII character to keycode sequence.
  176. extern const uint8_t ascii_to_keycode_lut[128];
  177. extern const uint8_t ascii_to_shift_lut[16];
  178. extern const uint8_t ascii_to_altgr_lut[16];
  179. // clang-format off
  180. #define KCLUT_ENTRY(a, b, c, d, e, f, g, h) \
  181. ( ((a) ? 1 : 0) << 0 \
  182. | ((b) ? 1 : 0) << 1 \
  183. | ((c) ? 1 : 0) << 2 \
  184. | ((d) ? 1 : 0) << 3 \
  185. | ((e) ? 1 : 0) << 4 \
  186. | ((f) ? 1 : 0) << 5 \
  187. | ((g) ? 1 : 0) << 6 \
  188. | ((h) ? 1 : 0) << 7 )
  189. // clang-format on
  190. void send_string(const char *str);
  191. void send_string_with_delay(const char *str, uint8_t interval);
  192. void send_string_P(const char *str);
  193. void send_string_with_delay_P(const char *str, uint8_t interval);
  194. void send_char(char ascii_code);
  195. // For tri-layer
  196. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
  197. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
  198. void set_single_persistent_default_layer(uint8_t default_layer);
  199. void tap_random_base64(void);
  200. #define IS_LAYER_ON(layer) (layer_state & (1UL << (layer)))
  201. #define IS_LAYER_OFF(layer) (~layer_state & (1UL << (layer)))
  202. void matrix_init_kb(void);
  203. void matrix_scan_kb(void);
  204. void matrix_init_user(void);
  205. void matrix_scan_user(void);
  206. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache);
  207. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache);
  208. bool process_action_kb(keyrecord_t *record);
  209. bool process_record_kb(uint16_t keycode, keyrecord_t *record);
  210. bool process_record_user(uint16_t keycode, keyrecord_t *record);
  211. void post_process_record_kb(uint16_t keycode, keyrecord_t *record);
  212. void post_process_record_user(uint16_t keycode, keyrecord_t *record);
  213. #ifndef BOOTMAGIC_LITE_COLUMN
  214. # define BOOTMAGIC_LITE_COLUMN 0
  215. #endif
  216. #ifndef BOOTMAGIC_LITE_ROW
  217. # define BOOTMAGIC_LITE_ROW 0
  218. #endif
  219. void bootmagic_lite(void);
  220. void reset_keyboard(void);
  221. void startup_user(void);
  222. void shutdown_user(void);
  223. void register_code16(uint16_t code);
  224. void unregister_code16(uint16_t code);
  225. void tap_code16(uint16_t code);
  226. void send_dword(uint32_t number);
  227. void send_word(uint16_t number);
  228. void send_byte(uint8_t number);
  229. void send_nibble(uint8_t number);
  230. uint16_t hex_to_keycode(uint8_t hex);
  231. void led_set_user(uint8_t usb_led);
  232. void led_set_kb(uint8_t usb_led);
  233. bool led_update_user(led_t led_state);
  234. bool led_update_kb(led_t led_state);
  235. void api_send_unicode(uint32_t unicode);