This commit is contained in:
2024-05-23 21:24:52 +02:00
commit a3efe8274b
166 changed files with 15713 additions and 0 deletions

View File

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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,41 @@
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_ID, CONF_LAMBDA
from esphome.const import CONF_DC_PIN, CONF_CS_PIN, CONF_ID, CONF_LAMBDA, CONF_PAGES
from esphome.const import CONF_EXTERNAL_VCC, CONF_LAMBDA, CONF_MODEL, CONF_RESET_PIN, \
CONF_BRIGHTNESS
from . import st7735_ns
DEPENDENCIES = ['spi']
ST7735 = st7735_ns.class_('ST7735', cg.PollingComponent, spi.SPIDevice)
ST7735Ref = ST7735.operator('ref')
CONFIG_SCHEMA = display.FULL_DISPLAY_SCHEMA.extend({
cv.GenerateID(): cv.declare_id(ST7735),
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.Optional(CONF_BRIGHTNESS, default=1.0): cv.percentage,
}).extend(cv.polling_component_schema('1s')).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))
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,351 @@
#include "st7735.h"
#include "esphome/core/log.h"
#include "esphome/components/display/display_buffer.h"
namespace esphome {
namespace st7735 {
#define ST_CMD_DELAY 0x80 // special signifier for command lists
#define ST77XX_NOP 0x00
#define ST77XX_SWRESET 0x01
#define ST77XX_RDDID 0x04
#define ST77XX_RDDST 0x09
#define ST77XX_SLPIN 0x10
#define ST77XX_SLPOUT 0x11
#define ST77XX_PTLON 0x12
#define ST77XX_NORON 0x13
#define ST77XX_INVOFF 0x20
#define ST77XX_INVON 0x21
#define ST77XX_DISPOFF 0x28
#define ST77XX_DISPON 0x29
#define ST77XX_CASET 0x2A
#define ST77XX_RASET 0x2B
#define ST77XX_RAMWR 0x2C
#define ST77XX_RAMRD 0x2E
#define ST77XX_PTLAR 0x30
#define ST77XX_TEOFF 0x34
#define ST77XX_TEON 0x35
#define ST77XX_MADCTL 0x36
#define ST77XX_COLMOD 0x3A
#define ST77XX_MADCTL_MY 0x80
#define ST77XX_MADCTL_MX 0x40
#define ST77XX_MADCTL_MV 0x20
#define ST77XX_MADCTL_ML 0x10
#define ST77XX_MADCTL_RGB 0x00
#define ST77XX_RDID1 0xDA
#define ST77XX_RDID2 0xDB
#define ST77XX_RDID3 0xDC
#define ST77XX_RDID4 0xDD
// some flags for initR() :(
#define INITR_GREENTAB 0x00
#define INITR_REDTAB 0x01
#define INITR_BLACKTAB 0x02
#define INITR_18GREENTAB INITR_GREENTAB
#define INITR_18REDTAB INITR_REDTAB
#define INITR_18BLACKTAB INITR_BLACKTAB
#define INITR_144GREENTAB 0x01
#define INITR_MINI160x80 0x04
#define INITR_HALLOWING 0x05
// Some register settings
#define ST7735_MADCTL_BGR 0x08
#define ST7735_MADCTL_MH 0x04
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
static const uint8_t PROGMEM
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
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
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
static const char *TAG = "st7735";
void ST7735::setup() {
ESP_LOGCONFIG(TAG, "Setting up SPI ST7735...");
this->spi_setup();
this->dc_pin_->setup(); // OUTPUT
this->init_reset_();
this->displayInit(Rcmd1);
this->displayInit(Rcmd2green);
this->displayInit(Rcmd3);
this->writecommand(ST7735_INVON);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//this->disable();
delay(120);
//this->enable();
this->writecommand(ST7735_DISPON); //Display on
delay(120);
this->init_internal_(this->get_buffer_length_());
memset(this->buffer_, 0x00, this->get_buffer_length_());
}
void ST7735::write_display_data() {
uint16_t offsetx = 26;
uint16_t offsety = 1;
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);
this->write_array(this->buffer_, this->get_buffer_length_());
this->disable();
}
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);
}
void ST7735::dump_config() {
LOG_DISPLAY("", "SPI ST7735", this);
LOG_PIN(" CS Pin: ", this->cs_);
LOG_PIN(" DC Pin: ", this->dc_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
LOG_UPDATE_INTERVAL(this);
}
float ST7735::get_setup_priority() const {
return setup_priority::PROCESSOR;
}
void ST7735::update() {
this->do_update_();
this->write_display_data();
}
void ST7735::loop() {
}
int ST7735::get_width_internal() {
return 80;
}
int ST7735::get_height_internal() {
return 160;
}
size_t ST7735::get_buffer_length_() {
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;
const uint32_t color565 = color.to_rgb_565();
// where should the bits go in the big buffer array? math...
uint16_t pos = (x + y * this->get_width_internal()) * 2;
this->buffer_[pos++] = (color565 >> 8) & 0xff;
this->buffer_[pos] = color565 & 0xff;
}
void ST7735::displayInit(const uint8_t *addr) {
uint8_t numCommands, cmd, numArgs;
uint16_t ms;
numCommands = pgm_read_byte(addr++); // Number of commands to follow
while(numCommands--) { // For each command...
cmd = pgm_read_byte(addr++); // Read command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & ST_CMD_DELAY; // If hibit set, delay follows args
numArgs &= ~ST_CMD_DELAY; // Mask out delay bit
this->sendcommand(cmd, addr, numArgs);
addr += numArgs;
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::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 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 ST7735::writedata(uint8_t value) {
this->dc_pin_->digital_write(true);
this->enable();
this->write_byte(value);
this->disable();
}
void ST7735::sendcommand(uint8_t cmd, const uint8_t* dataBytes, uint8_t numDataBytes) {
this->writecommand(cmd);
this->senddata(dataBytes,numDataBytes);
}
void ST7735::senddata(const uint8_t* dataBytes, uint8_t numDataBytes) {
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<numDataBytes; i++) {
this->transfer_byte(pgm_read_byte(dataBytes++)); //write byte - SPI library
}
this->cs_->digital_write(true);
this->disable();
}
} // namespace st7735
} // namespace esphome

