Skip to content

Commit

Permalink
[Core] Refactor OLED to allow easy addition of other types (qmk#13454)
Browse files Browse the repository at this point in the history
* add docs

* core changes

* update keyboards to new OLED

* updated users to new OLED

* update layouts to new OLED

* fixup docs

* drashna's suggestion

* fix up docs

* new keyboards with oled

* core split changes

* remaining keyboard files

* Fix The Helix keyboards oled options

* reflect develop

Co-authored-by: Drashna Jaelre <[email protected]>
Co-authored-by: mtei <[email protected]>
  • Loading branch information
3 people authored Aug 24, 2021
1 parent 18f710b commit d053338
Show file tree
Hide file tree
Showing 32 changed files with 92 additions and 58 deletions.
20 changes: 15 additions & 5 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,21 @@ ifeq ($(strip $(HD44780_ENABLE)), yes)
OPT_DEFS += -DHD44780_ENABLE
endif

ifeq ($(strip $(OLED_DRIVER_ENABLE)), yes)
OPT_DEFS += -DOLED_DRIVER_ENABLE
COMMON_VPATH += $(DRIVER_PATH)/oled
QUANTUM_LIB_SRC += i2c_master.c
SRC += oled_driver.c
VALID_OLED_DRIVER_TYPES := SSD1306 custom
OLED_DRIVER ?= SSD1306
ifeq ($(strip $(OLED_ENABLE)), yes)
ifeq ($(filter $(OLED_DRIVER),$(VALID_OLED_DRIVER_TYPES)),)
$(error OLED_DRIVER="$(OLED_DRIVER)" is not a valid OLED driver)
else
OPT_DEFS += -DOLED_ENABLE
COMMON_VPATH += $(DRIVER_PATH)/oled

OPT_DEFS += -DOLED_DRIVER_$(strip $(shell echo $(OLED_DRIVER) | tr '[:lower:]' '[:upper:]'))
ifeq ($(strip $(OLED_DRIVER)), SSD1306)
SRC += ssd1306_sh1106.c
QUANTUM_LIB_SRC += i2c_master.c
endif
endif
endif

ifeq ($(strip $(ST7565_ENABLE)), yes)
Expand Down
16 changes: 13 additions & 3 deletions docs/feature_oled_driver.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ Hardware configurations using Arm-based microcontrollers or different sizes of O
To enable the OLED feature, there are three steps. First, when compiling your keyboard, you'll need to add the following to your `rules.mk`:

```make
OLED_DRIVER_ENABLE = yes
OLED_ENABLE = yes
```

## OLED type
|OLED Driver |Supported Device |
|-------------------|---------------------------|
|SSD1306 (default) |For both SSD1306 and SH1106|

e.g.
```make
OLED_DRIVER = SSD1306
```

Then in your `keymap.c` file, implement the OLED task call. This example assumes your keymap has three layers named `_QWERTY`, `_FN` and `_ADJ`:

```c
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
void oled_task_user(void) {
// Host Keyboard Layer Status
oled_write_P(PSTR("Layer: "), false);
Expand Down Expand Up @@ -114,7 +124,7 @@ static void fade_display(void) {
In split keyboards, it is very common to have two OLED displays that each render different content and are oriented or flipped differently. You can do this by switching which content to render by using the return value from `is_keyboard_master()` or `is_keyboard_left()` found in `split_util.h`, e.g:

```c
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
oled_rotation_t oled_init_user(oled_rotation_t rotation) {
if (!is_keyboard_master()) {
return OLED_ROTATION_180; // flips the display 180 degrees if offhand
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion layouts/community/split_3x6_3/drashna/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
};
// clang-format on

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
oled_rotation_t oled_init_keymap(oled_rotation_t rotation) { return OLED_ROTATION_270; }
#endif

Expand Down
2 changes: 1 addition & 1 deletion layouts/community/split_3x6_3/drashna/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SWAP_HANDS_ENABLE = no # Enable one-hand typing
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend

ifeq ($(strip $(KEYBOARD)), crkbd/rev1)
OLED_DRIVER_ENABLE = yes
OLED_ENABLE = yes
RGB_MATRIX_ENABLE = yes
HAPTIC_ENABLE = no
BOOTLOADER = qmk-dfu
Expand Down
6 changes: 3 additions & 3 deletions quantum/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifdef QWIIC_ENABLE
# include "qwiic.h"
#endif
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
# include "oled_driver.h"
#endif
#ifdef ST7565_ENABLE
Expand Down Expand Up @@ -319,7 +319,7 @@ void keyboard_init(void) {
#ifdef QWIIC_ENABLE
qwiic_init();
#endif
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
oled_init(OLED_ROTATION_0);
#endif
#ifdef ST7565_ENABLE
Expand Down Expand Up @@ -477,7 +477,7 @@ void keyboard_task(void) {
qwiic_task();
#endif

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
oled_task();
# ifndef OLED_DISABLE_TIMEOUT
// Wake up oled if user is using those fabulous keys or spinning those encoders!
Expand Down
2 changes: 1 addition & 1 deletion quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ extern layer_state_t layer_state;
# include "process_haptic.h"
#endif

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
# include "oled_driver.h"
#endif

Expand Down
4 changes: 2 additions & 2 deletions quantum/split_common/transaction_id_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ enum serial_transaction_id {
PUT_WPM,
#endif // defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)

#if defined(OLED_DRIVER_ENABLE) && defined(SPLIT_OLED_ENABLE)
#if defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
PUT_OLED,
#endif // defined(OLED_DRIVER_ENABLE) && defined(SPLIT_OLED_ENABLE)
#endif // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)

#if defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
PUT_ST7565,
Expand Down
4 changes: 2 additions & 2 deletions quantum/split_common/transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ static void wpm_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_
////////////////////////////////////////////////////
// OLED

#if defined(OLED_DRIVER_ENABLE) && defined(SPLIT_OLED_ENABLE)
#if defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)

static bool oled_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
static uint32_t last_update = 0;
Expand All @@ -539,7 +539,7 @@ static void oled_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave
# define TRANSACTIONS_OLED_SLAVE() TRANSACTION_HANDLER_SLAVE(oled)
# define TRANSACTIONS_OLED_REGISTRATIONS [PUT_OLED] = trans_initiator2target_initializer(current_oled_state),

#else // defined(OLED_DRIVER_ENABLE) && defined(SPLIT_OLED_ENABLE)
#else // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)

# define TRANSACTIONS_OLED_MASTER()
# define TRANSACTIONS_OLED_SLAVE()
Expand Down
4 changes: 2 additions & 2 deletions quantum/split_common/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ typedef struct _split_shared_memory_t {
uint8_t current_wpm;
#endif // defined(WPM_ENABLE) && defined(SPLIT_WPM_ENABLE)

#if defined(OLED_DRIVER_ENABLE) && defined(SPLIT_OLED_ENABLE)
#if defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)
uint8_t current_oled_state;
#endif // defined(OLED_DRIVER_ENABLE) && defined(SPLIT_OLED_ENABLE)
#endif // defined(OLED_ENABLE) && defined(SPLIT_OLED_ENABLE)

#if defined(ST7565_ENABLE) && defined(SPLIT_ST7565_ENABLE)
uint8_t current_st7565_state;
Expand Down
2 changes: 1 addition & 1 deletion show_options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ OTHER_OPTION_NAMES = \
STENO_ENABLE \
TAP_DANCE_ENABLE \
VIRTSER_ENABLE \
OLED_DRIVER_ENABLE \
OLED_ENABLE \
OLED_DRIVER \
LED_BACK_ENABLE \
LED_UNDERGLOW_ENABLE \
LED_ANIMATIONS \
Expand Down
2 changes: 1 addition & 1 deletion users/curry/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ifeq ($(strip $(TAP_DANCE_ENABLE)), yes)
SRC += tap_dances.c
endif

ifeq ($(strip $(OLED_DRIVER_ENABLE)), yes)
ifeq ($(strip $(OLED_ENABLE)), yes)
SRC += oled.c
endif

Expand Down
2 changes: 1 addition & 1 deletion users/drashna/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
# define RGB_MATRIX_STARTUP_MODE RGB_MATRIX_REST_MODE
#endif // RGB_MATRIX_ENABLE

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
# ifdef SPLIT_KEYBOARD
# define OLED_UPDATE_INTERVAL 60
# else
Expand Down
5 changes: 3 additions & 2 deletions users/drashna/drashna.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ void shutdown_user(void) {
}

__attribute__((weak)) void suspend_power_down_keymap(void) {}
void suspend_power_down_user(void) {
#ifdef OLED_DRIVER_ENABLE

void suspend_power_down_user(void) {
#ifdef OLED_ENABLE
oled_off();
#endif
suspend_power_down_keymap();
Expand Down
2 changes: 1 addition & 1 deletion users/drashna/drashna.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#if defined(RGB_MATRIX_ENABLE)
# include "rgb_matrix_stuff.h"
#endif
#if defined(OLED_DRIVER_ENABLE)
#if defined(OLED_ENABLE)
# include "oled_stuff.h"
#endif
#if defined(PIMORONI_TRACKBALL_ENABLE)
Expand Down
2 changes: 1 addition & 1 deletion users/drashna/oled_stuff.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void add_keylog(uint16_t keycode) {

bool process_record_user_oled(uint16_t keycode, keyrecord_t *record) {
if (record->event.pressed) {
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
oled_timer = timer_read32();
add_keylog(keycode);
#endif
Expand Down
2 changes: 1 addition & 1 deletion users/drashna/process_records.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *re
#ifdef KEYLOGGER_ENABLE
uprintf("KL: kc: 0x%04X, col: %2u, row: %2u, pressed: %b, time: %5u, int: %b, count: %u\n", keycode, record->event.key.col, record->event.key.row, record->event.pressed, record->event.time, record->tap.interrupted, record->tap.count);
#endif // KEYLOGGER_ENABLE
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
process_record_user_oled(keycode, record);
#endif // OLED

Expand Down
2 changes: 1 addition & 1 deletion users/drashna/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ifeq ($(strip $(PROTOCOL)), VUSB)
endif

CUSTOM_OLED_DRIVER ?= yes
ifeq ($(strip $(OLED_DRIVER_ENABLE)), yes)
ifeq ($(strip $(OLED_ENABLE)), yes)
ifeq ($(strip $(CUSTOM_OLED_DRIVER)), yes)
SRC += oled_stuff.c
OPT_DEFS += -DCUSTOM_OLED_DRIVER_CODE
Expand Down
11 changes: 11 additions & 0 deletions users/drashna/transport_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ void keyboard_post_init_transport_sync(void) {

void user_transport_update(void) {
if (is_keyboard_master()) {
# ifdef OLED_ENABLE
user_state.oled_on = is_oled_on();
# endif

transport_keymap_config = keymap_config.raw;
transport_userspace_config = userspace_config.raw;
#ifdef AUDIO_ENABLE
Expand All @@ -85,6 +89,13 @@ void user_transport_update(void) {
#endif

} else {
# ifdef OLED_ENABLE
if (user_state.oled_on) {
oled_on();
} else {
oled_off();
}
# endif
keymap_config.raw = transport_keymap_config;
userspace_config.raw = transport_userspace_config;
#ifdef UNICODE_ENABLE
Expand Down
20 changes: 10 additions & 10 deletions users/ninjonas/oled.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdio.h>
#include "ninjonas.h"

#if defined(OLED_DRIVER_ENABLE) & !defined(KEYBOARD_kyria_rev1)
#if defined(OLED_ENABLE) & !defined(KEYBOARD_kyria_rev1)

static uint32_t oled_timer = 0;

Expand Down Expand Up @@ -49,16 +49,16 @@ void render_layer_state(void) {
bool adjust = layer_state_is(_ADJUST);
bool numpad = layer_state_is(_NUMPAD);

if(lower){
oled_write_P(PSTR(" Lower "), true);
} else if(raise){
oled_write_P(PSTR(" Raise "), true);
} else if(adjust){
oled_write_P(PSTR(" Adjust "), true);
if(lower){
oled_write_P(PSTR(" Lower "), true);
} else if(raise){
oled_write_P(PSTR(" Raise "), true);
} else if(adjust){
oled_write_P(PSTR(" Adjust "), true);
} else if(numpad) {
oled_write_P(PSTR(" Numpad "), true);
} else {
oled_write_P(PSTR(" Default"), false);
oled_write_P(PSTR(" Numpad "), true);
} else {
oled_write_P(PSTR(" Default"), false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions users/ninjonas/process_records.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true;
__attribute__((weak))
bool process_record_secrets(uint16_t keycode, keyrecord_t *record) { return true; }

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
__attribute__((weak))
bool process_record_oled(uint16_t keycode, keyrecord_t *record) { return true; }
#endif
Expand Down Expand Up @@ -110,7 +110,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
}

return process_record_keymap(keycode, record) && process_record_secrets(keycode, record)
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
&& process_record_oled(keycode, record)
#endif
; // Close return
Expand Down
2 changes: 1 addition & 1 deletion users/ninjonas/process_records.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ enum custom_keycodes {

bool process_record_secrets(uint16_t keycode, keyrecord_t *record);
bool process_record_keymap(uint16_t keycode, keyrecord_t *record);
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
bool process_record_oled(uint16_t keycode, keyrecord_t *record);
#endif
4 changes: 2 additions & 2 deletions users/riblee/riblee.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
return true;
};

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE

static char receive_buffer[128] = {};
static uint8_t receive_buffer_length = 0;
Expand Down Expand Up @@ -227,4 +227,4 @@ void raw_hid_receive(uint8_t *data, uint8_t length) {

#endif

#endif
#endif
2 changes: 1 addition & 1 deletion users/sethBarberee/sethBarberee.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void keyboard_post_init_user(void)
__attribute__((weak)) void suspend_power_down_keymap(void) {}

void suspend_power_down_user(void) {
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
oled_off();
#endif
suspend_power_down_keymap();
Expand Down
4 changes: 2 additions & 2 deletions users/snowe/oled_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE

# include QMK_KEYBOARD_H
# include "quantum.h"
Expand Down Expand Up @@ -138,4 +138,4 @@ void oled_task_user(void) {
}
}

#endif // OLED_DRIVER_ENABLE
#endif // OLED_ENABLE
4 changes: 2 additions & 2 deletions users/snowe/oled_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#pragma once

#include "quantum.h"
#ifdef OLED_DRIVER_ENABLE
#ifdef OLED_ENABLE
# include "oled_driver.h"
# define OLED_RENDER_WPM_COUNTER " WPM: "
#endif
Expand All @@ -27,4 +27,4 @@
#endif
#ifdef OCEAN_DREAM_ENABLE
# include "ocean_dream.h"
#endif
#endif
5 changes: 3 additions & 2 deletions users/snowe/readme_ocean_dream.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
```
4. In your `rules.mk` to make it easier to turn the animation on/off, add
```makefile
ifeq ($(strip $(OLED_DRIVER_ENABLE)), yes)
ifeq ($(strip $(OLED_ENABLE)), yes)
#... your code here...
ifdef OCEAN_DREAM_ENABLE
Expand All @@ -59,7 +59,8 @@ endif

You're done! Now you can enable **Ocean Dream** by simply turning on the OLED feature
```makefile
OLED_DRIVER_ENABLE = yes
OLED_ENABLE = yes
OLED_DRIVER = SSD1306
```

And if you want to disable it without turning off the OLED Driver you can simply set
Expand Down
Loading

0 comments on commit d053338

Please sign in to comment.