diff options
-rw-r--r-- | quantum/encoder.c | 33 | ||||
-rw-r--r-- | quantum/encoder.h | 8 | ||||
-rw-r--r-- | quantum/split_common/matrix.c | 7 | ||||
-rw-r--r-- | quantum/split_common/transport.c | 77 |
4 files changed, 106 insertions, 19 deletions
diff --git a/quantum/encoder.c b/quantum/encoder.c index 6629a098b..ddf6234ab 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c | |||
@@ -17,6 +17,10 @@ | |||
17 | 17 | ||
18 | #include "encoder.h" | 18 | #include "encoder.h" |
19 | 19 | ||
20 | // for memcpy | ||
21 | #include <string.h> | ||
22 | |||
23 | |||
20 | #ifndef ENCODER_RESOLUTION | 24 | #ifndef ENCODER_RESOLUTION |
21 | #define ENCODER_RESOLUTION 4 | 25 | #define ENCODER_RESOLUTION 4 |
22 | #endif | 26 | #endif |
@@ -35,7 +39,13 @@ static pin_t encoders_pad_b[NUMBER_OF_ENCODERS] = ENCODERS_PAD_B; | |||
35 | static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; | 39 | static int8_t encoder_LUT[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 }; |
36 | 40 | ||
37 | static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; | 41 | static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; |
42 | |||
43 | #ifdef SPLIT_KEYBOARD | ||
44 | // slave half encoders come over as second set of encoders | ||
45 | static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0}; | ||
46 | #else | ||
38 | static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; | 47 | static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; |
48 | #endif | ||
39 | 49 | ||
40 | __attribute__ ((weak)) | 50 | __attribute__ ((weak)) |
41 | void encoder_update_user(int8_t index, bool clockwise) { } | 51 | void encoder_update_user(int8_t index, bool clockwise) { } |
@@ -60,11 +70,30 @@ void encoder_read(void) { | |||
60 | encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1); | 70 | 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]; | 71 | encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF]; |
62 | if (encoder_value[i] >= ENCODER_RESOLUTION) { | 72 | if (encoder_value[i] >= ENCODER_RESOLUTION) { |
63 | encoder_update_kb(i, COUNTRECLOCKWISE); | 73 | encoder_update_kb(i, false); |
64 | } | 74 | } |
65 | if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise | 75 | if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise |
66 | encoder_update_kb(i, CLOCKWISE); | 76 | encoder_update_kb(i, true); |
67 | } | 77 | } |
68 | encoder_value[i] %= ENCODER_RESOLUTION; | 78 | encoder_value[i] %= ENCODER_RESOLUTION; |
69 | } | 79 | } |
70 | } | 80 | } |
81 | |||
82 | #ifdef SPLIT_KEYBOARD | ||
83 | void encoder_state_raw(uint8_t* slave_state) { | ||
84 | memcpy(slave_state, encoder_state, sizeof(encoder_state)); | ||
85 | } | ||
86 | |||
87 | void encoder_update_raw(uint8_t* slave_state) { | ||
88 | for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { | ||
89 | encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF]; | ||
90 | if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) { | ||
91 | encoder_update_kb(NUMBER_OF_ENCODERS + i, false); | ||
92 | } | ||
93 | if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise | ||
94 | encoder_update_kb(NUMBER_OF_ENCODERS + i, true); | ||
95 | } | ||
96 | encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION; | ||
97 | } | ||
98 | } | ||
99 | #endif | ||
diff --git a/quantum/encoder.h b/quantum/encoder.h index 2024fa303..ec09a8cc4 100644 --- a/quantum/encoder.h +++ b/quantum/encoder.h | |||
@@ -19,11 +19,13 @@ | |||
19 | 19 | ||
20 | #include "quantum.h" | 20 | #include "quantum.h" |
21 | 21 | ||
22 | #define COUNTRECLOCKWISE 0 | ||
23 | #define CLOCKWISE 1 | ||
24 | |||
25 | void encoder_init(void); | 22 | void encoder_init(void); |
26 | void encoder_read(void); | 23 | void encoder_read(void); |
27 | 24 | ||
28 | void encoder_update_kb(int8_t index, bool clockwise); | 25 | void encoder_update_kb(int8_t index, bool clockwise); |
29 | void encoder_update_user(int8_t index, bool clockwise); | 26 | void encoder_update_user(int8_t index, bool clockwise); |
27 | |||
28 | #ifdef SPLIT_KEYBOARD | ||
29 | void encoder_state_raw(uint8_t* slave_state); | ||
30 | void encoder_update_raw(uint8_t* slave_state); | ||
31 | #endif | ||
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index dcb96254f..eb110bd23 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c | |||
@@ -29,6 +29,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
29 | #include "debounce.h" | 29 | #include "debounce.h" |
30 | #include "transport.h" | 30 | #include "transport.h" |
31 | 31 | ||
32 | #ifdef ENCODER_ENABLE | ||
33 | #include "encoder.h" | ||
34 | #endif | ||
35 | |||
32 | #if (MATRIX_COLS <= 8) | 36 | #if (MATRIX_COLS <= 8) |
33 | # define print_matrix_header() print("\nr/c 01234567\n") | 37 | # define print_matrix_header() print("\nr/c 01234567\n") |
34 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | 38 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) |
@@ -320,6 +324,9 @@ uint8_t matrix_scan(void) { | |||
320 | matrix_scan_quantum(); | 324 | matrix_scan_quantum(); |
321 | } else { | 325 | } else { |
322 | transport_slave(matrix + thisHand); | 326 | transport_slave(matrix + thisHand); |
327 | #ifdef ENCODER_ENABLE | ||
328 | encoder_read(); | ||
329 | #endif | ||
323 | matrix_slave_scan_user(); | 330 | matrix_slave_scan_user(); |
324 | } | 331 | } |
325 | 332 | ||
diff --git a/quantum/split_common/transport.c b/quantum/split_common/transport.c index 8d408f6fd..ab055ee65 100644 --- a/quantum/split_common/transport.c +++ b/quantum/split_common/transport.c | |||
@@ -1,4 +1,5 @@ | |||
1 | #include <string.h> | 1 | #include <string.h> |
2 | #include <stddef.h> | ||
2 | 3 | ||
3 | #include "config.h" | 4 | #include "config.h" |
4 | #include "matrix.h" | 5 | #include "matrix.h" |
@@ -15,15 +16,45 @@ | |||
15 | extern backlight_config_t backlight_config; | 16 | extern backlight_config_t backlight_config; |
16 | #endif | 17 | #endif |
17 | 18 | ||
19 | #ifdef ENCODER_ENABLE | ||
20 | # include "encoder.h" | ||
21 | #endif | ||
22 | |||
18 | #if defined(USE_I2C) || defined(EH) | 23 | #if defined(USE_I2C) || defined(EH) |
19 | 24 | ||
20 | # include "i2c_master.h" | 25 | # include "i2c_master.h" |
21 | # include "i2c_slave.h" | 26 | # include "i2c_slave.h" |
22 | 27 | ||
23 | # define I2C_BACKLIT_START 0x00 | 28 | typedef struct __attribute__ ((__packed__)) { |
24 | // Need 4 bytes for RGB (32 bit) | 29 | #ifdef BACKLIGHT_ENABLE |
25 | # define I2C_RGB_START 0x01 | 30 | uint8_t backlight_level; |
26 | # define I2C_KEYMAP_START 0x05 | 31 | #endif |
32 | #ifdef RGBLIGHT_ENABLE | ||
33 | uint32_t rgb_settings; | ||
34 | #endif | ||
35 | #ifdef ENCODER_ENABLE | ||
36 | uint8_t encoder_state[NUMBER_OF_ENCODERS]; | ||
37 | #endif | ||
38 | // Keep matrix last, we are only using this for it's offset | ||
39 | uint8_t matrix_start[0]; | ||
40 | } transport_values_t; | ||
41 | |||
42 | __attribute__ ((unused)) | ||
43 | static transport_values_t transport_values; | ||
44 | |||
45 | #ifdef BACKLIGHT_ENABLE | ||
46 | # define I2C_BACKLIT_START (uint8_t)offsetof(transport_values_t, backlight_level) | ||
47 | #endif | ||
48 | |||
49 | #ifdef RGBLIGHT_ENABLE | ||
50 | # define I2C_RGB_START (uint8_t)offsetof(transport_values_t, rgb_settings) | ||
51 | #endif | ||
52 | |||
53 | #ifdef ENCODER_ENABLE | ||
54 | # define I2C_ENCODER_START (uint8_t)offsetof(transport_values_t, encoder_state) | ||
55 | #endif | ||
56 | |||
57 | #define I2C_KEYMAP_START (uint8_t)offsetof(transport_values_t, matrix_start) | ||
27 | 58 | ||
28 | # define TIMEOUT 100 | 59 | # define TIMEOUT 100 |
29 | 60 | ||
@@ -37,25 +68,28 @@ bool transport_master(matrix_row_t matrix[]) { | |||
37 | 68 | ||
38 | // write backlight info | 69 | // write backlight info |
39 | # ifdef BACKLIGHT_ENABLE | 70 | # ifdef BACKLIGHT_ENABLE |
40 | static uint8_t prev_level = ~0; | 71 | uint8_t level = get_backlight_level(); |
41 | uint8_t level = get_backlight_level(); | 72 | if (level != transport_values.backlight_level) { |
42 | if (level != prev_level) { | ||
43 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) { | 73 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_BACKLIT_START, (void *)&level, sizeof(level), TIMEOUT) >= 0) { |
44 | prev_level = level; | 74 | transport_values.backlight_level = level; |
45 | } | 75 | } |
46 | } | 76 | } |
47 | # endif | 77 | # endif |
48 | 78 | ||
49 | # ifdef RGBLIGHT_ENABLE | 79 | # ifdef RGBLIGHT_ENABLE |
50 | static uint32_t prev_rgb = ~0; | 80 | uint32_t rgb = rgblight_read_dword(); |
51 | uint32_t rgb = rgblight_read_dword(); | 81 | if (rgb != transport_values.rgb_settings) { |
52 | if (rgb != prev_rgb) { | ||
53 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START, (void *)&rgb, sizeof(rgb), TIMEOUT) >= 0) { | 82 | if (i2c_writeReg(SLAVE_I2C_ADDRESS, I2C_RGB_START, (void *)&rgb, sizeof(rgb), TIMEOUT) >= 0) { |
54 | prev_rgb = rgb; | 83 | transport_values.rgb_settings = rgb; |
55 | } | 84 | } |
56 | } | 85 | } |
57 | # endif | 86 | # endif |
58 | 87 | ||
88 | # ifdef ENCODER_ENABLE | ||
89 | i2c_readReg(SLAVE_I2C_ADDRESS, I2C_ENCODER_START, (void *)transport_values.encoder_state, sizeof(transport_values.encoder_state), TIMEOUT); | ||
90 | encoder_update_raw(&transport_values.encoder_state[0]); | ||
91 | # endif | ||
92 | |||
59 | return true; | 93 | return true; |
60 | } | 94 | } |
61 | 95 | ||
@@ -73,6 +107,10 @@ void transport_slave(matrix_row_t matrix[]) { | |||
73 | // Update the RGB with the new data | 107 | // Update the RGB with the new data |
74 | rgblight_update_dword(rgb); | 108 | rgblight_update_dword(rgb); |
75 | # endif | 109 | # endif |
110 | |||
111 | # ifdef ENCODER_ENABLE | ||
112 | encoder_state_raw((uint8_t*)(i2c_slave_reg + I2C_ENCODER_START)); | ||
113 | # endif | ||
76 | } | 114 | } |
77 | 115 | ||
78 | void transport_master_init(void) { i2c_init(); } | 116 | void transport_master_init(void) { i2c_init(); } |
@@ -83,12 +121,15 @@ void transport_slave_init(void) { i2c_slave_init(SLAVE_I2C_ADDRESS); } | |||
83 | 121 | ||
84 | # include "serial.h" | 122 | # include "serial.h" |
85 | 123 | ||
86 | typedef struct _Serial_s2m_buffer_t { | 124 | typedef struct __attribute__ ((__packed__)) { |
125 | # ifdef ENCODER_ENABLE | ||
126 | uint8_t encoder_state[NUMBER_OF_ENCODERS]; | ||
127 | # endif | ||
87 | // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack | 128 | // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack |
88 | matrix_row_t smatrix[ROWS_PER_HAND]; | 129 | matrix_row_t smatrix[ROWS_PER_HAND]; |
89 | } Serial_s2m_buffer_t; | 130 | } Serial_s2m_buffer_t; |
90 | 131 | ||
91 | typedef struct _Serial_m2s_buffer_t { | 132 | typedef struct __attribute__ ((__packed__)) { |
92 | # ifdef BACKLIGHT_ENABLE | 133 | # ifdef BACKLIGHT_ENABLE |
93 | uint8_t backlight_level; | 134 | uint8_t backlight_level; |
94 | # endif | 135 | # endif |
@@ -147,6 +188,10 @@ bool transport_master(matrix_row_t matrix[]) { | |||
147 | } | 188 | } |
148 | # endif | 189 | # endif |
149 | 190 | ||
191 | # ifdef ENCODER_ENABLE | ||
192 | encoder_update_raw((uint8_t*)&serial_s2m_buffer.encoder_state); | ||
193 | # endif | ||
194 | |||
150 | return true; | 195 | return true; |
151 | } | 196 | } |
152 | 197 | ||
@@ -162,6 +207,10 @@ void transport_slave(matrix_row_t matrix[]) { | |||
162 | // Update RGB config with the new data | 207 | // Update RGB config with the new data |
163 | rgblight_update_dword(serial_m2s_buffer.rgblight_config.raw); | 208 | rgblight_update_dword(serial_m2s_buffer.rgblight_config.raw); |
164 | # endif | 209 | # endif |
210 | |||
211 | # ifdef ENCODER_ENABLE | ||
212 | encoder_state_raw((uint8_t*)&serial_s2m_buffer.encoder_state); | ||
213 | # endif | ||
165 | } | 214 | } |
166 | 215 | ||
167 | #endif | 216 | #endif |