mirror of
https://github.com/esphome/esphome.git
synced 2025-09-18 03:02:20 +01:00
Support for LibreTiny platform (RTL8710, BK7231 & other modules) (#3509)
Co-authored-by: Kuba Szczodrzyński <kuba@szczodrzynski.pl> Co-authored-by: Sam Neirinck <git@samneirinck.com> Co-authored-by: David Buezas <dbuezas@users.noreply.github.com> Co-authored-by: Stroe Andrei Catalin <catalin2402@gmail.com> Co-authored-by: Sam Neirinck <github@samneirinck.be> Co-authored-by: Péter Sárközi <xmisterhu@gmail.com> Co-authored-by: Hajo Noerenberg <hn@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
22c0b0abaa
commit
a9630ac847
1
esphome/components/libretiny_pwm/__init__.py
Normal file
1
esphome/components/libretiny_pwm/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
CODEOWNERS = ["@kuba2k2"]
|
53
esphome/components/libretiny_pwm/libretiny_pwm.cpp
Normal file
53
esphome/components/libretiny_pwm/libretiny_pwm.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "libretiny_pwm.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
|
||||
namespace esphome {
|
||||
namespace libretiny_pwm {
|
||||
|
||||
static const char *const TAG = "libretiny.pwm";
|
||||
|
||||
void LibreTinyPWM::write_state(float state) {
|
||||
if (!this->initialized_) {
|
||||
ESP_LOGW(TAG, "LibreTinyPWM output hasn't been initialized yet!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->pin_->is_inverted())
|
||||
state = 1.0f - state;
|
||||
|
||||
this->duty_ = state;
|
||||
const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
|
||||
const float duty_rounded = roundf(state * max_duty);
|
||||
auto duty = static_cast<uint32_t>(duty_rounded);
|
||||
|
||||
analogWrite(this->pin_->get_pin(), duty); // NOLINT
|
||||
}
|
||||
|
||||
void LibreTinyPWM::setup() {
|
||||
this->update_frequency(this->frequency_);
|
||||
this->turn_off();
|
||||
}
|
||||
|
||||
void LibreTinyPWM::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "PWM Output:");
|
||||
LOG_PIN(" Pin ", this->pin_);
|
||||
ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
|
||||
}
|
||||
|
||||
void LibreTinyPWM::update_frequency(float frequency) {
|
||||
this->frequency_ = frequency;
|
||||
// force changing the frequency by removing PWM mode
|
||||
this->pin_->pin_mode(gpio::FLAG_INPUT);
|
||||
analogWriteResolution(this->bit_depth_); // NOLINT
|
||||
analogWriteFrequency(frequency); // NOLINT
|
||||
this->initialized_ = true;
|
||||
// re-apply duty
|
||||
this->write_state(this->duty_);
|
||||
}
|
||||
|
||||
} // namespace libretiny_pwm
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
55
esphome/components/libretiny_pwm/libretiny_pwm.h
Normal file
55
esphome/components/libretiny_pwm/libretiny_pwm.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
|
||||
namespace esphome {
|
||||
namespace libretiny_pwm {
|
||||
|
||||
class LibreTinyPWM : public output::FloatOutput, public Component {
|
||||
public:
|
||||
explicit LibreTinyPWM(InternalGPIOPin *pin) : pin_(pin) {}
|
||||
|
||||
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
||||
/// Dynamically change frequency at runtime
|
||||
void update_frequency(float frequency) override;
|
||||
|
||||
/// Setup LibreTinyPWM.
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
/// HARDWARE setup priority
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
/// Override FloatOutput's write_state.
|
||||
void write_state(float state) override;
|
||||
|
||||
protected:
|
||||
InternalGPIOPin *pin_;
|
||||
uint8_t bit_depth_{10};
|
||||
float frequency_{};
|
||||
float duty_{0.0f};
|
||||
bool initialized_ = false;
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
|
||||
public:
|
||||
SetFrequencyAction(LibreTinyPWM *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(float, frequency);
|
||||
|
||||
void play(Ts... x) {
|
||||
float freq = this->frequency_.value(x...);
|
||||
this->parent_->update_frequency(freq);
|
||||
}
|
||||
|
||||
protected:
|
||||
LibreTinyPWM *parent_;
|
||||
};
|
||||
|
||||
} // namespace libretiny_pwm
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
49
esphome/components/libretiny_pwm/output.py
Normal file
49
esphome/components/libretiny_pwm/output.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from esphome import pins, automation
|
||||
from esphome.components import output
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.const import (
|
||||
CONF_FREQUENCY,
|
||||
CONF_ID,
|
||||
CONF_PIN,
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["libretiny"]
|
||||
|
||||
libretinypwm_ns = cg.esphome_ns.namespace("libretiny_pwm")
|
||||
LibreTinyPWM = libretinypwm_ns.class_("LibreTinyPWM", output.FloatOutput, cg.Component)
|
||||
SetFrequencyAction = libretinypwm_ns.class_("SetFrequencyAction", automation.Action)
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(LibreTinyPWM),
|
||||
cv.Required(CONF_PIN): pins.internal_gpio_output_pin_schema,
|
||||
cv.Optional(CONF_FREQUENCY, default="1kHz"): cv.frequency,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
gpio = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||
var = cg.new_Pvariable(config[CONF_ID], gpio)
|
||||
await cg.register_component(var, config)
|
||||
await output.register_output(var, config)
|
||||
cg.add(var.set_frequency(config[CONF_FREQUENCY]))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"output.libretiny_pwm.set_frequency",
|
||||
SetFrequencyAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(LibreTinyPWM),
|
||||
cv.Required(CONF_FREQUENCY): cv.templatable(cv.int_),
|
||||
}
|
||||
),
|
||||
)
|
||||
async def libretiny_pwm_set_frequency_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
template_ = await cg.templatable(config[CONF_FREQUENCY], args, float)
|
||||
cg.add(var.set_frequency(template_))
|
||||
return var
|
Reference in New Issue
Block a user