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