mirror of
https://github.com/esphome/esphome.git
synced 2025-09-26 23:22:21 +01:00
🏗 Merge C++ into python codebase (#504)
## Description: Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97 Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍 Progress: - Core support (file copy etc): 80% - Base Abstractions (light, switch): ~50% - Integrations: ~10% - Working? Yes, (but only with ported components). Other refactors: - Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`) - Rework coroutine syntax - Move from `component/platform.py` to `domain/component.py` structure as with HA - Move all defaults out of C++ and into config validation. - Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration. - Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit. Future work: - Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block - Enable loading from `custom_components` folder. **Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97 **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## 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: - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
This commit is contained in:
36
esphome/components/lcd_base/__init__.py
Normal file
36
esphome/components/lcd_base/__init__.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import display
|
||||
from esphome.const import CONF_DIMENSIONS, CONF_LAMBDA, CONF_UPDATE_INTERVAL
|
||||
from esphome.core import coroutine
|
||||
|
||||
lcd_base_ns = cg.esphome_ns.namespace('lcd_base')
|
||||
LCDDisplay = lcd_base_ns.class_('LCDDisplay', cg.PollingComponent)
|
||||
LCDDisplayRef = LCDDisplay.operator('ref')
|
||||
|
||||
|
||||
def validate_lcd_dimensions(value):
|
||||
value = cv.dimensions(value)
|
||||
if value[0] > 0x40:
|
||||
raise cv.Invalid("LCD displays can't have more than 64 columns")
|
||||
if value[1] > 4:
|
||||
raise cv.Invalid("LCD displays can't have more than 4 rows")
|
||||
return value
|
||||
|
||||
|
||||
LCD_SCHEMA = display.BASIC_DISPLAY_SCHEMA.extend({
|
||||
cv.Required(CONF_DIMENSIONS): validate_lcd_dimensions,
|
||||
cv.Optional(CONF_UPDATE_INTERVAL, default='1s'): cv.update_interval,
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
@coroutine
|
||||
def setup_lcd_display(var, config):
|
||||
yield cg.register_component(var, config)
|
||||
yield display.register_display(var, config)
|
||||
cg.add(var.set_dimensions(config[CONF_DIMENSIONS][0], config[CONF_DIMENSIONS][1]))
|
||||
|
||||
if CONF_LAMBDA in config:
|
||||
lambda_ = yield cg.process_lambda(config[CONF_LAMBDA], [(LCDDisplayRef, 'it')],
|
||||
return_type=cg.void)
|
||||
cg.add(var.set_writer(lambda_))
|
162
esphome/components/lcd_base/lcd_display.cpp
Normal file
162
esphome/components/lcd_base/lcd_display.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
#include "lcd_display.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lcd_base {
|
||||
|
||||
static const char *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_];
|
||||
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() {
|
||||
for (uint8_t i = 0; i < this->rows_ * this->columns_; i++)
|
||||
this->buffer_[i] = ' ';
|
||||
|
||||
this->writer_(*this);
|
||||
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);
|
||||
}
|
||||
#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
|
67
esphome/components/lcd_base/lcd_display.h
Normal file
67
esphome/components/lcd_base/lcd_display.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#ifdef USE_TIME
|
||||
#include "esphome/components/time/real_time_clock.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace lcd_base {
|
||||
|
||||
class LCDDisplay;
|
||||
|
||||
using lcd_writer_t = std::function<void(LCDDisplay &)>;
|
||||
|
||||
class LCDDisplay : public PollingComponent {
|
||||
public:
|
||||
LCDDisplay(uint32_t update_interval) : PollingComponent(update_interval) {}
|
||||
|
||||
void set_writer(lcd_writer_t &&writer) { this->writer_ = std::move(writer); }
|
||||
void set_dimensions(uint8_t columns, uint8_t rows) {
|
||||
this->columns_ = columns;
|
||||
this->rows_ = rows;
|
||||
}
|
||||
|
||||
void setup() override;
|
||||
float get_setup_priority() const override;
|
||||
void update() override;
|
||||
void display();
|
||||
|
||||
/// Print the given text at the specified column and row.
|
||||
void print(uint8_t column, uint8_t row, const char *str);
|
||||
/// Print the given string at the specified column and row.
|
||||
void print(uint8_t column, uint8_t row, const std::string &str);
|
||||
/// Print the given text at column=0 and row=0.
|
||||
void print(const char *str);
|
||||
/// Print the given string at column=0 and row=0.
|
||||
void print(const std::string &str);
|
||||
/// Evaluate the printf-format and print the text at the specified column and row.
|
||||
void printf(uint8_t column, uint8_t row, const char *format, ...) __attribute__((format(printf, 4, 5)));
|
||||
/// Evaluate the printf-format and print the text at column=0 and row=0.
|
||||
void printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
|
||||
|
||||
#ifdef USE_TIME
|
||||
/// Evaluate the strftime-format and print the text at the specified column and row.
|
||||
void strftime(uint8_t column, uint8_t row, const char *format, time::ESPTime time)
|
||||
__attribute__((format(strftime, 4, 0)));
|
||||
/// Evaluate the strftime-format and print the text at column=0 and row=0.
|
||||
void strftime(const char *format, time::ESPTime time) __attribute__((format(strftime, 2, 0)));
|
||||
#endif
|
||||
|
||||
protected:
|
||||
virtual bool is_four_bit_mode() = 0;
|
||||
virtual void write_n_bits(uint8_t value, uint8_t n) = 0;
|
||||
virtual void send(uint8_t value, bool rs) = 0;
|
||||
|
||||
void command_(uint8_t value);
|
||||
|
||||
uint8_t columns_;
|
||||
uint8_t rows_;
|
||||
uint8_t *buffer_{nullptr};
|
||||
lcd_writer_t writer_;
|
||||
};
|
||||
|
||||
} // namespace lcd_base
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user