quantum.h 9.7 KB

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