Skip to content

Commit 5cecc1e

Browse files
authored
Add brightness level API to OLED driver (#10772)
* Add brightness level API to OLED driver * Set default brightness to 255
1 parent b9ed9d3 commit 5cecc1e

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

docs/feature_oled_driver.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,16 @@ void oled_task_user(void) {
140140
|---------------------------|-----------------|--------------------------------------------------------------------------------------------------------------------------|
141141
|`OLED_DISPLAY_ADDRESS` |`0x3C` |The i2c address of the OLED Display |
142142
|`OLED_FONT_H` |`"glcdfont.c"` |The font code file to use for custom fonts |
143-
|`OLED_FONT_START` |`0` |The starting characer index for custom fonts |
144-
|`OLED_FONT_END` |`223` |The ending characer index for custom fonts |
143+
|`OLED_FONT_START` |`0` |The starting character index for custom fonts |
144+
|`OLED_FONT_END` |`223` |The ending character index for custom fonts |
145145
|`OLED_FONT_WIDTH` |`6` |The font width |
146146
|`OLED_FONT_HEIGHT` |`8` |The font height (untested) |
147147
|`OLED_TIMEOUT` |`60000` |Turns off the OLED screen after 60000ms of keyboard inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
148148
|`OLED_SCROLL_TIMEOUT` |`0` |Scrolls the OLED screen after 0ms of OLED inactivity. Helps reduce OLED Burn-in. Set to 0 to disable. |
149149
|`OLED_SCROLL_TIMEOUT_RIGHT`|*Not defined* |Scroll timeout direction is right when defined, left when undefined. |
150150
|`OLED_IC` |`OLED_IC_SSD1306`|Set to `OLED_IC_SH1106` if you're using the SH1106 OLED controller. |
151151
|`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.|
152+
|`OLED_BRIGHTNESS` |`255` |The default brightness level of the OLED, from 0 to 255. |
152153

153154
## 128x64 & Custom sized OLED Displays
154155

@@ -304,6 +305,12 @@ bool oled_off(void);
304305
// not
305306
bool is_oled_on(void);
306307

308+
// Sets the brightness level of the display
309+
uint8_t oled_set_brightness(uint8_t level);
310+
311+
// Gets the current brightness level of the display
312+
uint8_t oled_get_brightness(void);
313+
307314
// Basically it's oled_render, but with timeout management and oled_task_user calling!
308315
void oled_task(void);
309316

drivers/oled/oled_driver.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ OLED_BLOCK_TYPE oled_dirty = 0;
107107
bool oled_initialized = false;
108108
bool oled_active = false;
109109
bool oled_scrolling = false;
110+
uint8_t oled_brightness = OLED_BRIGHTNESS;
110111
uint8_t oled_rotation = 0;
111112
uint8_t oled_rotation_width = 0;
112113
uint8_t oled_scroll_speed = 0; // this holds the speed after being remapped to ssd1306 internal values
@@ -193,7 +194,7 @@ bool oled_init(uint8_t rotation) {
193194
}
194195
}
195196

196-
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};
197+
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};
197198
if (I2C_TRANSMIT_P(display_setup2) != I2C_STATUS_SUCCESS) {
198199
print("display_setup2 failed\n");
199200
return false;
@@ -550,6 +551,20 @@ bool oled_off(void) {
550551

551552
bool is_oled_on(void) { return oled_active; }
552553

554+
uint8_t oled_set_brightness(uint8_t level) {
555+
uint8_t set_contrast[] = {I2C_CMD, CONTRAST, level};
556+
if (oled_brightness != level) {
557+
if (I2C_TRANSMIT(set_contrast) != I2C_STATUS_SUCCESS) {
558+
print("set_brightness cmd failed\n");
559+
return oled_brightness;
560+
}
561+
oled_brightness = level;
562+
}
563+
return oled_brightness;
564+
}
565+
566+
uint8_t oled_get_brightness(void) { return oled_brightness; }
567+
553568
// Set the specific 8 lines rows of the screen to scroll.
554569
// 0 is the default for start, and 7 for end, which is the entire
555570
// height of the screen. For 128x32 screens, rows 4-7 are not used.

drivers/oled/oled_driver.h

+10
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
141141
#if !defined(OLED_FONT_HEIGHT)
142142
# define OLED_FONT_HEIGHT 8
143143
#endif
144+
// Default brightness level
145+
#if !defined(OLED_BRIGHTNESS)
146+
# define OLED_BRIGHTNESS 255
147+
#endif
144148

145149
#if !defined(OLED_TIMEOUT)
146150
# if defined(OLED_DISABLE_TIMEOUT)
@@ -261,6 +265,12 @@ bool oled_off(void);
261265
// not
262266
bool is_oled_on(void);
263267

268+
// Sets the brightness of the display
269+
uint8_t oled_set_brightness(uint8_t level);
270+
271+
// Gets the current brightness of the display
272+
uint8_t oled_get_brightness(void);
273+
264274
// Basically it's oled_render, but with timeout management and oled_task_user calling!
265275
void oled_task(void);
266276

keyboards/handwired/onekey/keymaps/oled/keymap.c

+6
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ static void dance_oled_finished(qk_tap_dance_state_t *state, void *user_data) {
144144
}
145145
}
146146
break;
147+
case 4:
148+
if (!state->pressed) {
149+
// quadruple tap - step through brightness levels
150+
oled_set_brightness(oled_get_brightness() + 0x10);
151+
}
152+
break;
147153
default:
148154
break;
149155
}

0 commit comments

Comments
 (0)