quantum.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 "led_matrix.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 "sync_timer.h"
  51. #include "config_common.h"
  52. #include "led.h"
  53. #include "action_util.h"
  54. #include "action_tapping.h"
  55. #include "print.h"
  56. #include "send_string_keycodes.h"
  57. #include "suspend.h"
  58. #include <stddef.h>
  59. #include <stdlib.h>
  60. extern layer_state_t default_layer_state;
  61. #ifndef NO_ACTION_LAYER
  62. extern layer_state_t layer_state;
  63. #endif
  64. #if defined(SEQUENCER_ENABLE)
  65. # include "sequencer.h"
  66. # include "process_sequencer.h"
  67. #endif
  68. #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
  69. # include "process_midi.h"
  70. #endif
  71. #ifdef AUDIO_ENABLE
  72. # include "audio.h"
  73. # include "process_audio.h"
  74. # ifdef AUDIO_CLICKY
  75. # include "process_clicky.h"
  76. # endif
  77. #endif
  78. #ifdef STENO_ENABLE
  79. # include "process_steno.h"
  80. #endif
  81. #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
  82. # include "process_music.h"
  83. #endif
  84. #ifdef BACKLIGHT_ENABLE
  85. # include "process_backlight.h"
  86. #endif
  87. #ifdef LEADER_ENABLE
  88. # include "process_leader.h"
  89. #endif
  90. #ifdef UNICODE_ENABLE
  91. # include "process_unicode.h"
  92. #endif
  93. #ifdef UCIS_ENABLE
  94. # include "process_ucis.h"
  95. #endif
  96. #ifdef UNICODEMAP_ENABLE
  97. # include "process_unicodemap.h"
  98. #endif
  99. #ifdef TAP_DANCE_ENABLE
  100. # include "process_tap_dance.h"
  101. #endif
  102. #ifdef PRINTING_ENABLE
  103. # include "process_printer.h"
  104. #endif
  105. #ifdef AUTO_SHIFT_ENABLE
  106. # include "process_auto_shift.h"
  107. #endif
  108. #ifdef COMBO_ENABLE
  109. # include "process_combo.h"
  110. #endif
  111. #ifdef KEY_LOCK_ENABLE
  112. # include "process_key_lock.h"
  113. #endif
  114. #ifdef TERMINAL_ENABLE
  115. # include "process_terminal.h"
  116. #else
  117. # include "process_terminal_nop.h"
  118. #endif
  119. #ifdef SPACE_CADET_ENABLE
  120. # include "process_space_cadet.h"
  121. #endif
  122. #ifdef MAGIC_KEYCODE_ENABLE
  123. # include "process_magic.h"
  124. #endif
  125. #ifdef JOYSTICK_ENABLE
  126. # include "process_joystick.h"
  127. #endif
  128. #ifdef GRAVE_ESC_ENABLE
  129. # include "process_grave_esc.h"
  130. #endif
  131. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  132. # include "process_rgb.h"
  133. #endif
  134. #ifdef HD44780_ENABLE
  135. # include "hd44780.h"
  136. #endif
  137. #ifdef HAPTIC_ENABLE
  138. # include "haptic.h"
  139. #endif
  140. #ifdef OLED_DRIVER_ENABLE
  141. # include "oled_driver.h"
  142. #endif
  143. #ifdef DIP_SWITCH_ENABLE
  144. # include "dip_switch.h"
  145. #endif
  146. #ifdef DYNAMIC_MACRO_ENABLE
  147. # include "process_dynamic_macro.h"
  148. #endif
  149. #ifdef DYNAMIC_KEYMAP_ENABLE
  150. # include "dynamic_keymap.h"
  151. #endif
  152. #ifdef VIA_ENABLE
  153. # include "via.h"
  154. #endif
  155. #ifdef WPM_ENABLE
  156. # include "wpm.h"
  157. #endif
  158. // Function substitutions to ease GPIO manipulation
  159. #if defined(__AVR__)
  160. typedef uint8_t pin_t;
  161. # define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  162. # define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  163. # define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
  164. # define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
  165. # define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  166. # define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  167. # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
  168. # define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
  169. # define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF))
  170. #elif defined(PROTOCOL_CHIBIOS)
  171. typedef ioline_t pin_t;
  172. # define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
  173. # define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
  174. # define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
  175. # define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
  176. # define writePinHigh(pin) palSetLine(pin)
  177. # define writePinLow(pin) palClearLine(pin)
  178. # define writePin(pin, level) ((level) ? (writePinHigh(pin)) : (writePinLow(pin)))
  179. # define readPin(pin) palReadLine(pin)
  180. # define togglePin(pin) palToggleLine(pin)
  181. #endif
  182. // Atomic macro to help make GPIO and other controls atomic.
  183. #ifdef IGNORE_ATOMIC_BLOCK
  184. /* do nothing atomic macro */
  185. # define ATOMIC_BLOCK for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0)
  186. # define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK
  187. # define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK
  188. #elif defined(__AVR__)
  189. /* atomic macro for AVR */
  190. # include <util/atomic.h>
  191. # define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  192. # define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
  193. #elif defined(PROTOCOL_CHIBIOS) || defined(PROTOCOL_ARM_ATSAM)
  194. /* atomic macro for ChibiOS / ARM ATSAM */
  195. # if defined(PROTOCOL_ARM_ATSAM)
  196. # include "arm_atsam_protocol.h"
  197. # endif
  198. static __inline__ uint8_t __interrupt_disable__(void) {
  199. # if defined(PROTOCOL_CHIBIOS)
  200. chSysLock();
  201. # endif
  202. # if defined(PROTOCOL_ARM_ATSAM)
  203. __disable_irq();
  204. # endif
  205. return 1;
  206. }
  207. static __inline__ void __interrupt_enable__(const uint8_t *__s) {
  208. # if defined(PROTOCOL_CHIBIOS)
  209. chSysUnlock();
  210. # endif
  211. # if defined(PROTOCOL_ARM_ATSAM)
  212. __enable_irq();
  213. # endif
  214. __asm__ volatile("" ::: "memory");
  215. (void)__s;
  216. }
  217. # define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0)
  218. # define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0
  219. # define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE dose not implement")
  220. # define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
  221. /* Other platform */
  222. #else
  223. # define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE dose not implement")
  224. # define ATOMIC_BLOCK_FORCEON _Static_assert(0, "ATOMIC_BLOCK_FORCEON dose not implement")
  225. #endif
  226. #define SEND_STRING(string) send_string_P(PSTR(string))
  227. #define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval)
  228. // Look-Up Tables (LUTs) to convert ASCII character to keycode sequence.
  229. extern const uint8_t ascii_to_keycode_lut[128];
  230. extern const uint8_t ascii_to_shift_lut[16];
  231. extern const uint8_t ascii_to_altgr_lut[16];
  232. extern const uint8_t ascii_to_dead_lut[16];
  233. // clang-format off
  234. #define KCLUT_ENTRY(a, b, c, d, e, f, g, h) \
  235. ( ((a) ? 1 : 0) << 0 \
  236. | ((b) ? 1 : 0) << 1 \
  237. | ((c) ? 1 : 0) << 2 \
  238. | ((d) ? 1 : 0) << 3 \
  239. | ((e) ? 1 : 0) << 4 \
  240. | ((f) ? 1 : 0) << 5 \
  241. | ((g) ? 1 : 0) << 6 \
  242. | ((h) ? 1 : 0) << 7 )
  243. // clang-format on
  244. void send_string(const char *str);
  245. void send_string_with_delay(const char *str, uint8_t interval);
  246. void send_string_P(const char *str);
  247. void send_string_with_delay_P(const char *str, uint8_t interval);
  248. void send_char(char ascii_code);
  249. // For tri-layer
  250. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
  251. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
  252. void set_single_persistent_default_layer(uint8_t default_layer);
  253. void tap_random_base64(void);
  254. #define IS_LAYER_ON(layer) layer_state_is(layer)
  255. #define IS_LAYER_OFF(layer) !layer_state_is(layer)
  256. #define IS_LAYER_ON_STATE(state, layer) layer_state_cmp(state, layer)
  257. #define IS_LAYER_OFF_STATE(state, layer) !layer_state_cmp(state, layer)
  258. void matrix_init_kb(void);
  259. void matrix_scan_kb(void);
  260. void matrix_init_user(void);
  261. void matrix_scan_user(void);
  262. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache);
  263. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache);
  264. bool process_action_kb(keyrecord_t *record);
  265. bool process_record_kb(uint16_t keycode, keyrecord_t *record);
  266. bool process_record_user(uint16_t keycode, keyrecord_t *record);
  267. void post_process_record_kb(uint16_t keycode, keyrecord_t *record);
  268. void post_process_record_user(uint16_t keycode, keyrecord_t *record);
  269. #ifndef BOOTMAGIC_LITE_COLUMN
  270. # define BOOTMAGIC_LITE_COLUMN 0
  271. #endif
  272. #ifndef BOOTMAGIC_LITE_ROW
  273. # define BOOTMAGIC_LITE_ROW 0
  274. #endif
  275. void bootmagic_lite(void);
  276. void reset_keyboard(void);
  277. void startup_user(void);
  278. void shutdown_user(void);
  279. void register_code16(uint16_t code);
  280. void unregister_code16(uint16_t code);
  281. void tap_code16(uint16_t code);
  282. void send_dword(uint32_t number);
  283. void send_word(uint16_t number);
  284. void send_byte(uint8_t number);
  285. void send_nibble(uint8_t number);
  286. uint16_t hex_to_keycode(uint8_t hex);
  287. void led_set_user(uint8_t usb_led);
  288. void led_set_kb(uint8_t usb_led);
  289. bool led_update_user(led_t led_state);
  290. bool led_update_kb(led_t led_state);
  291. void api_send_unicode(uint32_t unicode);