Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFT-LCD ST7789V of ESP32 TTGO. #918

Closed
wants to merge 1 commit into from
Closed

TFT-LCD ST7789V of ESP32 TTGO. #918

wants to merge 1 commit into from

Conversation

musk95
Copy link
Contributor

@musk95 musk95 commented Dec 12, 2019

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

Description:

Related issue (if applicable): fixes

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

This patch allows you to use TFT-LCD ST7789V of ESP32 TTGO
@grinco
Copy link

grinco commented Jan 28, 2020

I have one of those boards, can help out with testing if needed.

@tzapu
Copy link

tzapu commented Feb 1, 2020

i also have one of these boards (TTGO T-Display) and could test

@mvturnho
Copy link
Contributor

mvturnho commented Feb 20, 2020

@musk95 Do you also have the corresponding configuration yaml with correct pins.

this->buffer_[pos++] |= (color>>8) & 0xff;
this->buffer_[pos] |= color & 0xff;
Copy link

@a74451 a74451 Mar 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, to work I have to remove de "|"
this->buffer_[pos++] = (color>>8) & 0xff;
this->buffer_[pos] = color & 0xff;

Copy link
Contributor

@grea09 grea09 Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with this correction, everything displays correctly.

Copy link

@chsw chsw Apr 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also confirm that the above suggestions work as expected (new text replaces previous text, instead of add graphics on top of each-other.

Copy link
Member

@kbx81 kbx81 May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here...this minor change fixed the overlaid pixels for me, too. 😄

@grea09
Copy link
Contributor

grea09 commented Apr 10, 2020

I just tested that PR on my hardware (a TTGO display) and it works great. Here's the configuration I found worked:

image:
  - file: "pic.png"
    id: pic
    type: RGB565

spi:
  clk_pin: 18
  mosi_pin: 19

display:
  - platform: st7789v
    reset_pin: 23
    bl_pin: 4
    cs_pin: 5
    dc_pin: 16
    lambda: |-
      it.image(0, 0, id(pic));

I hope it helps merging that amazing job !

@pulimento
Copy link

pulimento commented Apr 10, 2020

will test if needed. thanks

rhs = [HexInt(x) for x in data]
prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs)
cg.new_Pvariable(config[CONF_ID], prog_arr, width, height)
if config[CONF_TYPE].startswith('RGB565'):
Copy link
Contributor

@grea09 grea09 Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line needs a null check. If no type is specified the code generation crashes.

@spattinson
Copy link
Contributor

spattinson commented Apr 11, 2020

I hope this gets accepted, st7789v displays are a very nice colourful display and t-displays are cheap with a battery interface. I'll test it later, i have two of them.

@chsw
Copy link

chsw commented Apr 19, 2020

To be able to draw lines and write text, the color needs to be specified in the lambda:
This wasn't immediately obvious for me:

font:
  - file: "arial.ttf"
    id: font_medium
    size: 30

substitutions:
  WHITE: '0xFFFF'
  RED: '0xF800'
  BLUE: '0x001F'
  GREEN: '0x07E0'
  YELLOW: '0xFFE0'
  MAGENTA: '0xF81F'
  CYAN: '0x07FF'

spi:
  clk_pin: 18
  mosi_pin: 19

display:
  - platform: st7789v
    reset_pin: 23
    bl_pin: 4
    cs_pin: 5
    dc_pin: 16
    rotation: 270
    lambda: |-
      it.strftime(240, 0, id(font_medium), ${WHITE}, TextAlign::TOP_RIGHT, "%H:%M", id(esptime).now());
time:
  - platform: homeassistant
    id: esptime

@glmnet
Copy link
Member

glmnet commented Apr 19, 2020

this one needs lint and docs, is @musk95 around?


CONF_BACKLIGHT_PIN = 'bl_pin'

ST7789V = st7789v_ns.class_('ST7789V', cg.PollingComponent, spi.SPIDevice)
Copy link
Contributor

@grea09 grea09 Apr 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to add display.DisplayBufferin the list here

@kbx81
Copy link
Member

kbx81 commented May 4, 2020

I just picked up a couple of these boards last week and started working on my own branch to add this support. Then I found this! 😆 I've cloned this fork and I've got it up and running. Thanks for getting this rolling @musk95! Please let me know if there is anything I can do to help push this along so we can get it merged into dev in time for the next release! 😄🍻

@grea09
Copy link
Contributor

grea09 commented May 4, 2020

Since the original author seems to not respond, would it be wise to fork that branch in order to proceed with the PR ? I'm not sure about the code of conduct in that case and I want @musk95 to be credited for his awesome work but also to make that patch a reality. Any advice on that ? I'm ready to write the doc and all of needed.

@glmnet
Copy link
Member

glmnet commented May 4, 2020

Sure help is appreciated

I am still supporting ESPHome, Otto is away but I'm pretty sure he will be around at some time.
There are only 3 of us who can commit to ESPHome due to permission stuff and Otto is the only one who can change that.

I believe you can fork @musk95 branch make all the changes needed then create another PR, this way he will be still be credited as you will be adding commits on top of his work.

@kbx81
Copy link
Member

kbx81 commented May 4, 2020

