initial commit
This commit is contained in:
49
README.md
49
README.md
@@ -1,10 +1,41 @@
|
||||
Additional components to use esphome with a M5StickC.
|
||||
Mostly a work in progress.
|
||||
Copy the components to a `custom_components` directory next to your .yaml configuration file.
|
||||
# ESPHome AXP192 Component
|
||||
|
||||
- axp192 : power management - configuration hard coded for the M5StickC - should be made more generic.
|
||||
Must be present to initialize the screen power.
|
||||
Can be also used to read some of the power info - most sensors aren't published yet.
|
||||
Mostly the AXP192 from M5Stack sample code adapted for esphome.
|
||||
- st7735 : TFT screen - configuration hard coded for the M5StickC - should be made more generic.
|
||||
Adapted from https://github.com/musk95/esphome-1/tree/dev/esphome/components/st7789v + M5StickC sample code for the init
|
||||
This custom component it to implement support for the AXP192 for both the M5Stick-C, and the M5Stack Core2, building on top of airy10's code.
|
||||
|
||||
## Installation
|
||||
|
||||
Copy the components to a custom_components directory next to your .yaml configuration file.
|
||||
|
||||
## Configuration
|
||||
|
||||
Sample configurations can be found within `/sample-config`. Please note that I've not yet been able to correctly configure the M5Stick-C screen just yet, however the AXP192 component does initalise it, and the sample configuration currently demostrates a white screen, when it should present some text. I plan to fix this soon.
|
||||
|
||||
This component adds a new model configuration to the AXP192 sensor, `model: M5CORE2` & `model: M5STICKC`, as so the right pins are initalized and voltages go to the right places:
|
||||
|
||||
### M5Stick-C
|
||||
|
||||
```yaml
|
||||
sensor:
|
||||
- platform: axp192
|
||||
model: M5STICKC
|
||||
address: 0x34
|
||||
i2c_id: bus_a
|
||||
update_interval: 30s
|
||||
battery_level:
|
||||
name: "M5Stick Battery Level"
|
||||
id: "m5stick_batterylevel"
|
||||
```
|
||||
|
||||
### M5Stack Core2
|
||||
|
||||
```yaml
|
||||
sensor:
|
||||
- platform: axp192
|
||||
model: M5CORE2 |
|
||||
address: 0x34
|
||||
i2c_id: bus_a
|
||||
update_interval: 30s
|
||||
battery_level:
|
||||
name: "${upper_devicename} Battery Level"
|
||||
id: "${devicename}_batterylevel"
|
||||
```
|
||||
@@ -6,10 +6,19 @@ namespace esphome {
|
||||
namespace axp192 {
|
||||
|
||||
static const char *TAG = "axp192.sensor";
|
||||
|
||||
void AXP192Component::setup()
|
||||
{
|
||||
begin(false, false, false, false, false);
|
||||
switch (this->model_) {
|
||||
case AXP192_M5STICKC:
|
||||
{
|
||||
begin(false, false, false, false, false);
|
||||
}
|
||||
case AXP192_M5CORE2:
|
||||
{
|
||||
// disable LDO3 Vibration
|
||||
begin(false, true, false, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AXP192Component::dump_config() {
|
||||
@@ -41,8 +50,20 @@ void AXP192Component::update() {
|
||||
|
||||
void AXP192Component::begin(bool disableLDO2, bool disableLDO3, bool disableRTC, bool disableDCDC1, bool disableDCDC3)
|
||||
{
|
||||
// Set LDO2 & LDO3(TFT_LED & TFT) 3.0V
|
||||
Write1Byte(0x28, 0xcc);
|
||||
switch (this->model_) {
|
||||
case AXP192_M5STICKC:
|
||||
{
|
||||
// Set LDO2 & LDO3(TFT_LED & TFT) 3.0V
|
||||
Write1Byte(0x28, 0xcc);
|
||||
}
|
||||
case AXP192_M5CORE2:
|
||||
{
|
||||
// Set DCDC3 (TFT_LED & TFT) 3.0V
|
||||
Write1Byte(0x27, 0xcc);
|
||||
// Set LDO2 & LDO3(TFT_LED & TFT) 3.0V
|
||||
Write1Byte(0x28, 0xcc);
|
||||
}
|
||||
}
|
||||
|
||||
// Set ADC sample rate to 200hz
|
||||
Write1Byte(0x84, 0b11110010);
|
||||
@@ -177,8 +198,18 @@ void AXP192Component::UpdateBrightness()
|
||||
{
|
||||
ubri = c_max;
|
||||
}
|
||||
uint8_t buf = Read8bit( 0x28 );
|
||||
Write1Byte( 0x28 , ((buf & 0x0f) | (ubri << 4)) );
|
||||
switch (this->model_) {
|
||||
case AXP192_M5STICKC:
|
||||
{
|
||||
uint8_t buf = Read8bit( 0x28 );
|
||||
Write1Byte( 0x28 , ((buf & 0x0f) | (ubri << 4)) );
|
||||
}
|
||||
case AXP192_M5CORE2:
|
||||
{
|
||||
uint8_t buf = Read8bit( 0x27 );
|
||||
Write1Byte( 0x27 , ((buf & 0x0f) | (ubri << 4)) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AXP192Component::GetBatState()
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
namespace esphome {
|
||||
namespace axp192 {
|
||||
|
||||
enum AXP192Model {
|
||||
AXP192_M5STICKC = 0,
|
||||
AXP192_M5CORE2,
|
||||
};
|
||||
|
||||
#define SLEEP_MSEC(us) (((uint64_t)us) * 1000L)
|
||||
#define SLEEP_SEC(us) (((uint64_t)us) * 1000000L)
|
||||
#define SLEEP_MIN(us) (((uint64_t)us) * 60L * 1000000L)
|
||||
@@ -26,6 +31,7 @@ class AXP192Component : public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void set_batterylevel_sensor(sensor::Sensor *batterylevel_sensor) { batterylevel_sensor_ = batterylevel_sensor; }
|
||||
void set_brightness(float brightness) { brightness_ = brightness; }
|
||||
void set_model(AXP192Model model) { this->model_ = model; }
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
@@ -38,14 +44,20 @@ protected:
|
||||
sensor::Sensor *batterylevel_sensor_;
|
||||
float brightness_{1.0f};
|
||||
float curr_brightness_{-1.0f};
|
||||
AXP192Model model_;
|
||||
|
||||
/**
|
||||
/** M5 Stick Values
|
||||
* LDO2: Display backlight
|
||||
* LDO3: Display Control
|
||||
* RTC: Don't set GPIO1 as LDO
|
||||
* RTC: Don't set GPIO1 as LDO
|
||||
* DCDC1: Main rail. When not set the controller shuts down.
|
||||
* DCDC3: Use unknown
|
||||
*/
|
||||
***********************
|
||||
* M5Stack Core2 Values
|
||||
* LDO2: ILI9342C PWR (Display)
|
||||
* LD03: Vibration Motor
|
||||
*/
|
||||
|
||||
void begin(bool disableLDO2 = false, bool disableLDO3 = false, bool disableRTC = false, bool disableDCDC1 = false, bool disableDCDC3 = false);
|
||||
void UpdateBrightness();
|
||||
bool GetBatState();
|
||||
|
||||
@@ -2,16 +2,24 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import CONF_ID,\
|
||||
CONF_BATTERY_LEVEL, CONF_BRIGHTNESS, UNIT_PERCENT, ICON_BATTERY
|
||||
CONF_BATTERY_LEVEL, CONF_BRIGHTNESS, UNIT_PERCENT, ICON_BATTERY, CONF_MODEL
|
||||
|
||||
DEPENDENCIES = ['i2c']
|
||||
|
||||
axp192_ns = cg.esphome_ns.namespace('axp192')
|
||||
|
||||
AXP192Component = axp192_ns.class_('AXP192Component', cg.PollingComponent, i2c.I2CDevice)
|
||||
AXP192Model = axp192_ns.enum("AXP192Model")
|
||||
|
||||
MODELS = {
|
||||
"M5CORE2": AXP192Model.AXP192_M5CORE2,
|
||||
"M5STICKC": AXP192Model.AXP192_M5STICKC,
|
||||
}
|
||||
|
||||
AXP192_MODEL = cv.enum(MODELS, upper=True, space="_")
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(AXP192Component),
|
||||
cv.Required(CONF_MODEL): AXP192_MODEL,
|
||||
cv.Optional(CONF_BATTERY_LEVEL):
|
||||
sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_PERCENT,
|
||||
@@ -27,6 +35,7 @@ def to_code(config):
|
||||
yield cg.register_component(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
|
||||
cg.add(var.set_model(config[CONF_MODEL]))
|
||||
if CONF_BATTERY_LEVEL in config:
|
||||
conf = config[CONF_BATTERY_LEVEL]
|
||||
sens = yield sensor.new_sensor(conf)
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
import esphome.codegen as cg
|
||||
|
||||
st7735_ns = cg.esphome_ns.namespace('st7735')
|
||||
@@ -1,41 +0,0 @@
|
||||
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, display.DisplayBuffer)
|
||||
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)
|
||||
@@ -1,349 +0,0 @@
|
||||
#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);
|
||||
|
||||
delay(120);
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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
|
||||
@@ -1,163 +0,0 @@
|
||||
#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
|
||||
|
||||
namespace esphome {
|
||||
namespace st7735 {
|
||||
|
||||
template <int N>
|
||||
struct Color565 {
|
||||
operator Color () const
|
||||
{
|
||||
return Color((((N)&0xF800) >> 8), (((N)&0x07E0) >> 3), (((N)&0x001F) << 3));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace st7735
|
||||
} // namespace esphome
|
||||
|
||||
// Some ready-made 16-bit ('565') color settings:
|
||||
#define ST77XX_BLACK esphome::st7735::Color565<0x0000>{} /* 0, 0, 0 */
|
||||
#define ST77XX_WHITE esphome::st7735::Color565<0xFFFF>{} /* 255, 255, 255 */
|
||||
#define ST77XX_RED esphome::st7735::Color565<0xF800>{} /* 255, 0, 0 */
|
||||
#define ST77XX_GREEN esphome::st7735::Color565<0x07E0>{} /* 0, 255, 0 */
|
||||
#define ST77XX_BLUE esphome::st7735::Color565<0x001F>{} /* 0, 0, 255 */
|
||||
#define ST77XX_CYAN esphome::st7735::Color565<0x07FF>{} /* 0, 255, 255 */
|
||||
#define ST77XX_MAGENTA esphome::st7735::Color565<0xF81F>{} /* 255, 0, 255 */
|
||||
#define ST77XX_YELLOW esphome::st7735::Color565<0xFFE0>{} /* 255, 255, 0 */
|
||||
#define ST77XX_ORANGE esphome::st7735::Color565<0xFDA0>{} /* 255, 180, 0 */
|
||||
|
||||
// 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
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 90 KiB |
0
sample-config/m5core2.yaml
Normal file
0
sample-config/m5core2.yaml
Normal file
@@ -1,26 +1,32 @@
|
||||
substitutions:
|
||||
devicename: m5stick
|
||||
upper_devicename: M5StickC
|
||||
|
||||
esphome:
|
||||
name: $devicename
|
||||
name: m5stick-c
|
||||
platform: ESP32
|
||||
board: m5stick-c
|
||||
platformio_options:
|
||||
upload_speed: 115200
|
||||
|
||||
# Use this to hardlink the component if it is not discovered
|
||||
# includes:
|
||||
# - /config/custom_components/axp192/axp192.h
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_ssid
|
||||
password: !secret wifi_password
|
||||
|
||||
ssid: "SSID"
|
||||
password: "PASSPHRASE"
|
||||
|
||||
# Enable fallback hotspot (captive portal) in case wifi connection fails
|
||||
ap:
|
||||
ssid: $devicename Fallback Hotspot
|
||||
password: !secret wifi_password
|
||||
ssid: "XXX"
|
||||
password: "XXX"
|
||||
|
||||
# Enable logging
|
||||
logger:
|
||||
|
||||
substitutions:
|
||||
devicename: m5stick
|
||||
upper_devicename: M5StickC
|
||||
|
||||
captive_portal:
|
||||
|
||||
logger:
|
||||
|
||||
api:
|
||||
|
||||
ota:
|
||||
@@ -56,6 +62,7 @@ binary_sensor:
|
||||
# AXP192 power management - must be present to initialize TFT power on
|
||||
sensor:
|
||||
- platform: axp192
|
||||
model: M5STICKC
|
||||
address: 0x34
|
||||
i2c_id: bus_a
|
||||
update_interval: 30s
|
||||
@@ -82,15 +89,18 @@ output:
|
||||
inverted: true
|
||||
id: builtin_led
|
||||
|
||||
|
||||
# internal IR Transmitter
|
||||
remote_transmitter:
|
||||
- pin:
|
||||
number: GPIO9
|
||||
carrier_duty_percent: 50%
|
||||
id: internal
|
||||
|
||||
|
||||
#############################################
|
||||
### Currently causes an error with compilation
|
||||
##
|
||||
# internal IR Transmitter
|
||||
# remote_transmitter:
|
||||
# - pin:
|
||||
# number: GPIO9
|
||||
# carrier_duty_percent: 50%
|
||||
# id: internal
|
||||
##
|
||||
###
|
||||
#############################################
|
||||
spi:
|
||||
clk_pin: GPIO13
|
||||
mosi_pin: GPIO15
|
||||
@@ -102,41 +112,23 @@ i2c:
|
||||
scan: True
|
||||
|
||||
font:
|
||||
- file: 'arial.ttf'
|
||||
- file: 'fonts/arial.ttf'
|
||||
id: font1
|
||||
size: 8
|
||||
|
||||
- file: 'arial.ttf'
|
||||
id: font2
|
||||
size: 36
|
||||
|
||||
- file: 'arial.ttf'
|
||||
id: font3
|
||||
size: 14
|
||||
|
||||
# builtin 80x160 TFT
|
||||
display:
|
||||
- platform: st7735
|
||||
model: "INITR_MINI160X80"
|
||||
cs_pin: GPIO5
|
||||
dc_pin: GPIO23
|
||||
reset_pin: GPIO18
|
||||
rotation: 270
|
||||
device_width: 128
|
||||
device_height: 160
|
||||
col_start: 0
|
||||
row_start: 0
|
||||
eight_bit_color: true
|
||||
update_interval: 5s
|
||||
lambda: |-
|
||||
it.print(80, 0, id(font1), ST77XX_WHITE, TextAlign::TOP_CENTER, "M5Stick Test");
|
||||
if (id(wifi_dbm).has_state()) {
|
||||
char str[128];
|
||||
sprintf(str, "%.0f dBm", id(wifi_dbm).state);
|
||||
it.print(80, 40, id(font2), ST77XX_RED, TextAlign::CENTER, str);
|
||||
//it.print(80, 40, id(font2), ST77XX_RED, TextAlign::CENTER, "%.0f dBm", id(wifi_dbm).state);
|
||||
} else {
|
||||
it.print(80, 40, id(font2), ST77XX_RED, TextAlign::CENTER, "No Wifi");
|
||||
}
|
||||
// Print time in HH:MM format
|
||||
it.strftime(80, 60, id(font3), ST77XX_BLUE, TextAlign::TOP_CENTER, "%H:%M", id(sntp_time).now());
|
||||
|
||||
time:
|
||||
- platform: homeassistant
|
||||
id: homeassistant_time
|
||||
- platform: sntp
|
||||
id: sntp_time
|
||||
|
||||
it.print(80, 0, id(font1), TextAlign::TOP_CENTER, "M5Stick Test");
|
||||
Reference in New Issue
Block a user