149
components/st7735/st7735.h Normal file
View File

@@ -0,0 +1,149 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/display/display_buffer.h"
// ST7735 specific commands used in init
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_RDDPM 0x0A // Read display power mode
#define ST7735_RDD_MADCTL 0x0B // Read display MADCTL
#define ST7735_RDD_COLMOD 0x0C // Read display pixel format
#define ST7735_RDDIM 0x0D // Read display image mode
#define ST7735_RDDSM 0x0E // Read display signal mode
#define ST7735_RDDSR 0x0F // Read display self-diagnostic result (ST7735)
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_INVOFF 0x20
#define ST7735_INVON 0x21
#define ST7735_GAMSET 0x26 // Gamma set
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
#define ST7735_RGBSET 0x2D // Color setting for 4096, 64K and 262K colors
#define ST7735_RAMRD 0x2E
#define ST7735_PTLAR 0x30
#define ST7735_VSCRDEF 0x33 // Vertical scrolling definition (ST7735)
#define ST7735_TEOFF 0x34 // Tearing effect line off
#define ST7735_TEON 0x35 // Tearing effect line on
#define ST7735_MADCTL 0x36 // Memory data access control
#define ST7735_IDMOFF 0x38 // Idle mode off
#define ST7735_IDMON 0x39 // Idle mode on
#define ST7735_RAMWRC 0x3C // Memory write continue (ST7735)
#define ST7735_RAMRDC 0x3E // Memory read continue (ST7735)
#define ST7735_COLMOD 0x3A
#define ST7735_RAMCTRL 0xB0 // RAM control
#define ST7735_RGBCTRL 0xB1 // RGB control
#define ST7735_PORCTRL 0xB2 // Porch control
#define ST7735_FRCTRL1 0xB3 // Frame rate control
#define ST7735_PARCTRL 0xB5 // Partial mode control
#define ST7735_GCTRL 0xB7 // Gate control
#define ST7735_GTADJ 0xB8 // Gate on timing adjustment
#define ST7735_DGMEN 0xBA // Digital gamma enable
#define ST7735_VCOMS 0xBB // VCOMS setting
#define ST7735_LCMCTRL 0xC0 // LCM control
#define ST7735_IDSET 0xC1 // ID setting
#define ST7735_VDVVRHEN 0xC2 // VDV and VRH command enable
#define ST7735_VRHS 0xC3 // VRH set
#define ST7735_VDVSET 0xC4 // VDV setting
#define ST7735_VCMOFSET 0xC5 // VCOMS offset set
#define ST7735_FRCTR2 0xC6 // FR Control 2
#define ST7735_CABCCTRL 0xC7 // CABC control
#define ST7735_REGSEL1 0xC8 // Register value section 1
#define ST7735_REGSEL2 0xCA // Register value section 2
#define ST7735_PWMFRSEL 0xCC // PWM frequency selection
#define ST7735_PWCTRL1 0xD0 // Power control 1
#define ST7735_VAPVANEN 0xD2 // Enable VAP/VAN signal output
#define ST7735_CMD2EN 0xDF // Command 2 enable
#define ST7735_PVGAMCTRL 0xE0 // Positive voltage gamma control
#define ST7735_NVGAMCTRL 0xE1 // Negative voltage gamma control
#define ST7735_DGMLUTR 0xE2 // Digital gamma look-up table for red
#define ST7735_DGMLUTB 0xE3 // Digital gamma look-up table for blue
#define ST7735_GATECTRL 0xE4 // Gate control
#define ST7735_SPI2EN 0xE7 // SPI2 enable
#define ST7735_PWCTRL2 0xE8 // Power control 2
#define ST7735_EQCTRL 0xE9 // Equalize time control
#define ST7735_PROMCTRL 0xEC // Program control
#define ST7735_PROMEN 0xFA // Program mode enable
#define ST7735_NVMSET 0xFC // NVM setting
#define ST7735_PROMACT 0xFE // Program action
// Some ready-made 16-bit ('565') color settings:
#define ST77XX_BLACK 0x0000
#define ST77XX_WHITE 0xFFFF
#define ST77XX_RED 0xF800
#define ST77XX_GREEN 0x07E0
#define ST77XX_BLUE 0x001F
#define ST77XX_CYAN 0x07FF
#define ST77XX_MAGENTA 0xF81F
#define ST77XX_YELLOW 0xFFE0
#define ST77XX_ORANGE 0xFC00
// Some ready-made 16-bit ('565') color settings:
#define ST7735_BLACK ST77XX_BLACK
#define ST7735_WHITE ST77XX_WHITE
#define ST7735_RED ST77XX_RED
#define ST7735_GREEN ST77XX_GREEN
#define ST7735_BLUE ST77XX_BLUE
#define ST7735_CYAN ST77XX_CYAN
#define ST7735_MAGENTA ST77XX_MAGENTA
#define ST7735_YELLOW ST77XX_YELLOW
#define ST7735_ORANGE ST77XX_ORANGE
namespace esphome {
namespace st7735 {
class ST7735 : 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; }
// ========== 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};
void displayInit(const uint8_t *addr);
void sendcommand(uint8_t cmd, const uint8_t* dataBytes, uint8_t numDataBytes);
void senddata(const uint8_t* dataBytes, uint8_t numDataBytes);
void init_reset_();
void writecommand(uint8_t value);
void writedata(uint8_t value);
void spi_master_write_addr(uint16_t addr1, uint16_t addr2);
void spi_master_write_color(uint16_t color, uint16_t size);
void draw_absolute_pixel_internal(int x, int y, Color color) override;
int get_height_internal() override;
int get_width_internal() override;
size_t get_buffer_length_();
};
} // namespace st7735
} // namespace esphome