1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-08 04:43:46 +01:00

[lock] Replace std::set with bitmask (saves 388B flash + 23B RAM per lock) (#10977)

This commit is contained in:
J. Nick Koston
2025-10-05 15:33:58 -05:00
committed by GitHub
parent 0fd71ca211
commit 41c073a451
2 changed files with 13 additions and 7 deletions

View File

@@ -11,7 +11,7 @@ void CopyLock::setup() {
traits.set_assumed_state(source_->traits.get_assumed_state()); traits.set_assumed_state(source_->traits.get_assumed_state());
traits.set_requires_code(source_->traits.get_requires_code()); traits.set_requires_code(source_->traits.get_requires_code());
traits.set_supported_states(source_->traits.get_supported_states()); traits.set_supported_states_mask(source_->traits.get_supported_states_mask());
traits.set_supports_open(source_->traits.get_supports_open()); traits.set_supports_open(source_->traits.get_supports_open());
this->publish_state(source_->state); this->publish_state(source_->state);

View File

@@ -5,7 +5,7 @@
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/preferences.h" #include "esphome/core/preferences.h"
#include <set> #include <initializer_list>
namespace esphome { namespace esphome {
namespace lock { namespace lock {
@@ -44,16 +44,22 @@ class LockTraits {
bool get_assumed_state() const { return this->assumed_state_; } bool get_assumed_state() const { return this->assumed_state_; }
void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; } void set_assumed_state(bool assumed_state) { this->assumed_state_ = assumed_state; }
bool supports_state(LockState state) const { return supported_states_.count(state); } bool supports_state(LockState state) const { return supported_states_mask_ & (1 << state); }
std::set<LockState> get_supported_states() const { return supported_states_; } void set_supported_states(std::initializer_list<LockState> states) {
void set_supported_states(std::set<LockState> states) { supported_states_ = std::move(states); } supported_states_mask_ = 0;
void add_supported_state(LockState state) { supported_states_.insert(state); } for (auto state : states) {
supported_states_mask_ |= (1 << state);
}
}
uint8_t get_supported_states_mask() const { return supported_states_mask_; }
void set_supported_states_mask(uint8_t mask) { supported_states_mask_ = mask; }
void add_supported_state(LockState state) { supported_states_mask_ |= (1 << state); }
protected: protected:
bool supports_open_{false}; bool supports_open_{false};
bool requires_code_{false}; bool requires_code_{false};
bool assumed_state_{false}; bool assumed_state_{false};
std::set<LockState> supported_states_ = {LOCK_STATE_NONE, LOCK_STATE_LOCKED, LOCK_STATE_UNLOCKED}; uint8_t supported_states_mask_{(1 << LOCK_STATE_NONE) | (1 << LOCK_STATE_LOCKED) | (1 << LOCK_STATE_UNLOCKED)};
}; };
/** This class is used to encode all control actions on a lock device. /** This class is used to encode all control actions on a lock device.