Oh cool! Thanks @glmnet. I'm happy to fork the fork, make the changes suggested here and bring my changes in, as well. I had a bunch of other stuff I was cleaning up in my branch already, like getting rid of the macro nightmare that lives in st7789v.h among some other things. The C/C++ code here was pretty clearly 95% copy/pasted from https://github.com/Bodmer/TFT_eSPI which is great but I'm pretty sure the original author spent more time writing compiler macros than they did writing C/C++ code. 😆
Give me a day or two (it's pretty close already!) and I will get a new PR going so we can get this (ready to be) merged into dev ASAP. 🍻

@kbx81
Copy link
Member

kbx81 commented May 4, 2020

...as long as nobody objects, that is. 😇

@kbx81
Copy link
Member

kbx81 commented May 4, 2020

One related question regarding the bits @chsw pointed out...

Where is this stuff currently documented? I haven't done much digging just yet, but a cursory search around the existing ESPHome docs reveals little about substitutions: and I can't find anything about the extra parameters passed to the function(s) in the lambada. That said, it's quite possible I've overlooked something (or am just lacking experience here 😂). In any event, I'd like to get a branch going in esphome-docs to complement this and it would be helpful to refer back to such documentation, if it exists. @chsw would you mind sharing with us where you found this info? 😄

@chsw
Copy link

chsw commented May 4, 2020

According to the documentation, there is no support for color displays. Figured out how to set the colors by trial and error and looking at the source files. The display has 18 bit color depth, but I used a 16 bit color palette found online to set create the color definitions stored as substitutions.

@kbx81
Copy link
Member

kbx81 commented May 4, 2020

I was afraid you were going to say that. 😆 Thank you for (so very promptly) sharing! 🍻

@kbx81
Copy link
Member

kbx81 commented May 5, 2020

Alrighty...hopefully this will make all of you happy! 😄
#1050
Pulled in @musk95's changes and polished it all up a whole bunch. Works on my display...anybody else care to test?

Here is some fun YAML you can try:

font:
  - file: "Helvetica.ttf"
    id: helvetica_48
    size: 48
  - file: "Helvetica.ttf"
    id: helvetica_24
    size: 24
  - file: "Helvetica.ttf"
    id: helvetica_12
    size: 12

binary_sensor:
  - platform: status
    name: "Node Status"
    id: system_status
  - platform: gpio
    pin:
      number: GPIO0
      inverted: true
    name: "T-Display Button Input 0"
    id: tdisplay_button_input_0
  - platform: gpio
    pin:
      number: GPIO35
      inverted: true
    name: "T-Display Button Input 1"
    id: tdisplay_button_input_1

switch:
  - platform: gpio
    pin: GPIO4
    name: "Backlight"
    id: backlight

substitutions:
  WHITE: '(0x1F << 11) | (0x3F << 5) | (0x1F << 0)'
  GRAY: '(0x10 << 11) | (0x20 << 5) | (0x10 << 0)'
  RED: '(0x1F << 11)'
  GREEN: '(0x3F << 5)'
  BLUE: '(0x1F << 0)'
  YELLOW: '(0x1F << 11) | (0x3F << 5)'
  MAGENTA: '(0x1F << 11) | (0x1F << 0)'
  CYAN: '(0x3F << 5) | (0x1F << 0)'

image:
  - file: "image.png"
    id: my_image
    resize: 200x200
    type: RGB565

time:
  - platform: homeassistant
    id: esptime

spi:
  clk_pin: GPIO18
  mosi_pin: GPIO19

display:
  - platform: st7789v
    reset_pin: GPIO23
    cs_pin: GPIO5
    dc_pin: GPIO16
    backlight_pin: GPIO4
    rotation: 90
    lambda: |-
      it.rectangle(0,  0, it.get_width(), it.get_height(), ${BLUE});
      it.rectangle(0, 20, it.get_width(), it.get_height(), ${BLUE});   // header bar

      it.strftime((240 / 2), (140 / 3) * 1 + 5, id(helvetica_24), ${GRAY}, TextAlign::CENTER, "%Y-%m-%d", id(esptime).now());
      it.strftime((240 / 2), (140 / 3) * 2 + 5, id(helvetica_48), ${GRAY}, TextAlign::CENTER, "%H:%M:%S", id(esptime).now());
      it.print(5, 5, id(helvetica_12), ${YELLOW}, TextAlign::TOP_LEFT, "ESPHome");

      // it.image(0, 0, id(my_image));
      
      if (id(system_status).state) {
        it.print(235, 5, id(helvetica_12), ${GREEN}, TextAlign::TOP_RIGHT, "Online");
      }
      else {
        it.print(235, 5, id(helvetica_12), ${RED}, TextAlign::TOP_RIGHT, "Offline");
      }

You'll just need to supply Helvetica.ttf (or change it to a font of your liking) and image.png. Comment/uncomment/change the lines of C in the lambada to your liking. 😄

@kbx81
Copy link
Member

kbx81 commented May 6, 2020

Docs have landed: esphome/esphome-docs#594

@glmnet
Copy link
Member

glmnet commented May 28, 2020

continuing in #1050

@glmnet glmnet closed this May 28, 2020
@lock lock bot locked and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet