Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Configurables #705

Merged
merged 8 commits into from
Aug 2, 2024
Prev Previous commit
Next Next commit
Allow calling connect_to using a producer reference
mairas committed Jun 13, 2024
commit fc17f833ac896310ae937c9c6fff3f4b838ed8ee
27 changes: 27 additions & 0 deletions src/sensesp/system/valueproducer.h
Original file line number Diff line number Diff line change
@@ -45,6 +45,11 @@ class ValueProducer : virtual public Observable {
consumer->set(this->get(), input_channel);
});
}
void connect_to(ValueConsumer<T>& consumer, uint8_t input_channel = 0) {
this->attach([this, consumer, input_channel]() {
consumer.set(this->get(), input_channel);
});
}

/**
* @brief Connect a producer to a consumer of a different type.
@@ -62,6 +67,12 @@ class ValueProducer : virtual public Observable {
consumer->set(CT(this->get()), input_channel);
});
}
template <typename CT>
void connect_to(ValueConsumer<CT>& consumer, uint8_t input_channel = 0) {
this->attach([this, consumer, input_channel]() {
consumer.set(CT(this->get()), input_channel);
});
}

/**
* If the consumer this producer is connecting to is ALSO a producer
@@ -82,6 +93,14 @@ class ValueProducer : virtual public Observable {
});
return consumer_producer;
}
template <typename T2>
Transform<T, T2>* connect_to(Transform<T, T2>& consumer_producer,
uint8_t input_channel = 0) {
this->attach([this, consumer_producer, input_channel]() {
consumer_producer.set(T(this->get()), input_channel);
});
return &consumer_producer;
}

/**
* @brief Connect a producer to a transform with a different input type
@@ -103,6 +122,14 @@ class ValueProducer : virtual public Observable {
});
return consumer_producer;
}
template <typename TT, typename T2>
Transform<TT, T2>* connect_to(Transform<TT, T2>& consumer_producer,
uint8_t input_channel = 0) {
this->attach([this, consumer_producer, input_channel]() {
consumer_producer->set(TT(this->get()), input_channel);
});
return &consumer_producer;
}

/*
* Set a new output value and notify consumers about it