esphome/esphome/components/lcd_base/lcd_display.cpp
Otto Winter ac0d921413
ESP-IDF support and generic target platforms (#2303)
* Socket refactor and SSL

* esp-idf temp

* Fixes

* Echo component and noise

* Add noise API transport support

* Updates

* ESP-IDF

* Complete

* Fixes

* Fixes

* Versions update

* New i2c APIs

* Complete i2c refactor

* SPI migration

* Revert ESP Preferences migration, too complex for now

* OTA support

* Remove echo again

* Remove ssl again

* GPIOFlags updates

* Rename esphal and ICACHE_RAM_ATTR

* Make ESP32 arduino compilable again

* Fix GPIO flags

* Complete pin registry refactor and fixes

* Fixes to make test1 compile

* Remove sdkconfig file

* Ignore sdkconfig file

* Fixes in reviewing

* Make test2 compile

* Make test4 compile

* Make test5 compile

* Run clang-format

* Fix lint errors

* Use esp-idf APIs instead of btStart

* Another round of fixes

* Start implementing ESP8266

* Make test3 compile

* Guard esp8266 code

* Lint

* Reformat

* Fixes

* Fixes v2

* more fixes

* ESP-IDF tidy target

* Convert ARDUINO_ARCH_ESPxx

* Update WiFiSignalSensor

* Update time ifdefs

* OTA needs millis from hal

* RestartSwitch needs delay from hal

* ESP-IDF Uart

* Fix OTA blank password

* Allow setting sdkconfig

* Fix idf partitions and allow setting sdkconfig from yaml

* Re-add read/write compat APIs and fix esp8266 uart

* Fix esp8266 store log strings in flash

* Fix ESP32 arduino preferences not initialized

* Update ifdefs

* Change how sdkconfig change is detected

* Add checks to ci-custom and fix them

* Run clang-format

* Add esp-idf clang-tidy target and fix errors

* Fixes from clang-tidy idf round 2

* Fixes from compiling tests with esp-idf

* Run clang-format

* Switch test5.yaml to esp-idf

* Implement ESP8266 Preferences

* Lint

* Re-do PIO package version selection a bit

* Fix arduinoespressif32 package version

* Fix unit tests

* Lint

* Lint fixes

* Fix readv/writev not defined

* Fix graphing component

* Re-add all old options from core/config.py

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
2021-09-20 11:47:51 +02:00

165 lines
5.4 KiB
C++

#include "lcd_display.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace lcd_base {
static const char *const TAG = "lcd";
// First set bit determines command, bits after that are the data.
static const uint8_t LCD_DISPLAY_COMMAND_CLEAR_DISPLAY = 0x01;
static const uint8_t LCD_DISPLAY_COMMAND_RETURN_HOME = 0x02;
static const uint8_t LCD_DISPLAY_COMMAND_ENTRY_MODE_SET = 0x04;
static const uint8_t LCD_DISPLAY_COMMAND_DISPLAY_CONTROL = 0x08;
static const uint8_t LCD_DISPLAY_COMMAND_CURSOR_SHIFT = 0x10;
static const uint8_t LCD_DISPLAY_COMMAND_FUNCTION_SET = 0x20;
static const uint8_t LCD_DISPLAY_COMMAND_SET_CGRAM_ADDR = 0x40;
static const uint8_t LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR = 0x80;
static const uint8_t LCD_DISPLAY_ENTRY_SHIFT_INCREMENT = 0x01;
static const uint8_t LCD_DISPLAY_ENTRY_LEFT = 0x02;
static const uint8_t LCD_DISPLAY_DISPLAY_BLINK_ON = 0x01;
static const uint8_t LCD_DISPLAY_DISPLAY_CURSOR_ON = 0x02;
static const uint8_t LCD_DISPLAY_DISPLAY_ON = 0x04;
static const uint8_t LCD_DISPLAY_FUNCTION_8_BIT_MODE = 0x10;
static const uint8_t LCD_DISPLAY_FUNCTION_2_LINE = 0x08;
static const uint8_t LCD_DISPLAY_FUNCTION_5X10_DOTS = 0x04;
void LCDDisplay::setup() {
this->buffer_ = new uint8_t[this->rows_ * this->columns_]; // NOLINT
for (uint8_t i = 0; i < this->rows_ * this->columns_; i++)
this->buffer_[i] = ' ';
uint8_t display_function = 0;
if (!this->is_four_bit_mode())
display_function |= LCD_DISPLAY_FUNCTION_8_BIT_MODE;
if (this->rows_ > 1)
display_function |= LCD_DISPLAY_FUNCTION_2_LINE;
// TODO dotsize
// Commands can only be sent 40ms after boot-up, so let's wait if we're close
const uint8_t now = millis();
if (now < 40)
delay(40u - now);
if (this->is_four_bit_mode()) {
this->write_n_bits(0x03, 4);
delay(5); // 4.1ms
this->write_n_bits(0x03, 4);
delay(5);
this->write_n_bits(0x03, 4);
delayMicroseconds(150);
this->write_n_bits(0x02, 4);
} else {
this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
delay(5); // 4.1ms
this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
delayMicroseconds(150);
this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
}
this->command_(LCD_DISPLAY_COMMAND_FUNCTION_SET | display_function);
uint8_t display_control = LCD_DISPLAY_DISPLAY_ON;
this->command_(LCD_DISPLAY_COMMAND_DISPLAY_CONTROL | display_control);
// clear display, also sets DDRAM address to 0 (home)
this->command_(LCD_DISPLAY_COMMAND_CLEAR_DISPLAY);
delay(2); // 1.52ms
uint8_t entry_mode = LCD_DISPLAY_ENTRY_LEFT;
this->command_(LCD_DISPLAY_COMMAND_ENTRY_MODE_SET | entry_mode); // 37µs
this->command_(LCD_DISPLAY_COMMAND_RETURN_HOME);
delay(2); // 1.52ms
}
float LCDDisplay::get_setup_priority() const { return setup_priority::PROCESSOR; }
void HOT LCDDisplay::display() {
this->command_(LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR | 0);
for (uint8_t i = 0; i < this->columns_; i++)
this->send(this->buffer_[i], true);
if (this->rows_ >= 3) {
for (uint8_t i = 0; i < this->columns_; i++)
this->send(this->buffer_[this->columns_ * 2 + i], true);
}
if (this->rows_ >= 1) {
this->command_(LCD_DISPLAY_COMMAND_SET_DDRAM_ADDR | 0x40);
for (uint8_t i = 0; i < this->columns_; i++)
this->send(this->buffer_[this->columns_ + i], true);
if (this->rows_ >= 4) {
for (uint8_t i = 0; i < this->columns_; i++)
this->send(this->buffer_[this->columns_ * 3 + i], true);
}
}
}
void LCDDisplay::update() {
this->clear();
this->call_writer();
this->display();
}
void LCDDisplay::command_(uint8_t value) { this->send(value, false); }
void LCDDisplay::print(uint8_t column, uint8_t row, const char *str) {
uint8_t pos = column + row * this->columns_;
for (; *str != '\0'; str++) {
if (*str == '\n') {
pos = ((pos / this->columns_) + 1) * this->columns_;
continue;
}
if (pos >= this->rows_ * this->columns_) {
ESP_LOGW(TAG, "LCDDisplay writing out of range!");
break;
}
this->buffer_[pos] = *reinterpret_cast<const uint8_t *>(str);
pos++;
}
}
void LCDDisplay::print(uint8_t column, uint8_t row, const std::string &str) { this->print(column, row, str.c_str()); }
void LCDDisplay::print(const char *str) { this->print(0, 0, str); }
void LCDDisplay::print(const std::string &str) { this->print(0, 0, str.c_str()); }
void LCDDisplay::printf(uint8_t column, uint8_t row, const char *format, ...) {
va_list arg;
va_start(arg, format);
char buffer[256];
int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
va_end(arg);
if (ret > 0)
this->print(column, row, buffer);
}
void LCDDisplay::printf(const char *format, ...) {
va_list arg;
va_start(arg, format);
char buffer[256];
int ret = vsnprintf(buffer, sizeof(buffer), format, arg);
va_end(arg);
if (ret > 0)
this->print(0, 0, buffer);
}
void LCDDisplay::clear() {
for (uint8_t i = 0; i < this->rows_ * this->columns_; i++)
this->buffer_[i] = ' ';
}
#ifdef USE_TIME
void LCDDisplay::strftime(uint8_t column, uint8_t row, const char *format, time::ESPTime time) {
char buffer[64];
size_t ret = time.strftime(buffer, sizeof(buffer), format);
if (ret > 0)
this->print(column, row, buffer);
}
void LCDDisplay::strftime(const char *format, time::ESPTime time) { this->strftime(0, 0, format, time); }
#endif
} // namespace lcd_base
} // namespace esphome