19static constexpr uint32_t DIRECTION_CHANGE_POLL_MS = 200;
20static constexpr uint8_t DIRECTION_CHANGE_MAX_POLLS = 25;
22static constexpr uint32_t SAVE_PREF_HASH = 0x7B1D4C25;
42 this->
mark_failed(LOG_STR(
"Failed to write control mode"));
49 this->
mark_failed(LOG_STR(
"Failed to read the motor command"));
56 uint8_t current_direction;
59 if (!direction_matches &&
62 this->
mark_failed(LOG_STR(
"Failed to write initial direction"));
70 PUBLISH_STATE(this->command_number_, command);
75 this->
mark_failed(LOG_STR(
"Failed to write motor model/pole pairs"));
79 this->
mark_failed(LOG_STR(
"Failed to write PID gains"));
90 for (
const int32_t
gain : this->
pid_)
96 if (saved != wanted) {
100 ESP_LOGW(TAG,
"Failed to save settings to flash");
107 if (this->rpm_sensor_ !=
nullptr || this->frequency_sensor_ !=
nullptr)
110#ifdef USE_TEXT_SENSOR
111 if (this->status_text_sensor_ !=
nullptr)
121 if (this->rpm_sensor_ !=
nullptr) {
123 this->rpm_sensor_->publish_state(
value);
128 if (this->frequency_sensor_ !=
nullptr) {
130 this->frequency_sensor_->publish_state(
value);
136#ifdef USE_TEXT_SENSOR
138 if (this->status_text_sensor_ !=
nullptr) {
143 this->status_text_sensor_->publish_state(
144 MotorStatusStrings::get_progmem_str(
status, MotorStatusStrings::LAST_INDEX));
154 " Control mode: %s\n"
159 : LOG_STR_LITERAL(
"open loop"),
161 : LOG_STR_LITERAL(
"forward"),
163 : LOG_STR_LITERAL(
"low speed"),
165 LOG_I2C_DEVICE(
this);
166 LOG_UPDATE_INTERVAL(
this);
216 ESP_LOGW(TAG,
"Motor still turning after %u ms; output left at zero",
217 static_cast<unsigned>(DIRECTION_CHANGE_POLL_MS * DIRECTION_CHANGE_MAX_POLLS));
220 PUBLISH_STATE(this->command_number_, 0);
void mark_failed()
Mark this component as failed.
bool cancel_interval(const char *name)
Cancel an interval function.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
uint8_t address_
store the address of the device on the bus
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
Direction current_direction_
bool direction_change_pending_
uint8_t direction_change_polls_
void finish_direction_change_()
const uint8_t pole_pairs_
void dump_config() override
static constexpr uint32_t DIRECTION_CHANGE_POLL_ID
const Direction initial_direction_
const bool save_to_flash_
bool write_direction_raw_(Direction direction)
const MotorModel motor_model_
const ControlMode control_mode_
bool write_direction(Direction direction)
bool write_reg_(uint8_t reg, const T &value)
ESPPreferenceObject save_pref_
bool write_command_raw_(float value)
bool read_reg_(uint8_t reg, T *value)
bool read_command_raw_(float *value)
bool write_command(float value)
Send the PWM duty (open loop) or target RPM (closed loop).
PROGMEM_STRING_TABLE(MotorStatusStrings, "Standby", "Running", "Error", "Unknown")
ESPHOME_LOG_TAG(TAG, "m5stack_unit_bldc")
@ CONTROL_MODE_CLOSED_LOOP
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
constexpr uint32_t FNV1_OFFSET_BASIS
FNV-1 32-bit offset basis.
ESPPreferences * global_preferences
ESPPreferenceObject make_preference(size_t, uint32_t, bool)