Skip to content

Commit

Permalink
simplified codes and reduced size
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Mar 21, 2024
1 parent 29037cb commit ae63ea8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
67 changes: 29 additions & 38 deletions qmk_firmware/keyboards/keyball/lib/keyball/keyball.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "keyball.h"
#include "drivers/pmw3360/pmw3360.h"

#include <string.h>

const uint8_t CPI_DEFAULT = KEYBALL_CPI_DEFAULT / 100;
const uint8_t CPI_MAX = pmw3360_MAXCPI + 1;
const uint8_t SCROLL_DIV_MAX = 7;
Expand All @@ -41,7 +43,7 @@ keyball_t keyball = {
.scroll_mode = false,
.scroll_div = 0,

.pressing_kc = {0},
.pressing_keys = {' ', ' ', ' ', 0},
};

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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; 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);
}
}

// Draw pressing keys.
oled_write(keyball.pressing_keys, false);
#endif
}

Expand Down Expand Up @@ -520,30 +508,33 @@ 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;
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 `swap` 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;
}
Expand Down
4 changes: 2 additions & 2 deletions qmk_firmware/keyboards/keyball/lib/keyball/keyball.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ae63ea8

Please sign in to comment.