Extend ST7789V component to support additional displays (#3651)

Co-authored-by: definitio <37266727+definitio@users.noreply.github.com>
This commit is contained in:
Keith Burzinski 2022-08-07 14:54:48 -05:00 committed by GitHub
parent 4791093e48
commit 522646c64d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 166 additions and 33 deletions

View file

@ -4,15 +4,21 @@ from esphome import pins
from esphome.components import display, spi from esphome.components import display, spi
from esphome.const import ( from esphome.const import (
CONF_BACKLIGHT_PIN, CONF_BACKLIGHT_PIN,
CONF_BRIGHTNESS,
CONF_CS_PIN, CONF_CS_PIN,
CONF_DC_PIN, CONF_DC_PIN,
CONF_HEIGHT,
CONF_ID, CONF_ID,
CONF_LAMBDA, CONF_LAMBDA,
CONF_MODEL,
CONF_RESET_PIN, CONF_RESET_PIN,
CONF_WIDTH,
) )
from . import st7789v_ns from . import st7789v_ns
CONF_EIGHTBITCOLOR = "eightbitcolor"
CONF_OFFSET_HEIGHT = "offset_height"
CONF_OFFSET_WIDTH = "offset_width"
CODEOWNERS = ["@kbx81"] CODEOWNERS = ["@kbx81"]
DEPENDENCIES = ["spi"] DEPENDENCIES = ["spi"]
@ -21,28 +27,79 @@ ST7789V = st7789v_ns.class_(
"ST7789V", cg.PollingComponent, spi.SPIDevice, display.DisplayBuffer "ST7789V", cg.PollingComponent, spi.SPIDevice, display.DisplayBuffer
) )
ST7789VRef = ST7789V.operator("ref") ST7789VRef = ST7789V.operator("ref")
ST7789VModel = st7789v_ns.enum("ST7789VModel")
CONFIG_SCHEMA = ( MODELS = {
"TTGO_TDISPLAY_135X240": ST7789VModel.ST7789V_MODEL_TTGO_TDISPLAY_135_240,
"ADAFRUIT_FUNHOUSE_240X240": ST7789VModel.ST7789V_MODEL_ADAFRUIT_FUNHOUSE_240_240,
"ADAFRUIT_RR_280X240": ST7789VModel.ST7789V_MODEL_ADAFRUIT_RR_280_240,
"CUSTOM": ST7789VModel.ST7789V_MODEL_CUSTOM,
}
ST7789V_MODEL = cv.enum(MODELS, upper=True, space="_")
def validate_st7789v(config):
if config[CONF_MODEL].upper() == "CUSTOM" and (
CONF_HEIGHT not in config
or CONF_WIDTH not in config
or CONF_OFFSET_HEIGHT not in config
or CONF_OFFSET_WIDTH not in config
):
raise cv.Invalid(
f'{CONF_HEIGHT}, {CONF_WIDTH}, {CONF_OFFSET_HEIGHT} and {CONF_OFFSET_WIDTH} must be specified when {CONF_MODEL} is "CUSTOM"'
)
if config[CONF_MODEL].upper() != "CUSTOM" and (
CONF_HEIGHT in config
or CONF_WIDTH in config
or CONF_OFFSET_HEIGHT in config
or CONF_OFFSET_WIDTH in config
):
raise cv.Invalid(
f'Do not specify {CONF_HEIGHT}, {CONF_WIDTH}, {CONF_OFFSET_HEIGHT} or {CONF_OFFSET_WIDTH} when using {CONF_MODEL} that is not "CUSTOM"'
)
return config
CONFIG_SCHEMA = cv.All(
display.FULL_DISPLAY_SCHEMA.extend( display.FULL_DISPLAY_SCHEMA.extend(
{ {
cv.GenerateID(): cv.declare_id(ST7789V), cv.GenerateID(): cv.declare_id(ST7789V),
cv.Required(CONF_MODEL): ST7789V_MODEL,
cv.Required(CONF_RESET_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_RESET_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_BACKLIGHT_PIN): pins.gpio_output_pin_schema, cv.Optional(CONF_BACKLIGHT_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage, cv.Optional(CONF_EIGHTBITCOLOR, default=False): cv.boolean,
cv.Optional(CONF_HEIGHT): cv.int_,
cv.Optional(CONF_WIDTH): cv.int_,
cv.Optional(CONF_OFFSET_HEIGHT): cv.int_,
cv.Optional(CONF_OFFSET_WIDTH): cv.int_,
} }
) )
.extend(cv.polling_component_schema("5s")) .extend(cv.polling_component_schema("5s"))
.extend(spi.spi_device_schema()) .extend(spi.spi_device_schema()),
validate_st7789v,
) )
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)
await display.register_display(var, config)
await spi.register_spi_device(var, config) await spi.register_spi_device(var, config)
cg.add(var.set_model(config[CONF_MODEL]))
if config[CONF_MODEL].upper() == "CUSTOM":
cg.add(var.set_height(config[CONF_HEIGHT]))
cg.add(var.set_width(config[CONF_WIDTH]))
cg.add(var.set_offset_height(config[CONF_OFFSET_HEIGHT]))
cg.add(var.set_offset_width(config[CONF_OFFSET_WIDTH]))
cg.add(var.set_eightbitcolor(config[CONF_EIGHTBITCOLOR]))
dc = await cg.gpio_pin_expression(config[CONF_DC_PIN]) dc = await cg.gpio_pin_expression(config[CONF_DC_PIN])
cg.add(var.set_dc_pin(dc)) cg.add(var.set_dc_pin(dc))
@ -58,5 +115,3 @@ async def to_code(config):
config[CONF_LAMBDA], [(display.DisplayBufferRef, "it")], return_type=cg.void config[CONF_LAMBDA], [(display.DisplayBufferRef, "it")], return_type=cg.void
) )
cg.add(var.set_writer(lambda_)) cg.add(var.set_writer(lambda_))
await display.register_display(var, config)

