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

Add brightness level API to OLED driver #10772

Merged
merged 2 commits into from
Oct 29, 2020
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
11 changes: 9 additions & 2 deletions docs/feature_oled_driver.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ void oled_task_user(void) {
|---------------------------|-----------------|--------------------------------------------------------------------------------------------------------------------------|
|`OLED_DISPLAY_ADDRESS` |`0x3C` |The i2c address of the OLED Display |
|`OLED_FONT_H` |`"glcdfont.c"` |The font code file to use for custom fonts |
|`OLED_FONT_START` |`0` |The starting characer index for custom fonts |
|`OLED_FONT_END` |`223` |The ending characer index for custom fonts |
|`OLED_FONT_START` |`0` |The starting character index for custom fonts |
|`OLED_FONT_END` |`223` |The ending character index for custom fonts |
|`OLED_FONT_WIDTH` |`6` |The font width |
|`OLED_FONT_HEIGHT` |`8` |The font height (untested) |
|`OLED_TIMEOUT` |`60000` |Turns off the OLED screen after 60000ms of keyboard inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
|`OLED_SCROLL_TIMEOUT` |`0` |Scrolls the OLED screen after 0ms of OLED inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
|`OLED_SCROLL_TIMEOUT_RIGHT`|*Not defined* |Scroll timeout direction is right when defined, left when undefined. |
|`OLED_IC` |`OLED_IC_SSD1306`|Set to `OLED_IC_SH1106` if you're using the SH1106 OLED controller. |
|`OLED_COLUMN_OFFSET` |`0` |(SH1106 only.) Shift output to the right this many pixels.<br />Useful for 128x64 displays centered on a 132x64 SH1106 IC.|
|`OLED_BRIGHTNESS` |`255` |The default brightness level of the OLED, from 0 to 255. |

## 128x64 & Custom sized OLED Displays

Expand Down Expand Up @@ -304,6 +305,12 @@ bool oled_off(void);
// not
bool is_oled_on(void);

// Sets the brightness level of the display
uint8_t oled_set_brightness(uint8_t level);

// Gets the current brightness level of the display
uint8_t oled_get_brightness(void);

// Basically it's oled_render, but with timeout management and oled_task_user calling!
void oled_task(void);

Expand Down
17 changes: 16 additions & 1 deletion drivers/oled/oled_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ OLED_BLOCK_TYPE oled_dirty = 0;
bool oled_initialized = false;
bool oled_active = false;
bool oled_scrolling = false;
uint8_t oled_brightness = OLED_BRIGHTNESS;
uint8_t oled_rotation = 0;
uint8_t oled_rotation_width = 0;
uint8_t oled_scroll_speed = 0; // this holds the speed after being remapped to ssd1306 internal values
Expand Down Expand Up @@ -193,7 +194,7 @@ bool oled_init(uint8_t rotation) {
}
}

static const uint8_t PROGMEM display_setup2[] = {I2C_CMD, COM_PINS, OLED_COM_PINS, CONTRAST, 0x8F, PRE_CHARGE_PERIOD, 0xF1, VCOM_DETECT, 0x40, DISPLAY_ALL_ON_RESUME, NORMAL_DISPLAY, DEACTIVATE_SCROLL, DISPLAY_ON};
static const uint8_t PROGMEM display_setup2[] = {I2C_CMD, COM_PINS, OLED_COM_PINS, CONTRAST, OLED_BRIGHTNESS, PRE_CHARGE_PERIOD, 0xF1, VCOM_DETECT, 0x20, DISPLAY_ALL_ON_RESUME, NORMAL_DISPLAY, DEACTIVATE_SCROLL, DISPLAY_ON};
if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
print("display_setup2 failed\n");
return false;
Expand Down Expand Up @@ -550,6 +551,20 @@ bool oled_off(void) {

bool is_oled_on(void) { return oled_active; }

uint8_t oled_set_brightness(uint8_t level) {
uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level};
if (oled_brightness != level) {
if (I2C_TRANSMIT(set_contrast) != I2C_STATUS_SUCCESS) {
print("set_brightness cmd failed\n");
return oled_brightness;
}
oled_brightness = level;
}
return oled_brightness;
}

uint8_t oled_get_brightness(void) { return oled_brightness; }

// Set the specific 8 lines rows of the screen to scroll.
// 0 is the default for start, and 7 for end, which is the entire
// height of the screen. For 128x32 screens, rows 4-7 are not used.
Expand Down
10 changes: 10 additions & 0 deletions drivers/oled/oled_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#if !defined(OLED_FONT_HEIGHT)
# define OLED_FONT_HEIGHT 8
#endif
// Default brightness level
#if !defined(OLED_BRIGHTNESS)
# define OLED_BRIGHTNESS 255
#endif

#if !defined(OLED_TIMEOUT)
# if defined(OLED_DISABLE_TIMEOUT)
Expand Down Expand Up @@ -261,6 +265,12 @@ bool oled_off(void);
// not
bool is_oled_on(void);

// Sets the brightness of the display
uint8_t oled_set_brightness(uint8_t level);

// Gets the current brightness of the display
uint8_t oled_get_brightness(void);

// Basically it's oled_render, but with timeout management and oled_task_user calling!
void oled_task(void);

Expand Down
6 changes: 6 additions & 0 deletions keyboards/handwired/onekey/keymaps/oled/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ static void dance_oled_finished(qk_tap_dance_state_t *state, void *user_data) {
}
}
break;
case 4:
if (!state->pressed) {
// quadruple tap - step through brightness levels
oled_set_brightness(oled_get_brightness() + 0x10);
}
break;
default:
break;
}
Expand Down