quantum.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #endif
  25. #include "wait.h"
  26. #include "matrix.h"
  27. #include "keymap.h"
  28. #ifdef BACKLIGHT_ENABLE
  29. # ifdef LED_MATRIX_ENABLE
  30. # include "ledmatrix.h"
  31. # else
  32. # include "backlight.h"
  33. # endif
  34. #endif
  35. #if defined(RGBLIGHT_ENABLE)
  36. # include "rgblight.h"
  37. #elif defined(RGB_MATRIX_ENABLE)
  38. // Dummy define RGBLIGHT_MODE_xxxx
  39. # define RGBLIGHT_H_DUMMY_DEFINE
  40. # include "rgblight.h"
  41. #endif
  42. #ifdef RGB_MATRIX_ENABLE
  43. # include "rgb_matrix.h"
  44. #endif
  45. #include "action_layer.h"
  46. #include "eeconfig.h"
  47. #include "bootloader.h"
  48. #include "timer.h"
  49. #include "config_common.h"
  50. #include "led.h"
  51. #include "action_util.h"
  52. #include "print.h"
  53. #include "send_string_keycodes.h"
  54. #include "suspend.h"
  55. #include <stddef.h>
  56. #include <stdlib.h>
  57. extern layer_state_t default_layer_state;
  58. #ifndef NO_ACTION_LAYER
  59. extern layer_state_t layer_state;
  60. #endif
  61. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  62. # include "process_midi.h"
  63. #endif
  64. #ifdef AUDIO_ENABLE
  65. # include "audio.h"
  66. # include "process_audio.h"
  67. # ifdef AUDIO_CLICKY
  68. # include "process_clicky.h"
  69. # endif
  70. #endif
  71. #ifdef STENO_ENABLE
  72. # include "process_steno.h"
  73. #endif
  74. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  75. # include "process_music.h"
  76. #endif
  77. #ifdef LEADER_ENABLE
  78. # include "process_leader.h"
  79. #endif
  80. #ifdef UNICODE_ENABLE
  81. # include "process_unicode.h"
  82. #endif
  83. #ifdef UCIS_ENABLE
  84. # include "process_ucis.h"
  85. #endif
  86. #ifdef UNICODEMAP_ENABLE
  87. # include "process_unicodemap.h"
  88. #endif
  89. #ifdef TAP_DANCE_ENABLE
  90. # include "process_tap_dance.h"
  91. #endif
  92. #ifdef PRINTING_ENABLE
  93. # include "process_printer.h"
  94. #endif
  95. #ifdef AUTO_SHIFT_ENABLE
  96. # include "process_auto_shift.h"
  97. #endif
  98. #ifdef COMBO_ENABLE
  99. # include "process_combo.h"
  100. #endif
  101. #ifdef KEY_LOCK_ENABLE
  102. # include "process_key_lock.h"
  103. #endif
  104. #ifdef TERMINAL_ENABLE
  105. # include "process_terminal.h"
  106. #else
  107. # include "process_terminal_nop.h"
  108. #endif
  109. #ifdef SPACE_CADET_ENABLE
  110. # include "process_space_cadet.h"
  111. #endif
  112. #ifdef HD44780_ENABLE
  113. # include "hd44780.h"
  114. #endif
  115. #ifdef HAPTIC_ENABLE
  116. # include "haptic.h"
  117. #endif
  118. #ifdef OLED_DRIVER_ENABLE
  119. # include "oled_driver.h"
  120. #endif
  121. #ifdef DIP_SWITCH_ENABLE
  122. #include "dip_switch.h"
  123. #endif
  124. #ifdef DYNAMIC_MACRO_ENABLE
  125. #include "process_dynamic_macro.h"
  126. #endif
  127. // Function substitutions to ease GPIO manipulation
  128. #if defined(__AVR__)
  129. typedef uint8_t pin_t;
  130. # define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  131. # define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  132. # define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
  133. # define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
  134. # define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  135. # define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  136. # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
  137. # define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
  138. #elif defined(PROTOCOL_CHIBIOS)
  139. typedef ioline_t pin_t;
  140. # define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
  141. # define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
  142. # define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
  143. # define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
  144. # define writePinHigh(pin) palSetLine(pin)
  145. # define writePinLow(pin) palClearLine(pin)
  146. # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
  147. # define readPin(pin) palReadLine(pin)
  148. #endif
  149. // Send string macros
  150. #define STRINGIZE(z) #z
  151. #define ADD_SLASH_X(y) STRINGIZE(\x##y)
  152. #define SYMBOL_STR(x) ADD_SLASH_X(x)
  153. #define SS_TAP_CODE 1
  154. #define SS_DOWN_CODE 2
  155. #define SS_UP_CODE 3
  156. #define SS_TAP(keycode) "\1" SYMBOL_STR(keycode)
  157. #define SS_DOWN(keycode) "\2" SYMBOL_STR(keycode)
  158. #define SS_UP(keycode) "\3" SYMBOL_STR(keycode)
  159. // `string` arguments must not be parenthesized
  160. #define SS_LCTRL(string) SS_DOWN(X_LCTRL) string SS_UP(X_LCTRL)
  161. #define SS_LGUI(string) SS_DOWN(X_LGUI) string SS_UP(X_LGUI)
  162. #define SS_LCMD(string) SS_LGUI(string)
  163. #define SS_LWIN(string) SS_LGUI(string)
  164. #define SS_LALT(string) SS_DOWN(X_LALT) string SS_UP(X_LALT)
  165. #define SS_LSFT(string) SS_DOWN(X_LSHIFT) string SS_UP(X_LSHIFT)
  166. #define SS_RALT(string) SS_DOWN(X_RALT) string SS_UP(X_RALT)
  167. #define SS_ALGR(string) SS_RALT(string)
  168. #define SEND_STRING(string) send_string_P(PSTR(string))
  169. extern const bool ascii_to_shift_lut[128];
  170. extern const bool ascii_to_altgr_lut[128];
  171. extern const uint8_t ascii_to_keycode_lut[128];
  172. void send_string(const char *str);
  173. void send_string_with_delay(const char *str, uint8_t interval);
  174. void send_string_P(const char *str);
  175. void send_string_with_delay_P(const char *str, uint8_t interval);
  176. void send_char(char ascii_code);
  177. // For tri-layer
  178. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
  179. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
  180. void set_single_persistent_default_layer(uint8_t default_layer);
  181. void tap_random_base64(void);
  182. #define IS_LAYER_ON(layer) (layer_state & (1UL << (layer)))
  183. #define IS_LAYER_OFF(layer) (~layer_state & (1UL << (layer)))
  184. void matrix_init_kb(void);
  185. void matrix_scan_kb(void);
  186. void matrix_init_user(void);
  187. void matrix_scan_user(void);
  188. uint16_t get_record_keycode(keyrecord_t *record);
  189. uint16_t get_event_keycode(keyevent_t event);
  190. bool process_action_kb(keyrecord_t *record);
  191. bool process_record_kb(uint16_t keycode, keyrecord_t *record);
  192. bool process_record_user(uint16_t keycode, keyrecord_t *record);
  193. #ifndef BOOTMAGIC_LITE_COLUMN
  194. # define BOOTMAGIC_LITE_COLUMN 0
  195. #endif
  196. #ifndef BOOTMAGIC_LITE_ROW
  197. # define BOOTMAGIC_LITE_ROW 0
  198. #endif
  199. void bootmagic_lite(void);
  200. void reset_keyboard(void);
  201. void startup_user(void);
  202. void shutdown_user(void);
  203. void register_code16(uint16_t code);
  204. void unregister_code16(uint16_t code);
  205. void tap_code16(uint16_t code);
  206. #ifdef BACKLIGHT_ENABLE
  207. void backlight_init_ports(void);
  208. void backlight_task(void);
  209. void backlight_task_internal(void);
  210. void backlight_on(pin_t backlight_pin);
  211. void backlight_off(pin_t backlight_pin);
  212. # ifdef BACKLIGHT_BREATHING
  213. void breathing_task(void);
  214. void breathing_enable(void);
  215. void breathing_pulse(void);
  216. void breathing_disable(void);
  217. void breathing_self_disable(void);
  218. void breathing_toggle(void);
  219. bool is_breathing(void);
  220. void breathing_intensity_default(void);
  221. void breathing_period_default(void);
  222. void breathing_period_set(uint8_t value);
  223. void breathing_period_inc(void);
  224. void breathing_period_dec(void);
  225. # endif
  226. #endif
  227. void send_dword(uint32_t number);
  228. void send_word(uint16_t number);
  229. void send_byte(uint8_t number);
  230. void send_nibble(uint8_t number);
  231. uint16_t hex_to_keycode(uint8_t hex);
  232. void led_set_user(uint8_t usb_led);
  233. void led_set_kb(uint8_t usb_led);
  234. bool led_update_user(led_t led_state);
  235. bool led_update_kb(led_t led_state);
  236. void api_send_unicode(uint32_t unicode);