quantum.h 11 KB

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