mirror of
https://github.com/esphome/esphome.git
synced 2026-05-22 01:42:49 +08:00
[preferences] Compile out loop() when flash_write_interval is non-zero (#14943)
This commit is contained in:
@@ -21,5 +21,9 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
@coroutine_with_priority(CoroPriority.PREFERENCES)
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(var.set_write_interval(config[CONF_FLASH_WRITE_INTERVAL]))
|
||||
write_interval = config[CONF_FLASH_WRITE_INTERVAL]
|
||||
if write_interval.total_milliseconds == 0:
|
||||
cg.add_define("USE_PREFERENCES_SYNC_EVERY_LOOP")
|
||||
else:
|
||||
cg.add(var.set_write_interval(write_interval))
|
||||
await cg.register_component(var, config)
|
||||
|
||||
@@ -8,24 +8,21 @@ namespace preferences {
|
||||
|
||||
class IntervalSyncer final : public Component {
|
||||
public:
|
||||
#ifdef USE_PREFERENCES_SYNC_EVERY_LOOP
|
||||
void loop() override { global_preferences->sync(); }
|
||||
#else
|
||||
void set_write_interval(uint32_t write_interval) { this->write_interval_ = write_interval; }
|
||||
void setup() override {
|
||||
if (this->write_interval_ != 0) {
|
||||
set_interval(this->write_interval_, []() { global_preferences->sync(); });
|
||||
// When using interval-based syncing, we don't need the loop
|
||||
this->disable_loop();
|
||||
}
|
||||
}
|
||||
void loop() override {
|
||||
if (this->write_interval_ == 0) {
|
||||
global_preferences->sync();
|
||||
}
|
||||
this->set_interval(this->write_interval_, []() { global_preferences->sync(); });
|
||||
}
|
||||
#endif
|
||||
void on_shutdown() override { global_preferences->sync(); }
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
#ifndef USE_PREFERENCES_SYNC_EVERY_LOOP
|
||||
protected:
|
||||
uint32_t write_interval_{60000};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace preferences
|
||||
|
||||
@@ -118,6 +118,7 @@
|
||||
#define USE_NUMBER
|
||||
#define USE_OUTPUT
|
||||
#define USE_POWER_SUPPLY
|
||||
#define USE_PREFERENCES_SYNC_EVERY_LOOP
|
||||
#define USE_QR_CODE
|
||||
#define USE_SAFE_MODE_CALLBACK
|
||||
#define USE_SELECT
|
||||
|
||||
Reference in New Issue
Block a user