Skip to content

Commit

Permalink
Convert Encoder callbacks to be boolean functions (qmk#12805)
Browse files Browse the repository at this point in the history
Co-authored-by: James Young <18669334+noroadsleft@users.noreply.github.com>
  • Loading branch information
drashna and noroadsleft authored May 22, 2021
1 parent 765dc4b commit 1356fbf
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 48 deletions.
9 changes: 6 additions & 3 deletions docs/feature_encoders.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ If you are using different pinouts for the encoders on each half of a split keyb
The callback functions can be inserted into your `<keyboard>.c`:
```c
void encoder_update_kb(uint8_t index, bool clockwise) {
encoder_update_user(index, clockwise);
bool encoder_update_kb(uint8_t index, bool clockwise) {
return encoder_update_user(index, clockwise);
}
```
or `keymap.c`:
```c
void encoder_update_user(uint8_t index, bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (index == 0) { /* First encoder */
if (clockwise) {
tap_code(KC_PGDN);
Expand All @@ -75,9 +75,12 @@ void encoder_update_user(uint8_t index, bool clockwise) {
tap_code(KC_UP);
}
}
return true;
}
```
!> If you return `true`, this will allow the keyboard level code to run, as well. Returning `false` will override the keyboard level code. Depending on how the keyboard level function is set up.
## Hardware
The A an B lines of the encoders should be wired directly to the MCU, and the C/common lines should be wired to ground.
Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/bocaj/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void rgb_matrix_indicators_user(void) {
void matrix_init_keymap(void) {}

#ifdef ENCODER_ENABLE
void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
switch (get_highest_layer(layer_state)) {
case _RAISE:
clockwise ? tap_code(KC_VOLD) : tap_code(KC_VOLU);
Expand All @@ -260,6 +260,7 @@ void encoder_update(bool clockwise) {
# ifdef AUDIO_CLICKY
clicky_play();
# endif
return true;
}
#endif // ENCODER_ENABLE

Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/brandonschlack/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ uint16_t muse_counter = 0;
uint8_t muse_offset = 70;
uint16_t muse_tempo = 50;

void encoder_update_keymap(uint8_t index, bool clockwise) {
bool encoder_update_keymap(uint8_t index, bool clockwise) {
if (muse_mode) {
if (IS_LAYER_ON(_RAISE)) {
if (clockwise) {
Expand Down Expand Up @@ -151,6 +151,7 @@ void encoder_update_keymap(uint8_t index, bool clockwise) {
#endif
}
}
return true;
}

void dip_switch_update_user(uint8_t index, bool active) {
Expand Down
5 changes: 2 additions & 3 deletions layouts/community/ortho_4x12/buswerks/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
return true;
}

void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (IS_LAYER_ON(_RAISE) || IS_LAYER_ON(_LOWER)) {
if (clockwise) {
register_code(KC_VOLU);
Expand Down Expand Up @@ -170,6 +170,5 @@ void encoder_update(bool clockwise) {
#endif
}
}
return true;
}


3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/drashna/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void matrix_init_keymap(void) {
#endif // RGB_MATRIX_INIT

#ifdef ENCODER_ENABLE
void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
switch (get_highest_layer(layer_state)) {
case _RAISE:
clockwise ? tap_code(KC_VOLD) : tap_code(KC_VOLU);
Expand All @@ -371,6 +371,7 @@ void encoder_update(bool clockwise) {
# ifdef AUDIO_CLICKY
clicky_play();
# endif
return true;
}
#endif // ENCODER_ENABLE

Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/jackhumbert/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {
return update_tri_layer_state(state, _LOWER, _RAISE, _ADJUST);
};

void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (clockwise) {
#ifdef MOUSEKEY_ENABLE
tap_code(KC_MS_WH_DOWN);
Expand All @@ -144,6 +144,7 @@ void encoder_update(bool clockwise) {
tap_code(KC_PGUP);
#endif
}
return true;
}

bool music_mask_user(uint16_t keycode) {
Expand Down
35 changes: 18 additions & 17 deletions layouts/community/ortho_4x12/juno/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum planck_layers {
_RAISE,
_PLOVER,
_ADJUST,

_FN1,
_DPAD,
_DPADNUM
Expand All @@ -23,7 +23,7 @@ enum planck_keycodes {
PLOVER,
BACKLIT,
EXT_PLV,

DP_ON,
DP_OFF
};
Expand All @@ -49,7 +49,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
KC_ESC, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_BSPC,
KC_TAB, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT,
KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, RSFT_T(KC_ENT),
KC_APP, KC_LCTL, KC_LGUI, KC_LALT, LOWER, SPACEFN, SPACEFN, RAISE, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT
KC_APP, KC_LCTL, KC_LGUI, KC_LALT, LOWER, SPACEFN, SPACEFN, RAISE, KC_LEFT, KC_DOWN, KC_UP, KC_RIGHT
),

/* Colemak
Expand Down Expand Up @@ -218,10 +218,10 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
#ifdef AUDIO_ENABLE
float plover_song[][2] = SONG(PLOVER_SOUND);
float plover_gb_song[][2] = SONG(PLOVER_GOODBYE_SOUND);

// Borrowing audio from unused audio
// Caps Lock on and off sound too similar

float caps_song_on[][2] = SONG(NUM_LOCK_ON_SOUND);
float caps_song_off[][2] = SONG(SCROLL_LOCK_ON_SOUND);

Expand All @@ -236,7 +236,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {
}

#else

layer_state_t layer_state_set_user(layer_state_t state) {
// LED control, lighting up when Fn layer is activated
state = update_tri_layer_state(state, _LOWER, _RAISE, _ADJUST);
Expand All @@ -256,7 +256,7 @@ layer_state_t layer_state_set_user(layer_state_t state) {
return state;
}

#endif
#endif

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
switch (keycode) {
Expand Down Expand Up @@ -324,8 +324,8 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
}
return false;
break;


// Play audio upon switching Caps Lock and custom layers

case KC_CAPS:
Expand All @@ -339,26 +339,26 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
#endif
}
return true;

case DP_ON:
if (record->event.pressed) {

} else {
#ifdef AUDIO_ENABLE
PLAY_SONG(dpad_song_on);
#endif

layer_off(_FN1);
layer_on(_DPAD);
}

case DP_OFF:
if (record->event.pressed) {
#ifdef AUDIO_ENABLE
#ifdef AUDIO_ENABLE
PLAY_SONG(dpad_song_off);
#endif
layer_off(_DPAD);

layer_off(_DPAD);
}
}
return true;
Expand All @@ -370,7 +370,7 @@ uint16_t muse_counter = 0;
uint8_t muse_offset = 70;
uint16_t muse_tempo = 50;

void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (muse_mode) {
if (IS_LAYER_ON(_RAISE)) {
if (clockwise) {
Expand All @@ -392,6 +392,7 @@ void encoder_update(bool clockwise) {
tap_code(KC_PGUP);
}
}
return true;
}

void dip_update(uint8_t index, bool active) {
Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/junonum/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ uint16_t muse_counter = 0;
uint8_t muse_offset = 70;
uint16_t muse_tempo = 50;

void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (muse_mode) {
if (IS_LAYER_ON(_RAISE)) {
if (clockwise) {
Expand All @@ -290,6 +290,7 @@ void encoder_update(bool clockwise) {
tap_code(KC_PGUP);
}
}
return true;
}

void dip_update(uint8_t index, bool active) {
Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/mguterl/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ uint16_t muse_counter = 0;
uint8_t muse_offset = 70;
uint16_t muse_tempo = 50;

void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (muse_mode) {
if (IS_LAYER_ON(_RAISE)) {
if (clockwise) {
Expand Down Expand Up @@ -287,6 +287,7 @@ void encoder_update(bool clockwise) {
#endif
}
}
return true;
}

void dip_switch_update_user(uint8_t index, bool active) {
Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_4x12/mindsound/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
return true;
}

void encoder_update(bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
if (clockwise) {
register_code(KC_VOLU);
unregister_code(KC_VOLU);
} else {
register_code(KC_VOLD);
unregister_code(KC_VOLD);
}
return true;
}

// flicker implementation:
Expand Down
3 changes: 2 additions & 1 deletion layouts/community/ortho_5x12/brandonschlack/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ uint16_t muse_counter = 0;
uint8_t muse_offset = 70;
uint16_t muse_tempo = 50;

void encoder_update_keymap(uint8_t index, bool clockwise) {
bool encoder_update_keymap(uint8_t index, bool clockwise) {
if (muse_mode) {
if (IS_LAYER_ON(_RAISE)) {
if (clockwise) {
Expand Down Expand Up @@ -146,6 +146,7 @@ void encoder_update_keymap(uint8_t index, bool clockwise) {
#endif
}
}
return true;
}

void dip_switch_update_user(uint8_t index, bool active) {
Expand Down
10 changes: 5 additions & 5 deletions quantum/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static uint8_t thisHand, thatHand;
static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
#endif

__attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {}
__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) { return true; }

__attribute__((weak)) void encoder_update_kb(int8_t index, bool clockwise) { encoder_update_user(index, clockwise); }
__attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) { return encoder_update_user(index, clockwise); }

void encoder_init(void) {
#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
Expand Down Expand Up @@ -94,14 +94,14 @@ void encoder_init(void) {
#endif
}

static bool encoder_update(int8_t index, uint8_t state) {
static bool encoder_update(uint8_t index, uint8_t state) {
bool changed = false;
uint8_t i = index;

#ifdef ENCODER_RESOLUTIONS
int8_t resolution = encoder_resolutions[i];
uint8_t resolution = encoder_resolutions[i];
#else
int8_t resolution = ENCODER_RESOLUTION;
uint8_t resolution = ENCODER_RESOLUTION;
#endif

#ifdef SPLIT_KEYBOARD
Expand Down
4 changes: 2 additions & 2 deletions quantum/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
void encoder_init(void);
bool encoder_read(void);

void encoder_update_kb(int8_t index, bool clockwise);
void encoder_update_user(int8_t index, bool clockwise);
bool encoder_update_kb(uint8_t index, bool clockwise);
bool encoder_update_user(uint8_t index, bool clockwise);

#ifdef SPLIT_KEYBOARD
void encoder_state_raw(uint8_t* slave_state);
Expand Down
4 changes: 4 additions & 0 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ extern layer_state_t layer_state;
# include "usbpd.h"
#endif

#ifdef ENCODER_ENABLE
# include "encoder.h"
#endif

// For tri-layer
void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
Expand Down
3 changes: 2 additions & 1 deletion users/greatwizard/greatwizard.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
}

#ifdef ENCODER_ENABLE
void encoder_update_user(uint8_t index, bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
switch (get_highest_layer(layer_state)) {
case _QWERTY:
#ifdef LAYERS_PROGRAMMER
Expand All @@ -90,6 +90,7 @@ void encoder_update_user(uint8_t index, bool clockwise) {
}
break;
}
return true;
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion users/kuchosauronad0/encoder.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "encoder.h"
void encoder_update_user(uint8_t index, bool clockwise) {
bool encoder_update_user(uint8_t index, bool clockwise) {
static uint16_t kc;
uint8_t temp_mod = get_mods();
if (index == 0) { /* first encoder */
Expand Down Expand Up @@ -55,6 +55,7 @@ void encoder_update_user(uint8_t index, bool clockwise) {
tap_code(KC_1);
}
}
return true;
}
const uint16_t PROGMEM encoder_actions[][9] = { \
// None CTRL ALT SHIFT GUI CTRL+ALT CTRL+SHFT ALT+SHFT HYPER
Expand Down
2 changes: 1 addition & 1 deletion users/kuchosauronad0/encoder.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include "quantum.h"
const uint16_t PROGMEM encoder_actions[][9];
void encoder_update_user(uint8_t index, bool clockwise);
bool encoder_update_user(uint8_t index, bool clockwise);
Loading

0 comments on commit 1356fbf

Please sign in to comment.