summaryrefslogtreecommitdiffstats
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/backlight/backlight_avr.c12
-rw-r--r--quantum/backlight/backlight_chibios.c14
-rw-r--r--quantum/config_common.h92
-rw-r--r--quantum/encoder.c24
-rw-r--r--quantum/joystick.h12
-rw-r--r--quantum/led_matrix.c2
-rw-r--r--quantum/led_matrix.h (renamed from quantum/ledmatrix.h)0
-rw-r--r--quantum/led_matrix_drivers.c2
-rw-r--r--quantum/matrix.c35
-rw-r--r--quantum/mcu_selection.mk3
-rw-r--r--quantum/process_keycode/process_auto_shift.c199
-rw-r--r--quantum/process_keycode/process_auto_shift.h1
-rw-r--r--quantum/process_keycode/process_joystick.c8
-rw-r--r--quantum/process_keycode/process_midi.c25
-rw-r--r--quantum/process_keycode/process_midi.h2
-rw-r--r--quantum/process_keycode/process_sequencer.c62
-rw-r--r--quantum/process_keycode/process_sequencer.h21
-rw-r--r--quantum/quantum.c15
-rw-r--r--quantum/quantum.h62
-rw-r--r--quantum/quantum_keycodes.h38
-rw-r--r--quantum/rgb_matrix.c32
-rw-r--r--quantum/rgb_matrix.h9
-rw-r--r--quantum/rgb_matrix_animations/alpha_mods_anim.h4
-rw-r--r--quantum/rgb_matrix_animations/breathing_anim.h2
-rw-r--r--quantum/rgb_matrix_animations/gradient_left_right_anim.h2
-rw-r--r--quantum/rgb_matrix_animations/gradient_up_down_anim.h2
-rw-r--r--quantum/rgb_matrix_animations/jellybean_raindrops_anim.h2
-rw-r--r--quantum/rgb_matrix_animations/raindrops_anim.h2
-rw-r--r--quantum/rgb_matrix_animations/solid_color_anim.h2
-rw-r--r--quantum/rgb_matrix_animations/typing_heatmap_anim.h2
-rw-r--r--quantum/rgb_matrix_runners/effect_runner_dx_dy.h2
-rw-r--r--quantum/rgb_matrix_runners/effect_runner_dx_dy_dist.h2
-rw-r--r--quantum/rgb_matrix_runners/effect_runner_i.h2
-rw-r--r--quantum/rgb_matrix_runners/effect_runner_reactive.h2
-rw-r--r--quantum/rgb_matrix_runners/effect_runner_reactive_splash.h2
-rw-r--r--quantum/rgb_matrix_runners/effect_runner_sin_cos_i.h2
-rw-r--r--quantum/rgblight.c4
-rw-r--r--quantum/sequencer/sequencer.c275
-rw-r--r--quantum/sequencer/sequencer.h122
-rw-r--r--quantum/sequencer/tests/midi_mock.c26
-rw-r--r--quantum/sequencer/tests/midi_mock.h26
-rw-r--r--quantum/sequencer/tests/rules.mk11
-rw-r--r--quantum/sequencer/tests/sequencer_tests.cpp590
-rw-r--r--quantum/sequencer/tests/testlist.mk1
-rw-r--r--quantum/split_common/matrix.c35
45 files changed, 1668 insertions, 122 deletions
diff --git a/quantum/backlight/backlight_avr.c b/quantum/backlight/backlight_avr.c
index b3e882ffe1..4d66da80ba 100644
--- a/quantum/backlight/backlight_avr.c
+++ b/quantum/backlight/backlight_avr.c
@@ -3,6 +3,11 @@
#include "backlight_driver_common.h"
#include "debug.h"
+// Maximum duty cycle limit
+#ifndef BACKLIGHT_LIMIT_VAL
+# define BACKLIGHT_LIMIT_VAL 255
+#endif
+
// This logic is a bit complex, we support 3 setups:
//
// 1. Hardware PWM when backlight is wired to a PWM pin.
@@ -240,6 +245,9 @@ static uint16_t cie_lightness(uint16_t v) {
}
}
+// rescale the supplied backlight value to be in terms of the value limit
+static uint32_t rescale_limit_val(uint32_t val) { return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256; }
+
// range for val is [0..TIMER_TOP]. PWM pin is high while the timer count is below val.
static inline void set_pwm(uint16_t val) { OCRxx = val; }
@@ -269,7 +277,7 @@ void backlight_set(uint8_t level) {
#endif
}
// Set the brightness
- set_pwm(cie_lightness(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS));
+ set_pwm(cie_lightness(rescale_limit_val(TIMER_TOP * (uint32_t)level / BACKLIGHT_LEVELS)));
}
void backlight_task(void) {}
@@ -375,7 +383,7 @@ ISR(TIMERx_OVF_vect)
breathing_interrupt_disable();
}
- set_pwm(cie_lightness(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U)));
+ set_pwm(cie_lightness(rescale_limit_val(scale_backlight((uint16_t)pgm_read_byte(&breathing_table[index]) * 0x0101U))));
}
#endif // BACKLIGHT_BREATHING
diff --git a/quantum/backlight/backlight_chibios.c b/quantum/backlight/backlight_chibios.c
index 0fe812bf27..4d5a69e14e 100644
--- a/quantum/backlight/backlight_chibios.c
+++ b/quantum/backlight/backlight_chibios.c
@@ -3,6 +3,11 @@
#include <hal.h>
#include "debug.h"
+// Maximum duty cycle limit
+#ifndef BACKLIGHT_LIMIT_VAL
+# define BACKLIGHT_LIMIT_VAL 255
+#endif
+
// GPIOV2 && GPIOV3
#ifndef BACKLIGHT_PAL_MODE
# define BACKLIGHT_PAL_MODE 2
@@ -58,6 +63,11 @@ static uint16_t cie_lightness(uint16_t v) {
}
}
+static uint32_t rescale_limit_val(uint32_t val) {
+ // rescale the supplied backlight value to be in terms of the value limit
+ return (val * (BACKLIGHT_LIMIT_VAL + 1)) / 256;
+}
+
void backlight_init_ports(void) {
#ifdef USE_GPIOV1
palSetPadMode(PAL_PORT(BACKLIGHT_PIN), PAL_PAD(BACKLIGHT_PIN), PAL_MODE_STM32_ALTERNATE_PUSHPULL);
@@ -85,7 +95,7 @@ void backlight_set(uint8_t level) {
pwmDisableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1);
} else {
// Turn backlight on
- uint32_t duty = (uint32_t)(cie_lightness(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS));
+ uint32_t duty = (uint32_t)(cie_lightness(rescale_limit_val(0xFFFF * (uint32_t)level / BACKLIGHT_LEVELS)));
pwmEnableChannel(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
}
}
@@ -129,7 +139,7 @@ void breathing_callback(PWMDriver *pwmp) {
static uint16_t breathing_counter = 0;
breathing_counter = (breathing_counter + 1) % (breathing_period * 256);
uint8_t index = breathing_counter / interval % BREATHING_STEPS;
- uint32_t duty = cie_lightness(scale_backlight(breathing_table[index] * 256));
+ uint32_t duty = cie_lightness(rescale_limit_val(scale_backlight(breathing_table[index] * 256)));
chSysLockFromISR();
pwmEnableChannelI(pwmp, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
diff --git a/quantum/config_common.h b/quantum/config_common.h
index c1e6698e50..2d9c70b08d 100644
--- a/quantum/config_common.h
+++ b/quantum/config_common.h
@@ -39,7 +39,7 @@
# define PIND_ADDRESS 0x9
# define PINE_ADDRESS 0xC
# define PINF_ADDRESS 0xF
-# elif defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__)
+# elif defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
# define ADDRESS_BASE 0x00
# define PINB_ADDRESS 0x3
# define PINC_ADDRESS 0x6
@@ -58,11 +58,6 @@
# define PINC_ADDRESS 0x3
# define PINB_ADDRESS 0x6
# define PINA_ADDRESS 0x9
-# elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328__)
-# define ADDRESS_BASE 0x00
-# define PINB_ADDRESS 0x3
-# define PINC_ADDRESS 0x6
-# define PIND_ADDRESS 0x9
# elif defined(__AVR_ATtiny85__)
# define ADDRESS_BASE 0x10
# define PINB_ADDRESS 0x6
@@ -284,6 +279,91 @@
# define F13 PAL_LINE(GPIOF, 13)
# define F14 PAL_LINE(GPIOF, 14)
# define F15 PAL_LINE(GPIOF, 15)
+# define G0 PAL_LINE(GPIOG, 0)
+# define G1 PAL_LINE(GPIOG, 1)
+# define G2 PAL_LINE(GPIOG, 2)
+# define G3 PAL_LINE(GPIOG, 3)
+# define G4 PAL_LINE(GPIOG, 4)
+# define G5 PAL_LINE(GPIOG, 5)
+# define G6 PAL_LINE(GPIOG, 6)
+# define G7 PAL_LINE(GPIOG, 7)
+# define G8 PAL_LINE(GPIOG, 8)
+# define G9 PAL_LINE(GPIOG, 9)
+# define G10 PAL_LINE(GPIOG, 10)
+# define G11 PAL_LINE(GPIOG, 11)
+# define G12 PAL_LINE(GPIOG, 12)
+# define G13 PAL_LINE(GPIOG, 13)
+# define G14 PAL_LINE(GPIOG, 14)
+# define G15 PAL_LINE(GPIOG, 15)
+# define H0 PAL_LINE(GPIOH, 0)
+# define H1 PAL_LINE(GPIOH, 1)
+# define H2 PAL_LINE(GPIOH, 2)
+# define H3 PAL_LINE(GPIOH, 3)
+# define H4 PAL_LINE(GPIOH, 4)
+# define H5 PAL_LINE(GPIOH, 5)
+# define H6 PAL_LINE(GPIOH, 6)
+# define H7 PAL_LINE(GPIOH, 7)
+# define H8 PAL_LINE(GPIOH, 8)
+# define H9 PAL_LINE(GPIOH, 9)
+# define H10 PAL_LINE(GPIOH, 10)
+# define H11 PAL_LINE(GPIOH, 11)
+# define H12 PAL_LINE(GPIOH, 12)
+# define H13 PAL_LINE(GPIOH, 13)
+# define H14 PAL_LINE(GPIOH, 14)
+# define H15 PAL_LINE(GPIOH, 15)
+# define I0 PAL_LINE(GPIOI, 0)
+# define I1 PAL_LINE(GPIOI, 1)
+# define I2 PAL_LINE(GPIOI, 2)
+# define I3 PAL_LINE(GPIOI, 3)
+# define I4 PAL_LINE(GPIOI, 4)
+# define I5 PAL_LINE(GPIOI, 5)
+# define I6 PAL_LINE(GPIOI, 6)
+# define I7 PAL_LINE(GPIOI, 7)
+# define I8 PAL_LINE(GPIOI, 8)
+# define I9 PAL_LINE(GPIOI, 9)
+# define I10 PAL_LINE(GPIOI, 10)
+# define I11 PAL_LINE(GPIOI, 11)
+# define I12 PAL_LINE(GPIOI, 12)
+# define I13 PAL_LINE(GPIOI, 13)
+# define I14 PAL_LINE(GPIOI, 14)
+# define I15 PAL_LINE(GPIOI, 15)
+# define J0 PAL_LINE(GPIOJ, 0)
+# define J1 PAL_LINE(GPIOJ, 1)
+# define J2 PAL_LINE(GPIOJ, 2)
+# define J3 PAL_LINE(GPIOJ, 3)
+# define J4 PAL_LINE(GPIOJ, 4)
+# define J5 PAL_LINE(GPIOJ, 5)
+# define J6 PAL_LINE(GPIOJ, 6)
+# define J7 PAL_LINE(GPIOJ, 7)
+# define J8 PAL_LINE(GPIOJ, 8)
+# define J9 PAL_LINE(GPIOJ, 9)
+# define J10 PAL_LINE(GPIOJ, 10)
+# define J11 PAL_LINE(GPIOJ, 11)
+# define J12 PAL_LINE(GPIOJ, 12)
+# define J13 PAL_LINE(GPIOJ, 13)
+# define J14 PAL_LINE(GPIOJ, 14)
+# define J15 PAL_LINE(GPIOJ, 15)
+// Keyboards can `#define KEYBOARD_REQUIRES_GPIOK` if they need to access GPIO-K pins. These conflict with a whole
+// bunch of layout definitions, so it's intentionally left out unless absolutely required -- in that case, the
+// keyboard designer should use a different symbol when defining their layout macros.
+# ifdef KEYBOARD_REQUIRES_GPIOK
+# define K0 PAL_LINE(GPIOK, 0)
+# define K1 PAL_LINE(GPIOK, 1)
+# define K2 PAL_LINE(GPIOK, 2)
+# define K3 PAL_LINE(GPIOK, 3)
+# define K4 PAL_LINE(GPIOK, 4)
+# define K5 PAL_LINE(GPIOK, 5)
+# define K6 PAL_LINE(GPIOK, 6)
+# define K7 PAL_LINE(GPIOK, 7)
+# define K8 PAL_LINE(GPIOK, 8)
+# define K9 PAL_LINE(GPIOK, 9)
+# define K10 PAL_LINE(GPIOK, 10)
+# define K11 PAL_LINE(GPIOK, 11)
+# define K12 PAL_LINE(GPIOK, 12)
+# define K13 PAL_LINE(GPIOK, 13)
+# define K14 PAL_LINE(GPIOK, 14)
+# define K15 PAL_LINE(GPIOK, 15)
+# endif
# endif
#endif
diff --git a/quantum/encoder.c b/quantum/encoder.c
index 81ec1bb376..7ca31afedc 100644
--- a/quantum/encoder.c
+++ b/quantum/encoder.c
@@ -23,7 +23,7 @@
// for memcpy
#include <string.h>
-#ifndef ENCODER_RESOLUTION
+#if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
# define ENCODER_RESOLUTION 4
#endif
@@ -34,6 +34,9 @@
#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
+#ifdef ENCODER_RESOLUTIONS
+static uint8_t encoder_resolutions[] = ENCODER_RESOLUTIONS;
+#endif
#ifndef ENCODER_DIRECTION_FLIP
# define ENCODER_CLOCKWISE true
@@ -65,9 +68,15 @@ void encoder_init(void) {
if (!isLeftHand) {
const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
+# if defined(ENCODER_RESOLUTIONS_RIGHT)
+ const uint8_t encoder_resolutions_right[] = ENCODER_RESOLUTIONS_RIGHT;
+# endif
for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
encoders_pad_a[i] = encoders_pad_a_right[i];
encoders_pad_b[i] = encoders_pad_b_right[i];
+# if defined(ENCODER_RESOLUTIONS_RIGHT)
+ encoder_resolutions[i] = encoder_resolutions_right[i];
+# endif
}
}
#endif
@@ -87,19 +96,26 @@ void encoder_init(void) {
static void encoder_update(int8_t index, uint8_t state) {
uint8_t i = index;
+
+#ifdef ENCODER_RESOLUTIONS
+ int8_t resolution = encoder_resolutions[i];
+#else
+ int8_t resolution = ENCODER_RESOLUTION;
+#endif
+
#ifdef SPLIT_KEYBOARD
index += thisHand;
#endif
encoder_pulses[i] += encoder_LUT[state & 0xF];
- if (encoder_pulses[i] >= ENCODER_RESOLUTION) {
+ if (encoder_pulses[i] >= resolution) {
encoder_value[index]++;
encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
}
- if (encoder_pulses[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
+ if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
encoder_value[index]--;
encoder_update_kb(index, ENCODER_CLOCKWISE);
}
- encoder_pulses[i] %= ENCODER_RESOLUTION;
+ encoder_pulses[i] %= resolution;
}
void encoder_read(void) {
diff --git a/quantum/joystick.h b/quantum/joystick.h
index a95472b9fd..87dbc24aff 100644
--- a/quantum/joystick.h
+++ b/quantum/joystick.h
@@ -1,5 +1,9 @@
#pragma once
+#include "quantum.h"
+
+#include <stdint.h>
+
#ifndef JOYSTICK_BUTTON_COUNT
# define JOYSTICK_BUTTON_COUNT 8
#endif
@@ -8,9 +12,13 @@
# define JOYSTICK_AXES_COUNT 4
#endif
-#include "quantum.h"
+#ifndef JOYSTICK_AXES_RESOLUTION
+# define JOYSTICK_AXES_RESOLUTION 8
+#elif JOYSTICK_AXES_RESOLUTION < 8 || JOYSTICK_AXES_RESOLUTION > 16
+# error JOYSTICK_AXES_RESOLUTION must be between 8 and 16
+#endif
-#include <stdint.h>
+#define JOYSTICK_RESOLUTION ((1L << (JOYSTICK_AXES_RESOLUTION - 1)) - 1)
// configure on input_pin of the joystick_axes array entry to JS_VIRTUAL_AXIS
// to prevent it from being read from the ADC. This allows outputing forged axis value.
diff --git a/quantum/led_matrix.c b/quantum/led_matrix.c
index 5c24c797a9..eb523990a6 100644
--- a/quantum/led_matrix.c
+++ b/quantum/led_matrix.c
@@ -20,7 +20,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "quantum.h"
-#include "ledmatrix.h"
+#include "led_matrix.h"
#include "progmem.h"
#include "config.h"
#include "eeprom.h"
diff --git a/quantum/ledmatrix.h b/quantum/led_matrix.h
index 5867ba9876..5867ba9876 100644
--- a/quantum/ledmatrix.h
+++ b/quantum/led_matrix.h
diff --git a/quantum/led_matrix_drivers.c b/quantum/led_matrix_drivers.c
index 6877bf4c6b..9decaa33c2 100644
--- a/quantum/led_matrix_drivers.c
+++ b/quantum/led_matrix_drivers.c
@@ -18,7 +18,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "quantum.h"
-#include "ledmatrix.h"
+#include "led_matrix.h"
/* Each driver needs to define a struct:
*
diff --git a/quantum/matrix.c b/quantum/matrix.c
index c68c56cac2..cab0d2ddca 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -32,6 +32,19 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
+static inline void setPinOutput_writeLow(pin_t pin) {
+ ATOMIC_BLOCK_FORCEON {
+ setPinOutput(pin);
+ writePinLow(pin);
+ }
+}
+
+static inline void setPinInputHigh_atomic(pin_t pin) {
+ ATOMIC_BLOCK_FORCEON {
+ setPinInputHigh(pin);
+ }
+}
+
// matrix code
#ifdef DIRECT_PINS
@@ -70,22 +83,23 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
# if (DIODE_DIRECTION == COL2ROW)
static void select_row(uint8_t row) {
- setPinOutput(row_pins[row]);
- writePinLow(row_pins[row]);
+ setPinOutput_writeLow(row_pins[row]);
}
-static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
+static void unselect_row(uint8_t row) {
+ setPinInputHigh_atomic(row_pins[row]);
+}
static void unselect_rows(void) {
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
- setPinInputHigh(row_pins[x]);
+ setPinInputHigh_atomic(row_pins[x]);
}
}
static void init_pins(void) {
unselect_rows();
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
- setPinInputHigh(col_pins[x]);
+ setPinInputHigh_atomic(col_pins[x]);
}
}
@@ -120,22 +134,23 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
# elif (DIODE_DIRECTION == ROW2COL)
static void select_col(uint8_t col) {
- setPinOutput(col_pins[col]);
- writePinLow(col_pins[col]);
+ setPinOutput_writeLow(col_pins[col]);
}
-static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
+static void unselect_col(uint8_t col) {
+ setPinInputHigh_atomic(col_pins[col]);
+}
static void unselect_cols(void) {
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
- setPinInputHigh(col_pins[x]);
+ setPinInputHigh_atomic(col_pins[x]);
}
}
static void init_pins(void) {
unselect_cols();
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
- setPinInputHigh(row_pins[x]);
+ setPinInputHigh_atomic(row_pins[x]);
}
}
diff --git a/quantum/mcu_selection.mk b/quantum/mcu_selection.mk
index 295dfd3189..9518a6463f 100644
--- a/quantum/mcu_selection.mk
+++ b/quantum/mcu_selection.mk
@@ -318,6 +318,9 @@ ifneq (,$(filter $(MCU),atmega16u2 atmega32u2 atmega16u4 atmega32u4 at90usb646 a
ifeq (,$(filter $(NO_INTERRUPT_CONTROL_ENDPOINT),yes))
OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
endif
+ ifneq (,$(filter $(MCU),atmega16u2 atmega32u2))
+ NO_I2C = yes
+ endif
endif
ifneq (,$(filter $(MCU),atmega32a))
diff --git a/quantum/process_keycode/process_auto_shift.c b/quantum/process_keycode/process_auto_shift.c
index b1267922ce..a2d315408b 100644
--- a/quantum/process_keycode/process_auto_shift.c
+++ b/quantum/process_keycode/process_auto_shift.c
@@ -16,48 +16,149 @@
#ifdef AUTO_SHIFT_ENABLE
+# include <stdbool.h>
# include <stdio.h>
# include "process_auto_shift.h"
-static bool autoshift_enabled = true;
static uint16_t autoshift_time = 0;
static uint16_t autoshift_timeout = AUTO_SHIFT_TIMEOUT;
static uint16_t autoshift_lastkey = KC_NO;
+static struct {
+ // Whether autoshift is enabled.
+ bool enabled : 1;
+ // Whether the last auto-shifted key was released after the timeout. This
+ // is used to replicate the last key for a tap-then-hold.
+ bool lastshifted : 1;
+ // Whether an auto-shiftable key has been pressed but not processed.
+ bool in_progress : 1;
+ // Whether the auto-shifted keypress has been registered.
+ bool holding_shift : 1;
+} autoshift_flags = {true, false, false, false};
+
+/** \brief Record the press of an autoshiftable key
+ *
+ * \return Whether the record should be further processed.
+ */
+static bool autoshift_press(uint16_t keycode, uint16_t now, keyrecord_t *record) {
+ if (!autoshift_flags.enabled) {
+ return true;
+ }
+
+# ifndef AUTO_SHIFT_MODIFIERS
+ if (get_mods() & (~MOD_BIT(KC_LSFT))) {
+ return true;
+ }
+# endif
+# ifdef AUTO_SHIFT_REPEAT
+ const uint16_t elapsed = TIMER_DIFF_16(now, autoshift_time);
+# ifndef AUTO_SHIFT_NO_AUTO_REPEAT
+ if (!autoshift_flags.lastshifted) {
+# endif
+ if (elapsed < TAPPING_TERM && keycode == autoshift_lastkey) {
+ // Allow a tap-then-hold for keyrepeat.
+ if (!autoshift_flags.lastshifted) {
+ register_code(autoshift_lastkey);
+ } else {
+ // Simulate pressing the shift key.
+ add_weak_mods(MOD_BIT(KC_LSFT));
+ register_code(autoshift_lastkey);
+ }
+ return false;
+ }
+# ifndef AUTO_SHIFT_NO_AUTO_REPEAT
+ }
+# endif
+# endif
-void autoshift_flush(void) {
- if (autoshift_lastkey != KC_NO) {
- uint16_t elapsed = timer_elapsed(autoshift_time);
+ // Record the keycode so we can simulate it later.
+ autoshift_lastkey = keycode;
+ autoshift_time = now;
+ autoshift_flags.in_progress = true;
- if (elapsed > autoshift_timeout) {
- tap_code16(LSFT(autoshift_lastkey));
+# if !defined(NO_ACTION_ONESHOT) && !defined(NO_ACTION_TAPPING)
+ clear_oneshot_layer_state(ONESHOT_OTHER_KEY_PRESSED);
+# endif
+ return false;
+}
+
+/** \brief Registers an autoshiftable key under the right conditions
+ *
+ * If the autoshift delay has elapsed, register a shift and the key.
+ *
+ * If the autoshift key is released before the delay has elapsed, register the
+ * key without a shift.
+ */
+static void autoshift_end(uint16_t keycode, uint16_t now, bool matrix_trigger) {
+ // Called on key down with KC_NO, auto-shifted key up, and timeout.
+ if (autoshift_flags.in_progress) {
+ // Process the auto-shiftable key.
+ autoshift_flags.in_progress = false;
+
+ // Time since the initial press was recorded.
+ const uint16_t elapsed = TIMER_DIFF_16(now, autoshift_time);
+ if (elapsed < autoshift_timeout) {
+ register_code(autoshift_lastkey);
+ autoshift_flags.lastshifted = false;
} else {
- tap_code(autoshift_lastkey);
+ // Simulate pressing the shift key.
+ add_weak_mods(MOD_BIT(KC_LSFT));
+ register_code(autoshift_lastkey);
+ autoshift_flags.lastshifted = true;
+# if defined(AUTO_SHIFT_REPEAT) && !defined(AUTO_SHIFT_NO_AUTO_REPEAT)
+ if (matrix_trigger) {
+ // Prevents release.
+ return;
+ }
+# endif
}
- autoshift_time = 0;
- autoshift_lastkey = KC_NO;
+# if TAP_CODE_DELAY > 0
+ wait_ms(TAP_CODE_DELAY);
+# endif
+ unregister_code(autoshift_lastkey);
+ del_weak_mods(MOD_BIT(KC_LSFT));
+ } else {
+ // Release after keyrepeat.
+ unregister_code(keycode);
+ if (keycode == autoshift_lastkey) {
+ // This will only fire when the key was the last auto-shiftable
+ // pressed. That prevents aaaaBBBB then releasing a from unshifting
+ // later Bs (if B wasn't auto-shiftable).
+ del_weak_mods(MOD_BIT(KC_LSFT));
+ }
}
+ send_keyboard_report(); // del_weak_mods doesn't send one.
+ // Roll the autoshift_time forward for detecting tap-and-hold.
+ autoshift_time = now;
}
-void autoshift_on(uint16_t keycode) {
- autoshift_time = timer_read();
- autoshift_lastkey = keycode;
+/** \brief Simulates auto-shifted key releases when timeout is hit
+ *
+ * Can be called from \c matrix_scan_user so that auto-shifted keys are sent
+ * immediately after the timeout has expired, rather than waiting for the key
+ * to be released.
+ */
+void autoshift_matrix_scan(void) {
+ if (autoshift_flags.in_progress) {
+ const uint16_t now = timer_read();
+ const uint16_t elapsed = TIMER_DIFF_16(now, autoshift_time);
+ if (elapsed >= autoshift_timeout) {
+ autoshift_end(autoshift_lastkey, now, true);
+ }
+ }
}
void autoshift_toggle(void) {
- if (autoshift_enabled) {
- autoshift_enabled = false;
- autoshift_flush();
- } else {
- autoshift_enabled = true;
- }
+ autoshift_flags.enabled = !autoshift_flags.enabled;
+ del_weak_mods(MOD_BIT(KC_LSFT));
}
-void autoshift_enable(void) { autoshift_enabled = true; }
+void autoshift_enable(void) { autoshift_flags.enabled = true; }
+
void autoshift_disable(void) {
- autoshift_enabled = false;
- autoshift_flush();
+ autoshift_flags.enabled = false;
+ del_weak_mods(MOD_BIT(KC_LSFT));
}
# ifndef AUTO_SHIFT_NO_SETUP
@@ -70,19 +171,30 @@ void autoshift_timer_report(void) {
}
# endif
-bool get_autoshift_state(void) { return autoshift_enabled; }
+bool get_autoshift_state(void) { return autoshift_flags.enabled; }
uint16_t get_autoshift_timeout(void) { return autoshift_timeout; }
void set_autoshift_timeout(uint16_t timeout) { autoshift_timeout = timeout; }
bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
+ // Note that record->event.time isn't reliable, see:
+ // https://github.com/qmk/qmk_firmware/pull/9826#issuecomment-733559550
+ const uint16_t now = timer_read();
+
if (record->event.pressed) {
+ if (autoshift_flags.in_progress) {
+ // Evaluate previous key if there is one. Doing this elsewhere is
+ // more complicated and easier to break.
+ autoshift_end(KC_NO, now, false);
+ }
+ // For pressing another key while keyrepeating shifted autoshift.
+ del_weak_mods(MOD_BIT(KC_LSFT));
+
switch (keycode) {
case KC_ASTG:
autoshift_toggle();
return true;
-
case KC_ASON:
autoshift_enable();
return true;
@@ -102,41 +214,28 @@ bool process_auto_shift(uint16_t keycode, keyrecord_t *record) {
autoshift_timer_report();
return true;
# endif