View file

@ -102,7 +102,7 @@ void ST7789V::setup() {
this->write_command_(ST7789_INVON); this->write_command_(ST7789_INVON);
// Clear display - ensures we do not see garbage at power-on // Clear display - ensures we do not see garbage at power-on
this->draw_filled_rect_(0, 0, 239, 319, 0x0000); this->draw_filled_rect_(0, 0, this->get_width_internal(), this->get_height_internal(), 0x0000);
delay(120); // NOLINT delay(120); // NOLINT
@ -117,6 +117,12 @@ void ST7789V::setup() {
void ST7789V::dump_config() { void ST7789V::dump_config() {
LOG_DISPLAY("", "SPI ST7789V", this); LOG_DISPLAY("", "SPI ST7789V", this);
ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_());
if (this->model_ == ST7789V_MODEL_CUSTOM) {
ESP_LOGCONFIG(TAG, " Height Offset: %u", this->offset_height_);
ESP_LOGCONFIG(TAG, " Width Offset: %u", this->offset_width_);
}
ESP_LOGCONFIG(TAG, " 8-bit color mode: %s", YESNO(this->eightbitcolor_));
LOG_PIN(" CS Pin: ", this->cs_); LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" DC Pin: ", this->dc_pin_); LOG_PIN(" DC Pin: ", this->dc_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_);
@ -131,13 +137,41 @@ void ST7789V::update() {
this->write_display_data(); this->write_display_data();
} }
void ST7789V::loop() {} void ST7789V::set_model(ST7789VModel model) {
this->model_ = model;
switch (this->model_) {
case ST7789V_MODEL_TTGO_TDISPLAY_135_240:
this->height_ = 240;
this->width_ = 135;
this->offset_height_ = 52;
this->offset_width_ = 40;
break;
case ST7789V_MODEL_ADAFRUIT_FUNHOUSE_240_240:
this->height_ = 240;
this->width_ = 240;
this->offset_height_ = 0;
this->offset_width_ = 0;
break;
case ST7789V_MODEL_ADAFRUIT_RR_280_240:
this->height_ = 280;
this->width_ = 240;
this->offset_height_ = 0;
this->offset_width_ = 20;
break;
default:
break;
}
}
void ST7789V::write_display_data() { void ST7789V::write_display_data() {
uint16_t x1 = 52; // _offsetx uint16_t x1 = this->offset_height_;
uint16_t x2 = 186; // _offsetx uint16_t x2 = x1 + get_width_internal() - 1;
uint16_t y1 = 40; // _offsety uint16_t y1 = this->offset_width_;
uint16_t y2 = 279; // _offsety uint16_t y2 = y1 + get_height_internal() - 1;
this->enable(); this->enable();
@ -156,7 +190,19 @@ void ST7789V::write_display_data() {
this->write_byte(ST7789_RAMWR); this->write_byte(ST7789_RAMWR);
this->dc_pin_->digital_write(true); this->dc_pin_->digital_write(true);
this->write_array(this->buffer_, this->get_buffer_length_()); if (this->eightbitcolor_) {
for (int line = 0; line < this->get_buffer_length_(); line = line + this->get_width_internal()) {
for (int index = 0; index < this->get_width_internal(); ++index) {
auto color = display::ColorUtil::color_to_565(
display::ColorUtil::to_color(this->buffer_[index + line], display::ColorOrder::COLOR_ORDER_RGB,
display::ColorBitness::COLOR_BITNESS_332, true));
this->write_byte((color >> 8) & 0xff);
this->write_byte(color & 0xff);
}
}
} else {
this->write_array(this->buffer_, this->get_buffer_length_());
}
this->disable(); this->disable();
} }
@ -219,15 +265,10 @@ void ST7789V::write_color_(uint16_t color, uint16_t size) {
return write_array(byte, size * 2); return write_array(byte, size * 2);
} }
int ST7789V::get_height_internal() {
return 240; // 320;
}
int ST7789V::get_width_internal() {
return 135; // 240;
}
size_t ST7789V::get_buffer_length_() { size_t ST7789V::get_buffer_length_() {
if (this->eightbitcolor_) {
return size_t(this->get_width_internal()) * size_t(this->get_height_internal());
}
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2; return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
} }
@ -238,7 +279,6 @@ size_t ST7789V::get_buffer_length_() {
// y2: End Y coordinate // y2: End Y coordinate
// color: color // color: color
void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { void ST7789V::draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
// ESP_LOGD(TAG,"offset(x)=%d offset(y)=%d",dev->_offsetx,dev->_offsety);
this->enable(); this->enable();
this->dc_pin_->digital_write(false); this->dc_pin_->digital_write(false);
this->write_byte(ST7789_CASET); // set column(x) address this->write_byte(ST7789_CASET); // set column(x) address
@ -263,11 +303,29 @@ void HOT ST7789V::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;
auto color565 = display::ColorUtil::color_to_565(color); if (this->eightbitcolor_) {
auto color332 = display::ColorUtil::color_to_332(color);
uint32_t pos = (x + y * this->get_width_internal());
this->buffer_[pos] = color332;
} else {
auto color565 = display::ColorUtil::color_to_565(color);
uint32_t pos = (x + y * this->get_width_internal()) * 2;
this->buffer_[pos++] = (color565 >> 8) & 0xff;
this->buffer_[pos] = color565 & 0xff;
}
}
uint16_t pos = (x + y * this->get_width_internal()) * 2; const char *ST7789V::model_str_() {
this->buffer_[pos++] = (color565 >> 8) & 0xff; switch (this->model_) {
this->buffer_[pos] = color565 & 0xff; case ST7789V_MODEL_TTGO_TDISPLAY_135_240:
return "TTGO T-Display 135x240";
case ST7789V_MODEL_ADAFRUIT_FUNHOUSE_240_240:
return "Adafruit Funhouse 240x240";
case ST7789V_MODEL_ADAFRUIT_RR_280_240:
return "Adafruit Round-Rectangular 280x240";
default:
return "Custom";
}
} }
} // namespace st7789v } // namespace st7789v

