Skip to content

Commit bd677fc

Browse files
committed
Add setBrightness as convenience method
1 parent 4c93074 commit bd677fc

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

OLEDDisplay.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,24 @@ void OLEDDisplay::setContrast(uint8_t contrast, uint8_t precharge, uint8_t comde
578578
sendCommand(DISPLAYON);
579579
}
580580

581+
void OLEDDisplay::setBrightness(uint8_t brightness) {
582+
uint8_t contrast = brightness;
583+
if (brightness < 128) {
584+
// Magic values to get a smooth/ step-free transition
585+
contrast = brightness * 1.171;
586+
} else {
587+
contrast = brightness * 1.171 - 43;
588+
}
589+
590+
uint8_t precharge = 241;
591+
if (brightness == 0) {
592+
precharge = 0;
593+
}
594+
uint8_t comdetect = brightness / 8;
595+
596+
setContrast(contrast, precharge, comdetect);
597+
}
598+
581599
void OLEDDisplay::resetOrientation() {
582600
sendCommand(SEGREMAP);
583601
sendCommand(COMSCANINC); //Reset screen rotation or mirroring

OLEDDisplay.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ class OLEDDisplay : public Print {
222222
// normal brightness & contrast: contrast = 100
223223
void setContrast(uint8_t contrast, uint8_t precharge = 241, uint8_t comdetect = 64);
224224

225+
// Convenience method to access
226+
void setBrightness(uint8_t);
227+
225228
// Reset display rotation or mirroring
226229
void resetOrientation();
227230

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ void invertDisplay(void);
141141
void normalDisplay(void);
142142

143143
// Set display contrast
144-
void setContrast(uint8_t contrast);
144+
// really low brightness & contrast: contrast = 10, precharge = 5, comdetect = 0
145+
// normal brightness & contrast: contrast = 100
146+
void setContrast(uint8_t contrast, uint8_t precharge = 241, uint8_t comdetect = 64);
147+
148+
// Convenience method to access
149+
void setBrightness(uint8_t);
145150

146151
// Turn the display upside down
147152
void flipScreenVertically();

0 commit comments

Comments
 (0)