mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 00:48:19 +01:00
Fix SSD1306 post-setup brightness control (#1090)
* Fix post-setup brightness control * Add turn_on() and turn_off() methods * Added is_on() method * Set brightness later in setup() * Use clamp() for brightness validation
This commit is contained in:
parent
9a57e8fcb0
commit
69f5d8cd0f
3 changed files with 34 additions and 27 deletions
|
@ -41,7 +41,7 @@ def setup_ssd1036(var, config):
|
||||||
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN])
|
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN])
|
||||||
cg.add(var.set_reset_pin(reset))
|
cg.add(var.set_reset_pin(reset))
|
||||||
if CONF_BRIGHTNESS in config:
|
if CONF_BRIGHTNESS in config:
|
||||||
cg.add(var.set_brightness(config[CONF_BRIGHTNESS]))
|
cg.add(var.init_brightness(config[CONF_BRIGHTNESS]))
|
||||||
if CONF_EXTERNAL_VCC in config:
|
if CONF_EXTERNAL_VCC in config:
|
||||||
cg.add(var.set_external_vcc(config[CONF_EXTERNAL_VCC]))
|
cg.add(var.set_external_vcc(config[CONF_EXTERNAL_VCC]))
|
||||||
if CONF_LAMBDA in config:
|
if CONF_LAMBDA in config:
|
||||||
|
|
|
@ -5,7 +5,11 @@
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace ssd1306_base {
|
namespace ssd1306_base {
|
||||||
|
|
||||||
static const char *TAG = "sd1306";
|
static const char *TAG = "ssd1306";
|
||||||
|
|
||||||
|
static const uint8_t BLACK = 0;
|
||||||
|
static const uint8_t WHITE = 1;
|
||||||
|
static const uint8_t SSD1306_MAX_CONTRAST = 255;
|
||||||
|
|
||||||
static const uint8_t SSD1306_COMMAND_DISPLAY_OFF = 0xAE;
|
static const uint8_t SSD1306_COMMAND_DISPLAY_OFF = 0xAE;
|
||||||
static const uint8_t SSD1306_COMMAND_DISPLAY_ON = 0xAF;
|
static const uint8_t SSD1306_COMMAND_DISPLAY_ON = 0xAF;
|
||||||
|
@ -69,27 +73,6 @@ void SSD1306::setup() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->command(SSD1306_COMMAND_SET_CONTRAST);
|
|
||||||
switch (this->model_) {
|
|
||||||
case SSD1306_MODEL_128_32:
|
|
||||||
case SH1106_MODEL_128_32:
|
|
||||||
this->command(0x8F);
|
|
||||||
break;
|
|
||||||
case SSD1306_MODEL_128_64:
|
|
||||||
case SH1106_MODEL_128_64:
|
|
||||||
case SSD1306_MODEL_64_48:
|
|
||||||
case SH1106_MODEL_64_48:
|
|
||||||
this->command(int(255 * (this->brightness_)));
|
|
||||||
break;
|
|
||||||
case SSD1306_MODEL_96_16:
|
|
||||||
case SH1106_MODEL_96_16:
|
|
||||||
if (this->external_vcc_)
|
|
||||||
this->command(0x10);
|
|
||||||
else
|
|
||||||
this->command(0xAF);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
this->command(SSD1306_COMMAND_SET_PRE_CHARGE);
|
this->command(SSD1306_COMMAND_SET_PRE_CHARGE);
|
||||||
if (this->external_vcc_)
|
if (this->external_vcc_)
|
||||||
this->command(0x22);
|
this->command(0x22);
|
||||||
|
@ -104,7 +87,12 @@ void SSD1306::setup() {
|
||||||
|
|
||||||
this->command(SSD1306_COMMAND_DEACTIVATE_SCROLL);
|
this->command(SSD1306_COMMAND_DEACTIVATE_SCROLL);
|
||||||
|
|
||||||
this->command(SSD1306_COMMAND_DISPLAY_ON);
|
set_brightness(this->brightness_);
|
||||||
|
|
||||||
|
this->fill(BLACK); // clear display - ensures we do not see garbage at power-on
|
||||||
|
this->display(); // ...write buffer, which actually clears the display's memory
|
||||||
|
|
||||||
|
this->turn_on();
|
||||||
}
|
}
|
||||||
void SSD1306::display() {
|
void SSD1306::display() {
|
||||||
if (this->is_sh1106_()) {
|
if (this->is_sh1106_()) {
|
||||||
|
@ -140,6 +128,22 @@ void SSD1306::update() {
|
||||||
this->do_update_();
|
this->do_update_();
|
||||||
this->display();
|
this->display();
|
||||||
}
|
}
|
||||||
|
void SSD1306::set_brightness(float brightness) {
|
||||||
|
// validation
|
||||||
|
this->brightness_ = clamp(brightness, 0, 1);
|
||||||
|
// now write the new brightness level to the display
|
||||||
|
this->command(SSD1306_COMMAND_SET_CONTRAST);
|
||||||
|
this->command(int(SSD1306_MAX_CONTRAST * (this->brightness_)));
|
||||||
|
}
|
||||||
|
bool SSD1306::is_on() { return this->is_on_; }
|
||||||
|
void SSD1306::turn_on() {
|
||||||
|
this->command(SSD1306_COMMAND_DISPLAY_ON);
|
||||||
|
this->is_on_ = true;
|
||||||
|
}
|
||||||
|
void SSD1306::turn_off() {
|
||||||
|
this->command(SSD1306_COMMAND_DISPLAY_OFF);
|
||||||
|
this->is_on_ = false;
|
||||||
|
}
|
||||||
int SSD1306::get_height_internal() {
|
int SSD1306::get_height_internal() {
|
||||||
switch (this->model_) {
|
switch (this->model_) {
|
||||||
case SSD1306_MODEL_128_32:
|
case SSD1306_MODEL_128_32:
|
||||||
|
@ -178,7 +182,6 @@ int SSD1306::get_width_internal() {
|
||||||
size_t SSD1306::get_buffer_length_() {
|
size_t SSD1306::get_buffer_length_() {
|
||||||
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / 8u;
|
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) / 8u;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HOT SSD1306::draw_absolute_pixel_internal(int x, int y, Color color) {
|
void HOT SSD1306::draw_absolute_pixel_internal(int x, int y, Color color) {
|
||||||
if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
|
if (x >= this->get_width_internal() || x < 0 || y >= this->get_height_internal() || y < 0)
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -29,8 +29,11 @@ class SSD1306 : public PollingComponent, public display::DisplayBuffer {
|
||||||
void set_model(SSD1306Model model) { this->model_ = model; }
|
void set_model(SSD1306Model model) { this->model_ = model; }
|
||||||
void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
|
void set_reset_pin(GPIOPin *reset_pin) { this->reset_pin_ = reset_pin; }
|
||||||
void set_external_vcc(bool external_vcc) { this->external_vcc_ = external_vcc; }
|
void set_external_vcc(bool external_vcc) { this->external_vcc_ = external_vcc; }
|
||||||
void set_brightness(float brightness) { this->brightness_ = brightness; }
|
void init_brightness(float brightness) { this->brightness_ = brightness; }
|
||||||
|
void set_brightness(float brightness);
|
||||||
|
bool is_on();
|
||||||
|
void turn_on();
|
||||||
|
void turn_off();
|
||||||
float get_setup_priority() const override { return setup_priority::PROCESSOR; }
|
float get_setup_priority() const override { return setup_priority::PROCESSOR; }
|
||||||
void fill(Color color) override;
|
void fill(Color color) override;
|
||||||
|
|
||||||
|
@ -51,6 +54,7 @@ class SSD1306 : public PollingComponent, public display::DisplayBuffer {
|
||||||
SSD1306Model model_{SSD1306_MODEL_128_64};
|
SSD1306Model model_{SSD1306_MODEL_128_64};
|
||||||
GPIOPin *reset_pin_{nullptr};
|
GPIOPin *reset_pin_{nullptr};
|
||||||
bool external_vcc_{false};
|
bool external_vcc_{false};
|
||||||
|
bool is_on_{false};
|
||||||
float brightness_{1.0};
|
float brightness_{1.0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue