Skip to content

Commit

Permalink
[Keymap] Update Helix:five_rows OLED code (qmk#14427)
Browse files Browse the repository at this point in the history
* Stop using snprintf() in keymaps/five_rows/oled_display.c.

The binary size becomes 1350 bytes smaller.

make HELIX=verbose,core-oled helix/rev2/sc:five_rows
(104 bytes over) -> (95%, 1256 bytes free)

make helix/rev3_5rows:five_rows
(528 bytes over) -> (97%, 830 bytes free)

* add matrix scan rate display to OLED for keymaps/five_rows

* add matrix_output_unselect_delay.c to helix keymaps/five_rows

* add GPLv2 header

* apply review comment
  • Loading branch information
mtei authored and 0xcharly committed Oct 25, 2021
1 parent e6af2e5 commit c4884d5
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 28 deletions.
6 changes: 5 additions & 1 deletion keyboards/helix/rev2/keymaps/five_rows/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
see tmk_core/common/action_tapping.c */

#undef OLED_UPDATE_INTERVAL
#define OLED_UPDATE_INTERVAL 50
#ifdef DEBUG_MATRIX_SCAN_RATE
# define OLED_UPDATE_INTERVAL 500
#else
# define OLED_UPDATE_INTERVAL 50
#endif

// place overrides here

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright 2021 mtei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include QMK_KEYBOARD_H

void matrix_output_unselect_delay(uint8_t line, bool key_pressed) {
/* If none of the keys are pressed,
* there is no need to wait for time for the next line. */
if (key_pressed) {
# ifdef MATRIX_IO_DELAY
# if MATRIX_IO_DELAY > 0
wait_us(MATRIX_IO_DELAY);
# endif
# else
wait_us(30);
# endif
}
}
92 changes: 80 additions & 12 deletions keyboards/helix/rev2/keymaps/five_rows/oled_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,55 @@ void matrix_update(struct CharacterMatrix *dest,
}
# endif

static char *sprint_decimal(char *buf, int data) {
if (data > 9) {
buf = sprint_decimal(buf, data/10);
}
*buf++ = "0123456789"[data%10];
*buf = '\0';
return buf;
}

static char *sprint_hex(char *buf, uint32_t data) {
if (data > 0xf) {
buf = sprint_hex(buf, data/0x10);
}
*buf++ = "0123456789abcdef"[data & 0xf];
*buf = '\0';
return buf;
}

char *sprints(char *buf, char *src) {
while (*src) {
*buf++ = *src++;
}
*buf = '\0';
return buf;
}

char *sprintx(char *buf, char *leadstr, uint32_t data) {
buf = sprints(buf, leadstr);
buf = sprint_hex(buf, data);
return buf;
}

char *sprintd(char *buf, char *leadstr, int data) {
buf = sprints(buf, leadstr);
buf = sprint_decimal(buf, data);
return buf;
}

char *sprint2d(char *buf, char *leadstr, int data) {
buf = sprints(buf, leadstr);
if (data > 99) {
return sprint_decimal(buf, data);
}
if (data < 10) {
*buf++ = ' ';
}
return sprint_decimal(buf, data);
}

# ifdef SSD1306OLED
static void render_logo(struct CharacterMatrix *matrix) {
# else
Expand All @@ -76,20 +125,35 @@ static void render_logo(void) {
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0xd4,
0};
oled_write_P(helix_logo, false);
# ifdef RGBLIGHT_ENABLE
char buf[30];
char *bufp;
# ifdef RGBLIGHT_ENABLE
if (RGBLIGHT_MODES > 1 && rgblight_is_enabled()) {
snprintf(buf, sizeof(buf), " LED %2d: %d,%d,%d ",
rgblight_get_mode(),
rgblight_get_hue()/RGBLIGHT_HUE_STEP,
rgblight_get_sat()/RGBLIGHT_SAT_STEP,
rgblight_get_val()/RGBLIGHT_VAL_STEP);
bufp = sprint2d(buf, " LED ", rgblight_get_mode());
# ifdef DEBUG_MATRIX_SCAN_RATE
bufp = sprintd(bufp, " scan:", get_matrix_scan_rate());
# else
bufp = sprintd(bufp, ": ", rgblight_get_hue()/RGBLIGHT_HUE_STEP);
bufp = sprintd(bufp, ",", rgblight_get_sat()/RGBLIGHT_SAT_STEP);
bufp = sprintd(bufp, ",", rgblight_get_val()/RGBLIGHT_VAL_STEP);
bufp = sprints(bufp, " ");
# endif
oled_write(buf, false);
# ifndef SSD1306OLED
} else {
# ifdef DEBUG_MATRIX_SCAN_RATE
bufp = sprintd(buf, " scan:", get_matrix_scan_rate());
oled_write(buf, false);
# endif
oled_write_P( PSTR("\n"), false);
# endif
}
# else
# ifdef DEBUG_MATRIX_SCAN_RATE
bufp = sprintd(buf, " scan:", get_matrix_scan_rate());
bufp = sprints(bufp, " ");
oled_write(buf, false);
# endif
# endif
}

Expand Down Expand Up @@ -142,6 +206,11 @@ void render_status(void) {
int name_num;
uint32_t lstate;
oled_write_P(layer_names[current_default_layer], false);
# ifdef DEBUG_MATRIX_SCAN_RATE
char buf[16];
sprintd(buf, " scan:", get_matrix_scan_rate());
oled_write(buf, false);
# endif
oled_write_P(PSTR("\n"), false);
for (lstate = layer_state, name_num = 0;
lstate && name_num < sizeof(layer_names)/sizeof(char *);
Expand All @@ -152,14 +221,13 @@ void render_status(void) {
}
}
}
oled_write_P(PSTR("\n"), false);

// Host Keyboard LED Status
char led[40];
snprintf(led, sizeof(led), "\n%s %s %s",
(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? "NUMLOCK" : " ",
(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? "CAPS" : " ",
(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) ? "SCLK" : " ");
oled_write(led, false);
led_t led_state = host_keyboard_led_state();
oled_write_P(led_state.num_lock ? PSTR("NUMLOCK ") : PSTR(" "), false);
oled_write_P(led_state.caps_lock ? PSTR("CAPS ") : PSTR(" "), false);
oled_write_P(led_state.scroll_lock ? PSTR("SCLK ") : PSTR(" "), false);
}

# ifdef SSD1306OLED
Expand Down
13 changes: 12 additions & 1 deletion keyboards/helix/rev2/keymaps/five_rows/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ HELIX_ROWS = 5 # Helix Rows is 4 or 5
# LED_ANIMATIONS = yes # LED animations
# IOS_DEVICE_ENABLE = no # connect to IOS device (iPad,iPhone)

CUSTOM_DELAY = yes

ifneq ($(strip $(HELIX)),)
define KEYMAP_OPTION_PARSE
# parse 'dispoff', 'consloe', 'na', 'ani', 'mini-ani'
# parse 'dispoff', 'consloe', 'na', 'ani', 'mini-ani', 'scan-api',
$(if $(SHOW_PARCE),$(info parse -$1-)) #debug
ifeq ($(strip $1),dispoff)
OLED_ENABLE = no
Expand Down Expand Up @@ -72,6 +74,11 @@ ifneq ($(strip $(HELIX)),)
ifneq ($(filter nolto no-lto no_lto,$(strip $1)),)
LTO_ENABLE = no
endif
ifeq ($(strip $1),scan-api)
# use DEBUG_MATRIX_SCAN_RATE
# see docs/newbs_testing_debugging.md
DEBUG_MATRIX_SCAN_RATE_ENABLE = api
endif
endef # end of KEYMAP_OPTION_PARSE

COMMA=,
Expand All @@ -96,6 +103,10 @@ ifeq ($(strip $(OLED_ENABLE)), yes)
SRC += oled_display.c
endif

ifeq ($(strip $(CUSTOM_DELAY)),yes)
SRC += matrix_output_unselect_delay.c
endif

# convert Helix-specific options (that represent combinations of standard options)
# into QMK standard options.
include $(strip $(KEYBOARD_LOCAL_FEATURES_MK))
6 changes: 5 additions & 1 deletion keyboards/helix/rev3_5rows/keymaps/five_rows/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
see tmk_core/common/action_tapping.c */

#undef OLED_UPDATE_INTERVAL
#define OLED_UPDATE_INTERVAL 50
#ifdef DEBUG_MATRIX_SCAN_RATE
# define OLED_UPDATE_INTERVAL 500
#else
# define OLED_UPDATE_INTERVAL 50
#endif

// place overrides here

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright 2021 mtei
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include QMK_KEYBOARD_H

void matrix_output_unselect_delay(uint8_t line, bool key_pressed) {
/* If none of the keys are pressed,
* there is no need to wait for time for the next line. */
if (key_pressed) {
# ifdef MATRIX_IO_DELAY
# if MATRIX_IO_DELAY > 0
wait_us(MATRIX_IO_DELAY);
# endif
# else
wait_us(30);
# endif
}
}
92 changes: 80 additions & 12 deletions keyboards/helix/rev3_5rows/keymaps/five_rows/oled_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,55 @@ void matrix_update(struct CharacterMatrix *dest,
}
# endif

static char *sprint_decimal(char *buf, int data) {
if (data > 9) {
buf = sprint_decimal(buf, data/10);
}
*buf++ = "0123456789"[data%10];
*buf = '\0';
return buf;
}

static char *sprint_hex(char *buf, uint32_t data) {
if (data > 0xf) {
buf = sprint_hex(buf, data/0x10);
}
*buf++ = "0123456789abcdef"[data & 0xf];
*buf = '\0';
return buf;
}

char *sprints(char *buf, char *src) {
while (*src) {
*buf++ = *src++;
}
*buf = '\0';
return buf;
}

char *sprintx(char *buf, char *leadstr, uint32_t data) {
buf = sprints(buf, leadstr);
buf = sprint_hex(buf, data);
return buf;
}

char *sprintd(char *buf, char *leadstr, int data) {
buf = sprints(buf, leadstr);
buf = sprint_decimal(buf, data);
return buf;
}

char *sprint2d(char *buf, char *leadstr, int data) {
buf = sprints(buf, leadstr);
if (data > 99) {
return sprint_decimal(buf, data);
}
if (data < 10) {
*buf++ = ' ';
}
return sprint_decimal(buf, data);
}

# ifdef SSD1306OLED
static void render_logo(struct CharacterMatrix *matrix) {
# else
Expand All @@ -76,20 +125,35 @@ static void render_logo(void) {
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,0xd0,0xd1,0xd2,0xd3,0xd4,
0};
oled_write_P(helix_logo, false);
# ifdef RGBLIGHT_ENABLE
char buf[30];
char *bufp;
# ifdef RGBLIGHT_ENABLE
if (RGBLIGHT_MODES > 1 && rgblight_is_enabled()) {
snprintf(buf, sizeof(buf), " LED %2d: %d,%d,%d ",
rgblight_get_mode(),
rgblight_get_hue()/RGBLIGHT_HUE_STEP,
rgblight_get_sat()/RGBLIGHT_SAT_STEP,
rgblight_get_val()/RGBLIGHT_VAL_STEP);
bufp = sprint2d(buf, " LED ", rgblight_get_mode());
# ifdef DEBUG_MATRIX_SCAN_RATE
bufp = sprintd(bufp, " scan:", get_matrix_scan_rate());
# else
bufp = sprintd(bufp, ": ", rgblight_get_hue()/RGBLIGHT_HUE_STEP);
bufp = sprintd(bufp, ",", rgblight_get_sat()/RGBLIGHT_SAT_STEP);
bufp = sprintd(bufp, ",", rgblight_get_val()/RGBLIGHT_VAL_STEP);
bufp = sprints(bufp, " ");
# endif
oled_write(buf, false);
# ifndef SSD1306OLED
} else {
# ifdef DEBUG_MATRIX_SCAN_RATE
bufp = sprintd(buf, " scan:", get_matrix_scan_rate());
oled_write(buf, false);
# endif
oled_write_P( PSTR("\n"), false);
# endif
}
# else
# ifdef DEBUG_MATRIX_SCAN_RATE
bufp = sprintd(buf, " scan:", get_matrix_scan_rate());
bufp = sprints(bufp, " ");
oled_write(buf, false);
# endif
# endif
}

Expand Down Expand Up @@ -142,6 +206,11 @@ void render_status(void) {
int name_num;
uint32_t lstate;
oled_write_P(layer_names[current_default_layer], false);
# ifdef DEBUG_MATRIX_SCAN_RATE
char buf[16];
sprintd(buf, " scan:", get_matrix_scan_rate());
oled_write(buf, false);
# endif
oled_write_P(PSTR("\n"), false);
for (lstate = layer_state, name_num = 0;
lstate && name_num < sizeof(layer_names)/sizeof(char *);
Expand All @@ -152,14 +221,13 @@ void render_status(void) {
}
}
}
oled_write_P(PSTR("\n"), false);

// Host Keyboard LED Status
char led[40];
snprintf(led, sizeof(led), "\n%s %s %s",
(host_keyboard_leds() & (1<<USB_LED_NUM_LOCK)) ? "NUMLOCK" : " ",
(host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) ? "CAPS" : " ",
(host_keyboard_leds() & (1<<USB_LED_SCROLL_LOCK)) ? "SCLK" : " ");
oled_write(led, false);
led_t led_state = host_keyboard_led_state();
oled_write_P(led_state.num_lock ? PSTR("NUMLOCK ") : PSTR(" "), false);
oled_write_P(led_state.caps_lock ? PSTR("CAPS ") : PSTR(" "), false);
oled_write_P(led_state.scroll_lock ? PSTR("SCLK ") : PSTR(" "), false);
}

# ifdef SSD1306OLED
Expand Down
Loading

0 comments on commit c4884d5

Please sign in to comment.