aboutsummaryrefslogtreecommitdiff
path: root/keyboards/handwired/xealous
diff options
context:
space:
mode:
authorAlex Ong <the.onga@gmail.com>2019-04-05 15:44:33 +1100
committerDrashna Jaelre <drashna@live.com>2019-04-04 21:44:33 -0700
commitdad66cad40131bbc5b7c85bdf107c664a721ba07 (patch)
treed5088e28ecab6d9222f31ce82cf99d25f276944c /keyboards/handwired/xealous
parent7e5c107d43890eb517184d6fddff9a6da165fc3d (diff)
downloadqmk_firmware-dad66cad40131bbc5b7c85bdf107c664a721ba07.tar.gz
qmk_firmware-dad66cad40131bbc5b7c85bdf107c664a721ba07.zip
[Keyboard] Simplified handwired/xealous since most of the features are in core now. (#5556)
Diffstat (limited to 'keyboards/handwired/xealous')
-rw-r--r--keyboards/handwired/xealous/config.h12
-rw-r--r--keyboards/handwired/xealous/debounce.c63
-rw-r--r--keyboards/handwired/xealous/matrix.c329
-rw-r--r--keyboards/handwired/xealous/rules.mk6
4 files changed, 12 insertions, 398 deletions
diff --git a/keyboards/handwired/xealous/config.h b/keyboards/handwired/xealous/config.h
index 7bce502c1..251e85d4c 100644
--- a/keyboards/handwired/xealous/config.h
+++ b/keyboards/handwired/xealous/config.h
@@ -20,20 +20,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
20#include "config_common.h" 20#include "config_common.h"
21 21
22/* Use I2C or Serial, not both */ 22/* Use I2C or Serial, not both */
23
24#define USE_I2C 23#define USE_I2C
25#define SCL_CLOCK 400000UL 24#define SCL_CLOCK 800000UL
26
27// #define USE_SERIAL
28/* serial.c configuration for split keyboard */
29// #define SOFT_SERIAL_PIN D0
30 25
31/* Select hand configuration */ 26/* Select hand configuration */
32
33#define MASTER_LEFT 27#define MASTER_LEFT
34// #define MASTER_RIGHT
35// #define EE_HANDS
36
37 28
38//#define DEBUG_MATRIX_SCAN_RATE //Use this to determine scan-rate. 29//#define DEBUG_MATRIX_SCAN_RATE //Use this to determine scan-rate.
39#define FORCE_NKRO 30#define FORCE_NKRO
@@ -41,6 +32,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
41#define QMK_KEYS_PER_SCAN 4 //if we press four keys simultaneously, lets process them simultaneously... 32#define QMK_KEYS_PER_SCAN 4 //if we press four keys simultaneously, lets process them simultaneously...
42#define DIODE_DIRECTION COL2ROW 33#define DIODE_DIRECTION COL2ROW
43 34
35
44#ifdef AUDIO_ENABLE 36#ifdef AUDIO_ENABLE
45 #define C6_AUDIO 37 #define C6_AUDIO
46 #define STARTUP_SONG SONG(STARTUP_SOUND) 38 #define STARTUP_SONG SONG(STARTUP_SOUND)
diff --git a/keyboards/handwired/xealous/debounce.c b/keyboards/handwired/xealous/debounce.c
deleted file mode 100644
index 65a99f27f..000000000
--- a/keyboards/handwired/xealous/debounce.c
+++ /dev/null
@@ -1,63 +0,0 @@
1#include <string.h>
2#include "config.h"
3#include "matrix.h"
4#include "timer.h"
5#include "quantum.h"
6
7#ifndef DEBOUNCING_DELAY
8# define DEBOUNCING_DELAY 5
9#endif
10
11//Debouncing counters
12typedef uint8_t debounce_counter_t;
13#define DEBOUNCE_COUNTER_MODULO 250
14#define DEBOUNCE_COUNTER_INACTIVE 251
15
16static debounce_counter_t *debounce_counters;
17
18void debounce_init(uint8_t num_rows)
19{
20 debounce_counters = malloc(num_rows*MATRIX_COLS);
21 memset(debounce_counters, DEBOUNCE_COUNTER_INACTIVE, num_rows*MATRIX_COLS);
22}
23
24void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
25{
26 for (uint8_t row = 0; row < num_rows; row++)
27 {
28 for (uint8_t col = 0; col < MATRIX_COLS; col++)
29 {
30 if (debounce_counters[row*MATRIX_COLS + col] != DEBOUNCE_COUNTER_INACTIVE)
31 {
32 if (TIMER_DIFF(current_time, debounce_counters[row*MATRIX_COLS + col], DEBOUNCE_COUNTER_MODULO) >= DEBOUNCING_DELAY) {
33 debounce_counters[row*MATRIX_COLS + col] = DEBOUNCE_COUNTER_INACTIVE;
34 }
35 }
36 }
37 }
38}
39
40void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time)
41{
42 for (uint8_t row = 0; row < num_rows; row++)
43 {
44 matrix_row_t delta = raw[row] ^ cooked[row];
45
46 for (uint8_t col = 0; col < MATRIX_COLS; col++)
47 {
48 if (debounce_counters[row*MATRIX_COLS + col] == DEBOUNCE_COUNTER_INACTIVE && (delta & (1<<col)))
49 {
50 debounce_counters[row*MATRIX_COLS + col] = current_time;
51 cooked[row] ^= (1 << col);
52 }
53 }
54 }
55}
56
57void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed)
58{
59 uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
60
61 update_debounce_counters(num_rows, current_time);
62 transfer_matrix_values(raw, cooked, num_rows, current_time);
63} \ No newline at end of file
diff --git a/keyboards/handwired/xealous/matrix.c b/keyboards/handwired/xealous/matrix.c
index 27fad0008..e2d219939 100644
--- a/keyboards/handwired/xealous/matrix.c
+++ b/keyboards/handwired/xealous/matrix.c
@@ -34,311 +34,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "matrix_scanrate.h" 34 #include "matrix_scanrate.h"
35#endif 35#endif
36 36
37 37void matrix_scan_user(void)
38
39#ifdef USE_I2C
40# include "i2c.h"
41#else // USE_SERIAL
42# error "only i2c supported"
43#endif
44
45#ifndef DEBOUNCING_DELAY
46# define DEBOUNCING_DELAY 5
47#endif
48
49
50#if (MATRIX_COLS <= 8)
51# define print_matrix_header() print("\nr/c 01234567\n")
52# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
53# define matrix_bitpop(i) bitpop(matrix[i])
54# define ROW_SHIFTER ((uint8_t)1)
55#else
56# error "Currently only supports 8 COLS"
57#endif
58
59#define ERROR_DISCONNECT_COUNT 5
60
61#define ROWS_PER_HAND (MATRIX_ROWS/2)
62
63static uint8_t error_count = 0;
64
65static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
66static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
67
68/* matrix state(1:on, 0:off) */
69static matrix_row_t matrix[MATRIX_ROWS];
70static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71static matrix_row_t* debouncing_matrix_hand_offsetted; //pointer to matrix_debouncing for our hand
72static matrix_row_t* matrix_hand_offsetted; // pointer to matrix for our hand
73
74//Debouncing counters
75typedef uint8_t debounce_counter_t;
76#define DEBOUNCE_COUNTER_MODULO 250
77#define DEBOUNCE_COUNTER_INACTIVE 251
78static debounce_counter_t debounce_counters[MATRIX_ROWS * MATRIX_COLS];
79static debounce_counter_t *debounce_counters_hand_offsetted;
80
81
82#if (DIODE_DIRECTION == ROW2COL)
83 error "Only Col2Row supported";
84#endif
85static void init_cols(void);
86static void unselect_rows(void);
87static void select_row(uint8_t row);
88static void unselect_row(uint8_t row);
89static matrix_row_t optimized_col_reader(void);
90
91__attribute__ ((weak))
92void matrix_init_kb(void) {
93 matrix_init_user();
94}
95
96__attribute__ ((weak))
97void matrix_scan_kb(void) {
98 matrix_scan_user();
99}
100
101__attribute__ ((weak))
102void matrix_init_user(void) {
103}
104
105__attribute__ ((weak))
106void matrix_scan_user(void) {
107}
108
109__attribute__ ((weak))
110void matrix_slave_scan_user(void) {
111}
112
113inline
114uint8_t matrix_rows(void)
115{
116 return MATRIX_ROWS;
117}
118
119inline
120uint8_t matrix_cols(void)
121{
122 return MATRIX_COLS;
123}
124
125void matrix_init(void)
126{
127#ifdef DISABLE_JTAG
128 // JTAG disable for PORT F. write JTD bit twice within four cycles.
129 MCUCR |= (1<<JTD);
130 MCUCR |= (1<<JTD);
131#endif
132
133 debug_enable = true;
134 debug_matrix = false;
135 debug_mouse = false;
136 // initialize row and col
137 unselect_rows();
138 init_cols();
139
140 TX_RX_LED_INIT;
141
142 // initialize matrix state: all keys off
143 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
144 matrix[i] = 0;
145 matrix_debouncing[i] = 0;
146 }
147 int my_hand_offset = isLeftHand ? 0 : (ROWS_PER_HAND);
148 debouncing_matrix_hand_offsetted = matrix_debouncing + my_hand_offset;
149 matrix_hand_offsetted = matrix + my_hand_offset;
150 debounce_counters_hand_offsetted = debounce_counters + my_hand_offset;
151
152 for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS; i++) {
153 debounce_counters[i] = DEBOUNCE_COUNTER_INACTIVE;
154 }
155
156 matrix_init_quantum();
157
158}
159
160//#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a))
161void update_debounce_counters(uint8_t current_time)
162{
163 debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
164 for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
165 {
166 for (uint8_t col = 0; col < MATRIX_COLS; col++)
167 {
168 if (*debounce_pointer != DEBOUNCE_COUNTER_INACTIVE)
169 {
170 if (TIMER_DIFF(current_time, *debounce_pointer, DEBOUNCE_COUNTER_MODULO) >=
171 DEBOUNCING_DELAY) {
172 *debounce_pointer = DEBOUNCE_COUNTER_INACTIVE;
173 }
174 }
175 debounce_pointer++;
176 }
177 }
178}
179
180void transfer_matrix_values(uint8_t current_time)
181{
182 //upload from debounce_matrix to final matrix;
183 debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
184 for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
185 {
186 matrix_row_t row_value = matrix_hand_offsetted[row];
187 matrix_row_t debounce_value = debouncing_matrix_hand_offsetted[row];
188
189 for (uint8_t col = 0; col < MATRIX_COLS; col++)
190 {
191 bool final_value = debounce_value & (1 << col);
192 bool current_value = row_value & (1 << col);
193 if (*debounce_pointer == DEBOUNCE_COUNTER_INACTIVE
194 && (current_value != final_value))
195 {
196 *debounce_pointer = current_time;
197 row_value ^= (1 << col);
198 }
199 debounce_pointer++;
200 }
201 matrix_hand_offsetted[row] = row_value;
202 }
203}
204
205uint8_t _matrix_scan(void)
206{
207 uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
208
209 // Set row, read cols
210 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
211 select_row(current_row);
212 asm volatile ("nop"); asm volatile("nop");
213
214 debouncing_matrix_hand_offsetted[current_row] = optimized_col_reader();
215 // Unselect row
216 unselect_row(current_row);
217 }
218
219 update_debounce_counters(current_time);
220 transfer_matrix_values(current_time);
221
222 return 1;
223}
224
225
226// Get rows from other half over i2c
227int i2c_transaction(void) {
228 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
229
230 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
231 if (err) goto i2c_error;
232
233 // start of matrix stored at 0x00
234 err = i2c_master_write(I2C_KEYMAP_START);
235 if (err) goto i2c_error;
236
237 // Start read
238 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
239 if (err) goto i2c_error;
240
241 if (!err) {
242 int i;
243 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
244 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
245 }
246 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
247 i2c_master_stop();
248 } else {
249i2c_error: // the cable is disconnceted, or something else went wrong
250 i2c_reset_state();
251 return err;
252 }
253
254 return 0;
255}
256
257uint8_t matrix_scan(void)
258{ 38{
259#ifdef DEBUG_MATRIX_SCAN_RATE 39#ifdef DEBUG_MATRIX_SCAN_RATE
260 matrix_check_scan_rate(); 40 matrix_check_scan_rate();
261 matrix_time_between_scans(); 41 matrix_time_between_scans();
262#endif 42#endif
263 uint8_t ret = _matrix_scan(); 43
264
265 if( i2c_transaction() ) {
266 // turn on the indicator led when halves are disconnected
267 TXLED1;
268
269 error_count++;
270
271 if (error_count > ERROR_DISCONNECT_COUNT) {
272 // reset other half if disconnected
273 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
274 for (int i = 0; i < ROWS_PER_HAND; ++i) {
275 matrix[slaveOffset+i] = 0;
276 }
277 }
278 } else {
279 // turn off the indicator led on no error
280 TXLED0;
281 error_count = 0;
282 }
283 matrix_scan_quantum();
284 return ret;
285}
286
287void matrix_slave_scan(void) {
288 _matrix_scan();
289
290 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
291
292 for (int i = 0; i < ROWS_PER_HAND; ++i) {
293 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
294 }
295
296 matrix_slave_scan_user();
297}
298
299inline
300bool matrix_is_on(uint8_t row, uint8_t col)
301{
302 return (matrix[row] & ((matrix_row_t)1<<col));
303}
304
305
306inline
307matrix_row_t matrix_get_row(uint8_t row)
308{
309 return matrix[row];
310}
311
312void matrix_print(void)
313{
314 print("\nr/c 0123456789ABCDEF\n");
315 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
316 phex(row); print(": ");
317 pbin_reverse16(matrix_get_row(row));
318 print("\n");
319 }
320}
321
322uint8_t matrix_key_count(void)
323{
324 uint8_t count = 0;
325 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
326 count += bitpop16(matrix[i]);
327 }
328 return count;
329}
330
331static void init_cols(void)
332{
333 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
334 uint8_t pin = col_pins[x];
335 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
336 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
337 }
338} 44}
339 45
340inline 46// Copy this code to split_common/matrix.c,
341static matrix_row_t optimized_col_reader(void) { 47// and call it instead of the unoptimized col_reader. Scan-rate jumps from 1200->1920
48// Also remove the sleep_us(30), not necessary for this keyboard.
49// In usb_descriptor.c, set .PollingIntervalMS = 0x01
50#define ROW_SHIFTER ((uint8_t)1)
51inline static matrix_row_t optimized_col_reader(void) {
342 //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 } 52 //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 }
343 return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) | 53 return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) |
344 (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) | 54 (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) |
@@ -351,26 +61,3 @@ static matrix_row_t optimized_col_reader(void) {
351} 61}
352 62
353 63
354static void select_row(uint8_t row)
355{
356 uint8_t pin = row_pins[row];
357 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
358 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
359}
360
361static void unselect_row(uint8_t row)
362{
363 uint8_t pin = row_pins[row];
364 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
365 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
366}
367
368static void unselect_rows(void)
369{
370 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
371 uint8_t pin = row_pins[x];
372 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
373 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
374 }
375}
376
diff --git a/keyboards/handwired/xealous/rules.mk b/keyboards/handwired/xealous/rules.mk
index 7d07c9aa5..ef3357982 100644
--- a/keyboards/handwired/xealous/rules.mk
+++ b/keyboards/handwired/xealous/rules.mk
@@ -1,5 +1,4 @@
1#SRC += matrix_scanrate.c matrix.c 1SRC += matrix_scanrate.c matrix.c
2SRC += debounce.c
3 2
4# MCU name 3# MCU name
5MCU = atmega32u4 4MCU = atmega32u4
@@ -67,8 +66,7 @@ SUBPROJECT_rev1 = yes
67# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE 66# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
68SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend 67SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
69 68
70CUSTOM_MATRIX = no 69DEBOUNCE_TYPE = eager_pk
71DEBOUNCE_TYPE = custom
72 70
73LAYOUTS = split60 71LAYOUTS = split60
74 72