mirror of
https://github.com/esphome/esphome.git
synced 2025-02-08 14:10:54 +00:00
* 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>
116 lines
3.5 KiB
C++
116 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
|
|
#include "esphome/core/component.h"
|
|
#include "esphome/core/hal.h"
|
|
#include "esphome/core/automation.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};
|
|
|
|
std::array<int8_t, 8> rotation_events{};
|
|
bool rotation_events_overflow{false};
|
|
|
|
static void gpio_intr(RotaryEncoderSensorStore *arg);
|
|
};
|
|
|
|
class RotaryEncoderSensor : public sensor::Sensor, public Component {
|
|
public:
|
|
void set_pin_a(InternalGPIOPin *pin_a) { pin_a_ = pin_a; }
|
|
void set_pin_b(InternalGPIOPin *pin_b) { pin_b_ = 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);
|
|
|
|
/// Manually set the value of the counter.
|
|
void set_value(int value) {
|
|
this->store_.counter = value;
|
|
this->loop();
|
|
}
|
|
|
|
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;
|
|
|
|
void add_on_clockwise_callback(std::function<void()> callback) {
|
|
this->on_clockwise_callback_.add(std::move(callback));
|
|
}
|
|
|
|
void add_on_anticlockwise_callback(std::function<void()> callback) {
|
|
this->on_anticlockwise_callback_.add(std::move(callback));
|
|
}
|
|
|
|
protected:
|
|
InternalGPIOPin *pin_a_;
|
|
InternalGPIOPin *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_{};
|
|
|
|
CallbackManager<void()> on_clockwise_callback_;
|
|
CallbackManager<void()> on_anticlockwise_callback_;
|
|
};
|
|
|
|
template<typename... Ts> class RotaryEncoderSetValueAction : public Action<Ts...> {
|
|
public:
|
|
RotaryEncoderSetValueAction(RotaryEncoderSensor *encoder) : encoder_(encoder) {}
|
|
TEMPLATABLE_VALUE(int, value)
|
|
|
|
void play(Ts... x) override { this->encoder_->set_value(this->value_.value(x...)); }
|
|
|
|
protected:
|
|
RotaryEncoderSensor *encoder_;
|
|
};
|
|
|
|
class RotaryEncoderClockwiseTrigger : public Trigger<> {
|
|
public:
|
|
explicit RotaryEncoderClockwiseTrigger(RotaryEncoderSensor *parent) {
|
|
parent->add_on_clockwise_callback([this]() { this->trigger(); });
|
|
}
|
|
};
|
|
|
|
class RotaryEncoderAnticlockwiseTrigger : public Trigger<> {
|
|
public:
|
|
explicit RotaryEncoderAnticlockwiseTrigger(RotaryEncoderSensor *parent) {
|
|
parent->add_on_anticlockwise_callback([this]() { this->trigger(); });
|
|
}
|
|
};
|
|
|
|
} // namespace rotary_encoder
|
|
} // namespace esphome
|