aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDustin L. Howett <dustin@howett.net>2020-03-01 21:17:09 -0800
committerGitHub <noreply@github.com>2020-03-02 05:17:09 +0000
commitb72a1aa3fec986bfa7e439b68d6b7546ab1e280b (patch)
treed9d1de0e5f0893fc204dd712fd4b16b543c960e9
parent78069d482634b3b727e5d09b526fa24c227cc4e2 (diff)
downloadqmk_firmware-b72a1aa3fec986bfa7e439b68d6b7546ab1e280b.tar.gz
qmk_firmware-b72a1aa3fec986bfa7e439b68d6b7546ab1e280b.zip
Rewrite the Bathroom Epiphanies Frosty Flake matrix and LED handling (#8243)
* Keyboard: revamp frosty-flake leds This commit transitions bpiphany/frosty_flake to led_update_{kb,user} and rewrites the AVR bit twiddling logic to use the standard QMK GPIO API. * Keyboard: rewrite frosty_flake's matrix reader to be a lite custom matrix This commit replaces frosty_flake's custom matrix and debounce logic with a "lite" custom matrix. In addition to being somewhat clearer, this allows a consumer of the flake board to choose their own debouncing algorithm. The one closest to the implementation originally in use is sym_g, but this opens us up to supporting eager_pk and eager_pr. The original matrix code was 18 columns for 8 rows, but using a single row read and unpacking the bits into individual columns. To simplify, I've changed the key layout to be 8C 18R instead of 18C 8R: this lets us use a single read directly into the matrix _and_ drop down to a uint8_t instead of a uint32_t for matrix_row_t. Since we're no longer implementing our own debouncing and row unpacking, we save ~400 bytes on the final firmware image. Fully tested against a CM Storm QFR hosting the flake -- this commit message was written using the new matrix code. Firmware Sizes (assuming stock configuration as of 42d6270f2) Matrix+Debounce Size (bytes) --------------- ------------ original 17740 new + sym_g 17284 new + eager_pr 18106 new + eager_pk 18204 I expect that there are some scanning speed benefits as well. * Keyboard: update frosty_flake's UNUSED_PINS * Keyboard: Remove meaningless weak redefinitions from frosty These are not necessary (and all of them already live somewhere in Quantum).
-rw-r--r--keyboards/bpiphany/frosty_flake/config.h6
-rw-r--r--keyboards/bpiphany/frosty_flake/frosty_flake.c73
-rw-r--r--keyboards/bpiphany/frosty_flake/frosty_flake.h58
-rw-r--r--keyboards/bpiphany/frosty_flake/matrix.c106
-rw-r--r--keyboards/bpiphany/frosty_flake/rules.mk2
5 files changed, 77 insertions, 168 deletions
diff --git a/keyboards/bpiphany/frosty_flake/config.h b/keyboards/bpiphany/frosty_flake/config.h
index 250a1b775..79bc31aec 100644
--- a/keyboards/bpiphany/frosty_flake/config.h
+++ b/keyboards/bpiphany/frosty_flake/config.h
@@ -36,13 +36,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
36 */ 36 */
37 37
38/* key matrix size */ 38/* key matrix size */
39#define MATRIX_ROWS 8 // Row0 - Row7 in the schematic 39#define MATRIX_ROWS 18 // ColA - ColR in the schematic
40#define MATRIX_COLS 18 // ColA - ColR in the schematic 40#define MATRIX_COLS 8 // Row0 - Row7 in the schematic
41 41
42/* 42/*
43 * Keyboard Matrix Assignments 43 * Keyboard Matrix Assignments
44 */ 44 */
45#define UNUSED_PINS { B0, C4, D3 } 45#define UNUSED_PINS { C0, C1, C2, C3, C4, D2, D7 }
46 46
47/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ 47/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
48#define DEBOUNCE 5 48#define DEBOUNCE 5
diff --git a/keyboards/bpiphany/frosty_flake/frosty_flake.c b/keyboards/bpiphany/frosty_flake/frosty_flake.c
index 1cd476038..be4e1a312 100644
--- a/keyboards/bpiphany/frosty_flake/frosty_flake.c
+++ b/keyboards/bpiphany/frosty_flake/frosty_flake.c
@@ -1,63 +1,24 @@
1#include "frosty_flake.h" 1#include "frosty_flake.h"
2 2
3void matrix_init_kb(void) { 3void keyboard_pre_init_kb() {
4 // put your keyboard start-up code here 4 setPinOutput(B7); // num lock
5 // runs once when the firmware starts up 5 writePinHigh(B7);
6 6 setPinOutput(C5); // caps lock
7 matrix_init_user(); 7 writePinHigh(C7);
8 setPinOutput(C6); // scroll lock
9 writePinHigh(C6);
10
11 keyboard_pre_init_user();
8} 12}
9 13
10void matrix_scan_kb(void) { 14bool led_update_kb(led_t usb_led) {
11 // put your looping keyboard code here 15 // user requests no further processing
12 // runs every cycle (a lot) 16 if (!led_update_user(usb_led))
13 17 return true;
14 matrix_scan_user();
15}
16 18
17bool process_record_kb(uint16_t keycode, keyrecord_t *record) { 19 writePin(C5, !usb_led.caps_lock);
18 // put your per-action keyboard code here 20 writePin(B7, !usb_led.num_lock);
19 // runs for every action, just before processing by the firmware 21 writePin(C6, !usb_led.scroll_lock);
20 22
21 return process_record_user(keycode, record); 23 return true;
22} 24}
23
24void led_set_kb(uint8_t usb_led) {
25 DDRB |= (1<<7);
26 DDRC |= (1<<5) | (1<<6);
27
28 print_dec(usb_led);
29
30 if (usb_led & (1<<USB_LED_CAPS_LOCK))
31 PORTC &= ~(1<<5);
32 else
33 PORTC |= (1<<5);
34
35 if (usb_led & (1<<USB_LED_NUM_LOCK))
36 PORTB &= ~(1<<7);
37 else
38 PORTB |= (1<<7);
39
40 if (usb_led & (1<<USB_LED_SCROLL_LOCK))
41 PORTC &= ~(1<<6);
42 else
43 PORTC |= (1<<6);
44
45 led_set_user(usb_led);
46}
47
48__attribute__ ((weak))
49void matrix_init_user(void) {
50}
51
52__attribute__ ((weak))
53void matrix_scan_user(void) {
54}
55
56__attribute__ ((weak))
57bool process_record_user(uint16_t keycode, keyrecord_t *record) {
58 return true;
59}
60
61__attribute__ ((weak))
62void led_set_user(uint8_t usb_led) {
63} \ No newline at end of file
diff --git a/keyboards/bpiphany/frosty_flake/frosty_flake.h b/keyboards/bpiphany/frosty_flake/frosty_flake.h
index 824287396..cc870c277 100644
--- a/keyboards/bpiphany/frosty_flake/frosty_flake.h
+++ b/keyboards/bpiphany/frosty_flake/frosty_flake.h
@@ -34,16 +34,25 @@
34 KA4, KP2, KC6, KK6, KC0, KM3, KD0, KA1, KO0, KK0, KL0, KL6, KQ6 \ 34 KA4, KP2, KC6, KK6, KC0, KM3, KD0, KA1, KO0, KK0, KL0, KL6, KQ6 \
35) \ 35) \
36{ \ 36{ \
37/* Columns and rows need to be swapped in the below definition */ \ 37/* 0 1 2 3 4 5 6 7 */ \
38/* A B C D E F G H I J K L M N O P Q R */ \ 38/* A */ { KC_NO, KA1, KC_NO, KC_NO, KA4, KA5, KC_NO, KA7, }, \
39/* 0 */ { KC_NO, KB0, KC0, KD0, KC_NO, KF0, KG0, KC_NO, KC_NO, KC_NO, KK0, KL0, KC_NO, KC_NO, KO0, KC_NO, KQ0, KR0 }, \ 39/* B */ { KB0, KB1, KB2, KB3, KB4, KC_NO, KB6, KB7, }, \
40/* 1 */ { KA1, KB1, KC_NO, KD1, KE1, KF1, KG1, KH1, KI1, KJ1, KK1, KL1, KC_NO, KC_NO, KC_NO, KC_NO, KQ1, KC_NO }, \ 40/* C */ { KC0, KC_NO, KC_NO, KC_NO, KC_NO, KC5, KC6, KC7, }, \
41/* 2 */ { KC_NO, KB2, KC_NO, KD2, KE2, KF2, KG2, KH2, KI2, KJ2, KK2, KL2, KC_NO, KN2, KC_NO, KP2, KQ2, KR2 }, \ 41/* D */ { KD0, KD1, KD2, KD3, KD4, KD5, KC_NO, KD7, }, \
42/* 3 */ { KC_NO, KB3, KC_NO, KD3, KE3, KF3, KG3, KH3, KI3, KJ3, KK3, KL3, KM3, KN3, KO3, KC_NO, KQ3, KR3 }, \ 42/* E */ { KC_NO, KE1, KE2, KE3, KE4, KE5, KE6, KE7, }, \
43/* 4 */ { KA4, KB4, KC_NO, KD4, KE4, KF4, KG4, KH4, KI4, KJ4, KK4, KL4, KC_NO, KC_NO, KO4, KC_NO, KQ4, KR4 }, \ 43/* F */ { KF0, KF1, KF2, KF3, KF4, KF5, KF6, KF7, }, \
44/* 5 */ { KA5, KC_NO, KC5, KD5, KE5, KF5, KG5, KH5, KI5, KJ5, KK5, KL5, KC_NO, KC_NO, KO5, KC_NO, KQ5, KR5 }, \ 44/* G */ { KG0, KG1, KG2, KG3, KG4, KG5, KG6, KG7, }, \
45/* 6 */ { KC_NO, KB6, KC6, KC_NO, KE6, KF6, KG6, KH6, KI6, KJ6, KK6, KL6, KC_NO, KC_NO, KO6, KC_NO, KQ6, KR6 }, \ 45/* H */ { KC_NO, KH1, KH2, KH3, KH4, KH5, KH6, KH7, }, \
46/* 7 */ { KA7, KB7, KC7, KD7, KE7, KF7, KG7, KH7, KI7, KJ7, KC_NO, KC_NO, KC_NO, KC_NO, KO7, KC_NO, KQ7, KR7 } \ 46/* I */ { KC_NO, KI1, KI2, KI3, KI4, KI5, KI6, KI7, }, \
47/* J */ { KC_NO, KJ1, KJ2, KJ3, KJ4, KJ5, KJ6, KJ7, }, \
48/* K */ { KK0, KK1, KK2, KK3, KK4, KK5, KK6, KC_NO, }, \
49/* L */ { KL0, KL1, KL2, KL3, KL4, KL5, KL6, KC_NO, }, \
50/* M */ { KC_NO, KC_NO, KC_NO, KM3, KC_NO, KC_NO, KC_NO, KC_NO, }, \
51/* N */ { KC_NO, KC_NO, KN2, KN3, KC_NO, KC_NO, KC_NO, KC_NO, }, \
52/* O */ { KO0, KC_NO, KC_NO, KO3, KO4, KO5, KO6, KO7, }, \
53/* P */ { KC_NO, KC_NO, KP2, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, }, \
54/* Q */ { KQ0, KQ1, KQ2, KQ3, KQ4, KQ5, KQ6, KQ7, }, \
55/* R */ { KR0, KC_NO, KR2, KR3, KR4, KR5, KR6, KR7, }, \
47} 56}
48 57
49/* 58/*
@@ -74,16 +83,25 @@
74 KA4, KP2, KC6, KK6, KC0, KM3, KD0, KA1, KO0, KK0, KL0 \ 83 KA4, KP2, KC6, KK6, KC0, KM3, KD0, KA1, KO0, KK0, KL0 \
75) \ 84) \
76{ \ 85{ \
77/* Columns and rows need to be swapped in the below definition */ \ 86/* 0 1 2 3 4 5 6 7 */ \
78/* A B C D E F G H I J K L M N O P Q R */ \ 87/* A */ { KC_NO, KA1, KC_NO, KC_NO, KA4, KA5, KC_NO, KA7, }, \
79/* 0 */ { KC_NO, KB0, KC0, KD0, KC_NO, KF0, KG0, KC_NO, KC_NO, KC_NO, KK0, KL0, KC_NO, KC_NO, KO0, KC_NO, KC_NO, KR0 }, \ 88/* B */ { KB0, KB1, KB2, KB3, KB4, KC_NO, KB6, KB7, }, \
80/* 1 */ { KA1, KB1, KC_NO, KD1, KE1, KF1, KG1, KH1, KI1, KJ1, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO }, \ 89/* C */ { KC0, KC_NO, KC_NO, KC_NO, KC_NO, KC5, KC6, KC7, }, \
81/* 2 */ { KC_NO, KB2, KC_NO, KD2, KE2, KF2, KG2, KH2, KI2, KJ2, KC_NO, KC_NO, KC_NO, KN2, KC_NO, KP2, KC_NO, KR2 }, \ 90/* D */ { KD0, KD1, KD2, KD3, KD4, KD5, KC_NO, KD7, }, \
82/* 3 */ { KC_NO, KB3, KC_NO, KD3, KE3, KF3, KG3, KH3, KI3, KJ3, KC_NO, KC_NO, KM3, KN3, KC_NO, KC_NO, KC_NO, KR3 }, \ 91/* E */ { KC_NO, KE1, KE2, KE3, KE4, KE5, KE6, KE7, }, \
83/* 4 */ { KA4, KB4, KC_NO, KD4, KE4, KF4, KG4, KH4, KI4, KJ4, KK4, KL4, KC_NO, KC_NO, KO4, KC_NO, KQ4, KR4 }, \ 92/* F */ { KF0, KF1, KF2, KF3, KF4, KF5, KF6, KF7, }, \
84/* 5 */ { KA5, KC_NO, KC5, KD5, KE5, KF5, KG5, KH5, KI5, KJ5, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KR5 }, \ 93/* G */ { KG0, KG1, KG2, KG3, KG4, KG5, KG6, KG7, }, \
85/* 6 */ { KC_NO, KB6, KC6, KC_NO, KE6, KF6, KG6, KH6, KI6, KJ6, KK6, KC_NO, KC_NO, KC_NO, KO6, KC_NO, KC_NO, KR6 }, \ 94/* H */ { KC_NO, KH1, KH2, KH3, KH4, KH5, KH6, KH7, }, \
86/* 7 */ { KA7, KB7, KC7, KD7, KE7, KF7, KG7, KH7, KI7, KJ7, KC_NO, KC_NO, KC_NO, KC_NO, KO7, KC_NO, KQ7, KR7 } \ 95/* I */ { KC_NO, KI1, KI2, KI3, KI4, KI5, KI6, KI7, }, \
96/* J */ { KC_NO, KJ1, KJ2, KJ3, KJ4, KJ5, KJ6, KJ7, }, \
97/* K */ { KK0, KC_NO, KC_NO, KC_NO, KK4, KC_NO, KK6, KC_NO, }, \
98/* L */ { KL0, KC_NO, KC_NO, KC_NO, KL4, KC_NO, KC_NO, KC_NO, }, \
99/* M */ { KC_NO, KC_NO, KC_NO, KM3, KC_NO, KC_NO, KC_NO, KC_NO, }, \
100/* N */ { KC_NO, KC_NO, KN2, KN3, KC_NO, KC_NO, KC_NO, KC_NO, }, \
101/* O */ { KO0, KC_NO, KC_NO, KC_NO, KO4, KC_NO, KO6, KO7, }, \
102/* P */ { KC_NO, KC_NO, KP2, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, }, \
103/* Q */ { KC_NO, KC_NO, KC_NO, KC_NO, KQ4, KC_NO, KC_NO, KQ7, }, \
104/* R */ { KR0, KC_NO, KR2, KR3, KR4, KR5, KR6, KR7, }, \
87} 105}
88 106
89#define LAYOUT_tkl_ansi( \ 107#define LAYOUT_tkl_ansi( \
diff --git a/keyboards/bpiphany/frosty_flake/matrix.c b/keyboards/bpiphany/frosty_flake/matrix.c
index 3c49e9c00..4517c7af5 100644
--- a/keyboards/bpiphany/frosty_flake/matrix.c
+++ b/keyboards/bpiphany/frosty_flake/matrix.c
@@ -15,42 +15,15 @@
15 along with this program. If not, see <http://www.gnu.org/licenses/>. 15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17 17
18#include <stdint.h>
19#include <stdbool.h>
20#include <avr/io.h>
21#include <util/delay.h> 18#include <util/delay.h>
22#include "print.h"
23#include "debug.h"
24#include "util.h"
25#include "matrix.h" 19#include "matrix.h"
26 20
27#ifndef DEBOUNCE
28# define DEBOUNCE 5
29#endif
30static uint8_t debouncing = DEBOUNCE;
31
32static matrix_row_t matrix[MATRIX_ROWS];
33static matrix_row_t matrix_debouncing[MATRIX_ROWS];
34
35__attribute__ ((weak))
36void matrix_init_kb(void) {
37 matrix_init_user();
38}
39
40__attribute__ ((weak))
41void matrix_scan_kb(void) {
42 matrix_scan_user();
43}
44
45__attribute__ ((weak))
46void matrix_init_user(void) {
47}
48
49__attribute__ ((weak))
50void matrix_scan_user(void) {
51}
52
53static matrix_row_t scan_col(void) { 21static matrix_row_t scan_col(void) {
22 // Each of the 8 columns is read off pins as below
23 // 7 6 5 4 3 2 1 0
24 // ,--,--,--,--,--,--,--,--,
25 // |B0|B3|B2|B1|B6|B4|B5|C7|
26 // `--`--`--`--`--`--`--`--`
54 return ( 27 return (
55 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) | 28 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<0)) |
56 (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) | 29 (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<1)) |
@@ -63,8 +36,8 @@ static matrix_row_t scan_col(void) {
63 ); 36 );
64} 37}
65 38
66static void select_col(uint8_t col) { 39static void select_row(uint8_t row) {
67 switch (col) { 40 switch (row) {
68 case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break; 41 case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
69 case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break; 42 case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
70 case 2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break; 43 case 2: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
@@ -86,7 +59,7 @@ static void select_col(uint8_t col) {
86 } 59 }
87} 60}
88 61
89void matrix_init(void) { 62void matrix_init_custom(void) {
90 /* Row output pins */ 63 /* Row output pins */
91 DDRD |= 0b01111011; 64 DDRD |= 0b01111011;
92 /* Column input pins */ 65 /* Column input pins */
@@ -94,62 +67,19 @@ void matrix_init(void) {
94 DDRB &= ~0b01111111; 67 DDRB &= ~0b01111111;
95 PORTC |= 0b10000000; 68 PORTC |= 0b10000000;
96 PORTB |= 0b01111111; 69 PORTB |= 0b01111111;
97
98 for (uint8_t i=0; i < MATRIX_ROWS; i++)
99 matrix[i] = matrix_debouncing[i] = 0;
100
101 matrix_init_quantum();
102} 70}
103 71
104uint8_t matrix_scan(void) { 72// matrix is 18 uint8_t.
105 for (uint8_t col = 0; col < MATRIX_COLS; col++) { 73// we select the row (one of 18), then read the column
106 select_col(col); 74bool matrix_scan_custom(matrix_row_t current_matrix[]) {
107 _delay_us(3); 75 bool has_changed = false;
108 matrix_row_t col_scan = scan_col();
109 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
110 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
111 bool curr_bit = col_scan & (1<<row);
112 if (prev_bit != curr_bit) {
113 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
114 debouncing = DEBOUNCE;
115 }
116 }
117 }
118
119 if (debouncing) {
120 if (--debouncing)
121 _delay_ms(1);
122 else
123 for (uint8_t i = 0; i < MATRIX_ROWS; i++)
124 matrix[i] = matrix_debouncing[i];
125 }
126
127 matrix_scan_quantum();
128 return 1;
129}
130
131inline matrix_row_t matrix_get_row(uint8_t row) {
132 return matrix[row];
133}
134
135void matrix_print(void) {
136#ifndef NO_PRINT
137 print("\nr\\c ABCDEFGHIJKLMNOPQR\n");
138 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 76 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
139 matrix_row_t matrix_row = matrix_get_row(row); 77 matrix_row_t orig = current_matrix[row];
140 xprintf("%02X: ", row); 78 select_row(row);
141 for (uint8_t col = 0; col < MATRIX_COLS; col++) { 79 _delay_us(3);
142 bool curr_bit = matrix_row & (1<<col); 80 current_matrix[row] = scan_col();
143 xprintf("%c", curr_bit ? '*' : '.'); 81 has_changed |= (orig != current_matrix[row]);
144 }
145 print("\n");
146 } 82 }
147#endif
148}
149 83
150uint8_t matrix_key_count(void) { 84 return has_changed;
151 uint8_t count = 0;
152 for (uint8_t row = 0; row < MATRIX_ROWS; row++)
153 count += bitpop32(matrix[row]);
154 return count;
155} 85}
diff --git a/keyboards/bpiphany/frosty_flake/rules.mk b/keyboards/bpiphany/frosty_flake/rules.mk
index d5f943430..649fe6ee7 100644
--- a/keyboards/bpiphany/frosty_flake/rules.mk
+++ b/keyboards/bpiphany/frosty_flake/rules.mk
@@ -30,7 +30,7 @@ BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
30AUDIO_ENABLE = no # Audio output on port C6 30AUDIO_ENABLE = no # Audio output on port C6
31FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches 31FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches
32 32
33CUSTOM_MATRIX = yes 33CUSTOM_MATRIX = lite
34SRC += matrix.c 34SRC += matrix.c
35 35
36LAYOUTS = tkl_ansi 36LAYOUTS = tkl_ansi