mirror of
https://github.com/esphome/esphome.git
synced 2025-10-25 21:23:53 +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:
0
esphome/components/rotary_encoder/__init__.py
Normal file
0
esphome/components/rotary_encoder/__init__.py
Normal file
150
esphome/components/rotary_encoder/rotary_encoder.cpp
Normal file
150
esphome/components/rotary_encoder/rotary_encoder.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "rotary_encoder.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace rotary_encoder {
|
||||
|
||||
static const char *TAG = "rotary_encoder";
|
||||
|
||||
// based on https://github.com/jkDesignDE/MechInputs/blob/master/QEIx4.cpp
|
||||
static const uint8_t STATE_LUT_MASK = 0x1C; // clears upper counter increment/decrement bits and pin states
|
||||
static const uint16_t STATE_PIN_A_HIGH = 0x01;
|
||||
static const uint16_t STATE_PIN_B_HIGH = 0x02;
|
||||
static const uint16_t STATE_S0 = 0x00;
|
||||
static const uint16_t STATE_S1 = 0x04;
|
||||
static const uint16_t STATE_S2 = 0x08;
|
||||
static const uint16_t STATE_S3 = 0x0C;
|
||||
static const uint16_t STATE_CCW = 0x00;
|
||||
static const uint16_t STATE_CW = 0x10;
|
||||
static const uint16_t STATE_HAS_INCREMENTED = 0x0700;
|
||||
static const uint16_t STATE_INCREMENT_COUNTER_4 = 0x0700;
|
||||
static const uint16_t STATE_INCREMENT_COUNTER_2 = 0x0300;
|
||||
static const uint16_t STATE_INCREMENT_COUNTER_1 = 0x0100;
|
||||
static const uint16_t STATE_HAS_DECREMENTED = 0x7000;
|
||||
static const uint16_t STATE_DECREMENT_COUNTER_4 = 0x7000;
|
||||
static const uint16_t STATE_DECREMENT_COUNTER_2 = 0x3000;
|
||||
static const uint16_t STATE_DECREMENT_COUNTER_1 = 0x1000;
|
||||
|
||||
// State explanation: 8-bit uint
|
||||
// Bit 0 (0x01) encodes Pin A HIGH/LOW (reset before each read)
|
||||
// Bit 1 (0x02) encodes Pin B HIGH/LOW (reset before each read)
|
||||
// Bit 2&3 (0x0C) encodes state S0-S3
|
||||
// Bit 4 (0x10) encodes clockwise/counter-clockwise rotation
|
||||
|
||||
static const uint16_t STATE_LOOKUP_TABLE[32] = {
|
||||
// act state S0 in CCW direction
|
||||
STATE_CCW | STATE_S0, // 0x00: stay here
|
||||
STATE_CW | STATE_S1 | STATE_INCREMENT_COUNTER_1, // 0x01: goto CW+S1 and increment counter (dir change)
|
||||
STATE_CCW | STATE_S0, // 0x02: stay here
|
||||
STATE_CCW | STATE_S3 | STATE_DECREMENT_COUNTER_4, // 0x03: goto CCW+S3 and decrement counter
|
||||
// act state S1 in CCW direction
|
||||
STATE_CCW | STATE_S1, // 0x04: stay here
|
||||
STATE_CCW | STATE_S1, // 0x05: stay here
|
||||
STATE_CCW | STATE_S0 | STATE_DECREMENT_COUNTER_1, // 0x06: goto CCW+S0 and decrement counter
|
||||
STATE_CW | STATE_S2 | STATE_INCREMENT_COUNTER_4, // 0x07: goto CW+S2 and increment counter (dir change)
|
||||
// act state S2 in CCW direction
|
||||
STATE_CCW | STATE_S1 | STATE_DECREMENT_COUNTER_2, // 0x08: goto CCW+S1 and decrement counter
|
||||
STATE_CCW | STATE_S2, // 0x09: stay here
|
||||
STATE_CW | STATE_S3 | STATE_INCREMENT_COUNTER_1, // 0x0A: goto CW+S3 and increment counter (dir change)
|
||||
STATE_CCW | STATE_S2, // 0x0B: stay here
|
||||
// act state S3 in CCW direction
|
||||
STATE_CW | STATE_S0 | STATE_INCREMENT_COUNTER_2, // 0x0C: goto CW+S0 and increment counter (dir change)
|
||||
STATE_CCW | STATE_S2 | STATE_DECREMENT_COUNTER_1, // 0x0D: goto CCW+S2 and decrement counter
|
||||
STATE_CCW | STATE_S3, // 0x0E: stay here
|
||||
STATE_CCW | STATE_S3, // 0x0F: stay here
|
||||
|
||||
// act state S0 in CW direction
|
||||
STATE_CW | STATE_S0, // 0x10: stay here
|
||||
STATE_CW | STATE_S1 | STATE_INCREMENT_COUNTER_1, // 0x11: goto CW+S1 and increment counter
|
||||
STATE_CW | STATE_S0, // 0x12: stay here
|
||||
STATE_CCW | STATE_S3 | STATE_DECREMENT_COUNTER_4, // 0x13: goto CCW+S3 and decrement counter (dir change)
|
||||
// act state S1 in CW direction
|
||||
STATE_CW | STATE_S1, // 0x14: stay here
|
||||
STATE_CW | STATE_S1, // 0x15: stay here
|
||||
STATE_CCW | STATE_S0 | STATE_DECREMENT_COUNTER_1, // 0x16: goto CCW+S0 and decrement counter (dir change)
|
||||
STATE_CW | STATE_S2 | STATE_INCREMENT_COUNTER_4, // 0x17: goto CW+S2 and increment counter
|
||||
// act state S2 in CW direction
|
||||
STATE_CCW | STATE_S1 | STATE_DECREMENT_COUNTER_2, // 0x18: goto CCW+S1 and decrement counter (dir change)
|
||||
STATE_CW | STATE_S2, // 0x19: stay here
|
||||
STATE_CW | STATE_S3 | STATE_INCREMENT_COUNTER_1, // 0x1A: goto CW+S3 and increment counter
|
||||
STATE_CW | STATE_S2,
|
||||
// act state S3 in CW direction
|
||||
STATE_CW | STATE_S0 | STATE_INCREMENT_COUNTER_2, // 0x1C: goto CW+S0 and increment counter
|
||||
STATE_CCW | STATE_S2 | STATE_DECREMENT_COUNTER_1, // 0x1D: goto CCW+S2 and decrement counter (dir change)
|
||||
STATE_CW | STATE_S3, // 0x1E: stay here
|
||||
STATE_CW | STATE_S3 // 0x1F: stay here
|
||||
};
|
||||
|
||||
void ICACHE_RAM_ATTR HOT RotaryEncoderSensorStore::gpio_intr(RotaryEncoderSensorStore *arg) {
|
||||
// Forget upper bits and add pin states
|
||||
uint8_t input_state = arg->state & STATE_LUT_MASK;
|
||||
if (arg->pin_a->digital_read())
|
||||
input_state |= STATE_PIN_A_HIGH;
|
||||
if (arg->pin_b->digital_read())
|
||||
input_state |= STATE_PIN_B_HIGH;
|
||||
|
||||
uint16_t new_state = STATE_LOOKUP_TABLE[input_state];
|
||||
if ((new_state & arg->resolution & STATE_HAS_INCREMENTED) != 0) {
|
||||
if (arg->counter < arg->max_value)
|
||||
arg->counter++;
|
||||
}
|
||||
if ((new_state & arg->resolution & STATE_HAS_DECREMENTED) != 0) {
|
||||
if (arg->counter > arg->min_value)
|
||||
arg->counter--;
|
||||
}
|
||||
|
||||
arg->state = new_state;
|
||||
}
|
||||
RotaryEncoderSensor::RotaryEncoderSensor(const std::string &name, GPIOPin *pin_a, GPIOPin *pin_b)
|
||||
: Sensor(name), Component(), pin_a_(pin_a), pin_b_(pin_b) {}
|
||||
|
||||
void RotaryEncoderSensor::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up Rotary Encoder '%s'...", this->name_.c_str());
|
||||
this->pin_a_->setup();
|
||||
this->store_.pin_a = this->pin_a_->to_isr();
|
||||
this->pin_a_->attach_interrupt(RotaryEncoderSensorStore::gpio_intr, &this->store_, CHANGE);
|
||||
|
||||
this->pin_b_->setup();
|
||||
this->store_.pin_b = this->pin_b_->to_isr();
|
||||
this->pin_b_->attach_interrupt(RotaryEncoderSensorStore::gpio_intr, &this->store_, CHANGE);
|
||||
|
||||
if (this->pin_i_ != nullptr) {
|
||||
this->pin_i_->setup();
|
||||
}
|
||||
}
|
||||
void RotaryEncoderSensor::dump_config() {
|
||||
LOG_SENSOR("", "Rotary Encoder", this);
|
||||
LOG_PIN(" Pin A: ", this->pin_a_);
|
||||
LOG_PIN(" Pin B: ", this->pin_b_);
|
||||
LOG_PIN(" Pin I: ", this->pin_i_);
|
||||
switch (this->store_.resolution) {
|
||||
case ROTARY_ENCODER_1_PULSE_PER_CYCLE:
|
||||
ESP_LOGCONFIG(TAG, " Resolution: 1 Pulse Per Cycle");
|
||||
break;
|
||||
case ROTARY_ENCODER_2_PULSES_PER_CYCLE:
|
||||
ESP_LOGCONFIG(TAG, " Resolution: 2 Pulses Per Cycle");
|
||||
break;
|
||||
case ROTARY_ENCODER_4_PULSES_PER_CYCLE:
|
||||
ESP_LOGCONFIG(TAG, " Resolution: 4 Pulse Per Cycle");
|
||||
break;
|
||||
}
|
||||
}
|
||||
void RotaryEncoderSensor::loop() {
|
||||
if (this->pin_i_ != nullptr && this->pin_i_->digital_read()) {
|
||||
this->store_.counter = 0;
|
||||
}
|
||||
int counter = this->store_.counter;
|
||||
if (this->store_.last_read != counter) {
|
||||
this->store_.last_read = counter;
|
||||
this->publish_state(counter);
|
||||
}
|
||||
}
|
||||
|
||||
float RotaryEncoderSensor::get_setup_priority() const { return setup_priority::DATA; }
|
||||
void RotaryEncoderSensor::set_resolution(RotaryEncoderResolution mode) { this->store_.resolution = mode; }
|
||||
void RotaryEncoderSensor::set_min_value(int32_t min_value) { this->store_.min_value = min_value; }
|
||||
void RotaryEncoderSensor::set_max_value(int32_t max_value) { this->store_.max_value = max_value; }
|
||||
|
||||
} // namespace rotary_encoder
|
||||
} // namespace esphome
|
||||
66
esphome/components/rotary_encoder/rotary_encoder.h
Normal file
66
esphome/components/rotary_encoder/rotary_encoder.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/esphal.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace rotary_encoder {
|
||||
|
||||
/// All possible resolutions for the rotary encoder
|
||||
enum RotaryEncoderResolution {
|
||||
ROTARY_ENCODER_1_PULSE_PER_CYCLE =
|
||||
0x4400, /// increment counter by 1 with every A-B cycle, slow response but accurate
|
||||
ROTARY_ENCODER_2_PULSES_PER_CYCLE = 0x2200, /// increment counter by 2 with every A-B cycle
|
||||
ROTARY_ENCODER_4_PULSES_PER_CYCLE = 0x1100, /// increment counter by 4 with every A-B cycle, most inaccurate
|
||||
};
|
||||
|
||||
struct RotaryEncoderSensorStore {
|
||||
ISRInternalGPIOPin *pin_a;
|
||||
ISRInternalGPIOPin *pin_b;
|
||||
|
||||
volatile int32_t counter{0};
|
||||
RotaryEncoderResolution resolution{ROTARY_ENCODER_1_PULSE_PER_CYCLE};
|
||||
int32_t min_value{INT32_MIN};
|
||||
int32_t max_value{INT32_MAX};
|
||||
int32_t last_read{0};
|
||||
uint8_t state{0};
|
||||
|
||||
static void gpio_intr(RotaryEncoderSensorStore *arg);
|
||||
};
|
||||
|
||||
class RotaryEncoderSensor : public sensor::Sensor, public Component {
|
||||
public:
|
||||
RotaryEncoderSensor(const std::string &name, GPIOPin *pin_a, GPIOPin *pin_b);
|
||||
|
||||
/** Set the resolution of the rotary encoder.
|
||||
*
|
||||
* By default, this component will increment the counter by 1 with every A-B input cycle.
|
||||
* You can however change this behavior to have more coarse resolutions like 4 counter increases per A-B cycle.
|
||||
*
|
||||
* @param mode The new mode of the encoder.
|
||||
*/
|
||||
void set_resolution(RotaryEncoderResolution mode);
|
||||
|
||||
void set_reset_pin(GPIOPin *pin_i) { this->pin_i_ = pin_i; }
|
||||
void set_min_value(int32_t min_value);
|
||||
void set_max_value(int32_t max_value);
|
||||
|
||||
// ========== INTERNAL METHODS ==========
|
||||
// (In most use cases you won't need these)
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
void loop() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
|
||||
protected:
|
||||
GPIOPin *pin_a_;
|
||||
GPIOPin *pin_b_;
|
||||
GPIOPin *pin_i_{nullptr}; /// Index pin, if this is not nullptr, the counter will reset to 0 once this pin is HIGH.
|
||||
|
||||
RotaryEncoderSensorStore store_{};
|
||||
};
|
||||
|
||||
} // namespace rotary_encoder
|
||||
} // namespace esphome
|
||||
61
esphome/components/rotary_encoder/sensor.py
Normal file
61
esphome/components/rotary_encoder/sensor.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import pins
|
||||
from esphome.components import sensor
|
||||
from esphome.const import CONF_ID, CONF_NAME, CONF_RESOLUTION, CONF_MIN_VALUE, CONF_MAX_VALUE
|
||||
|
||||
rotary_encoder_ns = cg.esphome_ns.namespace('rotary_encoder')
|
||||
RotaryEncoderResolution = rotary_encoder_ns.enum('RotaryEncoderResolution')
|
||||
RESOLUTIONS = {
|
||||
1: RotaryEncoderResolution.ROTARY_ENCODER_1_PULSE_PER_CYCLE,
|
||||
2: RotaryEncoderResolution.ROTARY_ENCODER_2_PULSES_PER_CYCLE,
|
||||
4: RotaryEncoderResolution.ROTARY_ENCODER_4_PULSES_PER_CYCLE,
|
||||
}
|
||||
|
||||
CONF_PIN_A = 'pin_a'
|
||||
CONF_PIN_B = 'pin_b'
|
||||
CONF_PIN_RESET = 'pin_reset'
|
||||
|
||||
RotaryEncoderSensor = rotary_encoder_ns.class_('RotaryEncoderSensor', sensor.Sensor, cg.Component)
|
||||
|
||||
|
||||
def validate_min_max_value(config):
|
||||
if CONF_MIN_VALUE in config and CONF_MAX_VALUE in config:
|
||||
min_val = config[CONF_MIN_VALUE]
|
||||
max_val = config[CONF_MAX_VALUE]
|
||||
if min_val >= max_val:
|
||||
raise cv.Invalid("Max value {} must be smaller than min value {}"
|
||||
"".format(max_val, min_val))
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.nameable(sensor.SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_variable_id(RotaryEncoderSensor),
|
||||
cv.Required(CONF_PIN_A): cv.All(pins.internal_gpio_input_pin_schema,
|
||||
pins.validate_has_interrupt),
|
||||
cv.Required(CONF_PIN_B): cv.All(pins.internal_gpio_input_pin_schema,
|
||||
pins.validate_has_interrupt),
|
||||
cv.Optional(CONF_PIN_RESET): pins.internal_gpio_input_pin_schema,
|
||||
cv.Optional(CONF_RESOLUTION, default=1): cv.one_of(*RESOLUTIONS, int=True),
|
||||
cv.Optional(CONF_MIN_VALUE): cv.int_,
|
||||
cv.Optional(CONF_MAX_VALUE): cv.int_,
|
||||
}).extend(cv.COMPONENT_SCHEMA), validate_min_max_value)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
pin_a = yield cg.gpio_pin_expression(config[CONF_PIN_A])
|
||||
pin_b = yield cg.gpio_pin_expression(config[CONF_PIN_B])
|
||||
var = cg.new_Pvariable(config[CONF_ID], config[CONF_NAME], pin_a, pin_b)
|
||||
yield cg.register_component(var, config)
|
||||
yield sensor.register_sensor(var, config)
|
||||
|
||||
if CONF_PIN_RESET in config:
|
||||
pin_i = yield cg.gpio_pin_expression(config[CONF_PIN_RESET])
|
||||
cg.add(var.set_reset_pin(pin_i))
|
||||
if CONF_RESOLUTION in config:
|
||||
resolution = RESOLUTIONS[config[CONF_RESOLUTION]]
|
||||
cg.add(var.set_resolution(resolution))
|
||||
if CONF_MIN_VALUE in config:
|
||||
cg.add(var.set_min_value(config[CONF_MIN_VALUE]))
|
||||
if CONF_MAX_VALUE in config:
|
||||
cg.add(var.set_max_value(config[CONF_MAX_VALUE]))
|
||||
Reference in New Issue
Block a user