quantum.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef QUANTUM_H
  17. #define QUANTUM_H
  18. #if defined(__AVR__)
  19. #include <avr/pgmspace.h>
  20. #include <avr/io.h>
  21. #include <avr/interrupt.h>
  22. #endif
  23. #if defined(PROTOCOL_CHIBIOS)
  24. #include "hal.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 "ledmatrix.h"
  32. #else
  33. #include "backlight.h"
  34. #endif
  35. #endif
  36. #ifdef RGBLIGHT_ENABLE
  37. #include "rgblight.h"
  38. #else
  39. #ifdef RGB_MATRIX_ENABLE
  40. /* dummy define RGBLIGHT_MODE_xxxx */
  41. #define RGBLIGHT_H_DUMMY_DEFINE
  42. #include "rgblight.h"
  43. #endif
  44. #endif
  45. #ifdef RGB_MATRIX_ENABLE
  46. #include "rgb_matrix.h"
  47. #endif
  48. #include "action_layer.h"
  49. #include "eeconfig.h"
  50. #include <stddef.h>
  51. #include "bootloader.h"
  52. #include "timer.h"
  53. #include "config_common.h"
  54. #include "led.h"
  55. #include "action_util.h"
  56. #include <stdlib.h>
  57. #include "print.h"
  58. #include "send_string_keycodes.h"
  59. #include "suspend.h"
  60. extern uint32_t default_layer_state;
  61. #ifndef NO_ACTION_LAYER
  62. extern uint32_t layer_state;
  63. #endif
  64. #ifdef MIDI_ENABLE
  65. #ifdef MIDI_ADVANCED
  66. #include "process_midi.h"
  67. #endif
  68. #endif // MIDI_ENABLE
  69. #ifdef AUDIO_ENABLE
  70. #include "audio.h"
  71. #include "process_audio.h"
  72. #ifdef AUDIO_CLICKY
  73. #include "process_clicky.h"
  74. #endif // AUDIO_CLICKY
  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 LEADER_ENABLE
  83. #include "process_leader.h"
  84. #endif
  85. #ifdef UNICODE_ENABLE
  86. #include "process_unicode.h"
  87. #endif
  88. #ifdef UCIS_ENABLE
  89. #include "process_ucis.h"
  90. #endif
  91. #ifdef UNICODEMAP_ENABLE
  92. #include "process_unicodemap.h"
  93. #endif
  94. #ifdef TAP_DANCE_ENABLE
  95. #include "process_tap_dance.h"
  96. #endif
  97. #ifdef PRINTING_ENABLE
  98. #include "process_printer.h"
  99. #endif
  100. #ifdef AUTO_SHIFT_ENABLE
  101. #include "process_auto_shift.h"
  102. #endif
  103. #ifdef COMBO_ENABLE
  104. #include "process_combo.h"
  105. #endif
  106. #ifdef KEY_LOCK_ENABLE
  107. #include "process_key_lock.h"
  108. #endif
  109. #ifdef TERMINAL_ENABLE
  110. #include "process_terminal.h"
  111. #else
  112. #include "process_terminal_nop.h"
  113. #endif
  114. #ifdef SPACE_CADET_ENABLE
  115. #include "process_space_cadet.h"
  116. #endif
  117. #ifdef HD44780_ENABLE
  118. #include "hd44780.h"
  119. #endif
  120. #ifdef HAPTIC_ENABLE
  121. #include "haptic.h"
  122. #endif
  123. #ifdef OLED_DRIVER_ENABLE
  124. #include "oled_driver.h"
  125. #endif
  126. //Function substitutions to ease GPIO manipulation
  127. #ifdef __AVR__
  128. #define PIN_ADDRESS(p, offset) _SFR_IO8(ADDRESS_BASE + (p >> PORT_SHIFTER) + offset)
  129. #define pin_t uint8_t
  130. #define setPinInput(pin) PIN_ADDRESS(pin, 1) &= ~ _BV(pin & 0xF)
  131. #define setPinInputHigh(pin) ({\
  132. PIN_ADDRESS(pin, 1) &= ~ _BV(pin & 0xF);\
  133. PIN_ADDRESS(pin, 2) |= _BV(pin & 0xF);\
  134. })
  135. #define setPinInputLow(pin) _Static_assert(0, "AVR Processors cannot impliment an input as pull low")
  136. #define setPinOutput(pin) PIN_ADDRESS(pin, 1) |= _BV(pin & 0xF)
  137. #define writePinHigh(pin) PIN_ADDRESS(pin, 2) |= _BV(pin & 0xF)
  138. #define writePinLow(pin) PIN_ADDRESS(pin, 2) &= ~_BV(pin & 0xF)
  139. static inline void writePin(pin_t pin, uint8_t level){
  140. if (level){
  141. PIN_ADDRESS(pin, 2) |= _BV(pin & 0xF);
  142. } else {
  143. PIN_ADDRESS(pin, 2) &= ~_BV(pin & 0xF);
  144. }
  145. }
  146. #define readPin(pin) ((bool)(PIN_ADDRESS(pin, 0) & _BV(pin & 0xF)))
  147. #elif defined(PROTOCOL_CHIBIOS)
  148. #define pin_t ioline_t
  149. #define setPinInput(pin) palSetLineMode(pin, PAL_MODE_INPUT)
  150. #define setPinInputHigh(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)
  151. #define setPinInputLow(pin) palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)
  152. #define setPinOutput(pin) palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)
  153. #define writePinHigh(pin) palSetLine(pin)
  154. #define writePinLow(pin) palClearLine(pin)
  155. static inline void writePin(pin_t pin, uint8_t level){
  156. if (level){
  157. palSetLine(pin);
  158. } else {
  159. palClearLine(pin);
  160. }
  161. }
  162. #define readPin(pin) palReadLine(pin)
  163. #endif
  164. #define STRINGIZE(z) #z
  165. #define ADD_SLASH_X(y) STRINGIZE(\x ## y)
  166. #define SYMBOL_STR(x) ADD_SLASH_X(x)
  167. #define SS_TAP_CODE 1
  168. #define SS_DOWN_CODE 2
  169. #define SS_UP_CODE 3
  170. #define SS_TAP(keycode) "\1" SYMBOL_STR(keycode)
  171. #define SS_DOWN(keycode) "\2" SYMBOL_STR(keycode)
  172. #define SS_UP(keycode) "\3" SYMBOL_STR(keycode)
  173. #define SS_LCTRL(string) SS_DOWN(X_LCTRL) string SS_UP(X_LCTRL)
  174. #define SS_LGUI(string) SS_DOWN(X_LGUI) string SS_UP(X_LGUI)
  175. #define SS_LCMD(string) SS_LGUI(string)
  176. #define SS_LWIN(string) SS_LGUI(string)
  177. #define SS_LALT(string) SS_DOWN(X_LALT) string SS_UP(X_LALT)
  178. #define SS_LSFT(string) SS_DOWN(X_LSHIFT) string SS_UP(X_LSHIFT)
  179. #define SS_RALT(string) SS_DOWN(X_RALT) string SS_UP(X_RALT)
  180. #define SS_ALGR(string) SS_RALT(string)
  181. #define SEND_STRING(str) send_string_P(PSTR(str))
  182. extern const bool ascii_to_shift_lut[0x80];
  183. extern const bool ascii_to_altgr_lut[0x80];
  184. extern const uint8_t ascii_to_keycode_lut[0x80];
  185. void send_string(const char *str);
  186. void send_string_with_delay(const char *str, uint8_t interval);
  187. void send_string_P(const char *str);
  188. void send_string_with_delay_P(const char *str, uint8_t interval);
  189. void send_char(char ascii_code);
  190. // For tri-layer
  191. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
  192. uint32_t update_tri_layer_state(uint32_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
  193. void set_single_persistent_default_layer(uint8_t default_layer);
  194. void tap_random_base64(void);
  195. #define IS_LAYER_ON(layer) (layer_state & (1UL << (layer)))
  196. #define IS_LAYER_OFF(layer) (~layer_state & (1UL << (layer)))
  197. void matrix_init_kb(void);
  198. void matrix_scan_kb(void);
  199. void matrix_init_user(void);
  200. void matrix_scan_user(void);
  201. uint16_t get_record_keycode(keyrecord_t *record);
  202. uint16_t get_event_keycode(keyevent_t event);
  203. bool process_action_kb(keyrecord_t *record);
  204. bool process_record_kb(uint16_t keycode, keyrecord_t *record);
  205. bool process_record_user(uint16_t keycode, keyrecord_t *record);
  206. #ifndef BOOTMAGIC_LITE_COLUMN
  207. #define BOOTMAGIC_LITE_COLUMN 0
  208. #endif
  209. #ifndef BOOTMAGIC_LITE_ROW
  210. #define BOOTMAGIC_LITE_ROW 0
  211. #endif
  212. void bootmagic_lite(void);
  213. void reset_keyboard(void);
  214. void startup_user(void);
  215. void shutdown_user(void);
  216. void register_code16(uint16_t code);
  217. void unregister_code16(uint16_t code);
  218. void tap_code16(uint16_t code);
  219. #ifdef BACKLIGHT_ENABLE
  220. void backlight_init_ports(void);
  221. void backlight_task(void);
  222. void backlight_task_internal(void);
  223. void backlight_on(uint8_t backlight_pin);
  224. void backlight_off(uint8_t backlight_pin);
  225. #ifdef BACKLIGHT_BREATHING
  226. void breathing_task(void);
  227. void breathing_enable(void);
  228. void breathing_pulse(void);
  229. void breathing_disable(void);
  230. void breathing_self_disable(void);
  231. void breathing_toggle(void);
  232. bool is_breathing(void);
  233. void breathing_intensity_default(void);
  234. void breathing_period_default(void);
  235. void breathing_period_set(uint8_t value);
  236. void breathing_period_inc(void);
  237. void breathing_period_dec(void);
  238. #endif
  239. #endif
  240. void send_dword(uint32_t number);
  241. void send_word(uint16_t number);
  242. void send_byte(uint8_t number);
  243. void send_nibble(uint8_t number);
  244. uint16_t hex_to_keycode(uint8_t hex);
  245. void led_set_user(uint8_t usb_led);
  246. void led_set_kb(uint8_t usb_led);
  247. void api_send_unicode(uint32_t unicode);
  248. #endif