Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print (up to 3) pressing keys on OLED #439

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 43 additions & 9 deletions qmk_firmware/keyboards/keyball/lib/keyball/keyball.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ keyball_t keyball = {

.scroll_mode = false,
.scroll_div = 0,

.pressing_kc = {0},
};

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
Expand All @@ -398,13 +403,23 @@ 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_char(' ', false);
for (int i=0; i<KEYBALL_OLED_MAX_PRESSING_KEYCODES; i++) {
if (keyball.pressing_kc[i] == 0) {
oled_write_char(' ', false);
}
}
// then, writes pressing keys
for (int i=0; i<KEYBALL_OLED_MAX_PRESSING_KEYCODES; i++) {
if (keyball.pressing_kc[i]) {
// safety: if only 4 <= key < 57 keycodes are saved
char name = pgm_read_byte(code_to_name + keyball.pressing_kc[i] - 4);
oled_write_char(name, false);
}
}
#endif
}
Expand Down Expand Up @@ -499,6 +514,25 @@ void housekeeping_task_kb(void) {
#endif

bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
// update pressing_kc for OLED
uint8_t lower_keycode = keycode;
for (int i=0; i<KEYBALL_OLED_MAX_PRESSING_KEYCODES; i++) {
// releases the slot if the key is released
if (!record->event.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
// This simplifies the code for OLED printing.
if (lower_keycode >= 4 && lower_keycode < 57) {
keyball.pressing_kc[i] = lower_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;
Expand Down
5 changes: 5 additions & 0 deletions qmk_firmware/keyboards/keyball/lib/keyball/keyball.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# define KEYBALL_MODEL 44
#endif

#define KEYBALL_OLED_MAX_PRESSING_KEYCODES 3

//////////////////////////////////////////////////////////////////////////////
// Types

Expand Down Expand Up @@ -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 {
Expand Down