1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-02 11:22:24 +01:00

Add central function scheduler (#609)

* Add central function scheduler

* Avoid unnecessary copies

* Lint

* Prevent more copies, store pointers

* Add never update_interval
This commit is contained in:
Otto Winter
2019-06-07 14:26:40 +02:00
committed by GitHub
parent 7a895adec9
commit b51cbc4207
6 changed files with 273 additions and 128 deletions

49
esphome/core/scheduler.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include "esphome/core/component.h"
#include <vector>
namespace esphome {
class Component;
class Scheduler {
public:
void set_timeout(Component *component, const std::string &name, uint32_t timeout, std::function<void()> &&func);
bool cancel_timeout(Component *component, const std::string &name);
void set_interval(Component *component, const std::string &name, uint32_t interval, std::function<void()> &&func);
bool cancel_interval(Component *component, const std::string &name);
optional<uint32_t> next_schedule_in();
void call();
void process_to_add();
protected:
struct SchedulerItem {
Component *component;
std::string name;
enum Type { TIMEOUT, INTERVAL } type;
union {
uint32_t interval;
uint32_t timeout;
};
uint32_t last_execution;
std::function<void()> f;
bool remove;
bool operator<(const SchedulerItem &other) const;
};
void cleanup_();
bool peek_();
void pop_raw_();
void push_(SchedulerItem *item);
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type);
std::vector<SchedulerItem *> items_;
std::vector<SchedulerItem *> to_add_;
};
} // namespace esphome