summaryrefslogtreecommitdiff
path: root/quantum/encoder.h
blob: 317a91f1da5438be9fa62f785d03f3f86b21c0c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <stdint.h>
#include <stdbool.h>
#include "gpio.h"
#include "util.h"

#ifdef ENCODER_ENABLE

__attribute__((weak)) bool should_process_encoder(void);

void encoder_init(void);
bool encoder_task(void);
bool encoder_queue_event(uint8_t index, bool clockwise);
bool encoder_dequeue_event(uint8_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

#        if defined(ENCODERS_PAD_A_RIGHT)
#            ifndef NUM_ENCODERS_LEFT
#                define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
#            endif
#            ifndef NUM_ENCODERS_RIGHT
#                define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A_RIGHT))
#            endif
#        else
#            ifndef NUM_ENCODERS_LEFT
#                define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
#            endif
#            ifndef NUM_ENCODERS_RIGHT
#                define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
#            endif
#        endif
#        ifndef NUM_ENCODERS
#            define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
#        endif

#    else // SPLIT_KEYBOARD

#        ifndef NUM_ENCODERS
#            define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
#        endif
#        define NUM_ENCODERS_LEFT NUM_ENCODERS
#        define NUM_ENCODERS_RIGHT 0

#    endif // SPLIT_KEYBOARD

#    ifndef NUM_ENCODERS
#        define NUM_ENCODERS 0
#        define NUM_ENCODERS_LEFT 0
#        define NUM_ENCODERS_RIGHT 0
#    endif // NUM_ENCODERS

#    define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)

#    ifndef MAX_QUEUED_ENCODER_EVENTS
#        define MAX_QUEUED_ENCODER_EVENTS MAX(4, ((NUM_ENCODERS_MAX_PER_SIDE) + 1))
#    endif // MAX_QUEUED_ENCODER_EVENTS

typedef struct encoder_event_t {
    uint8_t index : 7;
    uint8_t clockwise : 1;
} encoder_event_t;

typedef struct encoder_events_t {
    uint8_t         enqueued;
    uint8_t         dequeued;
    uint8_t         head;
    uint8_t         tail;
    encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
} encoder_events_t;

// Get the current queued events
void encoder_retrieve_events(encoder_events_t *events);

// Encoder event queue management
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);

// Reset the queue to be empty
void encoder_signal_queue_drain(void);

#    ifdef ENCODER_MAP_ENABLE
#        define NUM_DIRECTIONS 2
#        define ENCODER_CCW_CW(ccw, cw) \
            { (cw), (ccw) }
extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS];
#    endif // ENCODER_MAP_ENABLE

// "Custom encoder lite" support
void encoder_driver_init(void);
void encoder_driver_task(void);

#endif // ENCODER_ENABLE