Skip to content
Permalink
Browse files
Simplify split_common Code significantly (#4772)
* Eliminate separate slave loop

Both master and slave run the standard keyboard_task main loop now.

* Refactor i2c/serial specific code

Simplify some of the preprocessor mess by using common function names.

* Fix missing #endif

* Move direct pin mapping support from miniaxe to split_common

For boards with more pins than sense--sorry, switches.

* Reordering and reformatting only

* Don't run matrix_scan_quantum on slave side

* Clean up the offset/slaveOffset calculations

* Cut undebounced matrix size in half

* Refactor debouncing

* Minor fixups

* Split split_common transport and debounce code into their own files

Can now be replaced with custom versions per keyboard using
CUSTOM_TRANSPORT = yes and CUSTOM_DEBOUNCE = yes

* Refactor debounce for non-split keyboards too

* Update handwired/xealous to build using new split_common

* Fix debounce breaking basic test

* Dodgy method to allow a split kb to only include one of i2c/serial

SPLIT_TRANSPORT = serial or SPLIT_TRANSPORT = i2c will include only
that driver code in the binary.

SPLIT_TRANSPORT = custom (or anything else) will include neither, the
keyboard must supply it's own code

if SPLIT_TRANSPORT is not defined then the original behaviour (include
both avr i2c and serial code) is maintained.

This could be better but it would require explicitly updating all the
existing split keyboards.

* Enable LTO to get lets_split/sockets under the line

* Add docs for SPLIT_TRANSPORT, CUSTOM_MATRIX, CUSTOM_DEBOUNCE

* Remove avr-specific sei() from split matrix_setup

Not needed now that slave doesn't have a separate main loop.
Both sides (on avr) call sei() in lufa's main() after exiting
keyboard_setup().

* Fix QUANTUM_LIB_SRC references and simplify SPLIT_TRANSPORT.

* Add comments and fix formatting.
  • Loading branch information
pelrun authored and drashna committed Jan 17, 2019
1 parent 5fcca9a commit 28929ad0174bdcb38e09f6d272a23b9be6aa430c
@@ -254,20 +254,34 @@ QUANTUM_SRC:= \
$(QUANTUM_DIR)/keymap_common.c \
$(QUANTUM_DIR)/keycode_config.c

ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
ifneq ($(strip $(CUSTOM_MATRIX)), yes)
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/matrix.c
# Do not use $(QUANTUM_DIR)/matrix.c.
CUSTOM_MATRIX=yes
# Include the standard or split matrix code if needed
ifneq ($(strip $(CUSTOM_MATRIX)), yes)
ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/matrix.c
else
QUANTUM_SRC += $(QUANTUM_DIR)/matrix.c
endif
endif

# Include the standard debounce code if needed
ifneq ($(strip $(CUSTOM_DEBOUNCE)), yes)
QUANTUM_SRC += $(QUANTUM_DIR)/debounce.c
endif

ifeq ($(strip $(SPLIT_KEYBOARD)), yes)
OPT_DEFS += -DSPLIT_KEYBOARD

# Include files used by all split keyboards
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/split_flags.c \
$(QUANTUM_DIR)/split_common/split_util.c
QUANTUM_LIB_SRC += $(QUANTUM_DIR)/split_common/i2c.c
QUANTUM_LIB_SRC += $(QUANTUM_DIR)/split_common/serial.c
$(QUANTUM_DIR)/split_common/split_util.c

# Determine which (if any) transport files are required
ifneq ($(strip $(SPLIT_TRANSPORT)), custom)
QUANTUM_SRC += $(QUANTUM_DIR)/split_common/transport.c
# Functions added via QUANTUM_LIB_SRC are only included in the final binary if they're called.
# Unused functions are pruned away, which is why we can add both drivers here without bloat.
QUANTUM_LIB_SRC += $(QUANTUM_DIR)/split_common/i2c.c \
$(QUANTUM_DIR)/split_common/serial.c
endif
COMMON_VPATH += $(QUANTUM_PATH)/split_common
endif

ifneq ($(strip $(CUSTOM_MATRIX)), yes)
QUANTUM_SRC += $(QUANTUM_DIR)/matrix.c
endif
@@ -143,7 +143,7 @@ If you define these options you will enable the associated feature, which may in
* Breaks any Tap Toggle functionality (`TT` or the One Shot Tap Toggle)
* `#define LEADER_TIMEOUT 300`
* how long before the leader key times out
* If you're having issues finishing the sequence before it times out, you may need to increase the timeout setting. Or you may want to enable the `LEADER_PER_KEY_TIMING` option, which resets the timeout after each key is tapped.
* If you're having issues finishing the sequence before it times out, you may need to increase the timeout setting. Or you may want to enable the `LEADER_PER_KEY_TIMING` option, which resets the timeout after each key is tapped.
* `#define LEADER_PER_KEY_TIMING`
* sets the timer for leader key chords to run on each key press rather than overall
* `#define LEADER_KEY_STRICT_KEY_PROCESSING`
@@ -197,6 +197,9 @@ If you define these options you will enable the associated feature, which may in

Split Keyboard specific options, make sure you have 'SPLIT_KEYBOARD = yes' in your rules.mk

* `SPLIT_TRANSPORT = custom`
* Allows replacing the standard split communication routines with a custom one. ARM based split keyboards must use this at present.

### Setting Handedness

One thing to remember, the side that the USB port is plugged into is always the master half. The side not plugged into USB is the slave.
@@ -208,7 +211,7 @@ There are a few different ways to set handedness for split keyboards (listed in
3. Set `MASTER_RIGHT`: Half that is plugged into the USB port is determined to be the master and right half (inverse of the default)
4. Default: The side that is plugged into the USB port is the master half and is assumed to be the left half. The slave side is the right half

* `#define SPLIT_HAND_PIN B7`
* `#define SPLIT_HAND_PIN B7`
* For using high/low pin to determine handedness, low = right hand, high = left hand. Replace `B7` with the pin you are using. This is optional, and if you leave `SPLIT_HAND_PIN` undefined, then you can still use the EE_HANDS method or MASTER_LEFT / MASTER_RIGHT defines like the stock Let's Split uses.

* `#define EE_HANDS` (only works if `SPLIT_HAND_PIN` is not defined)
@@ -302,6 +305,10 @@ Use these to enable or disable building certain features. The more you have enab
* Current options are AdafruitEzKey, AdafruitBLE, RN42
* `SPLIT_KEYBOARD`
* Enables split keyboard support (dual MCU like the let's split and bakingpy's boards) and includes all necessary files located at quantum/split_common
* `CUSTOM_MATRIX`
* Allows replacing the standard matrix scanning routine with a custom one.
* `CUSTOM_DEBOUNCE`
* Allows replacing the standard key debouncing routine with a custom one.
* `WAIT_FOR_USB`
* Forces the keyboard to wait for a USB connection to be established before it starts up
* `NO_USB_STARTUP_CHECK`
@@ -97,7 +97,7 @@ This allows you to send Unicode characters using `UC(<code point>)` in your keym

`UNICODEMAP_ENABLE`

This allows you to send Unicode characters using `X(<map index>)` in your keymap. You will need to maintain a mapping table in your keymap file. All possible code points (up to `0x10FFFF`) are supported.
This allows you to send Unicode characters using `X(<map index>)` in your keymap. You will need to maintain a mapping table in your keymap file. All possible code points (up to `0x10FFFF`) are supported.

`UCIS_ENABLE`

@@ -135,6 +135,18 @@ This enables [key lock](feature_key_lock.md). This consumes an additional 260 by

This enables split keyboard support (dual MCU like the let's split and bakingpy's boards) and includes all necessary files located at quantum/split_common

`SPLIT_TRANSPORT`

As there is no standard split communication driver for ARM-based split keyboards yet, `SPLIT_TRANSPORT = custom` must be used for these. It will prevent the standard split keyboard communication code (which is AVR-specific) from being included, allowing a custom implementation to be used.

`CUSTOM_MATRIX`

Lets you replace the default matrix scanning routine with your own code. You will need to provide your own implementations of matrix_init() and matrix_scan().

`CUSTOM_DEBOUNCE`

Lets you replace the default key debouncing routine with your own code. You will need to provide your own implementation of debounce().

## Customizing Makefile Options on a Per-Keymap Basis

If your keymap directory has a file called `rules.mk` any options you set in that file will take precedence over other `rules.mk` options for your particular keyboard.
@@ -0,0 +1,63 @@
#include <string.h>
#include "config.h"
#include "matrix.h"
#include "timer.h"
#include "quantum.h"

#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif

//Debouncing counters
typedef uint8_t debounce_counter_t;
#define DEBOUNCE_COUNTER_MODULO 250
#define DEBOUNCE_COUNTER_INACTIVE 251

static debounce_counter_t *debounce_counters;

void debounce_init(uint8_t num_rows)
{
debounce_counters = malloc(num_rows*MATRIX_COLS);
memset(debounce_counters, DEBOUNCE_COUNTER_INACTIVE, num_rows*MATRIX_COLS);
}

void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
{
for (uint8_t row = 0; row < num_rows; row++)
{
for (uint8_t col = 0; col < MATRIX_COLS; col++)
{
if (debounce_counters[row*MATRIX_COLS + col] != DEBOUNCE_COUNTER_INACTIVE)
{
if (TIMER_DIFF(current_time, debounce_counters[row*MATRIX_COLS + col], DEBOUNCE_COUNTER_MODULO) >= DEBOUNCING_DELAY) {
debounce_counters[row*MATRIX_COLS + col] = DEBOUNCE_COUNTER_INACTIVE;
}
}
}
}
}

void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time)
{
for (uint8_t row = 0; row < num_rows; row++)
{
matrix_row_t delta = raw[row] ^ cooked[row];

for (uint8_t col = 0; col < MATRIX_COLS; col++)
{
if (debounce_counters[row*MATRIX_COLS + col] == DEBOUNCE_COUNTER_INACTIVE && (delta & (1<<col)))
{
debounce_counters[row*MATRIX_COLS + col] = current_time;
cooked[row] ^= (1 << col);
}
}
}
}

void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed)
{
uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;

update_debounce_counters(num_rows, current_time);
transfer_matrix_values(raw, cooked, num_rows, current_time);
}
@@ -1,4 +1,5 @@
SRC += matrix_scanrate.c matrix.c
#SRC += matrix_scanrate.c matrix.c
SRC += debounce.c

