aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2018-10-26 16:19:23 -0400
committerDrashna Jaelre <drashna@live.com>2018-10-26 14:24:13 -0700
commit85688e5b52112c86895171d3dc8b26610480e932 (patch)
tree03f3f7bf585ebdf158576aeb6a8cf0c8c5319d6f /quantum
parente22c3992454ade93ccaef70ab077e79a4b2a5ef0 (diff)
downloadqmk_firmware-85688e5b52112c86895171d3dc8b26610480e932.tar.gz
qmk_firmware-85688e5b52112c86895171d3dc8b26610480e932.zip
add support for encoders to core
Diffstat (limited to 'quantum')
-rw-r--r--quantum/encoder.c70
-rw-r--r--quantum/encoder.h29
-rw-r--r--quantum/quantum.c12
3 files changed, 111 insertions, 0 deletions
diff --git a/quantum/encoder.c b/quantum/encoder.c
new file mode 100644
index 000000000..6629a098b
--- /dev/null
+++ b/quantum/encoder.c
@@ -0,0 +1,70 @@
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#include "encoder.h"
19
20#ifndef ENCODER_RESOLUTION
21 #define ENCODER_RESOLUTION 4
22#endif
23
24#ifndef NUMBER_OF_ENCODERS
25 #error "Number of encoders not defined by NUMBER_OF_ENCODERS"
26#endif
27
28#if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
29 #error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
30#endif
31
32static pin_t encoders_pad_a[NUMBER_OF_ENCODERS] = ENCODERS_PAD_A;
33static pin_t encoders_pad_b[NUMBER_OF_ENCODERS] = ENCODERS_PAD_B;
34
35static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
36
37static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
38static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
39
40__attribute__ ((weak))
41void encoder_update_user(int8_t index, bool clockwise) { }
42
43__attribute__ ((weak))
44void encoder_update_kb(int8_t index, bool clockwise) {
45 encoder_update_user(index, clockwise);
46}
47
48void encoder_init(void) {
49 for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
50 setPinInputHigh(encoders_pad_a[i]);
51 setPinInputHigh(encoders_pad_b[i]);
52
53 encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
54 }
55}
56
57void encoder_read(void) {
58 for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
59 encoder_state[i] <<= 2;
60 encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
61 encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF];
62 if (encoder_value[i] >= ENCODER_RESOLUTION) {
63 encoder_update_kb(i, COUNTRECLOCKWISE);
64 }
65 if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
66 encoder_update_kb(i, CLOCKWISE);
67 }
68 encoder_value[i] %= ENCODER_RESOLUTION;
69 }
70}
diff --git a/quantum/encoder.h b/quantum/encoder.h
new file mode 100644
index 000000000..2024fa303
--- /dev/null
+++ b/quantum/encoder.h
@@ -0,0 +1,29 @@
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 "quantum.h"
21
22#define COUNTRECLOCKWISE 0
23#define CLOCKWISE 1
24
25void encoder_init(void);
26void encoder_read(void);
27
28void encoder_update_kb(int8_t index, bool clockwise);
29void encoder_update_user(int8_t index, bool clockwise);
diff --git a/quantum/quantum.c b/quantum/quantum.c
index eed59f811..c9bec6740 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -42,6 +42,11 @@ extern backlight_config_t backlight_config;
42#include "process_midi.h" 42#include "process_midi.h"
43#endif 43#endif
44 44
45
46#ifdef ENCODER_ENABLE
47#include "encoder.h"
48#endif
49
45#ifdef AUDIO_ENABLE 50#ifdef AUDIO_ENABLE
46 #ifndef GOODBYE_SONG 51 #ifndef GOODBYE_SONG
47 #define GOODBYE_SONG SONG(GOODBYE_SOUND) 52 #define GOODBYE_SONG SONG(GOODBYE_SOUND)
@@ -957,6 +962,9 @@ void matrix_init_quantum() {
957 #ifdef RGB_MATRIX_ENABLE 962 #ifdef RGB_MATRIX_ENABLE
958 rgb_matrix_init(); 963 rgb_matrix_init();
959 #endif 964 #endif
965 #ifdef ENCODER_ENABLE
966 encoder_init();
967 #endif
960 matrix_init_kb(); 968 matrix_init_kb();
961} 969}
962 970
@@ -991,6 +999,10 @@ void matrix_scan_quantum() {
991 rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % (RGB_MATRIX_SKIP_FRAMES + 1)); 999 rgb_matrix_task_counter = ((rgb_matrix_task_counter + 1) % (RGB_MATRIX_SKIP_FRAMES + 1));
992 #endif 1000 #endif
993 1001
1002 #ifdef ENCODER_ENABLE
1003 encoder_read();
1004 #endif
1005
994 matrix_scan_kb(); 1006 matrix_scan_kb();
995} 1007}
996#if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN) 1008#if defined(BACKLIGHT_ENABLE) && defined(BACKLIGHT_PIN)