1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-04 04:12:23 +01:00

Add support for ST7789V display module (as on TTGO T-Display) (#1050)

* TFT-LCD ST7789V of ESP32 TTGO.

This patch allows you to use TFT-LCD ST7789V of ESP32 TTGO

* Lots of polish and a few tweaks

* Add test

* Add color to core, take 1

* Where did those tabs come from?

* Fix lines too long

* Added color component

* Linted

* Rebase, SPI fix, test

* Shuffle bits

* One more thing...oops

* Image type fix...oops

* Make display_buffer use Color

* Fix BGR/RGB, remove predefined colors

* Fix all the things

* renamed colors to color

* migrate max7219

Co-authored-by: musk95 <musk95@naver.com>
Co-authored-by: Guillermo Ruffino <glm.net@gmail.com>
This commit is contained in:
Keith Burzinski
2020-06-28 16:37:36 -05:00
committed by GitHub
parent bfb9cb6732
commit 491f7e96f0
21 changed files with 843 additions and 85 deletions

View File

@@ -0,0 +1,3 @@
import esphome.codegen as cg
st7789v_ns = cg.esphome_ns.namespace('st7789v')

View File

@@ -0,0 +1,44 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import display, spi
from esphome.const import CONF_BACKLIGHT_PIN, CONF_BRIGHTNESS, CONF_CS_PIN, CONF_DC_PIN, CONF_ID, \
CONF_LAMBDA, CONF_RESET_PIN
from . import st7789v_ns
DEPENDENCIES = ['spi']
ST7789V = st7789v_ns.class_('ST7789V', cg.PollingComponent, spi.SPIDevice,
display.DisplayBuffer)
ST7789VRef = ST7789V.operator('ref')
CONFIG_SCHEMA = display.FULL_DISPLAY_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(ST7789V),
cv.Required(CONF_RESET_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_BACKLIGHT_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage,
}).extend(cv.COMPONENT_SCHEMA).extend(spi.spi_device_schema())
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield spi.register_spi_device(var, config)
dc = yield cg.gpio_pin_expression(config[CONF_DC_PIN])
cg.add(var.set_dc_pin(dc))
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN])
cg.add(var.set_reset_pin(reset))
bl = yield cg.gpio_pin_expression(config[CONF_BACKLIGHT_PIN])
cg.add(var.set_backlight_pin(bl))
if CONF_LAMBDA in config:
lambda_ = yield cg.process_lambda(
config[CONF_LAMBDA], [(display.DisplayBufferRef, 'it')], return_type=cg.void)
cg.add(var.set_writer(lambda_))
yield display.register_display(var, config)

View File

