1
0

quantum.h 9.7 KB

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