New display ST7735 (#1066)

* Initial Commit - ST7735

* Updated for CI checks

* Updated for travis build

* Travis fixes

* Travis line too long

* Travis fixes

* Fixed up travis format issues

* Travis Fixes

* Initial Commit - ST7735

* Updated for CI checks

* Updated for travis build

* Travis fixes

* Travis line too long

* Travis fixes

* Fixed up travis format issues

* Travis Fixes

* Update to new color API and added test

* Check fixes

* Fixed sid length in test

* Cleaned up whitespaces

* kbx81 recommended fixes

* test fix

* Fixes of fixes

* Fixed test1.yaml

* Fixed test1.yaml

* Changed digital pin #s to gpio

* Updated to match kbx's color names

* Typo for ST7735_INITR_MINI_160X80

* Updated 8bit color space code
Added to_rgb_332 to color.h
fixed typo

* Added in to_rgb_332,to_bgr_332, rgb_332to_rgb_556 and a more generic scale

* Fixed MADCTL

* Fixp MADCTL

* Implemented usrbgr option
updated color to support 332 bgr conversion
typo fix

* Updated to_bgr_332

* Fix up for clang

* FIx up init code. type in buffer caused overrun

* fixup protected names

* typos

* Matched use_bgr to its conf

* color.h red fix in bgr_233to_rgb_565

* Fix ST7735_INITR_MINI_160X80

* Renamed bgr_233to_bgr_565 to match its function
Color space leak in bgr_233to_bgr_565.
cleaned up init code for displays.

* Fix

* clang fix

* Started Color Conversion

* Added various bit color functions
add triadto

* lint changes

* Various fixes

* Various formatting fixes. Wish my checks worked!

* Updated color api to support different formats
removed to_rgb_565

* lint clang fixes

* Test1 fix

* test1.yaml fix

* fixed 565 in ILI9341Display

* Added CodeOwners

* Updated CODEOWNERS

* changed to to332 and to565

* Waiting for color.h changes

* Stage changes

* Removed all changes except this driver

* Moved color functions into driver

* lint changes

* Lint and removed unrelated display driver changes

* Lint changes

* Initial Commit - ST7735

* Updated for CI checks

* Updated for travis build

* Travis fixes

* Travis line too long

* Travis fixes

* Fixed up travis format issues

* Travis Fixes

* Initial Commit - ST7735

* Updated for CI checks

* Updated for travis build

* Travis fixes

* Travis line too long

* Travis fixes

* Fixed up travis format issues

* Travis Fixes

* Update to new color API and added test

* Check fixes

* Fixed sid length in test

* Cleaned up whitespaces

* kbx81 recommended fixes

* test fix

* Fixes of fixes

* Fixed test1.yaml

* Fixed test1.yaml

* Changed digital pin #s to gpio

* Updated to match kbx's color names

* Typo for ST7735_INITR_MINI_160X80

* Updated 8bit color space code
Added to_rgb_332 to color.h
fixed typo

* Added in to_rgb_332,to_bgr_332, rgb_332to_rgb_556 and a more generic scale

* Fixed MADCTL

* Fixp MADCTL

* Implemented usrbgr option
updated color to support 332 bgr conversion
typo fix

* Updated to_bgr_332

* Fix up for clang

* FIx up init code. type in buffer caused overrun

* fixup protected names

* typos

* Matched use_bgr to its conf

* color.h red fix in bgr_233to_rgb_565

* Fix ST7735_INITR_MINI_160X80

* Renamed bgr_233to_bgr_565 to match its function
Color space leak in bgr_233to_bgr_565.
cleaned up init code for displays.

* Fix

* clang fix

* Started Color Conversion

* Added various bit color functions
add triadto

* lint changes

* Various fixes

* Various formatting fixes. Wish my checks worked!

* Updated color api to support different formats
removed to_rgb_565

* lint clang fixes

* Test1 fix

* test1.yaml fix

* fixed 565 in ILI9341Display

* Added CodeOwners

* Updated CODEOWNERS

* changed to to332 and to565

* Waiting for color.h changes

* Stage changes

* Removed all changes except this driver

* Moved color functions into driver

* lint changes

* Lint and removed unrelated display driver changes

* Lint changes

* Updated with latest color api

* pulled from origin

* Updated for color.h changes

* pulled test1 from dev

* Added test
This commit is contained in:
SenexCrenshaw 2020-11-23 12:37:43 -05:00 committed by GitHub
parent d821ead92a
commit 7ff3f752e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 660 additions and 1 deletions

View file

@ -64,6 +64,7 @@ esphome/components/ssd1331_base/* @kbx81
esphome/components/ssd1331_spi/* @kbx81 esphome/components/ssd1331_spi/* @kbx81
esphome/components/ssd1351_base/* @kbx81 esphome/components/ssd1351_base/* @kbx81
esphome/components/ssd1351_spi/* @kbx81 esphome/components/ssd1351_spi/* @kbx81
esphome/components/st7735/* @SenexCrenshaw
esphome/components/st7789v/* @kbx81 esphome/components/st7789v/* @kbx81
esphome/components/substitutions/* @esphome/core esphome/components/substitutions/* @esphome/core
esphome/components/sun/* @OttoWinter esphome/components/sun/* @OttoWinter

View file

@ -0,0 +1,2 @@
import esphome.codegen as cg
st7735_ns = cg.esphome_ns.namespace('st7735')

View file

@ -0,0 +1,75 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import spi
from esphome.components import display
from esphome.core import coroutine
from esphome.const import CONF_DC_PIN, CONF_ID, CONF_LAMBDA, CONF_MODEL, CONF_RESET_PIN, CONF_PAGES
from . import st7735_ns
CODEOWNERS = ['@SenexCrenshaw']
DEPENDENCIES = ['spi']
CONF_DEVICEWIDTH = 'devicewidth'
CONF_DEVICEHEIGHT = 'deviceheight'
CONF_ROWSTART = 'rowstart'
CONF_COLSTART = 'colstart'
CONF_EIGHTBITCOLOR = 'eightbitcolor'
CONF_USEBGR = 'usebgr'
SPIST7735 = st7735_ns.class_('ST7735', cg.PollingComponent, display.DisplayBuffer, spi.SPIDevice)
ST7735Model = st7735_ns.enum('ST7735Model')
MODELS = {
'INITR_GREENTAB': ST7735Model.ST7735_INITR_GREENTAB,
'INITR_REDTAB': ST7735Model.ST7735_INITR_REDTAB,
'INITR_BLACKTAB': ST7735Model.ST7735_INITR_BLACKTAB,
'INITR_MINI160X80': ST7735Model.ST7735_INITR_MINI_160X80,
'INITR_18BLACKTAB': ST7735Model.ST7735_INITR_18BLACKTAB,
'INITR_18REDTAB': ST7735Model.ST7735_INITR_18REDTAB
}
ST7735_MODEL = cv.enum(MODELS, upper=True, space="_")
ST7735_SCHEMA = display.FULL_DISPLAY_SCHEMA.extend({
cv.Required(CONF_MODEL): ST7735_MODEL,
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema
}).extend(cv.polling_component_schema('1s'))
CONFIG_SCHEMA = cv.All(ST7735_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(SPIST7735),
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_DEVICEWIDTH): cv.int_,
cv.Required(CONF_DEVICEHEIGHT): cv.int_,
cv.Required(CONF_COLSTART): cv.int_,
cv.Required(CONF_ROWSTART): cv.int_,
cv.Optional(CONF_EIGHTBITCOLOR, default=False): cv.boolean,
cv.Optional(CONF_USEBGR, default=False): cv.boolean,
}).extend(cv.COMPONENT_SCHEMA).extend(spi.spi_device_schema()),
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA))
@coroutine
def setup_st7735(var, config):
yield cg.register_component(var, config)
yield display.register_display(var, config)
if CONF_RESET_PIN in config:
reset = yield cg.gpio_pin_expression(config[CONF_RESET_PIN])
cg.add(var.set_reset_pin(reset))
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_))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID], config[CONF_MODEL], config[CONF_DEVICEWIDTH],
config[CONF_DEVICEHEIGHT], config[CONF_COLSTART], config[CONF_ROWSTART],
config[CONF_EIGHTBITCOLOR], config[CONF_USEBGR])
yield setup_st7735(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))

View file

@ -0,0 +1,483 @@
#include "st7735.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace st7735 {
static const uint8_t ST_CMD_DELAY = 0x80; // special signifier for command lists
static const uint8_t ST77XX_NOP = 0x00;
static const uint8_t ST77XX_SWRESET = 0x01;
static const uint8_t ST77XX_RDDID = 0x04;
static const uint8_t ST77XX_RDDST = 0x09;
static const uint8_t ST77XX_SLPIN = 0x10;
static const uint8_t ST77XX_SLPOUT = 0x11;
static const uint8_t ST77XX_PTLON = 0x12;
static const uint8_t ST77XX_NORON = 0x13;
static const uint8_t ST77XX_INVOFF = 0x20;
static const uint8_t ST77XX_INVON = 0x21;
static const uint8_t ST77XX_DISPOFF = 0x28;
static const uint8_t ST77XX_DISPON = 0x29;
static const uint8_t ST77XX_CASET = 0x2A;
static const uint8_t ST77XX_RASET = 0x2B;
static const uint8_t ST77XX_RAMWR = 0x2C;
static const uint8_t ST77XX_RAMRD = 0x2E;
static const uint8_t ST77XX_PTLAR = 0x30;
static const uint8_t ST77XX_TEOFF = 0x34;
static const uint8_t ST77XX_TEON = 0x35;
static const uint8_t ST77XX_MADCTL = 0x36;
static const uint8_t ST77XX_COLMOD = 0x3A;
static const uint8_t ST77XX_MADCTL_MY = 0x80;
static const uint8_t ST77XX_MADCTL_MX = 0x40;
static const uint8_t ST77XX_MADCTL_MV = 0x20;
static const uint8_t ST77XX_MADCTL_ML = 0x10;
static const uint8_t ST77XX_MADCTL_RGB = 0x00;
static const uint8_t ST77XX_RDID1 = 0xDA;
static const uint8_t ST77XX_RDID2 = 0xDB;
static const uint8_t ST77XX_RDID3 = 0xDC;
static const uint8_t ST77XX_RDID4 = 0xDD;
// Some register settings
static const uint8_t ST7735_MADCTL_BGR = 0x08;
static const uint8_t ST7735_MADCTL_MH = 0x04;
static const uint8_t ST7735_FRMCTR1 = 0xB1;
static const uint8_t ST7735_FRMCTR2 = 0xB2;
static const uint8_t ST7735_FRMCTR3 = 0xB3;
static const uint8_t ST7735_INVCTR = 0xB4;
static const uint8_t ST7735_DISSET5 = 0xB6;
static const uint8_t ST7735_PWCTR1 = 0xC0;
static const uint8_t ST7735_PWCTR2 = 0xC1;
static const uint8_t ST7735_PWCTR3 = 0xC2;
static const uint8_t ST7735_PWCTR4 = 0xC3;
static const uint8_t ST7735_PWCTR5 = 0xC4;
static const uint8_t ST7735_VMCTR1 = 0xC5;
static const uint8_t ST7735_PWCTR6 = 0xFC;
static const uint8_t ST7735_GMCTRP1 = 0xE0;
static const uint8_t ST7735_GMCTRN1 = 0xE1;
// clang-format off
static const uint8_t PROGMEM
BCMD[] = { // Init commands for 7735B screens
18, // 18 commands in list:
ST77XX_SWRESET, ST_CMD_DELAY, // 1: Software reset, no args, w/delay
50, // 50 ms delay
ST77XX_SLPOUT, ST_CMD_DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = max (500 ms) delay
ST77XX_COLMOD, 1+ST_CMD_DELAY, // 3: Set color mode, 1 arg + delay:
0x05, // 16-bit color
10, // 10 ms delay
ST7735_FRMCTR1, 3+ST_CMD_DELAY, // 4: Frame rate control, 3 args + delay:
0x00, // fastest refresh
0x06, // 6 lines front porch
0x03, // 3 lines back porch
10, // 10 ms delay
ST77XX_MADCTL, 1, // 5: Mem access ctl (directions), 1 arg:
0x08, // Row/col addr, bottom-top refresh
ST7735_DISSET5, 2, // 6: Display settings #5, 2 args:
0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// rise, 3 cycle osc equalize
0x02, // Fix on VTL
ST7735_INVCTR, 1, // 7: Display inversion control, 1 arg:
0x0, // Line inversion
ST7735_PWCTR1, 2+ST_CMD_DELAY, // 8: Power control, 2 args + delay:
0x02, // GVDD = 4.7V
0x70, // 1.0uA
10, // 10 ms delay
ST7735_PWCTR2, 1, // 9: Power control, 1 arg, no delay:
0x05, // VGH = 14.7V, VGL = -7.35V
ST7735_PWCTR3, 2, // 10: Power control, 2 args, no delay:
0x01, // Opamp current small
0x02, // Boost frequency
ST7735_VMCTR1, 2+ST_CMD_DELAY, // 11: Power control, 2 args + delay:
0x3C, // VCOMH = 4V
0x38, // VCOML = -1.1V
10, // 10 ms delay
ST7735_PWCTR6, 2, // 12: Power control, 2 args, no delay:
0x11, 0x15,
ST7735_GMCTRP1,16, // 13: Gamma Adjustments (pos. polarity), 16 args + delay:
0x09, 0x16, 0x09, 0x20, // (Not entirely necessary, but provides
0x21, 0x1B, 0x13, 0x19, // accurate colors)
0x17, 0x15, 0x1E, 0x2B,
0x04, 0x05, 0x02, 0x0E,
ST7735_GMCTRN1,16+ST_CMD_DELAY, // 14: Gamma Adjustments (neg. polarity), 16 args + delay:
0x0B, 0x14, 0x08, 0x1E, // (Not entirely necessary, but provides
0x22, 0x1D, 0x18, 0x1E, // accurate colors)
0x1B, 0x1A, 0x24, 0x2B,
0x06, 0x06, 0x02, 0x0F,
10, // 10 ms delay
ST77XX_CASET, 4, // 15: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 2
0x00, 0x81, // XEND = 129
ST77XX_RASET, 4, // 16: Row addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 1
0x00, 0x81, // XEND = 160
ST77XX_NORON, ST_CMD_DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms delay
ST77XX_DISPON, ST_CMD_DELAY, // 18: Main screen turn on, no args, delay
255 }, // 255 = max (500 ms) delay
RCMD1[] = { // 7735R init, part 1 (red or green tab)
15, // 15 commands in list:
ST77XX_SWRESET, ST_CMD_DELAY, // 1: Software reset, 0 args, w/delay
150, // 150 ms delay
ST77XX_SLPOUT, ST_CMD_DELAY, // 2: Out of sleep mode, 0 args, w/delay
255, // 500 ms delay
ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args:
0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D)
ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args:
0x01, 0x2C, 0x2D, // Dot inversion mode
0x01, 0x2C, 0x2D, // Line inversion mode
ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg:
0x07, // No inversion
ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay:
0xA2,
0x02, // -4.6V
0x84, // AUTO mode
ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay:
0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD
ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay:
0x0A, // Opamp current small
0x00, // Boost frequency
ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay:
0x8A, // BCLK/2,
0x2A, // opamp current small & medium low
ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay:
0x8A, 0xEE,
ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay:
0x0E,
ST77XX_INVOFF, 0, // 13: Don't invert display, no args
ST77XX_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg:
0xC8, // row/col addr, bottom-top refresh
ST77XX_COLMOD, 1, // 15: set color mode, 1 arg, no delay:
0x05 }, // 16-bit color
RCMD2GREEN[] = { // 7735R init, part 2 (green tab only)
2, // 2 commands in list:
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 0
0x00, 0x7F+0x02, // XEND = 127
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x01, // XSTART = 0
0x00, 0x9F+0x01 }, // XEND = 159
RCMD2RED[] = { // 7735R init, part 2 (red tab only)
2, // 2 commands in list:
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F }, // XEND = 159
RCMD2GREEN144[] = { // 7735R init, part 2 (green 1.44 tab)
2, // 2 commands in list:
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F, // XEND = 127
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x7F }, // XEND = 127
RCMD2GREEN160X80[] = { // 7735R init, part 2 (mini 160x80)
2, // 2 commands in list:
ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x4F, // XEND = 79
ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay:
0x00, 0x00, // XSTART = 0
0x00, 0x9F }, // XEND = 159
RCMD3[] = { // 7735R init, part 3 (red or green tab)
4, // 4 commands in list:
ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args + delay:
0x02, 0x1c, 0x07, 0x12, // (Not entirely necessary, but provides
0x37, 0x32, 0x29, 0x2d, // accurate colors)
0x29, 0x25, 0x2B, 0x39,
0x00, 0x01, 0x03, 0x10,
ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args + delay:
0x03, 0x1d, 0x07, 0x06, // (Not entirely necessary, but provides
0x2E, 0x2C, 0x29, 0x2D, // accurate colors)
0x2E, 0x2E, 0x37, 0x3F,
0x00, 0x00, 0x02, 0x10,
ST77XX_NORON, ST_CMD_DELAY, // 3: Normal display on, no args, w/delay
10, // 10 ms delay
ST77XX_DISPON, ST_CMD_DELAY, // 4: Main screen turn on, no args w/delay
100 }; // 100 ms delay
// clang-format on
static const char *TAG = "st7735";
ST7735::ST7735(ST7735Model model, int width, int height, int colstart, int rowstart, boolean eightbitcolor,
boolean usebgr) {
model_ = model;
this->width_ = width;
this->height_ = height;
this->colstart_ = colstart;
this->rowstart_ = rowstart;
this->eightbitcolor_ = eightbitcolor;
this->usebgr_ = usebgr;
}
void ST7735::setup() {
ESP_LOGCONFIG(TAG, "Setting up ST7735...");
this->spi_setup();
this->dc_pin_->setup(); // OUTPUT
this->cs_->setup(); // OUTPUT
this->dc_pin_->digital_write(true);
this->cs_->digital_write(true);
this->init_reset_();
delay(100); // NOLINT
ESP_LOGD(TAG, " START");
dump_config();
ESP_LOGD(TAG, " END");
display_init_(RCMD1);
if (this->model_ == INITR_GREENTAB) {
display_init_(RCMD2GREEN);
colstart_ == 0 ? colstart_ = 2 : colstart_;
rowstart_ == 0 ? rowstart_ = 1 : rowstart_;
} else if ((this->model_ == INITR_144GREENTAB) || (this->model_ == INITR_HALLOWING)) {
height_ == 0 ? height_ = ST7735_TFTHEIGHT_128 : height_;
width_ == 0 ? width_ = ST7735_TFTWIDTH_128 : width_;
display_init_(RCMD2GREEN144);
colstart_ == 0 ? colstart_ = 2 : colstart_;
rowstart_ == 0 ? rowstart_ = 3 : rowstart_;
} else if (this->model_ == INITR_MINI_160X80) {
height_ == 0 ? height_ = ST7735_TFTHEIGHT_160 : height_;
width_ == 0 ? width_ = ST7735_TFTWIDTH_80 : width_;
display_init_(RCMD2GREEN160X80);
colstart_ = 24;
rowstart_ = 0; // For default rotation 0
} else {
// colstart, rowstart left at default '0' values
display_init_(RCMD2RED);
}
display_init_(RCMD3);
uint8_t data = 0;
if (this->model_ != INITR_HALLOWING) {
uint8_t data = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY;
}
if (this->usebgr_) {
data = data | ST7735_MADCTL_BGR;
} else {
data = data | ST77XX_MADCTL_RGB;
}
sendcommand_(ST77XX_MADCTL, &data, 1);
this->init_internal_(this->get_buffer_length());
memset(this->buffer_, 0x00, this->get_buffer_length());
}
void ST7735::update() {
this->do_update_();
this->write_display_data_();
}
int ST7735::get_height_internal() { return height_; }
int ST7735::get_width_internal() { return width_; }
size_t ST7735::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;
}
void HOT ST7735::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;
if (this->eightbitcolor_) {
const uint32_t color332 = color.to_332();
uint16_t pos = (x + y * this->get_width_internal());
this->buffer_[pos] = color332;
} else {
const uint32_t color565 = color.to_565();
uint16_t pos = (x + y * this->get_width_internal()) * 2;
this->buffer_[pos++] = (color565 >> 8) & 0xff;
this->buffer_[pos] = color565 & 0xff;
}
}
void ST7735::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);
}
}
const char *ST7735::model_str_() {
switch (this->model_) {
case INITR_GREENTAB:
return "ST7735 GREENTAB";
case INITR_REDTAB:
return "ST7735 REDTAB";
case INITR_BLACKTAB:
return "ST7735 BLACKTAB";
case INITR_MINI_160X80:
return "ST7735 MINI160x80";
default:
return "Unknown";
}
}
void ST7735::display_init_(const uint8_t *addr) {
uint8_t num_commands, cmd, num_args;
uint16_t ms;
num_commands = pgm_read_byte(addr++); // Number of commands to follow
while (num_commands--) { // For each command...
cmd = pgm_read_byte(addr++); // Read command
num_args = pgm_read_byte(addr++); // Number of args to follow
ms = num_args & ST_CMD_DELAY; // If hibit set, delay follows args
num_args &= ~ST_CMD_DELAY; // Mask out delay bit
this->sendcommand_(cmd, addr, num_args);
addr += num_args;
if (ms) {
ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
if (ms == 255)
ms = 500; // If 255, delay for 500 ms
delay(ms);
}
}
}
void ST7735::dump_config() {
LOG_DISPLAY("", "ST7735", this);
ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_());
LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" DC Pin: ", this->dc_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
ESP_LOGD(TAG, " Buffer Size: %zu", this->get_buffer_length());
ESP_LOGD(TAG, " Height: %d", this->height_);
ESP_LOGD(TAG, " Width: %d", this->width_);
ESP_LOGD(TAG, " ColStart: %d", this->colstart_);
ESP_LOGD(TAG, " RowStart: %d", this->rowstart_);
LOG_UPDATE_INTERVAL(this);
}
void HOT ST7735::writecommand_(uint8_t value) {
this->enable();
this->dc_pin_->digital_write(false);
this->write_byte(value);
this->dc_pin_->digital_write(true);
this->disable();
}
void HOT ST7735::writedata_(uint8_t value) {
this->dc_pin_->digital_write(true);
this->enable();
this->write_byte(value);
this->disable();
}
void HOT ST7735::sendcommand_(uint8_t cmd, const uint8_t *data_bytes, uint8_t num_data_bytes) {
this->writecommand_(cmd);
this->senddata_(data_bytes, num_data_bytes);
}
void HOT ST7735::senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes) {
this->dc_pin_->digital_write(true); // pull DC high to indicate data
this->cs_->digital_write(false);
this->enable();
for (uint8_t i = 0; i < num_data_bytes; i++) {
this->transfer_byte(pgm_read_byte(data_bytes++)); // write byte - SPI library
}
this->cs_->digital_write(true);
this->disable();
}
void HOT ST7735::write_display_data_() {
uint16_t offsetx = colstart_;
uint16_t offsety = rowstart_;
uint16_t x1 = offsetx;
uint16_t x2 = x1 + get_width_internal() - 1;
uint16_t y1 = offsety;
uint16_t y2 = y1 + get_height_internal() - 1;
this->enable();
// set column(x) address
this->dc_pin_->digital_write(false);
this->write_byte(ST77XX_CASET);
this->dc_pin_->digital_write(true);
this->spi_master_write_addr_(x1, x2);
// set Page(y) address
this->dc_pin_->digital_write(false);
this->write_byte(ST77XX_RASET);
this->dc_pin_->digital_write(true);
this->spi_master_write_addr_(y1, y2);
// Memory Write
this->dc_pin_->digital_write(false);
this->write_byte(ST77XX_RAMWR);
this->dc_pin_->digital_write(true);
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 = Color(this->buffer_[index + line], Color::ColorOrder::COLOR_ORDER_RGB,
Color::ColorBitness::COLOR_BITNESS_332, true)
.to_565();
this->write_byte((color >> 8) & 0xff);
this->write_byte(color & 0xff);
}
}
} else {
this->write_array(this->buffer_, this->get_buffer_length());
}
}
void ST7735::spi_master_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 ST7735::spi_master_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);
}
} // namespace st7735
} // namespace esphome

View file

@ -0,0 +1,87 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/display/display_buffer.h"
namespace esphome {
namespace st7735 {
static const uint8_t ST7735_TFTWIDTH_128 = 128; // for 1.44 and mini^M
static const uint8_t ST7735_TFTWIDTH_80 = 80; // for mini^M
static const uint8_t ST7735_TFTHEIGHT_128 = 128; // for 1.44" display^M
static const uint8_t ST7735_TFTHEIGHT_160 = 160; // for 1.8" and mini display^M
// some flags for initR() :(
static const uint8_t INITR_GREENTAB = 0x00;
static const uint8_t INITR_REDTAB = 0x01;
static const uint8_t INITR_BLACKTAB = 0x02;
static const uint8_t INITR_144GREENTAB = 0x01;
static const uint8_t INITR_MINI_160X80 = 0x04;
static const uint8_t INITR_HALLOWING = 0x05;
static const uint8_t INITR_18GREENTAB = INITR_GREENTAB;
static const uint8_t INITR_18REDTAB = INITR_REDTAB;
static const uint8_t INITR_18BLACKTAB = INITR_BLACKTAB;
enum ST7735Model {
ST7735_INITR_GREENTAB = INITR_GREENTAB,
ST7735_INITR_REDTAB = INITR_REDTAB,
ST7735_INITR_BLACKTAB = INITR_BLACKTAB,
ST7735_INITR_MINI_160X80 = INITR_MINI_160X80,
ST7735_INITR_18BLACKTAB = INITR_18BLACKTAB,
ST7735_INITR_18REDTAB = INITR_18REDTAB
};
class ST7735 : public PollingComponent,
public display::DisplayBuffer,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
spi::DATA_RATE_8MHZ> {
public:
ST7735(ST7735Model model, int width, int height, int colstart, int rowstart, boolean eightbitcolor, boolean usebgr);
void dump_config() override;
void setup() override;
void display();
void update() override;
void set_model(ST7735Model model) { this->model_ = model; }
float get_setup_priority() const override { return setup_priority::PROCESSOR; }
void set_reset_pin(GPIOPin *value) { this->reset_pin_ = value; }
void set_dc_pin(GPIOPin *value) { dc_pin_ = value; }
size_t get_buffer_length();
protected:
void sendcommand_(uint8_t cmd, const uint8_t *data_bytes, uint8_t num_data_bytes);
void senddata_(const uint8_t *data_bytes, uint8_t num_data_bytes);
void writecommand_(uint8_t value);
void writedata_(uint8_t value);
void write_display_data_();
void init_reset_();
void display_init_(const uint8_t *addr);
void set_addr_window_(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
void draw_absolute_pixel_internal(int x, int y, Color color) override;
void spi_master_write_addr_(uint16_t addr1, uint16_t addr2);
void spi_master_write_color_(uint16_t color, uint16_t size);
int get_width_internal() override;
int get_height_internal() override;
const char *model_str_();
ST7735Model model_{ST7735_INITR_18BLACKTAB};
uint8_t colstart_ = 0, rowstart_ = 0;
boolean eightbitcolor_ = false;
boolean usebgr_ = false;
int16_t width_ = 80, height_ = 80; // Watch heap size
GPIOPin *reset_pin_{nullptr};
GPIOPin *dc_pin_{nullptr};
};
} // namespace st7735
} // namespace esphome

View file

@ -1704,7 +1704,18 @@ display:
backlight_pin: GPIO4 backlight_pin: GPIO4
lambda: |- lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height()); it.rectangle(0, 0, it.get_width(), it.get_height());
- platform: st7735
model: 'INITR_BLACKTAB'
cs_pin: GPIO5
dc_pin: GPIO16
reset_pin: GPIO23
rotation: 0
devicewidth: 128
deviceheight: 160
colstart: 0
rowstart: 0
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height());
tm1651: tm1651:
id: tm1651_battery id: tm1651_battery
clk_pin: GPIO23 clk_pin: GPIO23