@@ -0,0 +1,274 @@
#include "st7789v.h"
#include "esphome/core/log.h"
namespace esphome {
namespace st7789v {
static const char *TAG = "st7789v";
void ST7789V::setup() {
ESP_LOGCONFIG(TAG, "Setting up SPI ST7789V...");
this->spi_setup();
this->dc_pin_->setup(); // OUTPUT
this->init_reset_();
this->write_command_(ST7789_SLPOUT); // Sleep out
delay(120); // NOLINT
this->write_command_(ST7789_NORON); // Normal display mode on
// *** display and color format setting ***
this->write_command_(ST7789_MADCTL);
this->write_data_(ST7789_MADCTL_COLOR_ORDER);
// JLX240 display datasheet
this->write_command_(0xB6);
this->write_data_(0x0A);
this->write_data_(0x82);
this->write_command_(ST7789_COLMOD);
this->write_data_(0x55);
delay(10);
// *** ST7789V Frame rate setting ***
this->write_command_(ST7789_PORCTRL);
this->write_data_(0x0c);
this->write_data_(0x0c);
this->write_data_(0x00);
this->write_data_(0x33);
this->write_data_(0x33);
this->write_command_(ST7789_GCTRL); // Voltages: VGH / VGL
this->write_data_(0x35);
// *** ST7789V Power setting ***
this->write_command_(ST7789_VCOMS);
this->write_data_(0x28); // JLX240 display datasheet
this->write_command_(ST7789_LCMCTRL);
this->write_data_(0x0C);
this->write_command_(ST7789_VDVVRHEN);
this->write_data_(0x01);
this->write_data_(0xFF);
this->write_command_(ST7789_VRHS); // voltage VRHS
this->write_data_(0x10);
this->write_command_(ST7789_VDVS);
this->write_data_(0x20);
this->write_command_(ST7789_FRCTRL2);
this->write_data_(0x0f);
this->write_command_(ST7789_PWCTRL1);
this->write_data_(0xa4);
this->write_data_(0xa1);
// *** ST7789V gamma setting ***
this->write_command_(ST7789_PVGAMCTRL);
this->write_data_(0xd0);
this->write_data_(0x00);
this->write_data_(0x02);
this->write_data_(0x07);
this->write_data_(0x0a);
this->write_data_(0x28);
this->write_data_(0x32);
this->write_data_(0x44);
this->write_data_(0x42);
this->write_data_(0x06);
this->write_data_(0x0e);
this->write_data_(0x12);
this->write_data_(0x14);
this->write_data_(0x17);
this->write_command_(ST7789_NVGAMCTRL);
this->write_data_(0xd0);
this->write_data_(0x00);
this->write_data_(0x02);
this->write_data_(0x07);
this->write_data_(0x0a);
this->write_data_(0x28);
this->write_data_(0x31);
this->write_data_(0x54);
this->write_data_(0x47);
this->write_data_(0x0e);
this->write_data_(0x1c);
this->write_data_(0x17);
this->write_data_(0x1b);
this->write_data_(0x1e);
this->write_command_(ST7789_INVON);
// Clear display - ensures we do not see garbage at power-on
this->draw_filled_rect_(0, 0, 239, 319, 0x0000);
delay(120); // NOLINT
this->write_command_(ST7789_DISPON); // Display on
delay(120); // NOLINT
backlight_(true);
this->init_internal_(this->get_buffer_length_());
memset(this->buffer_, 0x00, this->get_buffer_length_());
}
void ST7789V::dump_config() {
LOG_DISPLAY("", "SPI ST7789V", this);
LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" DC Pin: ", this->dc_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
LOG_PIN(" B/L Pin: ", this->backlight_pin_);
LOG_UPDATE_INTERVAL(this);
}
float ST7789V::get_setup_priority() const { return setup_priority::PROCESSOR; }
void ST7789V::update() {
this->do_update_();
this->write_display_data();
}
void ST7789V::loop() {}
void ST7789V::write_display_data() {
uint16_t x1 = 52; // _offsetx
uint16_t x2 = 186; // _offsetx
uint16_t y1 = 40; // _offsety
uint16_t y2 = 279; // _offsety
this->enable();
// set column(x) address
this->dc_pin_->digital_write(false);
this->write_byte(ST7789_CASET);
this->dc_pin_->digital_write(true);
this->write_addr_(x1, x2);
// set page(y) address
this->dc_pin_->digital_write(false);
this->write_byte(ST7789_RASET);
this->dc_pin_->digital_write(true);
this->write_addr_(y1, y2);
// write display memory
this->dc_pin_->digital_write(false);
this->write_byte(ST7789_RAMWR);
this->dc_pin_->digital_write(true);
this->write_array(this->buffer_, this->get_buffer_length_());
this->disable();
}
void ST7789V::init_reset_() {
if (this->reset_pin_ != nullptr) {
this->reset_pin_->setup();
this->reset_pin_->digital_write(true);
delay(1);
// Trigger Reset
this->reset_pin_->digital_write(false);
delay(10);
// Wake up
this->reset_pin_->digital_write(true);
}
}
void ST7789V::backlight_(bool onoff) {
if (this->backlight_pin_ != nullptr) {
this->backlight_pin_->setup();
this->backlight_pin_->digital_write(onoff);
}
}
void ST7789V::write_command_(uint8_t value) {
this->enable();
this->dc_pin_->digital_write(false);
this->write_byte(value);
this->dc_pin_->digital_write(true);
this->disable();
}
void ST7789V::write_data_(uint8_t value) {
this->dc_pin_->digital_write(true);
this->enable();
this->write_byte(value);
this->disable();
}
void ST7789V::write_addr_(uint16_t addr1, uint16_t addr2) {
static uint8_t BYTE[4];
BYTE[0] = (addr1 >> 8) & 0xFF;
BYTE[1] = addr1 & 0xFF;
BYTE[2] = (addr2 >> 8) & 0xFF;
BYTE[3] = addr2 & 0xFF;
this->dc_pin_->digital_write(true);
this->write_array(BYTE, 4);
}
void ST7789V::write_color_(uint16_t color, uint16_t size) {
static uint8_t BYTE[1024];
int index = 0;
for (int i = 0; i < size; i++) {
BYTE[index++] = (color >> 8) & 0xFF;
BYTE[index++] = color & 0xFF;
}
this->dc_pin_->digital_write(true);
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_() {
return size_t(this->get_width_internal()) * size_t(this->get_height_internal()) * 2;
}
// Draw a filled rectangle
// x1: Start X coordinate
// y1: Start Y coordinate
// x2: End X coordinate
// y2: End Y coordinate
// color: 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->dc_pin_->digital_write(false);
this->write_byte(ST7789_CASET); // set column(x) address
this->dc_pin_->digital_write(true);
this->write_addr_(x1, x2);
this->dc_pin_->digital_write(false);
this->write_byte(ST7789_RASET); // set Page(y) address
this->dc_pin_->digital_write(true);
this->write_addr_(y1, y2);
this->dc_pin_->digital_write(false);
this->write_byte(ST7789_RAMWR); // begin a write to memory
this->dc_pin_->digital_write(true);
for (int i = x1; i <= x2; i++) {
uint16_t size = y2 - y1 + 1;
this->write_color_(color, size);
}
this->disable();
}
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)
return;
auto color565 = color.to_rgb_565();
uint16_t pos = (x + y * this->get_width_internal()) * 2;
this->buffer_[pos++] = (color565 >> 8) & 0xff;
this->buffer_[pos] = color565 & 0xff;
}
} // namespace st7789v
} // namespace esphome

