encoder.h (3859B)
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 44 void encoder_init(void); 45 bool encoder_task(void); 46 bool encoder_queue_event(uint8_t index, bool clockwise); 47 bool encoder_dequeue_event(uint8_t *index, bool *clockwise); 48 49 bool encoder_update_kb(uint8_t index, bool clockwise); 50 bool 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 89 typedef struct encoder_event_t { 90 uint8_t index : 7; 91 uint8_t clockwise : 1; 92 } encoder_event_t; 93 94 typedef 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 103 void encoder_retrieve_events(encoder_events_t *events); 104 105 // Encoder event queue management 106 bool encoder_queue_event_advanced(encoder_events_t *events, uint8_t index, bool clockwise); 107 bool encoder_dequeue_event_advanced(encoder_events_t *events, uint8_t *index, bool *clockwise); 108 109 // Reset the queue to be empty 110 void encoder_signal_queue_drain(void); 111 112 # ifdef ENCODER_MAP_ENABLE 113 # define NUM_DIRECTIONS 2 114 # define ENCODER_CCW_CW(ccw, cw) {(cw), (ccw)} 115 extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS]; 116 # endif // ENCODER_MAP_ENABLE 117 118 // "Custom encoder lite" support 119 void encoder_driver_init(void); 120 void encoder_driver_task(void); 121 122 #endif // ENCODER_ENABLE