# MCU name
MCU = atmega32u4
@@ -37,7 +38,7 @@ F_USB = $(F_CPU)

# Bootloader
# This definition is optional, and if your keyboard supports multiple bootloaders of
# different sizes, comment this out, and the correct address will be loaded
# different sizes, comment this out, and the correct address will be loaded
# automatically (+60). See bootloader.mk for all options.
BOOTLOADER = caterina

@@ -59,14 +60,15 @@ MIDI_ENABLE = no # MIDI controls
AUDIO_ENABLE = yes # Audio output on port C6
UNICODE_ENABLE = no # Unicode
BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight.
RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight.
SPLIT_KEYBOARD = yes # Use shared split_common code
SUBPROJECT_rev1 = yes

# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend

CUSTOM_MATRIX = yes
CUSTOM_MATRIX = no
CUSTOM_DEBOUNCE = yes

LAYOUTS = split60

@@ -85,3 +85,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//#define NO_ACTION_ONESHOT
//#define NO_ACTION_MACRO
//#define NO_ACTION_FUNCTION

#ifdef USE_Link_Time_Optimization
// LTO has issues with macros (action_get_macro) and "functions" (fn_actions),
// so just disable them
#define NO_ACTION_MACRO
#define NO_ACTION_FUNCTION

#define DISABLE_LEADER
#endif // USE_Link_Time_Optimization
@@ -1,3 +1,5 @@
BACKLIGHT_ENABLE = no
AUDIO_ENABLE = yes
RGBLIGHT_ENABLE = yes #Don't enable this along with I2C

EXTRAFLAGS += -flto -DUSE_Link_Time_Optimization
@@ -44,8 +44,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// #define MATRIX_ROW_PINS { D0, D5 }
// #define MATRIX_COL_PINS { F1, F0, B0 }
#define NO_PIN 0xFF
#define MATRIX_ROW_COL_PINS { \
#define DIRECT_PINS { \
{ F1, E6, B0, B2, B3 }, \
{ F5, F0, B1, B7, D2 }, \
{ F6, F7, C7, D5, D3 }, \
@@ -54,7 +53,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define UNUSED_PINS

/* COL2ROW, ROW2COL, or CUSTOM_MATRIX */
#define DIODE_DIRECTION CUSTOM_MATRIX
//#define DIODE_DIRECTION CUSTOM_MATRIX

// #define BACKLIGHT_PIN B7
// #define BACKLIGHT_BREATHING

0 comments on commit 28929ad

Please sign in to comment.