encoder.h
3875 bytes
1/*
2 * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <stdint.h>
21#include <stdbool.h>
22#include "gpio.h"
23#include "util.h"
24
25// ======== DEPRECATED DEFINES - DO NOT USE ========
26#ifdef ENCODERS_PAD_A
27# define ENCODER_A_PINS ENCODERS_PAD_A
28#endif
29#ifdef ENCODERS_PAD_B
30# define ENCODER_B_PINS ENCODERS_PAD_B
31#endif
32#ifdef ENCODERS_PAD_A_RIGHT
33# define ENCODER_A_PINS_RIGHT ENCODERS_PAD_A_RIGHT
34#endif
35#ifdef ENCODERS_PAD_B_RIGHT
36# define ENCODER_B_PINS_RIGHT ENCODERS_PAD_B_RIGHT
37#endif
38// ========
39
40#ifdef ENCODER_ENABLE
41
42__attribute__((weak)) bool should_process_encoder(void);
43
44void encoder_init(void);
45bool encoder_task(void);
46bool encoder_queue_event(uint8_t index, bool clockwise);
47bool encoder_dequeue_event(uint8_t *index, bool *clockwise);
48
49bool encoder_update_kb(uint8_t index, bool clockwise);
50bool encoder_update_user(uint8_t index, bool clockwise);
51
52# ifdef SPLIT_KEYBOARD
53
54# if defined(ENCODER_A_PINS_RIGHT)
55# ifndef NUM_ENCODERS_LEFT
56# define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODER_A_PINS))
57# endif
58# ifndef NUM_ENCODERS_RIGHT
59# define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODER_A_PINS_RIGHT))
60# endif
61# else
62# ifndef NUM_ENCODERS_LEFT
63# define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODER_A_PINS))
64# endif
65# ifndef NUM_ENCODERS_RIGHT
66# define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
67# endif
68# endif
69# ifndef NUM_ENCODERS
70# define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
71# endif
72
73# else // SPLIT_KEYBOARD
74
75# ifndef NUM_ENCODERS
76# define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODER_A_PINS))
77# endif
78# define NUM_ENCODERS_LEFT NUM_ENCODERS
79# define NUM_ENCODERS_RIGHT 0
80
81# endif // SPLIT_KEYBOARD
82
83# define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)
84
85# ifndef MAX_QUEUED_ENCODER_EVENTS
86# define MAX_QUEUED_ENCODER_EVENTS MAX(4, ((NUM_ENCODERS_MAX_PER_SIDE) + 1))
87# endif // MAX_QUEUED_ENCODER_EVENTS
88
89typedef struct encoder_event_t {
90 uint8_t index : 7;
91 uint8_t clockwise : 1;
92} encoder_event_t;
93
94typedef struct encoder_events_t {
95 uint8_t enqueued;
96 uint8_t dequeued;
97 uint8_t head;
98 uint8_t tail;
99 encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
100} encoder_events_t;
101
102// Get the current queued events
103void encoder_retrieve_events(encoder_events_t *events);
104
105// Encoder event queue management
106bool encoder_queue_event_advanced(encoder_events_t *events, uint8_t index, bool clockwise);
107bool encoder_dequeue_event_advanced(encoder_events_t *events, uint8_t *index, bool *clockwise);
108
109// Reset the queue to be empty
110void encoder_signal_queue_drain(void);
111
112# ifdef ENCODER_MAP_ENABLE
113# define NUM_DIRECTIONS 2
114# define ENCODER_CCW_CW(ccw, cw) \
115 { (cw), (ccw) }
116extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS];
117# endif // ENCODER_MAP_ENABLE
118
119// "Custom encoder lite" support
120void encoder_driver_init(void);
121void encoder_driver_task(void);
122
123#endif // ENCODER_ENABLE