View File

@@ -0,0 +1,151 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/display/display_buffer.h"
namespace esphome {
namespace st7789v {
static const uint8_t BLACK = 0;
static const uint8_t WHITE = 1;
static const uint8_t ST7789_NOP = 0x00; // No Operation
static const uint8_t ST7789_SWRESET = 0x01; // Software Reset
static const uint8_t ST7789_RDDID = 0x04; // Read Display ID
static const uint8_t ST7789_RDDST = 0x09; // Read Display Status
static const uint8_t ST7789_RDDPM = 0x0A; // Read Display Power Mode
static const uint8_t ST7789_RDDMADCTL = 0x0B; // Read Display MADCTL
static const uint8_t ST7789_RDDCOLMOD = 0x0C; // Read Display Pixel Format
static const uint8_t ST7789_RDDIM = 0x0D; // Read Display Image Mode
static const uint8_t ST7789_RDDSM = 0x0E; // Read Display Signal Mod
static const uint8_t ST7789_RDDSDR = 0x0F; // Read Display Self-Diagnostic Resul
static const uint8_t ST7789_SLPIN = 0x10; // Sleep in
static const uint8_t ST7789_SLPOUT = 0x11; // Sleep Out
static const uint8_t ST7789_PTLON = 0x12; // Partial Display Mode O
static const uint8_t ST7789_NORON = 0x13; // Normal Display Mode O
static const uint8_t ST7789_INVOFF = 0x20; // Display Inversion Off
static const uint8_t ST7789_INVON = 0x21; // Display Inversion O
static const uint8_t ST7789_GAMSET = 0x26; // Gamma Set
static const uint8_t ST7789_DISPOFF = 0x28; // Display Off
static const uint8_t ST7789_DISPON = 0x29; // Display On
static const uint8_t ST7789_CASET = 0x2A; // Column Address Set
static const uint8_t ST7789_RASET = 0x2B; // Row Address Set
static const uint8_t ST7789_RAMWR = 0x2C; // Memory Write
static const uint8_t ST7789_RAMRD = 0x2E; // Memory Read
static const uint8_t ST7789_PTLAR = 0x30; // Partial Area
static const uint8_t ST7789_VSCRDEF = 0x33; // Vertical Scrolling Definitio
static const uint8_t ST7789_TEOFF = 0x34; // Tearing Effect Line OFF
static const uint8_t ST7789_TEON = 0x35; // Tearing Effect Line On
static const uint8_t ST7789_MADCTL = 0x36; // Memory Data Access Control
static const uint8_t ST7789_VSCSAD = 0x37; // Vertical Scroll Start Address of RAM
static const uint8_t ST7789_IDMOFF = 0x38; // Idle Mode Off
static const uint8_t ST7789_IDMON = 0x39; // Idle mode on
static const uint8_t ST7789_COLMOD = 0x3A; // Interface Pixel Format
static const uint8_t ST7789_WRMEMC = 0x3C; // Write Memory Continue
static const uint8_t ST7789_RDMEMC = 0x3E; // Read Memory Continue
static const uint8_t ST7789_STE = 0x44; // Set Tear Scanline
static const uint8_t ST7789_GSCAN = 0x45; // Get Scanlin
static const uint8_t ST7789_WRDISBV = 0x51; // Write Display Brightness
static const uint8_t ST7789_RDDISBV = 0x52; // Read Display Brightness Value
static const uint8_t ST7789_WRCTRLD = 0x53; // Write CTRL Display
static const uint8_t ST7789_RDCTRLD = 0x54; // Read CTRL Value Display
static const uint8_t ST7789_WRCACE = 0x55; // Write Content Adaptive Brightness Control and Color Enhancement
static const uint8_t ST7789_RDCABC = 0x56; // Read Content Adaptive Brightness Control
static const uint8_t ST7789_WRCABCMB = 0x5E; // Write CABC Minimum Brightnes
static const uint8_t ST7789_RDCABCMB = 0x5F; // Read CABC Minimum Brightnes
static const uint8_t ST7789_RDABCSDR = 0x68; // Read Automatic Brightness Control Self-Diagnostic Result
static const uint8_t ST7789_RDID1 = 0xDA; // Read ID1
static const uint8_t ST7789_RDID2 = 0xDB; // Read ID2
static const uint8_t ST7789_RDID3 = 0xDC; // Read ID3
static const uint8_t ST7789_RAMCTRL = 0xB0; // RAM Control
static const uint8_t ST7789_RGBCTRL = 0xB1; // RGB Interface Contro
static const uint8_t ST7789_PORCTRL = 0xB2; // Porch Setting
static const uint8_t ST7789_FRCTRL1 = 0xB3; // Frame Rate Control 1 (In partial mode/ idle colors)
static const uint8_t ST7789_PARCTRL = 0xB5; // Partial mode Contro
static const uint8_t ST7789_GCTRL = 0xB7; // Gate Contro
static const uint8_t ST7789_GTADJ = 0xB8; // Gate On Timing Adjustmen
static const uint8_t ST7789_DGMEN = 0xBA; // Digital Gamma Enable
static const uint8_t ST7789_VCOMS = 0xBB; // VCOMS Setting
static const uint8_t ST7789_LCMCTRL = 0xC0; // LCM Control
static const uint8_t ST7789_IDSET = 0xC1; // ID Code Settin
static const uint8_t ST7789_VDVVRHEN = 0xC2; // VDV and VRH Command Enabl
static const uint8_t ST7789_VRHS = 0xC3; // VRH Set
static const uint8_t ST7789_VDVS = 0xC4; // VDV Set
static const uint8_t ST7789_VCMOFSET = 0xC5; // VCOMS Offset Set
static const uint8_t ST7789_FRCTRL2 = 0xC6; // Frame Rate Control in Normal Mode
static const uint8_t ST7789_CABCCTRL = 0xC7; // CABC Control
static const uint8_t ST7789_REGSEL1 = 0xC8; // Register Value Selection 1
static const uint8_t ST7789_REGSEL2 = 0xCA; // Register Value Selection
static const uint8_t ST7789_PWMFRSEL = 0xCC; // PWM Frequency Selection
static const uint8_t ST7789_PWCTRL1 = 0xD0; // Power Control 1
static const uint8_t ST7789_VAPVANEN = 0xD2; // Enable VAP/VAN signal output
static const uint8_t ST7789_CMD2EN = 0xDF; // Command 2 Enable
static const uint8_t ST7789_PVGAMCTRL = 0xE0; // Positive Voltage Gamma Control
static const uint8_t ST7789_NVGAMCTRL = 0xE1; // Negative Voltage Gamma Control
static const uint8_t ST7789_DGMLUTR = 0xE2; // Digital Gamma Look-up Table for Red
static const uint8_t ST7789_DGMLUTB = 0xE3; // Digital Gamma Look-up Table for Blue
static const uint8_t ST7789_GATECTRL = 0xE4; // Gate Control
static const uint8_t ST7789_SPI2EN = 0xE7; // SPI2 Enable
static const uint8_t ST7789_PWCTRL2 = 0xE8; // Power Control 2
static const uint8_t ST7789_EQCTRL = 0xE9; // Equalize time control
static const uint8_t ST7789_PROMCTRL = 0xEC; // Program Mode Contro
static const uint8_t ST7789_PROMEN = 0xFA; // Program Mode Enabl
static const uint8_t ST7789_NVMSET = 0xFC; // NVM Setting
static const uint8_t ST7789_PROMACT = 0xFE; // Program action
// Flags for ST7789_MADCTL
static const uint8_t ST7789_MADCTL_MY = 0x80;
static const uint8_t ST7789_MADCTL_MX = 0x40;
static const uint8_t ST7789_MADCTL_MV = 0x20;
static const uint8_t ST7789_MADCTL_ML = 0x10;
static const uint8_t ST7789_MADCTL_RGB = 0x00;
static const uint8_t ST7789_MADCTL_BGR = 0x08;
static const uint8_t ST7789_MADCTL_MH = 0x04;
static const uint8_t ST7789_MADCTL_SS = 0x02;
static const uint8_t ST7789_MADCTL_GS = 0x01;
static const uint8_t ST7789_MADCTL_COLOR_ORDER = ST7789_MADCTL_BGR;
class ST7789V : public PollingComponent,
public display::DisplayBuffer,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_HIGH, spi::CLOCK_PHASE_TRAILING,
spi::DATA_RATE_8MHZ> {
public:
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_backlight_pin(GPIOPin *backlight_pin) { this->backlight_pin_ = backlight_pin; }
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
void update() override;
void loop() override;
void write_display_data();
protected:
GPIOPin *dc_pin_;
GPIOPin *reset_pin_{nullptr};
GPIOPin *backlight_pin_{nullptr};
void init_reset_();
void backlight_(bool onoff);
void write_command_(uint8_t value);
void write_data_(uint8_t value);
void write_addr_(uint16_t addr1, uint16_t addr2);
void write_color_(uint16_t color, uint16_t size);
int get_height_internal() override;
int get_width_internal() override;
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_absolute_pixel_internal(int x, int y, Color color) override;
};
} // namespace st7789v
} // namespace esphome