Skip to content

Commit

Permalink
Move queue drain to main thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzarc committed Mar 8, 2024
1 parent c962a18 commit d4eda06
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
26 changes: 15 additions & 11 deletions quantum/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "action.h"
#include "encoder.h"
#include "wait.h"
#include "debug.h"

#ifndef ENCODER_MAP_KEY_DELAY
# define ENCODER_MAP_KEY_DELAY TAP_CODE_DELAY
Expand All @@ -15,12 +16,19 @@ __attribute__((weak)) bool should_process_encoder(void) {
}

static encoder_events_t encoder_events;
static bool signal_queue_drain = false;

void encoder_init(void) {
memset(&encoder_events, 0, sizeof(encoder_events));
encoder_driver_init();
}

static void encoder_queue_drain(void) {
// dprintf("Draining encoder queue\n");
encoder_events.tail = encoder_events.head;
encoder_events.dequeued = encoder_events.enqueued;
}

static bool encoder_handle_queue(void) {
bool changed = false;
uint8_t index;
Expand Down Expand Up @@ -60,6 +68,11 @@ bool encoder_task(void) {
}
#endif // SPLIT_KEYBOARD

if (signal_queue_drain) {
signal_queue_drain = false;
encoder_queue_drain();
}

// Let the encoder driver produce events
encoder_driver_task();

Expand Down Expand Up @@ -137,18 +150,9 @@ void encoder_retrieve_events(encoder_events_t *events) {
memcpy(events, &encoder_events, sizeof(encoder_events));
}

#ifdef SPLIT_KEYBOARD
void encoder_queue_drain(void) {
uint8_t index;
bool clockwise;

// dprintf("Draining encoder queue\n");

while (encoder_events.dequeued != encoder_events.enqueued) {
encoder_dequeue_event(&index, &clockwise);
}
void encoder_signal_queue_drain(void) {
signal_queue_drain = true;
}
#endif // SPLIT_KEYBOARD

__attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
return true;
Expand Down
9 changes: 4 additions & 5 deletions quantum/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ typedef struct encoder_event_t {
} encoder_event_t;

typedef struct encoder_events_t {
uint16_t enqueued;
uint16_t dequeued;
uint8_t enqueued;
uint8_t dequeued;
uint8_t head;
uint8_t tail;
encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
Expand All @@ -97,9 +97,8 @@ void encoder_retrieve_events(encoder_events_t *events);
bool encoder_queue_event_advanced(encoder_events_t *events, uint8_t index, bool clockwise);
bool encoder_dequeue_event_advanced(encoder_events_t *events, uint8_t *index, bool *clockwise);

# ifdef SPLIT_KEYBOARD
void encoder_queue_drain(void);
# endif // SPLIT_KEYBOARD
// Reset the queue to be empty
void encoder_signal_queue_drain(void);

# ifdef ENCODER_MAP_ENABLE
# define NUM_DIRECTIONS 2
Expand Down
27 changes: 16 additions & 11 deletions quantum/split_common/transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include <string.h>
#include <stddef.h>

Expand Down Expand Up @@ -238,21 +239,25 @@ static void master_matrix_handlers_slave(matrix_row_t master_matrix[], matrix_ro
#ifdef ENCODER_ENABLE

static bool encoder_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
static uint32_t last_update = 0;
static uint32_t last_update = 0;
static uint8_t last_checksum = 0;
encoder_events_t temp_events;

bool okay = read_if_checksum_mismatch(GET_ENCODERS_CHECKSUM, GET_ENCODERS_DATA, &last_update, &temp_events, &split_shmem->encoders.events, sizeof(temp_events));
if (okay) {
bool actioned = false;
uint8_t index;
bool clockwise;
while (okay && encoder_dequeue_event_advanced(&split_shmem->encoders.events, &index, &clockwise)) {
okay &= encoder_queue_event(index, clockwise);
actioned = true;
}
if (last_checksum != split_shmem->encoders.checksum) {
bool actioned = false;
uint8_t index;
bool clockwise;
while (okay && encoder_dequeue_event_advanced(&split_shmem->encoders.events, &index, &clockwise)) {
okay &= encoder_queue_event(index, clockwise);
actioned = true;
}

if (actioned) {
transport_exec(CMD_ENCODER_DRAIN);
if (actioned) {
okay &= transport_exec(CMD_ENCODER_DRAIN);
}
last_checksum = split_shmem->encoders.checksum;
}
}
return okay;
Expand All @@ -266,7 +271,7 @@ static void encoder_handlers_slave(matrix_row_t master_matrix[], matrix_row_t sl
}

static void encoder_handlers_slave_drain(uint8_t initiator2target_buffer_size, const void *initiator2target_buffer, uint8_t target2initiator_buffer_size, void *target2initiator_buffer) {
encoder_queue_drain();
encoder_signal_queue_drain();
}

// clang-format off
Expand Down

0 comments on commit d4eda06

Please sign in to comment.