View file

@ -7,8 +7,12 @@
namespace esphome { namespace esphome {
namespace st7789v { namespace st7789v {
static const uint8_t BLACK = 0; enum ST7789VModel {
static const uint8_t WHITE = 1; ST7789V_MODEL_TTGO_TDISPLAY_135_240,
ST7789V_MODEL_ADAFRUIT_FUNHOUSE_240_240,
ST7789V_MODEL_ADAFRUIT_RR_280_240,
ST7789V_MODEL_CUSTOM
};
static const uint8_t ST7789_NOP = 0x00; // No Operation static const uint8_t ST7789_NOP = 0x00; // No Operation
static const uint8_t ST7789_SWRESET = 0x01; // Software Reset static const uint8_t ST7789_SWRESET = 0x01; // Software Reset
@ -110,29 +114,42 @@ static const uint8_t ST7789_MADCTL_COLOR_ORDER = ST7789_MADCTL_BGR;
class ST7789V : public PollingComponent, class ST7789V : public PollingComponent,
public display::DisplayBuffer, public display::DisplayBuffer,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING, public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING,
spi::DATA_RATE_8MHZ> { spi::DATA_RATE_10MHZ> {
public: public:
void set_model(ST7789VModel model);
void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; } void set_dc_pin(GPIOPin *dc_pin) { this->dc_pin_ = dc_pin; }
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_backlight_pin(GPIOPin *backlight_pin) { this->backlight_pin_ = backlight_pin; } void set_backlight_pin(GPIOPin *backlight_pin) { this->backlight_pin_ = backlight_pin; }
void set_eightbitcolor(bool eightbitcolor) { this->eightbitcolor_ = eightbitcolor; }
void set_height(uint32_t height) { this->height_ = height; }
void set_width(uint16_t width) { this->width_ = width; }
void set_offset_height(uint32_t offset_height) { this->offset_height_ = offset_height; }
void set_offset_width(uint16_t offset_width) { this->offset_width_ = offset_width; }
// ========== INTERNAL METHODS ========== // ========== INTERNAL METHODS ==========
// (In most use cases you won't need these) // (In most use cases you won't need these)
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
float get_setup_priority() const override; float get_setup_priority() const override;
void update() override; void update() override;
void loop() override;
void write_display_data(); void write_display_data();
display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; } display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
protected: protected:
GPIOPin *dc_pin_; ST7789VModel model_{ST7789V_MODEL_TTGO_TDISPLAY_135_240};
GPIOPin *dc_pin_{nullptr};
GPIOPin *reset_pin_{nullptr}; GPIOPin *reset_pin_{nullptr};
GPIOPin *backlight_pin_{nullptr}; GPIOPin *backlight_pin_{nullptr};
bool eightbitcolor_{false};
uint16_t height_{0};
uint16_t width_{0};
uint16_t offset_height_{0};
uint16_t offset_width_{0};
void init_reset_(); void init_reset_();
void backlight_(bool onoff); void backlight_(bool onoff);
void write_command_(uint8_t value); void write_command_(uint8_t value);
@ -140,13 +157,15 @@ class ST7789V : public PollingComponent,
void write_addr_(uint16_t addr1, uint16_t addr2); void write_addr_(uint16_t addr1, uint16_t addr2);
void write_color_(uint16_t color, uint16_t size); void write_color_(uint16_t color, uint16_t size);
int get_height_internal() override; int get_height_internal() override { return this->height_; }
int get_width_internal() override; int get_width_internal() override { return this->width_; }
size_t get_buffer_length_(); size_t get_buffer_length_();
void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); void draw_filled_rect_(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
void draw_absolute_pixel_internal(int x, int y, Color color) override; void draw_absolute_pixel_internal(int x, int y, Color color) override;
const char *model_str_();
}; };
} // namespace st7789v } // namespace st7789v

View file

@ -2366,6 +2366,7 @@ display:
lambda: |- lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height()); it.rectangle(0, 0, it.get_width(), it.get_height());
- platform: st7789v - platform: st7789v
model: TTGO TDisplay 135x240
cs_pin: GPIO5 cs_pin: GPIO5
dc_pin: GPIO16 dc_pin: GPIO16
reset_pin: GPIO23 reset_pin: GPIO23