Fixes for CLANG-FORMAT + added Code owner

This commit is contained in:
Vladimir Kozhevnikov 2024-02-06 01:11:15 +03:00
parent d2f656ed13
commit 4fbe82a504
6 changed files with 91 additions and 128 deletions

View file

@ -91,6 +91,7 @@ esphome/components/dht/* @OttoWinter
esphome/components/display_menu_base/* @numo68 esphome/components/display_menu_base/* @numo68
esphome/components/dps310/* @kbx81 esphome/components/dps310/* @kbx81
esphome/components/ds1307/* @badbadc0ffee esphome/components/ds1307/* @badbadc0ffee
esphome/components/ds3232/* @ViKozh
esphome/components/dsmr/* @glmnet @zuidwijk esphome/components/dsmr/* @glmnet @zuidwijk
esphome/components/duty_time/* @dudanov esphome/components/duty_time/* @dudanov
esphome/components/ee895/* @Stock-M esphome/components/ee895/* @Stock-M

View file

@ -89,7 +89,7 @@ void DS3232Component::setup() {
void DS3232Component::update() { void DS3232Component::update() {
if(this->late_startup_) if (this->late_startup_)
return; return;
this->read_time(); this->read_time();
this->read_temperature_(); this->read_temperature_();
@ -103,12 +103,11 @@ float DS3232Component::get_setup_priority() const { return setup_priority::DATA
void DS3232Component::reinit_osf_() { void DS3232Component::reinit_osf_() {
this->read_data_(); this->read_data_();
if(!this->reg_data_.reg.osf_bit) if (!this->reg_data_.reg.osf_bit)
return; return;
ESP_LOGD(TAG, "Found disabled oscillator. Restarting it."); ESP_LOGD(TAG, "Found disabled oscillator. Restarting it.");
this->reg_data_.reg.osf_bit = false; this->reg_data_.reg.osf_bit = false;
if(!this->write_byte(I2C_REG_STATUS, this->reg_data_.raw_blocks.status_raw[0])) if (!this->write_byte(I2C_REG_STATUS, this->reg_data_.raw_blocks.status_raw[0])) {
{
ESP_LOGE(TAG, "Unable to restart oscillator."); ESP_LOGE(TAG, "Unable to restart oscillator.");
return; return;
} }
@ -165,21 +164,19 @@ void DS3232Component::loop() {
} }
this->pin_state_ = pin_state; this->pin_state_ = pin_state;
} }
switch (this->nvram_state_) switch (this->nvram_state_) {
{ case DS3232NVRAMState::NEED_INITIALIZATION:
case DS3232NVRAMState::NEED_INITIALIZATION: if (this->busy_) {
if(this->busy_) { ESP_LOGI(TAG, "Performing planned NVRAM full reset...");
ESP_LOGI(TAG, "Performing planned NVRAM full reset..."); clear_nvram_();
clear_nvram_(); }
} break;
break;
case DS3232NVRAMState::NEED_RESET: case DS3232NVRAMState::NEED_RESET:
if(this->busy_) { if (this->busy_) {
ESP_LOGI(TAG, "Performing planned NVRAM factory reset..."); ESP_LOGI(TAG, "Performing planned NVRAM factory reset...");
this->nvram_state_ = DS3232NVRAMState::OK; this->nvram_state_ = DS3232NVRAMState::OK;
this->variable_init_callback_.call(); this->variable_init_callback_.call();
if(this->nvram_state_ != DS3232NVRAMState::OK) if (this->nvram_state_ != DS3232NVRAMState::OK) {
{
ESP_LOGE(TAG, "NVRAM: Failed to reset to factory defaults."); ESP_LOGE(TAG, "NVRAM: Failed to reset to factory defaults.");
} }
this->busy_ = false; this->busy_ = false;
@ -212,10 +209,9 @@ void DS3232Component::clear_nvram_() {
std::vector<uint8_t> zeroes; std::vector<uint8_t> zeroes;
zeroes.resize(write_len_, 0); zeroes.resize(write_len_, 0);
while (step < total_size) while (step < total_size) {
{
bool res = false; bool res = false;
if((total_size - step) < write_len) { if ((total_size - step) < write_len) {
res_ = this->write_bytes(MIN_NVRAM_ADDRESS + step_, zeroes.data(), total_size - step_); res_ = this->write_bytes(MIN_NVRAM_ADDRESS + step_, zeroes.data(), total_size - step_);
step += (total_size - step); step += (total_size - step);
} else { } else {
@ -226,8 +222,7 @@ void DS3232Component::clear_nvram_() {
state &= res; state &= res;
} }
if(!state) if (!state) {
{
ESP_LOGE(TAG, "NVRAM: Unable to clear memory. Marking as failed."); ESP_LOGE(TAG, "NVRAM: Unable to clear memory. Marking as failed.");
this->nvram_state_ = DS3232NVRAMState::FAIL; this->nvram_state_ = DS3232NVRAMState::FAIL;
} else { } else {
@ -237,8 +232,7 @@ void DS3232Component::clear_nvram_() {
this->nvram_info_.info.is_initialized = false; this->nvram_info_.info.is_initialized = false;
this->nvram_info_.info.maj_version = NVRAM_DATA_MAJ_VERSION; this->nvram_info_.info.maj_version = NVRAM_DATA_MAJ_VERSION;
this->nvram_info_.info.min_version = NVRAM_DATA_MIN_VERSION; this->nvram_info_.info.min_version = NVRAM_DATA_MIN_VERSION;
if(!this->write_bytes(SVC_NVRAM_ADDRESS, this->nvram_info_.raw, sizeof(this->nvram_info_.raw))) if (!this->write_bytes(SVC_NVRAM_ADDRESS, this->nvram_info_.raw, sizeof(this->nvram_info_.raw))) {
{
ESP_LOGE(TAG, "NVRAM: Unable to write service NVRAM information. Marking as failed."); ESP_LOGE(TAG, "NVRAM: Unable to write service NVRAM information. Marking as failed.");
this->nvram_state_ = DS3232NVRAMState::FAIL; this->nvram_state_ = DS3232NVRAMState::FAIL;
} }
@ -247,8 +241,7 @@ void DS3232Component::clear_nvram_() {
ESP_LOGD(TAG, "NVRAM: Variables has been initialized. Saving state."); ESP_LOGD(TAG, "NVRAM: Variables has been initialized. Saving state.");
this->nvram_info_.info.is_initialized = true; this->nvram_info_.info.is_initialized = true;
if(!this->write_bytes(SVC_NVRAM_ADDRESS, this->nvram_info_.raw, sizeof(this->nvram_info_.raw))) if (!this->write_bytes(SVC_NVRAM_ADDRESS, this->nvram_info_.raw, sizeof(this->nvram_info_.raw))) {
{
ESP_LOGE(TAG, "NVRAM: Unable to write service NVRAM information. Marking as failed."); ESP_LOGE(TAG, "NVRAM: Unable to write service NVRAM information. Marking as failed.");
this->nvram_state_ = DS3232NVRAMState::FAIL; this->nvram_state_ = DS3232NVRAMState::FAIL;
} }
@ -267,7 +260,7 @@ bool DS3232Component::validate_mem_(uint8_t reg_id, uint8_t size, bool ignore_em
} }
void DS3232Component::reset_memory() { void DS3232Component::reset_memory() {
if(this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) { if (this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) {
ESP_LOGW(TAG, "NVRAM: Another memory reset process in progress already."); ESP_LOGW(TAG, "NVRAM: Another memory reset process in progress already.");
return; return;
} }
@ -276,7 +269,7 @@ void DS3232Component::reset_memory() {
} }
void DS3232Component::reset_to_factory() { void DS3232Component::reset_to_factory() {
if(this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) { if (this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) {
ESP_LOGW(TAG, "NVRAM: Another memory reset process in progress already."); ESP_LOGW(TAG, "NVRAM: Another memory reset process in progress already.");
return; return;
} }
@ -285,11 +278,11 @@ void DS3232Component::reset_to_factory() {
} }
bool DS3232Component::read_memory(uint8_t reg_id, std::vector<uint8_t> &data) { bool DS3232Component::read_memory(uint8_t reg_id, std::vector<uint8_t> &data) {
if(this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) { if (this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) {
ESP_LOGW(TAG, "NVRAM: Memory reset process in progress. Try later."); ESP_LOGW(TAG, "NVRAM: Memory reset process in progress. Try later.");
return false; return false;
} }
if(data.size() == 0) { if (data.size() == 0) {
ESP_LOGW(TAG, "NVRAM: Nothing to write to memory."); ESP_LOGW(TAG, "NVRAM: Nothing to write to memory.");
return true; return true;
} }
@ -297,36 +290,33 @@ bool DS3232Component::read_memory(uint8_t reg_id, std::vector<uint8_t> &data) {
ESP_LOGE(TAG, "Invalid NVRAM memory mapping."); ESP_LOGE(TAG, "Invalid NVRAM memory mapping.");
return false; return false;
} else { } else {
if(!this->read_bytes(reg_id, data.data(), data.size())) { if (!this->read_bytes(reg_id, data.data(), data.size())) {
ESP_LOGE(TAG, "NVRAM: Unable to read from %#02x register with size %u.", reg_id, data.size()); ESP_LOGE(TAG, "NVRAM: Unable to read from %#02x register with size %u.", reg_id, data.size());
this->nvram_state_ = DS3232NVRAMState::FAIL; this->nvram_state_ = DS3232NVRAMState::FAIL;
return false; return false;
}; };
ESP_LOGD(TAG, "NVRAM: Value read from %#02x register with size %u.", reg_id, data.size()); ESP_LOGD(TAG, "NVRAM: Value read from %#02x register with size %u.", reg_id, data.size());
return true; return true;
} }
} }
bool DS3232Component::write_memory(const uint8_t reg_id, const std::vector<uint8_t> &data) bool DS3232Component::write_memory(const uint8_t reg_id, const std::vector<uint8_t> &data) {
{ if (data.size() == 0) {
if(data.size() == 0) {
ESP_LOGW(TAG, "NVRAM: Nothing to write to memory."); ESP_LOGW(TAG, "NVRAM: Nothing to write to memory.");
return true; return true;
} }
if(this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) { if (this->nvram_state_ == DS3232NVRAMState::INITIALIZATION) {
ESP_LOGW(TAG, "NVRAM: Memory reset process in progress. Try later."); ESP_LOGW(TAG, "NVRAM: Memory reset process in progress. Try later.");
return false; return false;
} }
if(!this->validate_mem_(reg_id, data.size(), true)) if (!this->validate_mem_(reg_id, data.size(), true)) {
{
ESP_LOGE(TAG, "Invalid NVRAM memory mapping."); ESP_LOGE(TAG, "Invalid NVRAM memory mapping.");
return false; return false;
} }
if(!this->write_bytes(reg_id, data)) if (!this->write_bytes(reg_id, data)) {
{
ESP_LOGE(TAG, "NVRAM: Unable to write to %#02x register with size %u.", reg_id, data.size()); ESP_LOGE(TAG, "NVRAM: Unable to write to %#02x register with size %u.", reg_id, data.size());
this->nvram_state_ = DS3232NVRAMState::FAIL; this->nvram_state_ = DS3232NVRAMState::FAIL;
return false; return false;
@ -481,12 +471,9 @@ ds3232_alarm::DS3232Alarm DS3232Component::get_alarm_one() {
ds3232_alarm::DS3232Alarm alarm = {}; ds3232_alarm::DS3232Alarm alarm = {};
alarm.enabled = reg_data_.reg.alarm_1_enable; alarm.enabled = reg_data_.reg.alarm_1_enable;
alarm.seconds_supported = true; alarm.seconds_supported = true;
alarm.mode = ds3232_alarm::DS3232Alarm::alarm_mode( alarm.mode = ds3232_alarm::DS3232Alarm::alarm_mode(reg_data_.reg.alarm_1_mode_1, reg_data_.reg.alarm_1_mode_2,
reg_data_.reg.alarm_1_mode_1, reg_data_.reg.alarm_1_mode_3, reg_data_.reg.alarm_1_mode_4,
reg_data_.reg.alarm_1_mode_2, reg_data_.reg.alarm_1_use_day_of_week);
reg_data_.reg.alarm_1_mode_3,
reg_data_.reg.alarm_1_mode_4,
reg_data_.reg.alarm_1_use_day_of_week);
alarm.second = reg_data_.reg.alarm_1_second + 10u * reg_data_.reg.alarm_1_second_10; alarm.second = reg_data_.reg.alarm_1_second + 10u * reg_data_.reg.alarm_1_second_10;
alarm.minute = reg_data_.reg.alarm_1_minute + 10u * reg_data_.reg.alarm_1_minute_10; alarm.minute = reg_data_.reg.alarm_1_minute + 10u * reg_data_.reg.alarm_1_minute_10;
alarm.hour = reg_data_.reg.alarm_1_hour + 10u * reg_data_.reg.alarm_1_hour_10; alarm.hour = reg_data_.reg.alarm_1_hour + 10u * reg_data_.reg.alarm_1_hour_10;
@ -508,12 +495,9 @@ ds3232_alarm::DS3232Alarm DS3232Component::get_alarm_two() {
ds3232_alarm::DS3232Alarm alarm = {}; ds3232_alarm::DS3232Alarm alarm = {};
alarm.enabled = reg_data_.reg.alarm_2_enable; alarm.enabled = reg_data_.reg.alarm_2_enable;
alarm.seconds_supported = false; alarm.seconds_supported = false;
alarm.mode = ds3232_alarm::DS3232Alarm::alarm_mode( alarm.mode =
false, ds3232_alarm::DS3232Alarm::alarm_mode(false, reg_data_.reg.alarm_2_mode_2, reg_data_.reg.alarm_2_mode_3,
reg_data_.reg.alarm_2_mode_2, reg_data_.reg.alarm_2_mode_4, reg_data_.reg.alarm_2_use_day_of_week);
reg_data_.reg.alarm_2_mode_3,
reg_data_.reg.alarm_2_mode_4,
reg_data_.reg.alarm_2_use_day_of_week);
alarm.second = 0; alarm.second = 0;
alarm.minute = reg_data_.reg.alarm_2_minute + 10u * reg_data_.reg.alarm_2_minute_10; alarm.minute = reg_data_.reg.alarm_2_minute + 10u * reg_data_.reg.alarm_2_minute_10;
alarm.hour = reg_data_.reg.alarm_2_hour + 10u * reg_data_.reg.alarm_2_hour_10; alarm.hour = reg_data_.reg.alarm_2_hour + 10u * reg_data_.reg.alarm_2_hour_10;

View file

@ -85,11 +85,11 @@ class DS3232Component : public time::RealTimeClock, public i2c::I2CDevice {
/// @brief Perform full memory reinitialization. All available /// @brief Perform full memory reinitialization. All available
/// memory registers will be set to zeroes. /// memory registers will be set to zeroes.
/// This could help if memory structure is corrupted or faulty. /// This could help if memory structure is corrupted or faulty.
void reset_memory(); // Action void reset_memory(); // Action
/// @brief Sets all 'configuration' variables to their initial values. /// @brief Sets all 'configuration' variables to their initial values.
void reset_to_factory(); // Action void reset_to_factory(); // Action
void set_alarm_one(const ds3232_alarm::DS3232Alarm &alarm); // Action void set_alarm_one(const ds3232_alarm::DS3232Alarm &alarm); // Action
void set_alarm_two(const ds3232_alarm::DS3232Alarm &alarm); // Action void set_alarm_two(const ds3232_alarm::DS3232Alarm &alarm); // Action
ds3232_alarm::DS3232Alarm get_alarm_one(); ds3232_alarm::DS3232Alarm get_alarm_one();
ds3232_alarm::DS3232Alarm get_alarm_two(); ds3232_alarm::DS3232Alarm get_alarm_two();
void clear_alarm_one(); void clear_alarm_one();
@ -111,8 +111,12 @@ class DS3232Component : public time::RealTimeClock, public i2c::I2CDevice {
void add_on_power_change_callback(std::function<void(DS3232PowerState)> &&func) { void add_on_power_change_callback(std::function<void(DS3232PowerState)> &&func) {
this->power_state_callback_.add(std::move(func)); this->power_state_callback_.add(std::move(func));
} }
void add_on_variable_init_required_callback(std::function<void()> &&func) { this->variable_init_callback_.add(std::move(func)); } void add_on_variable_init_required_callback(std::function<void()> &&func) {
void add_on_variable_fail_callback(std::function<void()> &&func) { this->variable_fail_callback_.add(std::move(func)); } this->variable_init_callback_.add(std::move(func));
}
void add_on_variable_fail_callback(std::function<void()> &&func) {
this->variable_fail_callback_.add(std::move(func));
}
bool read_memory(uint8_t reg_id, std::vector<uint8_t> &data); bool read_memory(uint8_t reg_id, std::vector<uint8_t> &data);
bool write_memory(uint8_t reg_id, const std::vector<uint8_t> &data); bool write_memory(uint8_t reg_id, const std::vector<uint8_t> &data);
bool is_valid_nvram(const uint8_t reg_id, uint8_t size) { return this->validate_mem_(reg_id, size, true); } bool is_valid_nvram(const uint8_t reg_id, uint8_t size) { return this->validate_mem_(reg_id, size, true); }
@ -151,9 +155,9 @@ class DS3232Component : public time::RealTimeClock, public i2c::I2CDevice {
union DS3232NVRAMData { union DS3232NVRAMData {
struct { struct {
uint8_t magix_1 : 2; //Always should be 0b11; uint8_t magix_1 : 2; // Always should be 0b11;
uint8_t magix_2 : 6; //Always should be 'E' char. uint8_t magix_2 : 6; // Always should be 'E' char.
bool is_initialized : 1; //If set to 1 then it means that nvram has initialized values. bool is_initialized : 1; // If set to 1 then it means that nvram has initialized values.
uint8_t min_version : 3; uint8_t min_version : 3;
uint8_t maj_version : 4; uint8_t maj_version : 4;
} info; } info;
@ -292,13 +296,13 @@ class PowerStateEvent : public Trigger<DS3232PowerState> {
}; };
template<typename... Ts> class FactoryResetNVRAMAction : public Action<Ts...>, public Parented<DS3232Component> { template<typename... Ts> class FactoryResetNVRAMAction : public Action<Ts...>, public Parented<DS3232Component> {
public: public:
void play(Ts... x) override { this->parent_->reset_to_factory(); } void play(Ts... x) override { this->parent_->reset_to_factory(); }
}; };
template<typename... Ts> class EraseNVRAMAction : public Action<Ts...>, public Parented<DS3232Component> { template<typename... Ts> class EraseNVRAMAction : public Action<Ts...>, public Parented<DS3232Component> {
public: public:
void play(Ts... x) override { this->parent_->reset_memory(); } void play(Ts... x) override { this->parent_->reset_memory(); }
}; };
template<typename... Ts> class WriteAction : public Action<Ts...>, public Parented<DS3232Component> { template<typename... Ts> class WriteAction : public Action<Ts...>, public Parented<DS3232Component> {

View file

@ -32,13 +32,6 @@ static const char *const FORMAT_STRING_INVALID = INVALID;
// So, the recommended formatted_string length initialization: 128 symbols (35 * 3 (105) growed to 2^x(128)) // So, the recommended formatted_string length initialization: 128 symbols (35 * 3 (105) growed to 2^x(128))
static const size_t DS3232_ALARM_FORMAT_STRING_LENGTH = 128; static const size_t DS3232_ALARM_FORMAT_STRING_LENGTH = 128;
/// Converts alarm match bits to mode
// static AlarmMode bits_to_mode(bool use_week, bool match_day, bool match_hour, bool match_min, bool match_sec = false) {
// uint8_t mode_bits =
// 0x00001 * match_sec + 0x00010 * match_min + 0x00100 * match_hour + 0x01000 * match_day + 0x10000 * use_week;
// return static_cast<AlarmMode>(mode_bits);
// };
DS3232AlarmMode DS3232Alarm::alarm_mode(const AlarmMode mode) { DS3232AlarmMode DS3232Alarm::alarm_mode(const AlarmMode mode) {
DS3232AlarmMode am = {}; DS3232AlarmMode am = {};
am.alarm_mode = mode; am.alarm_mode = mode;
@ -46,12 +39,12 @@ DS3232AlarmMode DS3232Alarm::alarm_mode(const AlarmMode mode) {
}; };
DS3232AlarmMode DS3232Alarm::alarm_mode(const bool bit_seconds, const bool bit_minutes, const bool bit_hours, DS3232AlarmMode DS3232Alarm::alarm_mode(const bool bit_seconds, const bool bit_minutes, const bool bit_hours,
const bool bit_days, const bool use_weekdays) { const bool bit_days, const bool use_weekdays) {
bool weekdays_bit = use_weekdays && !(bit_seconds || bit_minutes || bit_hours); bool weekdays_bit = use_weekdays && !(bit_seconds || bit_minutes || bit_hours);
uint8_t mode_bits = 0b00001 * bit_seconds + 0b00010 * bit_minutes + 0b00100 * bit_hours + 0b01000 * bit_days + uint8_t mode_bits =
0b10000 * weekdays_bit; 0b00001 * bit_seconds + 0b00010 * bit_minutes + 0b00100 * bit_hours + 0b01000 * bit_days + 0b10000 * weekdays_bit;
ESP_LOGVV(TAG, "Converted alarm mode from %.1u, %.1u, %.1u, %.1u, %.1u to bits %.2u", ESP_LOGVV(TAG, "Converted alarm mode from %.1u, %.1u, %.1u, %.1u, %.1u to bits %.2u", bit_seconds, bit_minutes,
bit_seconds, bit_minutes, bit_hours, bit_days, use_weekdays, mode_bits); bit_hours, bit_days, use_weekdays, mode_bits);
return DS3232Alarm::alarm_mode(static_cast<AlarmMode>(mode_bits)); return DS3232Alarm::alarm_mode(static_cast<AlarmMode>(mode_bits));
}; };
@ -66,21 +59,9 @@ bool DS3232Alarm::is_valid() const {
} }
DS3232Alarm DS3232Alarm::create(const bool is_enabled, const DS3232AlarmMode mode, const bool use_weekdays, DS3232Alarm DS3232Alarm::create(const bool is_enabled, const DS3232AlarmMode mode, const bool use_weekdays,
const uint8_t day, const uint8_t hour, const uint8_t minute, const uint8_t day, const uint8_t hour, const uint8_t minute, const uint8_t second,
const uint8_t second, const bool is_fired, const bool is_fired, const bool is_seconds_supported) {
const bool is_seconds_supported)
{
DS3232Alarm alarm = {}; DS3232Alarm alarm = {};
// .enabled = is_enabled,
// .seconds_supported = is_seconds_supported,
// .mode = mode,
// .second = second,
// .minute = minute,
// .hour = hour,
// .day_of_week = (use_weekdays ? day : 1),
// .day_of_month = (use_weekdays ? 1 : day),
// .is_fired = is_fired
// };
ESP_LOGVV(TAG, "Initializing new instance of alarm:"); ESP_LOGVV(TAG, "Initializing new instance of alarm:");
alarm.enabled = is_enabled; alarm.enabled = is_enabled;
ESP_LOGVV(TAG, " Alarm state: %s", ONOFF(alarm.enabled)); ESP_LOGVV(TAG, " Alarm state: %s", ONOFF(alarm.enabled));
@ -105,7 +86,7 @@ DS3232Alarm DS3232Alarm::create(const bool is_enabled, const DS3232AlarmMode mod
std::string DS3232Alarm::to_string() const { std::string DS3232Alarm::to_string() const {
if (!this->is_valid()) if (!this->is_valid())
return std::string(INVALID); return std::string(INVALID);
char formatted_string[DS3232_ALARM_FORMAT_STRING_LENGTH]; char formatted_string[DS3232_ALARM_FORMAT_STRING_LENGTH];
size_t len; size_t len;
switch (this->mode.alarm_mode) { switch (this->mode.alarm_mode) {
@ -125,8 +106,7 @@ std::string DS3232Alarm::to_string() const {
return std::string(formatted_string); return std::string(formatted_string);
case AlarmMode::MATCH_MINUTES_SECONDS: case AlarmMode::MATCH_MINUTES_SECONDS:
case 28: case 28:
len = len = snprintf(formatted_string, sizeof(formatted_string), FORMAT_STRING_EVERY_HOUR, this->minute, this->second);
snprintf(formatted_string, sizeof(formatted_string), FORMAT_STRING_EVERY_HOUR, this->minute, this->second);
if (len < 0) { if (len < 0) {
ESP_LOGE(TAG, "Unable to format alarm data."); ESP_LOGE(TAG, "Unable to format alarm data.");
return std::string(INVALID); return std::string(INVALID);

View file

@ -90,21 +90,19 @@ struct DS3232Alarm {
/// Formats alarm into human-readable text. /// Formats alarm into human-readable text.
/// @return Textual description of alarm. /// @return Textual description of alarm.
std::string to_string() const; std::string to_string() const;
/// @brief Sets target alarm mode /// @brief Sets target alarm mode
/// @param target_mode Mode of alarm to set /// @param target_mode Mode of alarm to set
void set_mode(AlarmMode target_mode) { mode.alarm_mode = target_mode; } void set_mode(AlarmMode target_mode) { mode.alarm_mode = target_mode; }
static DS3232AlarmMode alarm_mode(AlarmMode mode); static DS3232AlarmMode alarm_mode(AlarmMode mode);
static DS3232AlarmMode alarm_mode(bool bit_seconds = false, bool bit_minutes = false, static DS3232AlarmMode alarm_mode(bool bit_seconds = false, bool bit_minutes = false, bool bit_hours = false,
bool bit_hours = false, bool bit_days = false, bool bit_days = false, bool use_weekdays = false);
bool use_weekdays = false);
static DS3232Alarm create(bool is_enabled, DS3232AlarmMode mode, bool use_weekdays = true, static DS3232Alarm create(bool is_enabled, DS3232AlarmMode mode, bool use_weekdays = true, uint8_t day = 1,
uint8_t day = 1, uint8_t hour = 0, uint8_t minute = 0, uint8_t hour = 0, uint8_t minute = 0, uint8_t second = 0, bool is_fired = false,
uint8_t second = 0, bool is_fired = false, bool is_seconds_supported = false);
bool is_seconds_supported = false);
}; };
} // namespace ds3232_alarm } // namespace ds3232_alarm

View file

@ -23,7 +23,8 @@ template<typename T> class DS3232Variable : public Component {
parent_ = parent; parent_ = parent;
} }
explicit DS3232Variable(DS3232Component *parent, const uint8_t reg_id, T initial_value) : initial_value_(initial_value) { explicit DS3232Variable(DS3232Component *parent, const uint8_t reg_id, T initial_value)
: initial_value_(initial_value) {
reg_ = reg_id; reg_ = reg_id;
parent_ = parent; parent_ = parent;
} }
@ -48,33 +49,32 @@ template<typename T> class DS3232Variable : public Component {
DS3232NVRAMState nvram_state_ = this->parent_->get_nvram_state(); DS3232NVRAMState nvram_state_ = this->parent_->get_nvram_state();
while(nvram_state_ != DS3232NVRAMState::OK) while (nvram_state_ != DS3232NVRAMState::OK) {
{ if (nvram_state_ == DS3232NVRAMState::FAIL) {
if(nvram_state_ == DS3232NVRAMState::FAIL)
{
this->log_error_("Unable to init with failed NVRAM."); this->log_error_("Unable to init with failed NVRAM.");
this->mark_failed(); this->mark_failed();
break; break;
} }
if(nvram_state_ == DS3232NVRAMState::NEED_RESET) if (nvram_state_ == DS3232NVRAMState::NEED_RESET) {
{
this->set_to_default_(); this->set_to_default_();
} }
delay_microseconds_safe(10); delay_microseconds_safe(10);
nvram_state_ = this->parent_->get_nvram_state(); nvram_state_ = this->parent_->get_nvram_state();
} }
if(!this->try_read_()) { if (!this->try_read_()) {
this->mark_failed_(); this->mark_failed_();
} else { } else {
this->is_initialized_ = true; this->is_initialized_ = true;
} }
} }
float get_setup_priority() const override { return this->parent_->get_setup_priority() - 0.5f; } //Ensure that variables will be initialized after ds3232 component. float get_setup_priority() const override {
return this->parent_->get_setup_priority() - 0.5f;
} // Ensure that variables will be initialized after ds3232 component.
void loop() override { void loop() override {
if(!this->try_write_()) if (!this->try_write_())
this->mark_failed_(); this->mark_failed_();
} }
@ -92,14 +92,14 @@ template<typename T> class DS3232Variable : public Component {
bool is_initialized_{false}; bool is_initialized_{false};
std::va_list llist_{}; std::va_list llist_{};
void log_error_(const char* log_str) { void log_error_(const char *log_str) {
#ifdef USE_LOGGER #ifdef USE_LOGGER
auto *log = logger::global_logger; auto *log = logger::global_logger;
if (log == nullptr) if (log == nullptr)
return; return;
log->log_vprintf_(ESPHOME_LOG_LEVEL_ERROR, TAG, __LINE__, log_str, this->llist_); log->log_vprintf_(ESPHOME_LOG_LEVEL_ERROR, TAG, __LINE__, log_str, this->llist_);
#endif #endif
} }
void mark_failed_() { void mark_failed_() {
@ -113,8 +113,7 @@ template<typename T> class DS3232Variable : public Component {
std::vector<uint8_t> buffer; std::vector<uint8_t> buffer;
buffer.resize(sizeof(T)); buffer.resize(sizeof(T));
memcpy(buffer.data(), &this->initial_value_, sizeof(T)); memcpy(buffer.data(), &this->initial_value_, sizeof(T));
if(!this->parent_->write_memory(this->reg_, buffer)) if (!this->parent_->write_memory(this->reg_, buffer)) {
{
this->mark_failed_(); this->mark_failed_();
} else { } else {
memcpy(&this->value_, &this->initial_value_, sizeof(T)); memcpy(&this->value_, &this->initial_value_, sizeof(T));
@ -153,8 +152,7 @@ template<typename T> class DS3232Variable : public Component {
this->log_error_("No crystal defined. Read from NVRAM failed."); this->log_error_("No crystal defined. Read from NVRAM failed.");
return false; return false;
} }
if (this->parent_->get_nvram_state() != ds3232::DS3232NVRAMState::OK) if (this->parent_->get_nvram_state() != ds3232::DS3232NVRAMState::OK) {
{
this->log_error_("Invalid NVRAM state. Unable to read data."); this->log_error_("Invalid NVRAM state. Unable to read data.");
return false; return false;
} }
@ -173,8 +171,8 @@ template<typename T> class DS3232Variable : public Component {
}; };
template<typename T> T &id(DS3232Variable<T> *value) { template<typename T> T &id(DS3232Variable<T> *value) {
value->try_read_(); value->try_read_();
return value->value(); return value->value();
} }
template<class C, typename... Ts> class DS3232VariableSetAction : public Action<Ts...> { template<class C, typename... Ts> class DS3232VariableSetAction : public Action<Ts...> {
@ -185,9 +183,7 @@ template<class C, typename... Ts> class DS3232VariableSetAction : public Action<
TEMPLATABLE_VALUE(T, value); TEMPLATABLE_VALUE(T, value);
void play(Ts... x) override { void play(Ts... x) override { this->parent_->value() = this->value_.value(x...); }
this->parent_->value() = this->value_.value(x...);
}
protected: protected:
C *parent_; C *parent_;