From cf1f87462dd310de2c9755b6923a6f40441707ea Mon Sep 17 00:00:00 2001 From: Ryo Hirayama Date: Wed, 6 Dec 2023 22:56:48 +0900 Subject: [PATCH 1/5] Print up to 3 pressing keys on OLED --- .../keyboards/keyball/lib/keyball/keyball.c | 50 +++++++++++++++---- .../keyboards/keyball/lib/keyball/keyball.h | 5 ++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c index 051be1841..a4b3b6ad5 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c @@ -40,6 +40,8 @@ keyball_t keyball = { .scroll_mode = false, .scroll_div = 0, + + .pressing_kc = {0}, }; ////////////////////////////////////////////////////////////////////////////// @@ -377,15 +379,18 @@ void keyball_oled_render_ballinfo(void) { void keyball_oled_render_keyinfo(void) { #ifdef OLED_ENABLE - // Format: `Key : R{row} C{col} K{kc} '{name}` + // Format: `Key : R{row} C{col} K{kc} {name}{name}{name}` // // Where `kc` is lower 8 bit of keycode. - // Where `name` is readable label for `kc`, valid between 4 and 56. + // Where `name`s are readable labels for pressing keys, valid between 4 and 56. + // + // `row`, `col`, and `kc` indicates the last processed key, + // but `name`s indicate unreleased keys in best effort. // // It is aligned to fit with output of keyball_oled_render_ballinfo(). // For example: // - // Key : R2 C3 K06 'c + // Key : R2 C3 K06 abc // Ball: 0 0 0 0 // uint8_t keycode = keyball.last_kc; @@ -398,13 +403,22 @@ void keyball_oled_render_keyinfo(void) { oled_write_P(PSTR(" K"), false); oled_write_char(to_1x(keycode >> 4), false); oled_write_char(to_1x(keycode), false); - } - if (keycode >= 4 && keycode < 57) { - oled_write_P(PSTR(" '"), false); - char name = pgm_read_byte(code_to_name + keycode - 4); - oled_write_char(name, false); } else { - oled_advance_page(true); + oled_write_P(PSTR(" "), false); + } + // pads spaces to align pressing keys to the right + oled_write_P(PSTR(" "), false); + for (int i=0; ievent.pressed && keyball.pressing_kc[i] == lower_keycode) { + keyball.pressing_kc[i] = 0; + break; + } + // stores the pressed key if the slot is vacant + if (record->event.pressed && keyball.pressing_kc[i] == 0) { + // store only valid keycodes + if (lower_keycode >= 4 && lower_keycode < 57) { + keyball.pressing_kc[i] = keycode; + } + // no need to check other slots in either case above + break; + } + } // store last keycode, row, and col for OLED keyball.last_kc = keycode; keyball.last_pos = record->event.key; diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h index aeb55fe5f..ed2923f9d 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h @@ -67,6 +67,8 @@ along with this program. If not, see . # define KEYBALL_MODEL 44 #endif +#define KEYBALL_OLED_MAX_PRESSING_KEYCODES 3 + ////////////////////////////////////////////////////////////////////////////// // Types @@ -130,6 +132,9 @@ typedef struct { uint16_t last_kc; keypos_t last_pos; report_mouse_t last_mouse; + + // It needs only the lower 8 bits of each key code to show on OLED. + uint8_t pressing_kc[KEYBALL_OLED_MAX_PRESSING_KEYCODES]; } keyball_t; typedef enum { From a41386b4fafacf1f5b395a5aab5bdb4e9f94db99 Mon Sep 17 00:00:00 2001 From: Ryo Hirayama Date: Wed, 6 Dec 2023 23:20:40 +0900 Subject: [PATCH 2/5] Reduce 8 bytes in firmware size --- qmk_firmware/keyboards/keyball/lib/keyball/keyball.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c index a4b3b6ad5..f6f3fc7df 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c @@ -407,10 +407,10 @@ void keyball_oled_render_keyinfo(void) { oled_write_P(PSTR(" "), false); } // pads spaces to align pressing keys to the right - oled_write_P(PSTR(" "), false); + oled_write_char(' ', false); for (int i=0; i Date: Wed, 6 Dec 2023 23:29:34 +0900 Subject: [PATCH 3/5] refactor --- qmk_firmware/keyboards/keyball/lib/keyball/keyball.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c index f6f3fc7df..efc4ab52f 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c @@ -525,7 +525,7 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) { if (record->event.pressed && keyball.pressing_kc[i] == 0) { // store only valid keycodes if (lower_keycode >= 4 && lower_keycode < 57) { - keyball.pressing_kc[i] = keycode; + keyball.pressing_kc[i] = lower_keycode; } // no need to check other slots in either case above break; From 7249215ccc1e49cdb44f22d09552d87aa49f24e4 Mon Sep 17 00:00:00 2001 From: Ryo Hirayama Date: Wed, 6 Dec 2023 23:40:26 +0900 Subject: [PATCH 4/5] Add comments --- qmk_firmware/keyboards/keyball/lib/keyball/keyball.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c index efc4ab52f..df03b044c 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c @@ -416,6 +416,7 @@ void keyball_oled_render_keyinfo(void) { // then, writes pressing keys for (int i=0; ievent.pressed && keyball.pressing_kc[i] == 0) { // store only valid keycodes + // This simplifies the code for OLED printing. if (lower_keycode >= 4 && lower_keycode < 57) { keyball.pressing_kc[i] = lower_keycode; } From aa7006fd17a15bd04a1fb2757d71cd7bc2f9edb2 Mon Sep 17 00:00:00 2001 From: MURAOKA Taro Date: Thu, 21 Mar 2024 22:09:58 +0900 Subject: [PATCH 5/5] simplified codes and reduced size --- .../keyboards/keyball/lib/keyball/keyball.c | 67 ++++++++----------- .../keyboards/keyball/lib/keyball/keyball.h | 4 +- 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c index ff9f19716..81278f665 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.c @@ -23,6 +23,8 @@ along with this program. If not, see . #include "keyball.h" #include "drivers/pmw3360/pmw3360.h" +#include + const uint8_t CPI_DEFAULT = KEYBALL_CPI_DEFAULT / 100; const uint8_t CPI_MAX = pmw3360_MAXCPI + 1; const uint8_t SCROLL_DIV_MAX = 7; @@ -41,7 +43,7 @@ keyball_t keyball = { .scroll_mode = false, .scroll_div = 0, - .pressing_kc = {0}, + .pressing_keys = {' ', ' ', ' ', 0}, }; ////////////////////////////////////////////////////////////////////////////// @@ -407,28 +409,14 @@ void keyball_oled_render_keyinfo(void) { oled_write_char(to_1x(keyball.last_pos.row), false); oled_write_P(PSTR(" C"), false); oled_write_char(to_1x(keyball.last_pos.col), false); - if (keycode) { - oled_write_P(PSTR(" K"), false); - oled_write_char(to_1x(keycode >> 4), false); - oled_write_char(to_1x(keycode), false); - } else { - oled_write_P(PSTR(" "), false); - } - // pads spaces to align pressing keys to the right + oled_write_P(PSTR(" K"), false); + oled_write_char(to_1x(keycode >> 4), false); + oled_write_char(to_1x(keycode), false); + oled_write_char(' ', false); - for (int i=0; ievent.pressed && keyball.pressing_kc[i] == lower_keycode) { - keyball.pressing_kc[i] = 0; - break; +static void pressing_keys_update(uint16_t keycode, keyrecord_t *record) { + // Process only valid keycodes. + if (keycode >= 4 || keycode < 57) { + char value = pgm_read_byte(code_to_name + keycode - 4); + char where = ' '; + if (!record->event.pressed) { + // Swap `value` and `where` when releasing. + where = value; + value = ' '; } - // stores the pressed key if the slot is vacant - if (record->event.pressed && keyball.pressing_kc[i] == 0) { - // store only valid keycodes - // This simplifies the code for OLED printing. - if (lower_keycode >= 4 && lower_keycode < 57) { - keyball.pressing_kc[i] = lower_keycode; + // Rewrite the last `where` of pressing_keys to `value` . + for (int i = KEYBALL_OLED_MAX_PRESSING_KEYCODES - 1; i >= 0; i--) { + if (keyball.pressing_keys[i] == where) { + keyball.pressing_keys[i] = value; + break; } - // no need to check other slots in either case above - break; } } +} + +bool process_record_kb(uint16_t keycode, keyrecord_t *record) { // store last keycode, row, and col for OLED keyball.last_kc = keycode; keyball.last_pos = record->event.key; + pressing_keys_update(keycode, record); + if (!process_record_user(keycode, record)) { return false; } diff --git a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h index 069b9beee..9c6669e28 100644 --- a/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h +++ b/qmk_firmware/keyboards/keyball/lib/keyball/keyball.h @@ -140,8 +140,8 @@ typedef struct { keypos_t last_pos; report_mouse_t last_mouse; - // It needs only the lower 8 bits of each key code to show on OLED. - uint8_t pressing_kc[KEYBALL_OLED_MAX_PRESSING_KEYCODES]; + // Buffer to indicate pressing keys. + char pressing_keys[KEYBALL_OLED_MAX_PRESSING_KEYCODES + 1]; } keyball_t; typedef enum {