quantum.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #ifdef USBPD_ENABLE
  159. # include "usbpd.h"
  160. #endif
  161. // Function substitutions to ease GPIO manipulation
  162. #if defined(__AVR__)
  163. typedef uint8_t pin_t;
  164. # define setPinInput(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  165. # define setPinInputHigh(pin) (DDRx_ADDRESS(pin) &= ~_BV((pin)&0xF), PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  166. # define setPinInputLow(pin) _Static_assert(0, "AVR processors cannot implement an input as pull low")
  167. # define setPinOutput(pin) (DDRx_ADDRESS(pin) |= _BV((pin)&0xF))
  168. # define writePinHigh(pin) (PORTx_ADDRESS(pin) |= _BV((pin)&0xF))
  169. # define writePinLow(pin) (PORTx_ADDRESS(pin) &= ~_BV((pin)&0xF))
  170. # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin))
  171. # define readPin(pin) ((bool)(PINx_ADDRESS(pin) & _BV((pin)&0xF)))
  172. # define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF))
  173. /* The AVR series GPIOs have a one clock read delay for changes in the digital input signal.
  174. * But here's more margin to make it two clocks. */
  175. # if !defined(GPIO_INPUT_PIN_DELAY)
  176. # define GPIO_INPUT_PIN_DELAY 2
  177. # endif
  178. # define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
  179. #elif defined(PROTOCOL_CHIBIOS)
  180. typedef ioline_t pin_t;
  181. # define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
  182. # define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
  183. # define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
  184. # define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
  185. # define writePinHigh(pin) palSetLine(pin)
  186. # define writePinLow(pin) palClearLine(pin)
  187. # define writePin(pin, level) ((level) ? (writePinHigh(pin)) : (writePinLow(pin)))
  188. # define readPin(pin) palReadLine(pin)
  189. # define togglePin(pin) palToggleLine(pin)
  190. #endif
  191. #if defined(__ARMEL__) || defined(__ARMEB__)
  192. /* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus
  193. * to which the GPIO is connected.
  194. * The connected buses differ depending on the various series of MCUs.
  195. * And since the instruction execution clock of the CPU and the bus clock of GPIO are different,
  196. * there is a delay of several clocks to read the change of the input signal.
  197. *
  198. * Define this delay with the GPIO_INPUT_PIN_DELAY macro.
  199. * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used.
  200. * (A fairly large value of 0.25 microseconds is set.)
  201. */
  202. # if !defined(GPIO_INPUT_PIN_DELAY)
  203. # if defined(STM32_SYSCLK)
  204. # define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK / 1000000L / 4)
  205. # elif defined(KINETIS_SYSCLK_FREQUENCY)
  206. # define GPIO_INPUT_PIN_DELAY (KINETIS_SYSCLK_FREQUENCY / 1000000L / 4)
  207. # endif
  208. # endif
  209. # define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)
  210. #endif
  211. // Atomic macro to help make GPIO and other controls atomic.
  212. #ifdef IGNORE_ATOMIC_BLOCK
  213. /* do nothing atomic macro */
  214. # define ATOMIC_BLOCK for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0)
  215. # define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK
  216. # define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK
  217. #elif defined(__AVR__)
  218. /* atomic macro for AVR */
  219. # include <util/atomic.h>
  220. # define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  221. # define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
  222. #elif defined(PROTOCOL_CHIBIOS) || defined(PROTOCOL_ARM_ATSAM)
  223. /* atomic macro for ChibiOS / ARM ATSAM */
  224. # if defined(PROTOCOL_ARM_ATSAM)
  225. # include "arm_atsam_protocol.h"
  226. # endif
  227. static __inline__ uint8_t __interrupt_disable__(void) {
  228. # if defined(PROTOCOL_CHIBIOS)
  229. chSysLock();
  230. # endif
  231. # if defined(PROTOCOL_ARM_ATSAM)
  232. __disable_irq();
  233. # endif
  234. return 1;
  235. }
  236. static __inline__ void __interrupt_enable__(const uint8_t *__s) {
  237. # if defined(PROTOCOL_CHIBIOS)
  238. chSysUnlock();
  239. # endif
  240. # if defined(PROTOCOL_ARM_ATSAM)
  241. __enable_irq();
  242. # endif
  243. __asm__ volatile("" ::: "memory");
  244. (void)__s;
  245. }
  246. # define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0)
  247. # define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0
  248. # define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE dose not implement")
  249. # define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
  250. /* Other platform */
  251. #else
  252. # define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE dose not implement")
  253. # define ATOMIC_BLOCK_FORCEON _Static_assert(0, "ATOMIC_BLOCK_FORCEON dose not implement")
  254. #endif
  255. #define SEND_STRING(string) send_string_P(PSTR(string))
  256. #define SEND_STRING_DELAY(string, interval) send_string_with_delay_P(PSTR(string), interval)
  257. // Look-Up Tables (LUTs) to convert ASCII character to keycode sequence.
  258. extern const uint8_t ascii_to_keycode_lut[128];
  259. extern const uint8_t ascii_to_shift_lut[16];
  260. extern const uint8_t ascii_to_altgr_lut[16];
  261. extern const uint8_t ascii_to_dead_lut[16];
  262. // clang-format off
  263. #define KCLUT_ENTRY(a, b, c, d, e, f, g, h) \
  264. ( ((a) ? 1 : 0) << 0 \
  265. | ((b) ? 1 : 0) << 1 \
  266. | ((c) ? 1 : 0) << 2 \
  267. | ((d) ? 1 : 0) << 3 \
  268. | ((e) ? 1 : 0) << 4 \
  269. | ((f) ? 1 : 0) << 5 \
  270. | ((g) ? 1 : 0) << 6 \
  271. | ((h) ? 1 : 0) << 7 )
  272. // clang-format on
  273. void send_string(const char *str);
  274. void send_string_with_delay(const char *str, uint8_t interval);
  275. void send_string_P(const char *str);
  276. void send_string_with_delay_P(const char *str, uint8_t interval);
  277. void send_char(char ascii_code);
  278. // For tri-layer
  279. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
  280. layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
  281. void set_single_persistent_default_layer(uint8_t default_layer);
  282. void tap_random_base64(void);
  283. #define IS_LAYER_ON(layer) layer_state_is(layer)
  284. #define IS_LAYER_OFF(layer) !layer_state_is(layer)
  285. #define IS_LAYER_ON_STATE(state, layer) layer_state_cmp(state, layer)
  286. #define IS_LAYER_OFF_STATE(state, layer) !layer_state_cmp(state, layer)
  287. void matrix_init_kb(void);
  288. void matrix_scan_kb(void);
  289. void matrix_init_user(void);
  290. void matrix_scan_user(void);
  291. uint16_t get_record_keycode(keyrecord_t *record, bool update_layer_cache);
  292. uint16_t get_event_keycode(keyevent_t event, bool update_layer_cache);
  293. bool process_action_kb(keyrecord_t *record);
  294. bool process_record_kb(uint16_t keycode, keyrecord_t *record);
  295. bool process_record_user(uint16_t keycode, keyrecord_t *record);
  296. void post_process_record_kb(uint16_t keycode, keyrecord_t *record);
  297. void post_process_record_user(uint16_t keycode, keyrecord_t *record);
  298. #ifndef BOOTMAGIC_LITE_COLUMN
  299. # define BOOTMAGIC_LITE_COLUMN 0
  300. #endif
  301. #ifndef BOOTMAGIC_LITE_ROW
  302. # define BOOTMAGIC_LITE_ROW 0
  303. #endif
  304. void bootmagic_lite(void);
  305. void reset_keyboard(void);
  306. void startup_user(void);
  307. void shutdown_user(void);
  308. void register_code16(uint16_t code);
  309. void unregister_code16(uint16_t code);
  310. void tap_code16(uint16_t code);
  311. void send_dword(uint32_t number);
  312. void send_word(uint16_t number);
  313. void send_byte(uint8_t number);
  314. void send_nibble(uint8_t number);
  315. uint16_t hex_to_keycode(uint8_t hex);
  316. void led_set_user(uint8_t usb_led);
  317. void led_set_kb(uint8_t usb_led);
  318. bool led_update_user(led_t led_state);
  319. bool led_update_kb(led_t led_state);
  320. void api_send_unicode(uint32_t unicode);